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 org.apache.myfaces.shared_tomahawk.util.StringUtils;
22  
23  import javax.faces.component.UICommand;
24  import javax.faces.context.FacesContext;
25  import javax.faces.el.MethodBinding;
26  import javax.faces.el.ValueBinding;
27  import java.io.IOException;
28  import java.util.Arrays;
29  import java.util.Collection;
30  
31  /**
32   * end a conversation
33   *
34   * @JSFComponent
35   *   name = "s:endConversation"
36   *   tagClass = "org.apache.myfaces.custom.conversation.EndConversationTag"
37   *   
38   * @author imario@apache.org
39   */
40  public class UIEndConversation extends AbstractConversationComponent
41  {
42      public static final String COMPONENT_TYPE = "org.apache.myfaces.EndConversation";
43  
44      private String onOutcome;
45      private String errorOutcome;
46      private Boolean restart;
47      private MethodBinding restartAction;
48  
49      private boolean inited = false;
50  
51      /*
52      public static class ConversationEndAction extends AbstractConversationActionListener
53      {
54          public void doConversationAction(AbstractConversationComponent abstractConversationComponent)
55          {
56              ConversationManager.getInstance().registerEndConversation(getConversationName());
57          }
58      }
59      */
60  
61      public void encodeBegin(FacesContext context) throws IOException
62      {
63          super.encodeBegin(context);
64  
65          UICommand command = ConversationUtils.findParentCommand(this);
66          if (command != null)
67          {
68              if (!inited)
69              {
70                  /*
71                  ConversationEndAction actionListener = new ConversationEndAction();
72                  actionListener.setConversationName(getName());
73                  command.addActionListener(actionListener);
74                  */
75                  MethodBinding original = command.getAction();
76                  command.setAction(new EndConversationMethodBindingFacade(
77                      getName(),
78                      getOnOutcomes(),
79                      original,
80                      getErrorOutcome(),
81                      getRestart(),
82                      getRestartAction()));
83                  inited = true;
84              }
85          }
86          else
87          {
88              ConversationUtils.endAndRestartConversation(context,
89                  getName(),
90                  getRestart(),
91                  getRestartAction());
92          }
93      }
94  
95      private Collection getOnOutcomes()
96      {
97          String onOutcome = getOnOutcome();
98          if (onOutcome == null || onOutcome.trim().length() < 1)
99          {
100             return null;
101         }
102 
103         return Arrays.asList(StringUtils.trim(StringUtils.splitShortString(onOutcome, ',')));
104     }
105 
106     public void restoreState(FacesContext context, Object state)
107     {
108         Object[] states = (Object[]) state;
109         super.restoreState(context, states[0]);
110         inited = ((Boolean) states[1]).booleanValue();
111         onOutcome = (String) states[2];
112         errorOutcome = (String) states[3];
113         restart = (Boolean) states[4];
114         restartAction = (MethodBinding) restoreAttachedState(context, states[5]);
115     }
116 
117     public Object saveState(FacesContext context)
118     {
119         return new Object[]
120             {
121                 super.saveState(context),
122                 inited ? Boolean.TRUE : Boolean.FALSE,
123                 onOutcome,
124                 errorOutcome,
125                 restart,
126                 saveAttachedState(context, restartAction)
127             };
128     }
129 
130     /**
131      * end the conversation only if the action outcome matches the given onOutcome. 
132      * 
133      * This can be a comma separated list.
134      * 
135      * @JSFProperty
136      * @return
137      */
138     public String getOnOutcome()
139     {
140         if (onOutcome != null)
141         {
142             return onOutcome;
143         }
144         ValueBinding vb = getValueBinding("onOutcome");
145         if (vb == null)
146         {
147             return null;
148         }
149         return (String) vb.getValue(getFacesContext());
150     }
151 
152     public void setOnOutcome(String onOutcome)
153     {
154         this.onOutcome = onOutcome;
155     }
156 
157     /**
158      * on exception use the given outcome for further navigation
159      * 
160      * @JSFProperty
161      * @return
162      */
163     public String getErrorOutcome()
164     {
165         if (errorOutcome != null)
166         {
167             return errorOutcome;
168         }
169         ValueBinding vb = getValueBinding("errorOutcome");
170         if (vb == null)
171         {
172             return null;
173         }
174         return (String) vb.getValue(getFacesContext());
175     }
176 
177     public void setErrorOutcome(String errorOutcome)
178     {
179         this.errorOutcome = errorOutcome;
180     }
181 
182     /**
183      * true|false|valueBinding - true if the conversation should be restarted immediately
184      * 
185      * @JSFProperty
186      * @return
187      */
188     public Boolean getRestart()
189     {
190         if (restart != null)
191         {
192             return restart;
193         }
194         ValueBinding vb = getValueBinding("restart");
195         if (vb == null)
196         {
197             return null;
198         }
199         return (Boolean) vb.getValue(getFacesContext());
200     }
201 
202     public void setRestart(Boolean restart)
203     {
204         this.restart = restart;
205     }
206 
207     /**
208      * the action which should be called in case of a restart
209      * 
210      * @JSFProperty
211      *   methodSignature = "java.lang.String"
212      *   returnSignature = "void"
213      *   stateHolder = "true"
214      * @return
215      */
216     public MethodBinding getRestartAction()
217     {
218         return restartAction;
219     }
220 
221     public void setRestartAction(MethodBinding restartAction)
222     {
223         this.restartAction = restartAction;
224     }
225 }