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