1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.orchestra.conversation.versioning;
21
22 import java.util.Map;
23 import java.util.Stack;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.myfaces.orchestra.conversation.Conversation;
28 import org.apache.myfaces.orchestra.conversation.ConversationContext;
29 import org.apache.myfaces.orchestra.conversation.ConversationDataHolder;
30 import org.apache.myfaces.orchestra.conversation.ConversationFactory;
31 import org.apache.myfaces.orchestra.conversation.SerializingConversationDataHolder;
32 import org.apache.myfaces.orchestra.conversation.versioning.spring.SpringVersioningScope;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class VersioningConversation extends Conversation
48 {
49 private final Log log = LogFactory.getLog(VersioningConversation.class);
50
51
52 private Stack<SavePoint> savePoints = new Stack<SavePoint>();
53
54
55 private Stack<ConversationDataHolder> conversationVersions = new Stack<ConversationDataHolder>();
56
57
58 private String versioningStrategy;
59
60 public VersioningConversation(ConversationContext conversationContext,
61 String name, ConversationFactory factory, String strategy)
62 {
63 super(conversationContext, name, factory);
64 versioningStrategy = strategy;
65 }
66
67
68
69
70
71
72
73
74
75
76 public SavePoint createSavePoint()
77 {
78 SavePoint savePoint = new SavePoint();
79 createConversationDataSnapshot(savePoint);
80 return savePoint;
81 }
82
83
84
85
86
87
88
89
90 public void createSavePoint(String savePointName)
91 {
92 SavePoint savePoint = new SavePoint(savePointName);
93 createConversationDataSnapshot(savePoint);
94 }
95
96
97
98
99
100 public void revertToSavePoint(SavePoint savePoint)
101 {
102 int savePointIndex = savePoints.indexOf(savePoint);
103 removeConversationDataSnapshot(savePointIndex);
104 }
105
106
107
108
109 public void revertToSavePoint(String savePointName)
110 {
111 int savePointIndex = findSavePointIndex(savePointName);
112 removeConversationDataSnapshot(savePointIndex);
113 }
114
115
116
117
118 public void revertToLastSavePoint()
119 {
120 if(!savePoints.isEmpty())
121 {
122 SavePoint lastSavePoint = savePoints.pop();
123 ConversationDataHolder conversationDataHolder = conversationVersions.pop();
124 Map restoredBeans = conversationDataHolder.getConversationBeans();
125 setBeans(restoredBeans);
126 }
127 else
128 {
129 log.error("No savepoints stored before. Unable to revert to last save point!");
130 }
131 }
132
133
134
135
136 public void clearAllSavePoints()
137 {
138 savePoints.removeAllElements();
139 conversationVersions.removeAllElements();
140 }
141
142 private void createConversationDataSnapshot(SavePoint savePoint)
143 {
144 ConversationDataHolder conversationDataHolder = createConversationDataHolder();
145 conversationVersions.push(conversationDataHolder);
146 savePoints.push(savePoint);
147 }
148
149
150
151
152
153 private void removeConversationDataSnapshot(int savePointIndex)
154 {
155 if(savePointIndex != -1)
156 {
157 savePoints.remove(savePointIndex);
158 ConversationDataHolder conversationDataHolder = conversationVersions.get(savePointIndex);
159 Map restoredBeans = conversationDataHolder.getConversationBeans();
160 setBeans(restoredBeans);
161 conversationVersions.remove(savePointIndex);
162 }
163 else
164 {
165 log.error("No savepoint to revert found!");
166 }
167 }
168
169
170
171
172 private ConversationDataHolder createConversationDataHolder()
173 {
174 if(versioningStrategy.equals(SpringVersioningScope.VERSIONING_STRATEGY_SERIALIZATION))
175 {
176 return new SerializingConversationDataHolder(getBeans());
177 }
178 else
179 {
180 throw new UnsupportedOperationException("Cloning not implemented yet!");
181 }
182 }
183
184 private int findSavePointIndex(String savePointName)
185 {
186 for(int i = 0; i < savePoints.size(); i++)
187 {
188 SavePoint savePoint = savePoints.elementAt(i);
189 String name = savePoint.getSavePointName();
190 if(name.equals(savePointName))
191 {
192 return i;
193 }
194 }
195 return -1;
196 }
197
198 }