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 java.util.Iterator;
22  
23  import javax.faces.component.UICommand;
24  import javax.faces.component.UIComponent;
25  import javax.faces.el.ValueBinding;
26  import javax.faces.el.MethodBinding;
27  import javax.faces.context.FacesContext;
28  
29  public class ConversationUtils
30  {
31      private ConversationUtils()
32      {
33      }
34  
35      /**
36       * Find the first parent which is a command
37       */
38      public static UICommand findParentCommand(UIComponent base)
39      {
40          UIComponent parent = base;
41          do
42          {
43              parent = parent.getParent();
44              if (parent instanceof UICommand)
45              {
46                  return (UICommand) parent;
47              }
48          }
49          while (parent != null);
50  
51          return null;
52      }
53  
54      /**
55       * Find a child start or end conversation component for the given conversation name
56       */
57      public static AbstractConversationComponent findStartOrEndConversationComponent(UIComponent component, String conversationName)
58      {
59          Iterator iterComponents = component.getFacetsAndChildren();
60          while (iterComponents.hasNext())
61          {
62              Object child = iterComponents.next();
63              AbstractConversationComponent conversation;
64  
65              if (child instanceof UIStartConversation || child instanceof UIEndConversation)
66              {
67                  conversation = (AbstractConversationComponent) child;
68                  if (conversation.getName().equals(conversationName))
69                  {
70                      return conversation;
71                  }
72              }
73              else if (child instanceof UIComponent)
74              {
75                  conversation = findStartOrEndConversationComponent((UIComponent) child, conversationName);
76                  if (conversation != null)
77                  {
78                      return conversation;
79                  }
80              }
81          }
82  
83          return null;
84      }
85  
86      public static String extractBeanName(ValueBinding vb)
87      {
88          String valueBinding = vb.getExpressionString();
89          return valueBinding.substring(2, valueBinding.length()-1);
90      }
91  
92      /**
93       * end and restart a conversation
94       */
95      static void endAndRestartConversation(FacesContext context, String conversationName, Boolean restart, MethodBinding restartAction)
96      {
97          ConversationManager conversationManager = ConversationManager.getInstance(context);
98          Conversation conversation = conversationManager.getConversation(conversationName);
99  
100         conversationManager.endConversation(conversationName, true);
101 
102         if (restart != null && restart.booleanValue() && conversation != null)
103         {
104             conversationManager.startConversation(conversationName, conversation.isPersistence());
105 
106             if (restartAction != null)
107             {
108                 restartAction.invoke(context, null);
109             }
110         }
111     }
112 }