1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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 }