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 java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  import javax.el.ValueExpression;
26  import javax.faces.flow.builder.NavigationCaseBuilder;
27  import org.apache.myfaces.flow.FlowImpl;
28  import org.apache.myfaces.flow.NavigationCaseImpl;
29  
30  /**
31   *
32   * @author lu4242
33   */
34  public class NavigationCaseBuilderImpl extends NavigationCaseBuilder
35  {
36      private FlowImpl _facesFlow;
37      private FlowBuilderImpl _flowBuilder;
38      private NavigationCaseImpl _navigationCaseImpl;
39      
40      public NavigationCaseBuilderImpl(FlowBuilderImpl flowBuilder, FlowImpl facesFlow)
41      {
42          this._flowBuilder = flowBuilder;
43          this._facesFlow = facesFlow;
44          this._navigationCaseImpl = new NavigationCaseImpl();
45      }
46      
47      @Override
48      public NavigationCaseBuilder fromViewId(String fromViewId)
49      {
50          // This is the best place to add the navigation case into the flow, because
51          // fromViewId is required (cannot be null, and null does not mean '*')
52          if (this._navigationCaseImpl.getFromViewId() != null)
53          {
54              this._facesFlow.removeNavigationCase(_navigationCaseImpl);
55          }
56          if (fromViewId != null)
57          {
58              this._navigationCaseImpl.setFromViewId(fromViewId);
59              this._facesFlow.addNavigationCase(_navigationCaseImpl);
60          }
61          return this;
62      }
63  
64      @Override
65      public NavigationCaseBuilder fromAction(String fromAction)
66      {
67          this._navigationCaseImpl.setFromAction(fromAction);
68          return this;
69      }
70  
71      @Override
72      public NavigationCaseBuilder fromOutcome(String fromOutcome)
73      {
74          this._navigationCaseImpl.setFromOutcome(fromOutcome);
75          return this;
76      }
77  
78      @Override
79      public NavigationCaseBuilder toViewId(String toViewId)
80      {
81          this._navigationCaseImpl.setToViewId(toViewId);
82          return this;
83      }
84  
85      @Override
86      public NavigationCaseBuilder toFlowDocumentId(String toFlowDocumentId) 
87      {
88          this._navigationCaseImpl.setToFlowDocumentId(toFlowDocumentId);
89          return this;
90      }
91  
92      @Override
93      public NavigationCaseBuilder condition(String condition)
94      {
95          this._navigationCaseImpl.setConditionExpression(null);
96          this._navigationCaseImpl.setCondition(condition);
97          return this;
98      }
99  
100     @Override
101     public NavigationCaseBuilder condition(ValueExpression condition)
102     {
103         this._navigationCaseImpl.setCondition(null);
104         this._navigationCaseImpl.setConditionExpression(condition);
105         return this;
106     }
107 
108     @Override
109     public RedirectBuilder redirect()
110     {
111         this._navigationCaseImpl.setRedirect(true);
112         return new RedirectBuilderImpl();
113     }
114     
115     public class RedirectBuilderImpl extends RedirectBuilder
116     {
117 
118         @Override
119         public RedirectBuilder parameter(String name, String value)
120         {
121             //_navigationCaseImpl.
122             Map<String, List<String>> map = _navigationCaseImpl.getParameters();
123             if (map == null)
124             {
125                 map = new HashMap<String, List<String>>();
126                 _navigationCaseImpl.setParameters(map);
127             }
128             List<String> values = map.get(name);
129             if (values == null)
130             {
131                 values = new ArrayList<String>();
132                 map.put(name, values);
133             }
134             values.add(value);
135             return this;
136         }
137 
138         @Override
139         public RedirectBuilder includeViewParams()
140         {
141             _navigationCaseImpl.setIncludeViewParams(true);
142             return this;
143         }
144         
145     }
146 }