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.custom.conversation;
20  
21  import javax.faces.component.UICommand;
22  import javax.faces.context.FacesContext;
23  import javax.faces.el.ValueBinding;
24  import javax.faces.el.VariableResolver;
25  import java.io.IOException;
26  import java.util.ArrayList;
27  import java.util.Iterator;
28  import java.util.List;
29  
30  /**
31   * start a conversation
32   *
33   *   
34   * @author imario@apache.org
35   */
36  public class UIStartConversation extends AbstractConversationComponent
37  {
38      public final static String COMPONENT_TYPE = "org.apache.myfaces.StartConversation";
39      private final static String CONVERSATION_SYSTEM_SETUP = "org.apache.myfaces.ConversationSystemSetup";
40  
41      private boolean inited;
42      private Boolean persistence;
43  
44      public static class ConversationStartAction extends AbstractConversationActionListener
45      {
46          private boolean persistence;
47          private List beanToElevate;
48  
49          public void doConversationAction(AbstractConversationComponent abstractConversationComponent)
50          {
51              ConversationManager conversationManager = ConversationManager.getInstance();
52              conversationManager.startConversation(getConversationName(), isPersistence());
53  
54              if (beanToElevate != null)
55              {
56                  FacesContext context = FacesContext.getCurrentInstance();
57  
58                  Iterator iterBeans = beanToElevate.iterator();
59                  while (iterBeans.hasNext())
60                  {
61                      String vb = (String) iterBeans.next();
62                      UIConversation.elevateBean(
63                          context,
64                          getConversationName(),
65                          context.getApplication().createValueBinding(vb));
66                  }
67              }
68          }
69  
70          public boolean isPersistence()
71          {
72              return persistence;
73          }
74  
75          public void setPersistence(boolean persistence)
76          {
77              this.persistence = persistence;
78          }
79  
80          public void addBeanToElevate(String beanBinding)
81          {
82              if (beanToElevate == null)
83              {
84                  beanToElevate = new ArrayList();
85              }
86              beanToElevate.add(beanBinding);
87          }
88  
89  
90          public Object saveState(FacesContext context)
91          {
92              return new Object[]
93              {
94                  super.saveState(context),
95                  Boolean.valueOf(persistence),
96                  beanToElevate
97              };
98          }
99  
100         public void restoreState(FacesContext context, Object state)
101         {
102             Object[] states = (Object[]) state;
103             super.restoreState(context, states[0]);
104             persistence = ((Boolean) states[1]).booleanValue();
105             beanToElevate = (List) states[2];
106         }
107     }
108 
109     public void encodeEnd(FacesContext context) throws IOException
110     {
111         super.encodeEnd(context);
112 
113         setupConversationSystem(context);
114 
115         UICommand command = ConversationUtils.findParentCommand(this);
116         if (command != null)
117         {
118             if (!inited)
119             {
120                 ConversationStartAction actionListener = new ConversationStartAction();
121                 actionListener.setConversationName(getName());
122                 actionListener.setPersistence(toBoolean(getPersistence()));
123                 command.addActionListener(actionListener);
124 
125                 Iterator iterChildren = getChildren().iterator();
126                 while (iterChildren.hasNext())
127                 {
128                     Object child = iterChildren.next();
129                     if (child instanceof UIConversation)
130                     {
131                         UIConversation conversation = (UIConversation) child;
132                         actionListener.addBeanToElevate(conversation.getBeanBinding().getExpressionString());
133                     }
134                 }
135 
136                 inited = true;
137             }
138         }
139         else
140         {
141             ConversationManager conversationManager = ConversationManager.getInstance();
142             conversationManager.startConversation(getName(), toBoolean(getPersistence()));
143         }
144     }
145 
146     protected boolean toBoolean(Boolean bool)
147     {
148         if (bool != null)
149         {
150             return bool.booleanValue();
151         }
152         return false;
153     }
154 
155     protected void setupConversationSystem(FacesContext context)
156     {
157         if (Boolean.TRUE.equals(context.getExternalContext().getApplicationMap().get(CONVERSATION_SYSTEM_SETUP)))
158         {
159             return;
160         }
161 
162         VariableResolver originalVR = context.getApplication().getVariableResolver();
163         context.getApplication().setVariableResolver(new ConversationVariableResolver(originalVR));
164 
165         context.getExternalContext().getApplicationMap().put(CONVERSATION_SYSTEM_SETUP, Boolean.TRUE);
166     }
167 
168     public void restoreState(FacesContext context, Object state)
169     {
170         Object[] states = (Object[]) state;
171         super.restoreState(context, states[0]);
172         inited = ((Boolean) states[1]).booleanValue();
173         persistence = (Boolean) states[2];
174     }
175 
176     public Object saveState(FacesContext context)
177     {
178         return new Object[]
179                           {
180                 super.saveState(context),
181                 inited?Boolean.TRUE:Boolean.FALSE,
182                 persistence
183                           };
184     }
185 
186     /**
187      * true|false - if this conversation requires a persistence manager. Default: false
188      * 
189      * @JSFProperty
190      * @return
191      */
192     public Boolean getPersistence()
193     {
194         if (persistence != null)
195         {
196                 return persistence;
197         }
198         ValueBinding vb = getValueBinding("persistence");
199         return vb != null ? (Boolean) vb.getValue(getFacesContext()) : null;
200     }
201 
202     public void setPersistence(Boolean persistence)
203     {
204         this.persistence = persistence;
205     }
206 }