KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > config > NavigationConfigElement


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.config;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.alfresco.config.ConfigElement;
23 import org.alfresco.config.element.ConfigElementAdapter;
24 import org.alfresco.config.element.GenericConfigElement;
25
26 /**
27  * Custom config element that represents the config data for navigation
28  *
29  * @author gavinc
30  */

31 public class NavigationConfigElement extends ConfigElementAdapter
32 {
33    private HashMap JavaDoc<String JavaDoc, NavigationResult> viewIds = new HashMap JavaDoc<String JavaDoc, NavigationResult>();
34    private HashMap JavaDoc<String JavaDoc, NavigationResult> outcomes = new HashMap JavaDoc<String JavaDoc, NavigationResult>();
35    
36    private boolean kidsPopulated = false;
37    
38    /**
39     * Default constructor
40     */

41    public NavigationConfigElement()
42    {
43       super("navigation");
44    }
45    
46    /**
47     * Constructor
48     *
49     * @param name Name of the element this config element represents
50     */

51    public NavigationConfigElement(String JavaDoc name)
52    {
53       super(name);
54    }
55    
56    /**
57     * @see org.alfresco.config.ConfigElement#getChildren()
58     */

59    public List JavaDoc<ConfigElement> getChildren()
60    {
61       // lazily build the list of generic config elements representing
62
// the navigation overrides as the caller may not even call this method
63

64       List JavaDoc<ConfigElement> kids = null;
65       
66       if (this.viewIds.size() > 0 || this.outcomes.size() > 0)
67       {
68          if (this.kidsPopulated == false)
69          {
70             // create generic config elements for the from-view-id items
71
for (String JavaDoc fromViewId : this.viewIds.keySet())
72             {
73                GenericConfigElement ce = new GenericConfigElement(NavigationElementReader.ELEMENT_OVERRIDE);
74                ce.addAttribute(NavigationElementReader.ATTR_FROM_VIEWID, fromViewId);
75
76                NavigationResult navRes = this.viewIds.get(fromViewId);
77                String JavaDoc result = navRes.getResult();
78                if (navRes.isOutcome())
79                {
80                   ce.addAttribute(NavigationElementReader.ATTR_TO_OUTCOME, result);
81                }
82                else
83                {
84                   ce.addAttribute(NavigationElementReader.ATTR_TO_VIEWID, result);
85                }
86                
87                // add the element
88
this.children.add(ce);
89             }
90             
91             // create generic config elements for the from-outcome items
92
for (String JavaDoc fromOutcome : this.outcomes.keySet())
93             {
94                GenericConfigElement ce = new GenericConfigElement(NavigationElementReader.ELEMENT_OVERRIDE);
95                ce.addAttribute(NavigationElementReader.ATTR_FROM_OUTCOME, fromOutcome);
96
97                NavigationResult navRes = this.outcomes.get(fromOutcome);
98                String JavaDoc result = navRes.getResult();
99                if (navRes.isOutcome())
100                {
101                   ce.addAttribute(NavigationElementReader.ATTR_TO_OUTCOME, result);
102                }
103                else
104                {
105                   ce.addAttribute(NavigationElementReader.ATTR_TO_VIEWID, result);
106                }
107                
108                // add the element
109
this.children.add(ce);
110             }
111             
112             this.kidsPopulated = true;
113          }
114          
115          kids = super.getChildren();
116       }
117       
118       return kids;
119    }
120    
121    /**
122     * @see org.alfresco.config.ConfigElement#combine(org.alfresco.config.ConfigElement)
123     */

124    public ConfigElement combine(ConfigElement configElement)
125    {
126       NavigationConfigElement combined = new NavigationConfigElement();
127
128       // add all the existing from view id overrides
129
for (String JavaDoc fromViewId : this.viewIds.keySet())
130       {
131          combined.addOverride(fromViewId, null, this.viewIds.get(fromViewId));
132       }
133       
134       // add all the existing from outcome overrides
135
for (String JavaDoc fromOutcome : this.outcomes.keySet())
136       {
137          combined.addOverride(null, fromOutcome, this.outcomes.get(fromOutcome));
138       }
139       
140       // add all the from view id overrides from the given element
141
NavigationConfigElement navCfg = (NavigationConfigElement)configElement;
142       HashMap JavaDoc<String JavaDoc, NavigationResult> viewIds = navCfg.getViewIds();
143       for (String JavaDoc fromViewId : viewIds.keySet())
144       {
145          combined.addOverride(fromViewId, null, viewIds.get(fromViewId));
146       }
147       
148       // add all the from outcome overrides from the given element
149
HashMap JavaDoc<String JavaDoc, NavigationResult> outcomes = navCfg.getOutcomes();
150       for (String JavaDoc fromOutcome : outcomes.keySet())
151       {
152          combined.addOverride(null, fromOutcome, outcomes.get(fromOutcome));
153       }
154       
155       return combined;
156    }
157    
158    /**
159     * Returns the list of view ids that have overrides defined
160     *
161     * @return Map of view ids and navigation results
162     */

163    public HashMap JavaDoc<String JavaDoc, NavigationResult> getViewIds()
164    {
165       return this.viewIds;
166    }
167    
168    /**
169     * Returns the list of outcomes that have overrides defined
170     *
171     * @return Map of outcomes and navigation results
172     */

173    public HashMap JavaDoc<String JavaDoc, NavigationResult> getOutcomes()
174    {
175       return this.outcomes;
176    }
177    
178    /**
179     * Adds an override configuration item
180     *
181     * @param fromViewId The from-view-id value from the config
182     * @param fromOutcome The from-outcome value from the config
183     * @param toViewId The to-view-id value from the config
184     * @param toOutcome The to-outcome value from the config
185     */

186    public void addOverride(String JavaDoc fromViewId, String JavaDoc fromOutcome,
187          String JavaDoc toViewId, String JavaDoc toOutcome)
188    {
189       // NOTE: the constructor will check the validity of the to* parameters
190
NavigationResult result = new NavigationResult(toViewId, toOutcome);
191       addOverride(fromViewId, fromOutcome, result);
192    }
193    
194    /**
195     * Adds an override configuration item
196     *
197     * @param fromViewId The from-view-id value from the config
198     * @param fromOutcome The from-outcome value from the config
199     * @param result The navigation result object to add
200     */

201    public void addOverride(String JavaDoc fromViewId, String JavaDoc fromOutcome,
202          NavigationResult result)
203    {
204       if (fromViewId != null && fromOutcome != null)
205       {
206          throw new IllegalStateException JavaDoc("You can not have both a from-view-id and from-outcome");
207       }
208       
209       if (fromViewId != null)
210       {
211          this.viewIds.put(fromViewId, result);
212       }
213       else if (fromOutcome != null)
214       {
215          this.outcomes.put(fromOutcome, result);
216       }
217    }
218    
219    /**
220     * Returns the best match navigation override configured for the given
221     * current view id and/or outcome.
222     *
223     * If an outcome is passed it takes precedence, the view id will not be
224     * used.
225     *
226     * @param fromViewId The current view id
227     * @param fromOutcome The current outcome
228     * @return The navigation result
229     */

230    public NavigationResult getOverride(String JavaDoc fromViewId, String JavaDoc fromOutcome)
231    {
232       NavigationResult result = null;
233       
234       // look for a match for the outcome if one was provided
235
if (fromOutcome != null)
236       {
237          result = this.outcomes.get(fromOutcome);
238       }
239       else if (fromViewId != null)
240       {
241          result = this.viewIds.get(fromViewId);
242       }
243       
244       return result;
245    }
246 }
247
Popular Tags