001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.uif.util;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.krad.web.form.UifFormBase;
020
021import java.io.Serializable;
022import java.util.HashMap;
023import java.util.Map;
024
025/**
026 * Manages Uif form objects for a session
027 *
028 * @author Kuali Rice Team (rice.collab@kuali.org)
029 */
030public class UifFormManager implements Serializable {
031    private static final long serialVersionUID = -6323378881342207080L;
032
033    private UifFormBase currentForm;
034    private Map<String, UifFormBase> uifForms;
035
036    public UifFormManager() {
037        this.uifForms = new HashMap<String, UifFormBase>();
038    }
039
040    public UifFormBase getCurrentForm() {
041        return currentForm;
042    }
043
044    public void setCurrentForm(UifFormBase currentForm) {
045        this.currentForm = currentForm;
046        addForm(currentForm);
047    }
048
049    public void addForm(UifFormBase form) {
050        if (form == null) {
051            return;
052        }
053
054        uifForms.put(form.getFormKey(), form);
055    }
056
057    public UifFormBase getForm(String formKey) {
058        if (uifForms.containsKey(formKey)) {
059            return uifForms.get(formKey);
060        }
061
062        return null;
063    }
064
065    public void removeForm(UifFormBase form) {
066        if (form == null) {
067            return;
068        }
069
070        removeFormByKey(form.getFormKey());
071    }
072
073    public void removeFormByKey(String formKey) {
074        if (uifForms.containsKey(formKey)) {
075            uifForms.remove(formKey);
076        }
077
078        if ((currentForm != null) && StringUtils.equals(currentForm.getFormKey(), formKey)) {
079            currentForm = null;
080        }
081    }
082}