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.StateHolder;
22  import javax.faces.component.UIComponentBase;
23  import javax.faces.context.FacesContext;
24  import javax.faces.el.EvaluationException;
25  import javax.faces.el.MethodBinding;
26  import javax.faces.el.MethodNotFoundException;
27  import javax.faces.FacesException;
28  import java.util.Collection;
29  
30  /**
31   * a facade for the original method binding to deal with end conversation conditions
32   *
33   * @author imario@apache.org
34   */
35  public class EndConversationMethodBindingFacade extends MethodBinding implements StateHolder
36  {
37      private MethodBinding original;
38      private String conversationName;
39      private Collection onOutcomes;
40      private String errorOutcome;
41      private Boolean restart;
42      private MethodBinding restartAction;
43  
44      private boolean _transient = false;
45  
46      public EndConversationMethodBindingFacade()
47      {
48      }
49  
50      public EndConversationMethodBindingFacade(
51          String conversation, Collection onOutcomes, MethodBinding original,
52          String errorOutcome, Boolean restart, MethodBinding restartAction)
53      {
54          this.original = original;
55          this.conversationName = conversation;
56          this.onOutcomes = onOutcomes;
57          this.errorOutcome = errorOutcome;
58          this.restart = restart;
59          this.restartAction = restartAction;
60      }
61  
62      public String getConversationName()
63      {
64          return conversationName;
65      }
66  
67      public String getExpressionString()
68      {
69          if (original == null)
70          {
71              return null;
72          }
73          return original.getExpressionString();
74      }
75  
76      public Class getType(FacesContext context) throws MethodNotFoundException
77      {
78          if (original == null)
79          {
80              return null;
81          }
82          return original.getType(context);
83      }
84  
85      public Object invoke(FacesContext context, Object[] values) throws EvaluationException, MethodNotFoundException
86      {
87          boolean ok = false;
88          Object returnValue = null;
89          try
90          {
91              if (original != null)
92              {
93                  returnValue = original.invoke(context, values);
94              }
95              ok = true;
96          }
97          catch (Throwable t)
98          {
99              ConversationManager conversationManager = ConversationManager.getInstance(context);
100             conversationManager.purgePersistence();
101 
102             if (errorOutcome != null)
103             {
104                 conversationManager.getMessager().setConversationException(context, t);
105 
106                 returnValue = errorOutcome;
107             }
108             else
109             {
110                 throw new FacesException(t);
111             }
112         }
113         finally
114         {
115             boolean end = true;
116             if (ok)
117             {
118                 if (onOutcomes != null && onOutcomes.size() > 0)
119                 {
120                     end = onOutcomes.contains(returnValue);
121                 }
122 
123                 if (end)
124                 {
125                     ConversationUtils.endAndRestartConversation(context, conversationName, restart, restartAction);
126                 }
127             }
128         }
129         return returnValue;
130     }
131 
132     public void setTransient(boolean newTransientValue)
133     {
134         _transient = newTransientValue;
135     }
136 
137     public boolean isTransient()
138     {
139         return _transient;
140     }
141 
142     public void restoreState(FacesContext context, Object states)
143     {
144         Object[] state = (Object[]) states;
145 
146         original = (MethodBinding) UIComponentBase.restoreAttachedState(context, state[0]);
147         conversationName = (String) state[1];
148         onOutcomes = (Collection) state[2];
149         errorOutcome = (String) state[3];
150         restart = (Boolean) state[4];
151         restartAction = (MethodBinding) UIComponentBase.restoreAttachedState(context, state[5]);
152     }
153 
154     public Object saveState(FacesContext context)
155     {
156         return new Object[]
157             {
158                 UIComponentBase.saveAttachedState(context, original),
159                 conversationName,
160                 onOutcomes,
161                 errorOutcome,
162                 restart,
163                 UIComponentBase.saveAttachedState(context, restartAction)
164             };
165     }
166 }