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.builder;
20  
21  import javax.el.MethodExpression;
22  import javax.el.ValueExpression;
23  import javax.faces.context.FacesContext;
24  import javax.faces.flow.Flow;
25  import javax.faces.flow.builder.FlowBuilder;
26  import javax.faces.flow.builder.FlowCallBuilder;
27  import javax.faces.flow.builder.MethodCallBuilder;
28  import javax.faces.flow.builder.NavigationCaseBuilder;
29  import javax.faces.flow.builder.ReturnBuilder;
30  import javax.faces.flow.builder.SwitchBuilder;
31  import javax.faces.flow.builder.ViewBuilder;
32  import org.apache.myfaces.flow.FlowImpl;
33  import org.apache.myfaces.flow.ParameterImpl;
34  
35  /**
36   *
37   * @since 2.2
38   * @author Leonardo Uribe
39   */
40  public class FlowBuilderImpl extends FlowBuilder
41  {
42  
43      private static final Class[] EMPTY_PARAMS = new Class[]
44      {
45      };
46      private FlowImpl _facesFlow;
47      private FacesContext _facesContext;
48  
49      public FlowBuilderImpl()
50      {
51          _facesFlow = new FlowImpl();
52      }
53  
54      public FlowBuilderImpl(FacesContext context)
55      {
56          super();
57          _facesContext = context;
58      }
59  
60      @Override
61      public FlowBuilder id(String definingDocumentId, String id)
62      {
63          _facesFlow.setDefiningDocumentId(definingDocumentId);
64          _facesFlow.setId(id);
65          return this;
66      }
67  
68      @Override
69      public ViewBuilder viewNode(String viewNodeId, String vdlDocumentId)
70      {
71          return new ViewBuilderImpl(_facesFlow, viewNodeId, vdlDocumentId);
72      }
73  
74      @Override
75      public SwitchBuilder switchNode(String switchNodeId)
76      {
77          return new SwitchBuilderImpl(this, _facesFlow, switchNodeId);
78      }
79  
80      @Override
81      public ReturnBuilder returnNode(String returnNodeId)
82      {
83          return new ReturnBuilderImpl(this, _facesFlow, returnNodeId);
84      }
85  
86      @Override
87      public MethodCallBuilder methodCallNode(String methodCallNodeId)
88      {
89          return new MethodCallBuilderImpl(this, _facesFlow, methodCallNodeId);
90      }
91  
92      @Override
93      public FlowCallBuilder flowCallNode(String flowCallNodeId)
94      {
95          return new FlowCallBuilderImpl(this, _facesFlow, flowCallNodeId);
96      }
97  
98      @Override
99      public FlowBuilder initializer(MethodExpression methodExpression)
100     {
101         _facesFlow.setInitializer(methodExpression);
102         return this;
103     }
104 
105     @Override
106     public FlowBuilder initializer(String methodExpression)
107     {
108         _facesFlow.setInitializer(createMethodExpression(methodExpression));
109         return this;
110     }
111 
112     @Override
113     public FlowBuilder finalizer(MethodExpression methodExpression)
114     {
115         _facesFlow.setFinalizer(methodExpression);
116         return this;
117     }
118 
119     @Override
120     public FlowBuilder finalizer(String methodExpression)
121     {
122         _facesFlow.setFinalizer(createMethodExpression(methodExpression));
123         return this;
124     }
125 
126     @Override
127     public FlowBuilder inboundParameter(String name, ValueExpression value)
128     {
129         _facesFlow.putInboundParameter(name, new ParameterImpl(name, value));
130         return this;
131     }
132 
133     @Override
134     public FlowBuilder inboundParameter(String name, String value)
135     {
136         _facesFlow.putInboundParameter(name, new ParameterImpl(name,
137             createValueExpression(value)));
138         return this;
139     }
140 
141     @Override
142     public Flow getFlow()
143     {
144         _facesContext = null;
145         return _facesFlow;
146     }
147 
148     /**
149      * The idea is grab FacesContext just once and then when the flow is returned clear the variable.
150      *
151      * @return
152      */
153     FacesContext getFacesContext()
154     {
155         if (_facesContext == null)
156         {
157             _facesContext = FacesContext.getCurrentInstance();
158         }
159         return _facesContext;
160     }
161     
162     public MethodExpression createMethodExpression(String methodExpression)
163     {
164         FacesContext facesContext = getFacesContext();
165         return facesContext.getApplication().getExpressionFactory().createMethodExpression(
166             facesContext.getELContext(), methodExpression, null, EMPTY_PARAMS);
167     }
168     
169     public MethodExpression createMethodExpression(String methodExpression, Class[] paramTypes)
170     {
171         FacesContext facesContext = getFacesContext();
172         return facesContext.getApplication().getExpressionFactory().createMethodExpression(
173             facesContext.getELContext(), methodExpression, null, paramTypes);
174     }
175     
176     public ValueExpression createValueExpression(String value)
177     {
178         FacesContext facesContext = getFacesContext();
179         return facesContext.getApplication().getExpressionFactory()
180             .createValueExpression(facesContext.getELContext(), value, Object.class); 
181     }
182 
183     @Override
184     public NavigationCaseBuilder navigationCase()
185     {
186         return new NavigationCaseBuilderImpl(this, _facesFlow);
187     }
188 }