View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.flow;
20  
21  import java.util.List;
22  import java.util.Map;
23  import javax.el.ExpressionFactory;
24  import javax.el.ValueExpression;
25  import javax.faces.application.NavigationCase;
26  import javax.faces.context.FacesContext;
27  
28  /**
29   *
30   * @author lu4242
31   */
32  public class NavigationCaseImpl extends NavigationCase implements Freezable
33  {
34  
35      private String _condition;
36      private String _fromAction;
37      private String _fromOutcome;
38      private String _fromViewId;
39      private String _toViewId;
40      private String _toFlowDocumentId;
41      private boolean _includeViewParams;
42      private boolean _redirect;
43      private Map<String, List<String>> _parameters;
44      private ValueExpression _conditionExpression;
45      private ValueExpression _toViewIdExpression;
46  
47      private boolean _initialized;
48  
49      public NavigationCaseImpl()
50      {
51          super(null, null, null, null, null, null, false, false);
52      }
53  
54      public NavigationCaseImpl(String fromViewId, String fromAction, String fromOutcome, String condition, 
55              String toViewId,
56              Map<String, List<String>> parameters, boolean redirect, boolean includeViewParams)
57      {
58          super(fromViewId, fromAction, fromOutcome, condition, toViewId, parameters, redirect, includeViewParams);
59          _condition = condition;
60          _fromViewId = fromViewId;
61          _fromAction = fromAction;
62          _fromOutcome = fromOutcome;
63          _toViewId = toViewId;
64          _toFlowDocumentId = null;
65          _redirect = redirect;
66          _includeViewParams = includeViewParams;
67          _parameters = parameters;
68      }
69  
70      public NavigationCaseImpl(String fromViewId, String fromAction, String fromOutcome, String condition, 
71              String toViewId,
72              String toFlowDocumentId, Map<String, List<String>> parameters, boolean redirect,
73              boolean includeViewParams)
74      {
75          super(fromViewId, fromAction, fromOutcome, condition, toViewId, toFlowDocumentId, parameters, redirect, 
76                  includeViewParams);
77          _condition = condition;
78          _fromViewId = fromViewId;
79          _fromAction = fromAction;
80          _fromOutcome = fromOutcome;
81          _toViewId = toViewId;
82          _toFlowDocumentId = toFlowDocumentId;
83          _redirect = redirect;
84          _includeViewParams = includeViewParams;
85          _parameters = parameters;
86  
87      }
88  
89      /**
90       * @return the _condition
91       */
92      public String getCondition()
93      {
94          return _condition;
95      }
96  
97      /**
98       * @param condition the _condition to set
99       */
100     public void setCondition(String condition)
101     {
102         checkInitialized();
103         this._condition = condition;
104     }
105 
106     /**
107      * @return the _fromAction
108      */
109     public String getFromAction()
110     {
111         return _fromAction;
112     }
113 
114     /**
115      * @param fromAction the _fromAction to set
116      */
117     public void setFromAction(String fromAction)
118     {
119         checkInitialized();
120         this._fromAction = fromAction;
121     }
122 
123     /**
124      * @return the _fromOutcome
125      */
126     public String getFromOutcome()
127     {
128         return _fromOutcome;
129     }
130 
131     /**
132      * @param fromOutcome the _fromOutcome to set
133      */
134     public void setFromOutcome(String fromOutcome)
135     {
136         checkInitialized();
137         this._fromOutcome = fromOutcome;
138     }
139 
140     /**
141      * @return the _fromViewId
142      */
143     public String getFromViewId()
144     {
145         return _fromViewId;
146     }
147 
148     /**
149      * @param fromViewId the _fromViewId to set
150      */
151     public void setFromViewId(String fromViewId)
152     {
153         checkInitialized();
154         this._fromViewId = fromViewId;
155     }
156 
157     /**
158      * @return the _toViewId
159      */
160     public String getToViewId()
161     {
162         return _toViewId;
163     }
164 
165     /**
166      * @param toViewId the _toViewId to set
167      */
168     public void setToViewId(String toViewId)
169     {
170         checkInitialized();
171         this._toViewId = toViewId;
172     }
173 
174     /**
175      * @return the _toFlowDocumentId
176      */
177     public String getToFlowDocumentId()
178     {
179         return _toFlowDocumentId;
180     }
181 
182     /**
183      * @param toFlowDocumentId the _toFlowDocumentId to set
184      */
185     public void setToFlowDocumentId(String toFlowDocumentId)
186     {
187         checkInitialized();
188         this._toFlowDocumentId = toFlowDocumentId;
189     }
190 
191     /**
192      * @return the _includeViewParams
193      */
194     public boolean isIncludeViewParams()
195     {
196         return _includeViewParams;
197     }
198 
199     /**
200      * @param includeViewParams the _includeViewParams to set
201      */
202     public void setIncludeViewParams(boolean includeViewParams)
203     {
204         checkInitialized();
205         this._includeViewParams = includeViewParams;
206     }
207 
208     /**
209      * @return the _redirect
210      */
211     public boolean isRedirect()
212     {
213         return _redirect;
214     }
215 
216     /**
217      * @param redirect the _redirect to set
218      */
219     public void setRedirect(boolean redirect)
220     {
221         checkInitialized();
222         this._redirect = redirect;
223     }
224 
225     /**
226      * @return the _parameters
227      */
228     public Map<String, List<String>> getParameters()
229     {
230         return _parameters;
231     }
232 
233     /**
234      * @param parameters the _parameters to set
235      */
236     public void setParameters(Map<String, List<String>> parameters)
237     {
238         checkInitialized();
239         this._parameters = parameters;
240     }
241 
242     /**
243      * @return the _conditionExpression
244      */
245     public ValueExpression getConditionExpression()
246     {
247         return _conditionExpression;
248     }
249 
250     /**
251      * @param conditionExpression the _conditionExpression to set
252      */
253     public void setConditionExpression(ValueExpression conditionExpression)
254     {
255         checkInitialized();
256         this._conditionExpression = conditionExpression;
257     }
258 
259     /**
260      * @return the _toViewIdExpression
261      */
262     public ValueExpression getToViewIdExpression()
263     {
264         return _toViewIdExpression;
265     }
266 
267     /**
268      * @param toViewIdExpression the _toViewIdExpression to set
269      */
270     public void setToViewIdExpression(ValueExpression toViewIdExpression)
271     {
272         checkInitialized();
273         this._toViewIdExpression = toViewIdExpression;
274     }
275 
276     public Boolean getCondition(FacesContext context)
277     {
278         if (_condition == null)
279         {
280             return null;
281         }
282 
283         ValueExpression expression = _getConditionExpression(context);
284 
285         return Boolean.TRUE.equals(expression.getValue(context.getELContext()));
286     }
287 
288     private ValueExpression _getConditionExpression(FacesContext context)
289     {
290         assert _condition != null;
291 
292         if (_conditionExpression == null)
293         {
294             ExpressionFactory factory = context.getApplication().getExpressionFactory();
295             _conditionExpression = factory.createValueExpression(context.getELContext(), _condition, Boolean.class);
296         }
297 
298         return _conditionExpression;
299     }
300 
301     public String getToViewId(FacesContext context)
302     {
303         if (_toViewId == null)
304         {
305             return null;
306         }
307 
308         ValueExpression expression = _getToViewIdExpression(context);
309 
310         return (String) ((expression.isLiteralText())
311                 ? expression.getExpressionString() : expression.getValue(context.getELContext()));
312     }
313 
314     private ValueExpression _getToViewIdExpression(FacesContext context)
315     {
316         assert _toViewId != null;
317 
318         if (_toViewIdExpression == null)
319         {
320             ExpressionFactory factory = context.getApplication().getExpressionFactory();
321             _toViewIdExpression = factory.createValueExpression(context.getELContext(), _toViewId, String.class);
322         }
323 
324         return _toViewIdExpression;
325     }
326 
327     public boolean hasCondition()
328     {
329         return _condition != null && _condition.length() > 0;
330     }
331 
332     @Override
333     public int hashCode()
334     {
335         int hash = 5;
336         hash = 47 * hash + (this._condition != null ? this._condition.hashCode() : 0);
337         hash = 47 * hash + (this._fromAction != null ? this._fromAction.hashCode() : 0);
338         hash = 47 * hash + (this._fromOutcome != null ? this._fromOutcome.hashCode() : 0);
339         hash = 47 * hash + (this._fromViewId != null ? this._fromViewId.hashCode() : 0);
340         hash = 47 * hash + (this._toViewId != null ? this._toViewId.hashCode() : 0);
341         hash = 47 * hash + (this._toFlowDocumentId != null ? this._toFlowDocumentId.hashCode() : 0);
342         hash = 47 * hash + (this._includeViewParams ? 1 : 0);
343         hash = 47 * hash + (this._redirect ? 1 : 0);
344         hash = 47 * hash + (this._parameters != null ? this._parameters.hashCode() : 0);
345         hash = 47 * hash + (this._conditionExpression != null ? this._conditionExpression.hashCode() : 0);
346         hash = 47 * hash + (this._toViewIdExpression != null ? this._toViewIdExpression.hashCode() : 0);
347         return hash;
348     }
349 
350     @Override
351     public boolean equals(Object obj)
352     {
353         if (obj == null)
354         {
355             return false;
356         }
357         if (getClass() != obj.getClass())
358         {
359             return false;
360         }
361         final NavigationCaseImpl other = (NavigationCaseImpl) obj;
362         if ((this._condition == null) ? (other._condition != null) : !this._condition.equals(other._condition))
363         {
364             return false;
365         }
366         if ((this._fromAction == null) ? (other._fromAction != null) : !this._fromAction.equals(other._fromAction))
367         {
368             return false;
369         }
370         if ((this._fromOutcome == null) ? (other._fromOutcome != null) : 
371                 !this._fromOutcome.equals(other._fromOutcome))
372         {
373             return false;
374         }
375         if ((this._fromViewId == null) ? (other._fromViewId != null) : !this._fromViewId.equals(other._fromViewId))
376         {
377             return false;
378         }
379         if ((this._toViewId == null) ? (other._toViewId != null) : !this._toViewId.equals(other._toViewId))
380         {
381             return false;
382         }
383         if ((this._toFlowDocumentId == null) ? (other._toFlowDocumentId != null) : 
384                 !this._toFlowDocumentId.equals(other._toFlowDocumentId))
385         {
386             return false;
387         }
388         if (this._includeViewParams != other._includeViewParams)
389         {
390             return false;
391         }
392         if (this._redirect != other._redirect)
393         {
394             return false;
395         }
396         if (this._parameters != other._parameters && (this._parameters == null || 
397                 !this._parameters.equals(other._parameters)))
398         {
399             return false;
400         }
401         if (this._conditionExpression != other._conditionExpression && (this._conditionExpression == null || 
402                 !this._conditionExpression.equals(other._conditionExpression)))
403         {
404             return false;
405         }
406         if (this._toViewIdExpression != other._toViewIdExpression && (this._toViewIdExpression == null || 
407                 !this._toViewIdExpression.equals(other._toViewIdExpression)))
408         {
409             return false;
410         }
411         return true;
412     }
413 
414     public void freeze()
415     {
416         _initialized = true;
417     }
418     
419     private void checkInitialized() throws IllegalStateException
420     {
421         if (_initialized)
422         {
423             throw new IllegalStateException("Flow is inmutable once initialized");
424         }
425     }
426 }