View Javadoc

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  public class NavigationHistoryImpl implements INavigationHistory {
30  
31      /** The authorization manager instance */
32      static private IAuthorizationManager authorizationManager = DIFIoCRegistry.getRegistry().getImplementation(
33              IAuthorizationManager.class);
34  
35      /** The DEM manager instance */
36      static private IDEMManager demManager = DIFIoCRegistry.getRegistry().getImplementation(IDEMManager.class);
37  
38      /** The crumbs history list */
39      private List<Breadcrumb> historyFirstAccess = new ArrayList<Breadcrumb>();
40  
41      /** The crumbs history list */
42      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          total = historyFirstAccess.size();
58          current = 0;
59          added = false;
60  
61          while (!added && current < total)
62          {
63              if (historyFirstAccess.get(current).getStageID().equals(crumb.getStageID()))
64              {
65                  // Already present, so replace it...
66                  historyFirstAccess.set(current, crumb);
67                  added = true;
68              }
69              else
70                  current++;
71          }
72  
73          if (!added)
74              // Does not exist, so add it...
75              historyFirstAccess.add(crumb);
76  
77          /*
78           * Add to last accessed ----------------------------
79           */
80          total = historyLastAccess.size();
81          current = 0;
82          added = false;
83  
84          while (current < total)
85          {
86              if (historyLastAccess.get(current).getStageID().equals(crumb.getStageID()))
87              {
88                  historyLastAccess.remove(current);
89                  total--;
90              }
91              else
92                  current++;
93          }
94  
95          historyLastAccess.add(crumb);
96      }
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         if (stage.hasCallbackEnabled() && !stage.getContext().getRequest().isPopupMode())
106         {
107             Breadcrumb crumb = new Breadcrumb(stage.getID());
108 
109             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                 crumb.setDifSession(stage.getContext().getSession());
113 
114             if (stage.getCallbackType().equals(CallbackType.SAVE_PARAMETERS)
115                     || stage.getCallbackType().equals(CallbackType.SAVE_SESSION))
116             {
117                 String parameters = null;
118 
119                 for (Entry<String, ? extends Object> entry: stage.getContext().getRequest().getParameters().entrySet())
120                 {
121                     if (HTTPConstants.getPrivateParameters().indexOf(entry.getKey()) == -1)
122                     {
123                         Object value = entry.getValue();
124 
125                         if (value != null)
126                         {
127                             if (parameters == null)
128                                 parameters = entry.getKey() + "=" + entry.getValue().toString();
129                             else
130                                 parameters += "&" + entry.getKey() + "=" + entry.getValue().toString();
131                         }
132                     }
133                 }
134 
135                 crumb.setParameterPassed(parameters);
136             }
137 
138             this.addBreadcrumb(crumb);
139         }
140     }
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         IDIFUser currentUser = session.getUser();
148         boolean hasAccess = false;
149 
150         List<Breadcrumb> crumbsList = new ArrayList<Breadcrumb>();
151         crumbsList.addAll(historyFirstAccess);
152 
153         for (Breadcrumb crumb: crumbsList)
154         {
155             IStage stage = demManager.getStage(crumb.getStageID());
156 
157             if (currentUser == null)
158                 hasAccess = authorizationManager.hasAccessPublic(stage);
159             else
160                 hasAccess = currentUser.canAccess(stage);
161 
162             if (!hasAccess)
163             {
164                 removeStage(crumb.getStageID());
165             }
166         }
167     }
168 
169     /**
170      * @see pt.digitalis.dif.controller.interfaces.INavigationHistory#getHistoryFirstAccess()
171      */
172     public List<Breadcrumb> getHistoryFirstAccess()
173     {
174         return historyFirstAccess;
175     }
176 
177     /**
178      * @see pt.digitalis.dif.controller.interfaces.INavigationHistory#getHistoryLastAccessed()
179      */
180     public List<Breadcrumb> getHistoryLastAccessed()
181     {
182         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         Breadcrumb previous = null;
191         stageToFindPrevious = stageToFindPrevious.toLowerCase();
192 
193         for (Breadcrumb current: historyFirstAccess)
194         {
195             if (current.getStageID().equals(stageToFindPrevious))
196                 break;
197 
198             previous = current;
199         }
200 
201         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         Breadcrumb previous = null;
210         stageToFindPrevious = stageToFindPrevious.toLowerCase();
211 
212         for (Breadcrumb current: historyLastAccess)
213         {
214             if (current.getStageID().equals(stageToFindPrevious))
215                 break;
216 
217             previous = current;
218         }
219 
220         return previous;
221     }
222 
223     /**
224      * @see pt.digitalis.dif.controller.interfaces.INavigationHistory#isEmpty()
225      */
226     public boolean isEmpty()
227     {
228         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         int index = 0;
237 
238         while (index < historyFirstAccess.size())
239         {
240             if (historyFirstAccess.get(index).getStageID().equals(stage))
241                 historyFirstAccess.remove(index);
242             else
243                 index++;
244         }
245 
246         index = 0;
247 
248         while (index < historyLastAccess.size())
249         {
250             if (historyLastAccess.get(index).getStageID().equals(stage))
251                 historyLastAccess.remove(index);
252             else
253                 index++;
254         }
255     }
256 }