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.kuali.rice.krad.uif.util.BreadcrumbItem;
19 import org.kuali.rice.krad.uif.util.UrlInfo;
20
21 import java.io.Serializable;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Stack;
25
26 /**
27 * HistoryFlow represents the a flow of urls. When the flow is continued, the flow inherits the urls/breadcrumbs from
28 * a previous flow. Using a flow key it is possible to jump back to previous flows.
29 */
30 public class HistoryFlow implements Serializable {
31 protected Stack<UrlInfo> flowUrls = new Stack<UrlInfo>();
32 protected String flowReturnPoint;
33 protected String flowStartPoint;
34 protected String flowKey;
35
36 protected BreadcrumbItem currentViewItem;
37 protected List<BreadcrumbItem> pastItems;
38
39 /**
40 * Initialize a new HistoryFlow with a key
41 *
42 * @param flowKey the flowKey to use
43 */
44 public HistoryFlow (String flowKey) {
45 this.flowKey = flowKey;
46 }
47
48 /**
49 * Get all urls in the HistoryFlow stack
50 *
51 * @return the stack of HistoryFlow urls
52 */
53 public Stack<UrlInfo> getFlowUrls() {
54 return flowUrls;
55 }
56
57 /**
58 * Set the flowUrls for this HistoryFlow
59 *
60 * @param flowUrls
61 */
62 public void setFlowUrls(Stack<UrlInfo> flowUrls) {
63 this.flowUrls = flowUrls;
64 }
65
66 /**
67 * The returnPoint used to jump back to a previous flow
68 *
69 * @return the flowReturnPoint
70 */
71 public String getFlowReturnPoint() {
72 return flowReturnPoint;
73 }
74
75 /**
76 * Set the returnPoint for use to jump back to the previous flow
77 *
78 * @param flowReturnPoint
79 */
80 public void setFlowReturnPoint(String flowReturnPoint) {
81 this.flowReturnPoint = flowReturnPoint;
82 }
83
84 public String getFlowStartPoint() {
85 return flowStartPoint;
86 }
87
88 public void setFlowStartPoint(String flowStartPoint) {
89 this.flowStartPoint = flowStartPoint;
90 }
91
92 /**
93 * The flowKey used to identify the HistoryFlow
94 *
95 * @return
96 */
97 public String getFlowKey() {
98 return flowKey;
99 }
100
101 /**
102 * Set the flowKey for this HistoryFlow
103 *
104 * @param flowKey
105 */
106 public void setFlowKey(String flowKey) {
107 this.flowKey = flowKey;
108 }
109
110 /**
111 * Push the url onto the history stack
112 *
113 * @param url the url to push
114 */
115 public void push(String url){
116 UrlInfo urlInfo = new UrlInfo();
117 urlInfo.setHref(url);
118 flowUrls.push(urlInfo);
119 }
120
121 /**
122 * Push the url onto the history stack
123 *
124 * @param urlInfo the urlInfo object to push
125 */
126 public void push(UrlInfo urlInfo){
127 flowUrls.push(urlInfo);
128 }
129
130 /**
131 * Update the last url on the history stack with the new value
132 *
133 * @param url the url to update to
134 */
135 public void update(String url){
136 UrlInfo urlInfo = new UrlInfo();
137 urlInfo.setHref(url);
138
139 if(flowUrls.empty()){
140 flowUrls.push(urlInfo);
141 }
142 else{
143 flowUrls.pop();
144 flowUrls.push(urlInfo);
145 }
146 }
147
148 /**
149 * Update the last url on the history stack with the new value
150 *
151 * @param urlInfo the UrlInfo object to update to
152 */
153 public void update(UrlInfo urlInfo){
154 if(flowUrls.empty()){
155 flowUrls.push(urlInfo);
156 }
157 else{
158 flowUrls.pop();
159 flowUrls.push(urlInfo);
160 }
161 }
162
163 /**
164 * Get the last url on the history stack
165 *
166 * @return the last url on the history stack
167 */
168 public String getCurrentLocation(){
169 if(flowUrls != null && !flowUrls.isEmpty()){
170 return flowUrls.peek().getHref();
171 }
172
173 return null;
174 }
175
176 /**
177 * Clear all urls on the history stack and accumulated breadcrumbs
178 */
179 public void clear(){
180 flowUrls.clear();
181 pastItems.clear();
182 currentViewItem = null;
183 flowReturnPoint = null;
184 }
185
186 /**
187 * Continue a new flow from a previous flow. This will copy the prevFlow's flow urls to flowUrls,
188 * pastItems and currentViewItem to the new flow's pastItems, and set the flowReturnPoint to the currentLocation
189 * of the prevFlow.
190 *
191 * @param prevFlow
192 */
193 public void continueFlow(HistoryFlow prevFlow){
194 if(prevFlow != null){
195 flowReturnPoint = prevFlow.getCurrentLocation();
196 this.setFlowUrls(prevFlow.getFlowUrls());
197
198 if(this.getFlowUrls() != null && !this.getFlowUrls().isEmpty()){
199 flowStartPoint = this.getFlowUrls().firstElement().getHref();
200 }
201
202 pastItems = new ArrayList<BreadcrumbItem>();
203
204 if (prevFlow.getPastItems() != null){
205 pastItems.addAll(prevFlow.getPastItems());
206 }
207
208 if (prevFlow.getCurrentViewItem() != null){
209 pastItems.add(prevFlow.getCurrentViewItem());
210 }
211 }
212 }
213
214 /**
215 * Get the item which represents the most current BreadcrumbItem for the View for this flow
216 *
217 * @return the View BreadcrumbItem
218 */
219 public BreadcrumbItem getCurrentViewItem() {
220 return currentViewItem;
221 }
222
223 /**
224 * Set the current BreadcrumbItem
225 *
226 * @param currentViewItem
227 */
228 public void setCurrentViewItem(BreadcrumbItem currentViewItem) {
229 this.currentViewItem = currentViewItem;
230 }
231
232 /**
233 * Get all the past BreadcrumbItems (these represents the path to the current item)
234 *
235 * @return the past BreadcrumbItems
236 */
237 public List<BreadcrumbItem> getPastItems() {
238 return pastItems;
239 }
240
241 /**
242 * Set the past BreadcrumbItems
243 *
244 * @param pastItems
245 */
246 public void setPastItems(List<BreadcrumbItem> pastItems) {
247 this.pastItems = pastItems;
248 }
249 }