View Javadoc

1   /*
2    * Copyright (c) 2007, Your Corporation. All Rights Reserved.
3    */
4   
5   /*
6    * Licensed to the Apache Software Foundation (ASF) under one
7    * or more contributor license agreements.  See the NOTICE file
8    * distributed with this work for additional information
9    * regarding copyright ownership.  The ASF licenses this file
10   * to you under the Apache License, Version 2.0 (the
11   * "License"); you may not use this file except in compliance
12   * with the License.  You may obtain a copy of the License at
13   *
14   *   http://www.apache.org/licenses/LICENSE-2.0
15   *
16   * Unless required by applicable law or agreed to in writing,
17   * software distributed under the License is distributed on an
18   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19   * KIND, either express or implied.  See the License for the
20   * specific language governing permissions and limitations
21   * under the License.
22   */
23  
24  package org.apache.myfaces.orchestra.conversation;
25  
26  import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter;
27  
28  import java.io.IOException;
29  
30  /***
31   * Some helpers usable for public use
32   */
33  public final class ConversationUtils
34  {
35  	private ConversationUtils()
36  	{
37  	}
38  
39  	/***
40  	 * End and restart the given conversation.
41  	 * <p>
42  	 * In contrast to {@link Conversation#invalidateAndRestart()} this
43  	 * method returns a new instance of the "controller bean" for the given conversation.
44  	 * <p>
45  	 * The "controller bean" is the one you configured in your dependency injection framework.
46  	 */
47  	public static Object invalidateAndRestart(Conversation conversation)
48  	{
49  		String name = conversation.getName();
50  
51  		conversation.invalidateAndRestart();
52  
53  		return FrameworkAdapter.getCurrentInstance().getBean(name);
54  	}
55  
56  	/***
57  	 * End and restart the current conversation.
58  	 * <p>
59  	 * In contrast to {@link #invalidateAndRestart(Conversation)} this
60  	 * method returns a new instance of the "current bean".
61  	 */
62  	public static Object invalidateAndRestartCurrent()
63  	{
64  		CurrentConversationInfo currentConversationInfo = Conversation.getCurrentInstanceInfo();
65  		String name = currentConversationInfo.getBeanName();
66  
67  		currentConversationInfo.getConversation().invalidateAndRestart();
68  
69  		return FrameworkAdapter.getCurrentInstance().getBean(name);
70  	}
71  
72  	/***
73  	 * If no conversation with name <code>conversationName</code> is active a redirect to
74  	 * <code>redirectViewId</code> will be issued.
75  	 * <p>
76  	 * If <code>redirectViewId</code> starts with an slash ('/') the context path will be added.
77  	 */
78  	public static void ensureConversationRedirect(String conversationName, String redirectViewId)
79  	{
80  		if (!ConversationManager.getInstance().hasConversation(conversationName))
81  		{
82  			try
83  			{
84  				FrameworkAdapter.getCurrentInstance().redirect(redirectViewId);
85  			}
86  			catch (IOException e)
87  			{
88  				throw new RuntimeException(e);
89  			}
90  		}
91  	}
92  
93  	/***
94  	 * Invalidates a conversation if it exists.
95  	 */
96  	public static void invalidateIfExists(String name)
97  	{
98  		Conversation conversation = ConversationManager.getInstance().getConversation(name);
99  		if (conversation != null)
100 		{
101 			conversation.invalidate();
102 		}
103 	}
104 }