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  
20  package org.apache.myfaces.orchestra.conversation.jsf;
21  
22  import java.util.Iterator;
23  
24  import javax.faces.component.UICommand;
25  import javax.faces.component.UIComponent;
26  import javax.faces.context.FacesContext;
27  import javax.faces.el.MethodBinding;
28  
29  import org.apache.myfaces.orchestra.conversation.Conversation;
30  import org.apache.myfaces.orchestra.conversation.ConversationManager;
31  import org.apache.myfaces.orchestra.conversation.jsf.components.AbstractConversationComponent;
32  import org.apache.myfaces.orchestra.conversation.jsf.components.UIEndConversation;
33  import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter;
34  
35  /**
36   * <p>Various utilities used by the conversation framework</p>
37   * <p>Notice: this class is not meant to be used outside of this library</p>
38   */
39  public class _JsfConversationUtils
40  {
41      private _JsfConversationUtils()
42      {
43      }
44  
45      /**
46       * Find the first parent which is a command
47       */
48      public static UICommand findParentCommand(UIComponent base)
49      {
50          UIComponent parent = base;
51          do
52          {
53              parent = parent.getParent();
54              if (parent instanceof UICommand)
55              {
56                  return (UICommand) parent;
57              }
58          }
59          while (parent != null);
60  
61          return null;
62      }
63  
64      /**
65       * Find a child start or end conversation component for the given conversation name
66       */
67      public static AbstractConversationComponent findEndConversationComponent(
68              UIComponent component, String conversationName)
69      {
70          Iterator iterComponents = component.getFacetsAndChildren();
71          while (iterComponents.hasNext())
72          {
73              Object child = iterComponents.next();
74              AbstractConversationComponent conversation;
75  
76              if (child instanceof UIEndConversation)
77              {
78                  conversation = (AbstractConversationComponent) child;
79                  if (conversation.getName().equals(conversationName))
80                  {
81                      return conversation;
82                  }
83              }
84              else if (child instanceof UIComponent)
85              {
86                  conversation = findEndConversationComponent((UIComponent) child, conversationName);
87                  if (conversation != null)
88                  {
89                      return conversation;
90                  }
91              }
92          }
93  
94          return null;
95      }
96  
97      /**
98       * end and restart a conversation
99       */
100     public static void endAndRestartConversation(FacesContext context,
101             String conversationName, Boolean restart, MethodBinding restartAction)
102     {
103         ConversationManager conversationManager = ConversationManager.getInstance();
104         Conversation conversation = conversationManager.getConversation(conversationName);
105         if (conversation != null)
106         {
107             conversation.invalidate();
108         }
109 
110         if (restart != null && restart.booleanValue())
111         {
112             FrameworkAdapter.getCurrentInstance().getBean(conversationName);
113 
114             if (restartAction != null)
115             {
116                 restartAction.invoke(context, null);
117             }
118         }
119     }
120 }