001    /**
002     * Copyright 2005-2014 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     */
016    package org.kuali.rice.krad.web.form;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.rice.krad.uif.UifConstants;
020    import org.kuali.rice.krad.uif.UifParameters;
021    
022    import java.io.Serializable;
023    import java.util.HashMap;
024    import java.util.Map;
025    import java.util.UUID;
026    
027    /**
028     * HistoryManager stores the map of the most recentFlows and a map of flows stored by flowId concatenated with formId.
029     * HistoryManager is used in session.
030     */
031    public class HistoryManager implements Serializable {
032    
033        private static final long serialVersionUID = 7612500634309569727L;
034        private Map<String, HistoryFlow> historyFlowMap = new HashMap<String, HistoryFlow>();
035        private Map<String, HistoryFlow> recentFlows = new HashMap<String, HistoryFlow>();
036    
037        /**
038         * Process/update the HistoryFlow stored in session for the flowKey, formKey, and currentUrl passed in.
039         *
040         * <p>If flowKey is blank or equal to "start", this will begin a new flow, otherwise the flow will continue by
041         * picking
042         * up the last flow that matches the flowKey, but if it also matches to a formKey that already is keyed to that
043         * flowKey, it will "jump" back to that HistoryFlow instead.</p>
044         *
045         * @param flowKey the flow key
046         * @param formKey the form key
047         * @param currentUrl the currentUrl being process
048         * @return the HistoryFlow which represents the current HistoryFlow based on the parameters passed in
049         */
050        public HistoryFlow process(String flowKey, String formKey, String currentUrl) {
051            if (StringUtils.isBlank(flowKey) || flowKey.equalsIgnoreCase(UifConstants.HistoryFlow.START)) {
052                flowKey = UUID.randomUUID().toString();
053            }
054    
055            HistoryFlow newFlow = new HistoryFlow(flowKey);
056    
057            if (currentUrl.contains("?") && !currentUrl.contains(UifParameters.FORM_KEY) && StringUtils.isNotBlank(formKey)){
058                currentUrl = currentUrl + "&" + UifParameters.FORM_KEY + "=" + formKey;
059            }
060    
061            if (getMostRecentFlowByFormKey(flowKey, formKey) != null) {
062                newFlow = getMostRecentFlowByFormKey(flowKey, formKey);
063                newFlow.update(currentUrl);
064            } else if (StringUtils.isNotBlank(flowKey)) {
065                HistoryFlow recentFlow = recentFlows.get(flowKey);
066                newFlow.continueFlow(recentFlow);
067                newFlow.push(currentUrl);
068            }
069    
070            recentFlows.put(flowKey, newFlow);
071            historyFlowMap.put(flowKey + UifConstants.HistoryFlow.SEPARATOR + formKey, newFlow);
072    
073            return newFlow;
074        }
075    
076        /**
077         * Get the flow that matches the flow key.  This represents the most recent flow tied to this flow key.
078         *
079         * @param key the flow key
080         * @return the HistoryFlow, null if not found
081         */
082        public HistoryFlow getMostRecentFlowByKey(String key) {
083            if (StringUtils.isBlank(key)) {
084                return null;
085            }
086    
087            return recentFlows.get(key);
088        }
089    
090        /**
091         * Get the flow by flowKey and formKey.  This represents a flow specific to this combination.
092         *
093         * @param key the flow key
094         * @param formKey the form key
095         * @return the HistoryFlow if found, null otherwise
096         */
097        public HistoryFlow getMostRecentFlowByFormKey(String key, String formKey) {
098            if (StringUtils.isBlank(key) || StringUtils.isBlank(formKey)) {
099                return null;
100            }
101    
102            return historyFlowMap.get(key + UifConstants.HistoryFlow.SEPARATOR + formKey);
103        }
104    }