1 /**
2 * Copyright 2005-2014 The Kuali Foundation
3 *
4 * Licensed under the Educational Community License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/ecl2.php
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.kuali.rice.krad.web.form;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.krad.uif.UifConstants;
20 import org.kuali.rice.krad.uif.UifParameters;
21
22 import java.io.Serializable;
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.UUID;
26
27 /**
28 * HistoryManager stores the map of the most recentFlows and a map of flows stored by flowId concatenated with formId.
29 * HistoryManager is used in session.
30 */
31 public class HistoryManager implements Serializable {
32
33 private static final long serialVersionUID = 7612500634309569727L;
34 private Map<String, HistoryFlow> historyFlowMap = new HashMap<String, HistoryFlow>();
35 private Map<String, HistoryFlow> recentFlows = new HashMap<String, HistoryFlow>();
36
37 /**
38 * Process/update the HistoryFlow stored in session for the flowKey, formKey, and currentUrl passed in.
39 *
40 * <p>If flowKey is blank or equal to "start", this will begin a new flow, otherwise the flow will continue by
41 * picking
42 * up the last flow that matches the flowKey, but if it also matches to a formKey that already is keyed to that
43 * flowKey, it will "jump" back to that HistoryFlow instead.</p>
44 *
45 * @param flowKey the flow key
46 * @param formKey the form key
47 * @param currentUrl the currentUrl being process
48 * @return the HistoryFlow which represents the current HistoryFlow based on the parameters passed in
49 */
50 public HistoryFlow process(String flowKey, String formKey, String currentUrl) {
51 if (StringUtils.isBlank(flowKey) || flowKey.equalsIgnoreCase(UifConstants.HistoryFlow.START)) {
52 flowKey = UUID.randomUUID().toString();
53 }
54
55 HistoryFlow newFlow = new HistoryFlow(flowKey);
56
57 if (currentUrl.contains("?") && !currentUrl.contains(UifParameters.FORM_KEY) && StringUtils.isNotBlank(formKey)){
58 currentUrl = currentUrl + "&" + UifParameters.FORM_KEY + "=" + formKey;
59 }
60
61 if (getMostRecentFlowByFormKey(flowKey, formKey) != null) {
62 newFlow = getMostRecentFlowByFormKey(flowKey, formKey);
63 newFlow.update(currentUrl);
64 } else if (StringUtils.isNotBlank(flowKey)) {
65 HistoryFlow recentFlow = recentFlows.get(flowKey);
66 newFlow.continueFlow(recentFlow);
67 newFlow.push(currentUrl);
68 }
69
70 recentFlows.put(flowKey, newFlow);
71 historyFlowMap.put(flowKey + UifConstants.HistoryFlow.SEPARATOR + formKey, newFlow);
72
73 return newFlow;
74 }
75
76 /**
77 * Get the flow that matches the flow key. This represents the most recent flow tied to this flow key.
78 *
79 * @param key the flow key
80 * @return the HistoryFlow, null if not found
81 */
82 public HistoryFlow getMostRecentFlowByKey(String key) {
83 if (StringUtils.isBlank(key)) {
84 return null;
85 }
86
87 return recentFlows.get(key);
88 }
89
90 /**
91 * Get the flow by flowKey and formKey. This represents a flow specific to this combination.
92 *
93 * @param key the flow key
94 * @param formKey the form key
95 * @return the HistoryFlow if found, null otherwise
96 */
97 public HistoryFlow getMostRecentFlowByFormKey(String key, String formKey) {
98 if (StringUtils.isBlank(key) || StringUtils.isBlank(formKey)) {
99 return null;
100 }
101
102 return historyFlowMap.get(key + UifConstants.HistoryFlow.SEPARATOR + formKey);
103 }
104 }