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(UIComponent component, String conversationName)
68  	{
69  		Iterator iterComponents = component.getFacetsAndChildren();
70  		while (iterComponents.hasNext())
71  		{
72  			Object child = iterComponents.next();
73  			AbstractConversationComponent conversation;
74  
75  			if (child instanceof UIEndConversation)
76  			{
77  				conversation = (AbstractConversationComponent) child;
78  				if (conversation.getName().equals(conversationName))
79  				{
80  					return conversation;
81  				}
82  			}
83  			else if (child instanceof UIComponent)
84  			{
85  				conversation = findEndConversationComponent((UIComponent) child, conversationName);
86  				if (conversation != null)
87  				{
88  					return conversation;
89  				}
90  			}
91  		}
92  
93  		return null;
94  	}
95  
96  	/***
97  	 * end and restart a conversation
98  	 */
99  	public static void endAndRestartConversation(FacesContext context, String conversationName, Boolean restart, MethodBinding restartAction)
100 	{
101 		ConversationManager conversationManager = ConversationManager.getInstance();
102 		Conversation conversation = conversationManager.getConversation(conversationName);
103 		if (conversation != null)
104 		{
105 			conversation.invalidate();
106 		}
107 
108 		if (restart != null && restart.booleanValue())
109 		{
110 			FrameworkAdapter.getCurrentInstance().getBean(conversationName);
111 
112 			if (restartAction != null)
113 			{
114 				restartAction.invoke(context, null);
115 			}
116 		}
117 	}
118 }