Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
NavigationHistoryImpl |
|
| 4.0;4 |
1 | /** | |
2 | * 2008, Digitalis Informatica. All rights reserved. Distribuicao e Gestao de Informatica, Lda. Estrada de Paco de Arcos | |
3 | * num.9 - Piso -1 2780-666 Paco de Arcos Telefone: (351) 21 4408990 Fax: (351) 21 4408999 http://www.digitalis.pt | |
4 | */ | |
5 | ||
6 | package pt.digitalis.dif.controller.objects; | |
7 | ||
8 | import java.util.ArrayList; | |
9 | import java.util.List; | |
10 | import java.util.Map.Entry; | |
11 | ||
12 | import pt.digitalis.dif.controller.http.HTTPConstants; | |
13 | import pt.digitalis.dif.controller.interfaces.IDIFSession; | |
14 | import pt.digitalis.dif.controller.interfaces.INavigationHistory; | |
15 | import pt.digitalis.dif.controller.security.managers.IAuthorizationManager; | |
16 | import pt.digitalis.dif.controller.security.objects.IDIFUser; | |
17 | import pt.digitalis.dif.dem.CallbackType; | |
18 | import pt.digitalis.dif.dem.interfaces.IStage; | |
19 | import pt.digitalis.dif.dem.interfaces.IStageInstance; | |
20 | import pt.digitalis.dif.dem.managers.IDEMManager; | |
21 | import pt.digitalis.dif.ioc.DIFIoCRegistry; | |
22 | ||
23 | /** | |
24 | * The default navigation history implementation | |
25 | * | |
26 | * @author Pedro Viegas <a href="mailto:pviegas@digitalis.pt">pviegas@digitalis.pt</a> | |
27 | * @created 2008/07/20 | |
28 | */ | |
29 | 0 | public class NavigationHistoryImpl implements INavigationHistory { |
30 | ||
31 | /** The authorization manager instance */ | |
32 | 0 | static private IAuthorizationManager authorizationManager = DIFIoCRegistry.getRegistry().getImplementation( |
33 | 0 | IAuthorizationManager.class); |
34 | ||
35 | /** The DEM manager instance */ | |
36 | 0 | static private IDEMManager demManager = DIFIoCRegistry.getRegistry().getImplementation(IDEMManager.class); |
37 | ||
38 | /** The crumbs history list */ | |
39 | 0 | private List<Breadcrumb> historyFirstAccess = new ArrayList<Breadcrumb>(); |
40 | ||
41 | /** The crumbs history list */ | |
42 | 0 | private List<Breadcrumb> historyLastAccess = new ArrayList<Breadcrumb>(); |
43 | ||
44 | /** | |
45 | * @see pt.digitalis.dif.controller.interfaces.INavigationHistory#addBreadcrumb(pt.digitalis.dif.controller.objects.Breadcrumb) | |
46 | */ | |
47 | synchronized public void addBreadcrumb(Breadcrumb crumb) | |
48 | { | |
49 | ||
50 | int total; | |
51 | int current; | |
52 | boolean added; | |
53 | ||
54 | /* | |
55 | * Add to first accessed ---------------------------- | |
56 | */ | |
57 | 0 | total = historyFirstAccess.size(); |
58 | 0 | current = 0; |
59 | 0 | added = false; |
60 | ||
61 | 0 | while (!added && current < total) |
62 | { | |
63 | 0 | if (historyFirstAccess.get(current).getStageID().equals(crumb.getStageID())) |
64 | { | |
65 | // Already present, so replace it... | |
66 | 0 | historyFirstAccess.set(current, crumb); |
67 | 0 | added = true; |
68 | } | |
69 | else | |
70 | 0 | current++; |
71 | } | |
72 | ||
73 | 0 | if (!added) |
74 | // Does not exist, so add it... | |
75 | 0 | historyFirstAccess.add(crumb); |
76 | ||
77 | /* | |
78 | * Add to last accessed ---------------------------- | |
79 | */ | |
80 | 0 | total = historyLastAccess.size(); |
81 | 0 | current = 0; |
82 | 0 | added = false; |
83 | ||
84 | 0 | while (current < total) |
85 | { | |
86 | 0 | if (historyLastAccess.get(current).getStageID().equals(crumb.getStageID())) |
87 | { | |
88 | 0 | historyLastAccess.remove(current); |
89 | 0 | total--; |
90 | } | |
91 | else | |
92 | 0 | current++; |
93 | } | |
94 | ||
95 | 0 | historyLastAccess.add(crumb); |
96 | 0 | } |
97 | ||
98 | /** | |
99 | * @see pt.digitalis.dif.controller.interfaces.INavigationHistory#addStage(pt.digitalis.dif.dem.interfaces.IStageInstance) | |
100 | */ | |
101 | public void addStage(IStageInstance stage) | |
102 | { | |
103 | ||
104 | // Only add if the stage has callback enabled | |
105 | 0 | if (stage.hasCallbackEnabled() && !stage.getContext().getRequest().isPopupMode()) |
106 | { | |
107 | 0 | Breadcrumb crumb = new Breadcrumb(stage.getID()); |
108 | ||
109 | 0 | if (stage.getCallbackType().equals(CallbackType.SAVE_SESSION)) |
110 | // If the stage is configured to save the session do it | |
111 | // VALIDATE: Should we save the parameters also? If not the result may differ from the previous response | |
112 | 0 | crumb.setDifSession(stage.getContext().getSession()); |
113 | ||
114 | 0 | if (stage.getCallbackType().equals(CallbackType.SAVE_PARAMETERS) |
115 | 0 | || stage.getCallbackType().equals(CallbackType.SAVE_SESSION)) |
116 | { | |
117 | 0 | String parameters = null; |
118 | ||
119 | 0 | for (Entry<String, ? extends Object> entry: stage.getContext().getRequest().getParameters().entrySet()) |
120 | { | |
121 | 0 | if (HTTPConstants.getPrivateParameters().indexOf(entry.getKey()) == -1) |
122 | { | |
123 | 0 | Object value = entry.getValue(); |
124 | ||
125 | 0 | if (value != null) |
126 | { | |
127 | 0 | if (parameters == null) |
128 | 0 | parameters = entry.getKey() + "=" + entry.getValue().toString(); |
129 | else | |
130 | 0 | parameters += "&" + entry.getKey() + "=" + entry.getValue().toString(); |
131 | } | |
132 | } | |
133 | } | |
134 | ||
135 | 0 | crumb.setParameterPassed(parameters); |
136 | } | |
137 | ||
138 | 0 | this.addBreadcrumb(crumb); |
139 | } | |
140 | 0 | } |
141 | ||
142 | /** | |
143 | * @see pt.digitalis.dif.controller.interfaces.INavigationHistory#cleanUpAfterLogout(pt.digitalis.dif.controller.interfaces.IDIFSession) | |
144 | */ | |
145 | public void cleanUpAfterLogout(IDIFSession session) | |
146 | { | |
147 | 0 | IDIFUser currentUser = session.getUser(); |
148 | 0 | boolean hasAccess = false; |
149 | ||
150 | 0 | List<Breadcrumb> crumbsList = new ArrayList<Breadcrumb>(); |
151 | 0 | crumbsList.addAll(historyFirstAccess); |
152 | ||
153 | 0 | for (Breadcrumb crumb: crumbsList) |
154 | { | |
155 | 0 | IStage stage = demManager.getStage(crumb.getStageID()); |
156 | ||
157 | 0 | if (currentUser == null) |
158 | 0 | hasAccess = authorizationManager.hasAccessPublic(stage); |
159 | else | |
160 | 0 | hasAccess = currentUser.canAccess(stage); |
161 | ||
162 | 0 | if (!hasAccess) |
163 | { | |
164 | 0 | removeStage(crumb.getStageID()); |
165 | } | |
166 | } | |
167 | 0 | } |
168 | ||
169 | /** | |
170 | * @see pt.digitalis.dif.controller.interfaces.INavigationHistory#getHistoryFirstAccess() | |
171 | */ | |
172 | public List<Breadcrumb> getHistoryFirstAccess() | |
173 | { | |
174 | 0 | return historyFirstAccess; |
175 | } | |
176 | ||
177 | /** | |
178 | * @see pt.digitalis.dif.controller.interfaces.INavigationHistory#getHistoryLastAccessed() | |
179 | */ | |
180 | public List<Breadcrumb> getHistoryLastAccessed() | |
181 | { | |
182 | 0 | return historyLastAccess; |
183 | } | |
184 | ||
185 | /** | |
186 | * @see pt.digitalis.dif.controller.interfaces.INavigationHistory#getPreviousForByFirstAccess(java.lang.String) | |
187 | */ | |
188 | public Breadcrumb getPreviousForByFirstAccess(String stageToFindPrevious) | |
189 | { | |
190 | 0 | Breadcrumb previous = null; |
191 | 0 | stageToFindPrevious = stageToFindPrevious.toLowerCase(); |
192 | ||
193 | 0 | for (Breadcrumb current: historyFirstAccess) |
194 | { | |
195 | 0 | if (current.getStageID().equals(stageToFindPrevious)) |
196 | 0 | break; |
197 | ||
198 | 0 | previous = current; |
199 | } | |
200 | ||
201 | 0 | return previous; |
202 | } | |
203 | ||
204 | /** | |
205 | * @see pt.digitalis.dif.controller.interfaces.INavigationHistory#getPreviousForByLastAccess(java.lang.String) | |
206 | */ | |
207 | public Breadcrumb getPreviousForByLastAccess(String stageToFindPrevious) | |
208 | { | |
209 | 0 | Breadcrumb previous = null; |
210 | 0 | stageToFindPrevious = stageToFindPrevious.toLowerCase(); |
211 | ||
212 | 0 | for (Breadcrumb current: historyLastAccess) |
213 | { | |
214 | 0 | if (current.getStageID().equals(stageToFindPrevious)) |
215 | 0 | break; |
216 | ||
217 | 0 | previous = current; |
218 | } | |
219 | ||
220 | 0 | return previous; |
221 | } | |
222 | ||
223 | /** | |
224 | * @see pt.digitalis.dif.controller.interfaces.INavigationHistory#isEmpty() | |
225 | */ | |
226 | public boolean isEmpty() | |
227 | { | |
228 | 0 | return historyFirstAccess.isEmpty() || historyLastAccess.isEmpty(); |
229 | } | |
230 | ||
231 | /** | |
232 | * @see pt.digitalis.dif.controller.interfaces.INavigationHistory#removeStage(java.lang.String) | |
233 | */ | |
234 | public void removeStage(String stage) | |
235 | { | |
236 | 0 | int index = 0; |
237 | ||
238 | 0 | while (index < historyFirstAccess.size()) |
239 | { | |
240 | 0 | if (historyFirstAccess.get(index).getStageID().equals(stage)) |
241 | 0 | historyFirstAccess.remove(index); |
242 | else | |
243 | 0 | index++; |
244 | } | |
245 | ||
246 | 0 | index = 0; |
247 | ||
248 | 0 | while (index < historyLastAccess.size()) |
249 | { | |
250 | 0 | if (historyLastAccess.get(index).getStageID().equals(stage)) |
251 | 0 | historyLastAccess.remove(index); |
252 | else | |
253 | 0 | index++; |
254 | } | |
255 | 0 | } |
256 | } |