View Javadoc

1   /*
2    * WebFlow Navigation Manager: webflow definiton, server side navigation history and automatic session cleaning.
3    * Distributed under LGPL license at web site http://wfnm.sourceforge.net .
4    */
5   package net.sf.wfnm;
6   
7   import org.apache.commons.logging.Log;
8   import org.apache.commons.logging.LogFactory;
9   
10  import java.io.Serializable;
11  
12  import java.util.ArrayList;
13  import java.util.HashSet;
14  import java.util.Iterator;
15  import java.util.List;
16  import java.util.Set;
17  
18  
19  /***
20   * The navigation context default implementation.
21   *
22   * @author <a href="mailto:malbari@users.sourceforge.net">Maurizio Albari</a>
23   * @version 1.0.6
24   */
25  public class NavigationContextImpl implements NavigationContext, Serializable {
26      /*** 
27       * The log
28       */
29      private static final Log log = LogFactory.getLog(NavigationContextImpl.class);
30  
31      /*** 
32       * A list of ownership.
33       */
34      protected List ownershipList = new ArrayList();
35  
36      /*** 
37       * The added object set.
38       */
39      protected Set addedObjectSet = new HashSet();
40  
41      /*** 
42       * The removed object set.
43       */
44      protected Set removedObjectSet = new HashSet();
45  
46      /*** 
47       * The previous webflow to force
48       */
49      protected String previousWebflowToForce;
50  
51      /*** 
52       * The webflow name.
53       */
54      protected String webflowName;
55  
56      /*** 
57       * Indicates that the page has to be ignored.
58       */
59      protected boolean ignorePage = false;
60  
61      /*** 
62       * Indicates if the Webflow Navigation Manager has been reset.
63       */
64      protected boolean reset = false;
65  
66      /*** 
67       * The webflow default ownership.
68       */
69      protected int defaultOwnership = Config.getInstance().getDefaultOwnership();
70  
71      /***
72       * Creates a new DeferredSetting object.
73       */
74      protected NavigationContextImpl() {
75          super();
76      }
77  
78      /***
79       * Sets an attribute.
80       *
81       * @param container the http session
82       * @param objectKey the object key
83       */
84      public void setAttribute(AttributeContainer container, String objectKey) {
85          if (Config.getInstance().isEnabled() && Config.getInstance().isCleanerEnabled() &&
86                  !NavigationManagerFactory.isNavigationManagerKey(objectKey)) {
87              if (removedObjectSet.contains(objectKey)) {
88                  removedObjectSet.remove(objectKey);
89              } else {
90                  addedObjectSet.add(objectKey);
91              }
92          }
93      }
94  
95      /***
96       * Sets the webflow default ownership.
97       *
98       * @param defaultOwnership the webflow default ownership
99       */
100     public void setDefaultOwnership(int defaultOwnership) {
101         this.defaultOwnership = defaultOwnership;
102     }
103 
104     /***
105      * Returns true if the framework has to be resetted.
106      *
107      * @return true if the framework has to be resetted
108      */
109     public boolean isFrameworkToReset() {
110         return reset;
111     }
112 
113     /***
114      * Sets whether the page has to be ignored.
115      *
116      * @param ignorePage true if the page has to be ignored
117      */
118     public void setIgnorePage(boolean ignorePage) {
119         this.ignorePage = ignorePage;
120     }
121 
122     /***
123      * Indicates if the page has to be ignored.
124      *
125      * @return true if the page has to be ignored
126      */
127     public boolean isIgnorePage() {
128         return ignorePage;
129     }
130 
131     /***
132      * Force the previous webflow.
133      *
134      * @param previousWebflowToForce the previous webflow to force
135      */
136     public void setPreviousWebflowToForce(String previousWebflowToForce) {
137         this.previousWebflowToForce = previousWebflowToForce;
138     }
139 
140     /***
141      * Sets the webflow name.
142      *
143      * @param webflowName the webflow name
144      */
145     public void setWebflowName(String webflowName) {
146         this.webflowName = webflowName;
147     }
148 
149     /***
150      * Adds a deferred ownership.
151      *
152      * @param objectKey the object key
153      * @param objectOwnership the object
154      */
155     public void addOwnership(String objectKey, int objectOwnership) {
156         ownershipList.add(new Ownership(objectKey, objectOwnership));
157     }
158 
159     /***
160      * Notify that a page has been reached.
161      *
162      * @param container the attribute container
163      * @param url the http session
164      */
165     public void notifyPage(AttributeContainer container, String url) {
166         if (NavigationManagerFactory.existsInstance(container) || frameworkJustActivated()) {
167             NavigationManager navigationManager = NavigationManagerFactory.getInstance(container);
168 
169             // force a previous webflow, if necessary
170             if (previousWebflowToForce != null) {
171                 if (navigationManager.isWebflowVisited(previousWebflowToForce)) {
172                     String topPage = navigationManager.getPreviousWebflow(previousWebflowToForce);
173 
174                     if (topPage != null) {
175                         navigationManager.notifyPage(container, topPage, null, null);
176                     }
177                 } else {
178                     log.warn("The specified previous webflow '" + previousWebflowToForce +
179                         "' to force is noy currently open.");
180                 }
181             }
182 
183             // notify
184             if (webflowName == null) {
185                 navigationManager.notifyPage(container, url, addedObjectSet, removedObjectSet);
186             } else {
187                 navigationManager.notifyPage(container, url, webflowName, defaultOwnership, addedObjectSet,
188                     removedObjectSet);
189             }
190 
191             // set ownership for single objectspreviousWebflowToForce
192             for (Iterator i = ownershipList.iterator(); i.hasNext();) {
193                 Ownership deferredOwnership = (Ownership) i.next();
194                 navigationManager.setObjectOwnership(deferredOwnership.getKey(), deferredOwnership.getOwnership());
195             }
196         }
197     }
198 
199     /***
200      * Removes an attribute.
201      *
202      * @param container the attribute container
203      * @param objectKey the object key to remove
204      */
205     public void removeAttribute(AttributeContainer container, String objectKey) {
206         if (Config.getInstance().isEnabled() && Config.getInstance().isCleanerEnabled() &&
207                 !NavigationManagerFactory.isNavigationManagerKey(objectKey)) {
208             if (addedObjectSet.contains(objectKey)) {
209                 addedObjectSet.remove(objectKey);
210             } else {
211                 removedObjectSet.add(objectKey);
212             }
213         }
214     }
215 
216     /***
217      * Reset the Webflow Navigation Manager framework.
218      */
219     public void resetFramework() {
220         this.reset = true;
221     }
222 
223     /***
224      * Returns true if the framework has just been activated.
225      *
226      * @return true if the framework has just been activated
227      */
228     protected boolean frameworkJustActivated() {
229         return ((webflowName != null) || (ownershipList.size() > 0));
230     }
231 
232     /***
233      * Defines a ownership as a key-ownership pair.
234      *
235      * @author <a href="mailto:malbari@users.sourceforge.net">Maurizio Albari</a>
236      * @version 1.0.6
237      */
238     protected class Ownership implements Serializable {
239         /*** 
240          * The object key.
241          */
242         protected String key;
243 
244         /*** 
245          * The object ownership.
246          */
247         protected int ownership;
248 
249         /***
250          * Creates a new Ownership object.
251          *
252          * @param key the object key
253          * @param ownership the object ownership
254          */
255         public Ownership(String key, int ownership) {
256             this.key = key;
257             this.ownership = ownership;
258         }
259 
260         /***
261          * The object key.
262          *
263          * @return the object key
264          */
265         public String getKey() {
266             return key;
267         }
268 
269         /***
270          * Gets the object ownership.
271          *
272          * @return the object ownership
273          */
274         public int getOwnership() {
275             return ownership;
276         }
277     }
278 }