1 package org.telscenter.sailotrunk;
2
3 import java.awt.BorderLayout;
4 import java.awt.Component;
5 import java.io.ByteArrayInputStream;
6 import java.io.ByteArrayOutputStream;
7 import java.io.File;
8 import java.io.FileNotFoundException;
9 import java.io.FileOutputStream;
10 import java.net.URL;
11
12 import javax.swing.JDialog;
13 import javax.swing.JPanel;
14
15 import net.sf.sail.core.beans.Pod;
16 import net.sf.sail.core.beans.SessionContext;
17 import net.sf.sail.core.beans.event.SessionEvent;
18 import net.sf.sail.core.beans.event.SessionEventListener;
19 import net.sf.sail.core.beans.service.AgentService;
20 import net.sf.sail.core.curnit.ICurnit;
21 import net.sf.sail.core.entity.IAgent;
22 import net.sf.sail.core.entity.ISock;
23 import net.sf.sail.core.entity.MismatchedAgentSetSizeException;
24 import net.sf.sail.core.entity.Rim;
25 import net.sf.sail.core.entity.Role;
26 import net.sf.sail.core.entity.UnsupportedRimShapeException;
27 import net.sf.sail.core.session.SessionDataService;
28 import net.sf.sail.core.uuid.CurnitUuid;
29 import net.sf.sail.core.uuid.PodUuid;
30
31 import org.concord.framework.otrunk.OTControllerService;
32 import org.concord.framework.otrunk.OTID;
33 import org.concord.framework.otrunk.OTObject;
34 import org.concord.framework.otrunk.OTrunk;
35 import org.concord.framework.otrunk.view.OTFrameManager;
36 import org.concord.framework.otrunk.view.OTViewContext;
37 import org.concord.framework.otrunk.view.OTViewFactory;
38 import org.concord.otrunk.OTrunkImpl;
39 import org.concord.otrunk.datamodel.OTDatabase;
40 import org.concord.otrunk.datamodel.OTUUID;
41 import org.concord.otrunk.view.OTViewContainerPanel;
42 import org.concord.otrunk.view.OTViewerHelper;
43 import org.concord.otrunk.xml.XMLDatabase;
44
45 public class OTrunkCurnit implements ICurnit
46 {
47 private CurnitUuid curnitId;
48 private OTDatabase otDb;
49 private OTrunk otrunk;
50 private URL otmlUrl;
51 private OTViewerHelper otViewerHelper;
52 private Object rootBean;
53 private OTObject otrunkRoot;
54 private OTControllerService otControllerService;
55 private SessionContext sessionContext;
56 private Pod pod;
57 private Rim<byte []> rim;
58 private AgentService agentService;
59 private ISock workSock;
60 private boolean authoring = false;
61
62 private final int PREVIEW_WINDOW_WIDTH = 800;
63 private final int PREVIEW_WINDOW_HEIGHT = 600;
64
65 public OTrunkCurnit(URL otmlUrl)
66 {
67 this.otmlUrl = otmlUrl;
68 otViewerHelper = new OTViewerHelper();
69
70 }
71
72 /***
73 * This returns a CurnitUUID
74 *
75 * @see net.sf.sail.core.curnit.ICurnit#getCurnitId()
76 */
77 public CurnitUuid getCurnitId() {
78 if(curnitId != null){
79 return curnitId;
80 }
81
82 try {
83 OTDatabase otDb = getOTDatabase();
84 if(otDb == null){
85 System.err.println("No valid ot database");
86 return null;
87 }
88
89 OTID dbId = otDb.getDatabaseId();
90 if(!(dbId instanceof OTUUID)){
91 System.err.println("OTDatabase doesn't have a uuid");
92 return null;
93 }
94
95 curnitId = new CurnitUuid(((OTUUID)dbId).asByteArray());
96 return curnitId;
97 } catch (Exception e) {
98
99 e.printStackTrace();
100 }
101
102 return null;
103 }
104
105 public Object getRootBean() {
106 if(rootBean != null){
107 return rootBean;
108 }
109
110 rootBean = otControllerService.getRealObject(otrunkRoot);
111 return rootBean;
112 }
113
114 public void initialize()
115 {
116 try {
117 OTDatabase otDb = getOTDatabase();
118 otrunk = new OTrunkImpl(otDb);
119 otViewerHelper.initOTrunk(otrunk);
120
121 otrunkRoot = otrunk.getRoot();
122
123
124
125
126 pod = new Pod();
127
128 if(otrunkRoot.getGlobalId() instanceof OTUUID){
129 pod.setPodId(new PodUuid(otrunkRoot.getGlobalId().toExternalForm()));
130 } else if(otDb.getDatabaseId() instanceof OTUUID){
131 pod.setPodId(new PodUuid(otDb.getDatabaseId().toExternalForm()));
132 } else {
133 throw new IllegalStateException("The otml file loaded in needs an id on the top" +
134 "level otrunk element or root object");
135 }
136
137 rim = new Rim<byte []>();
138 pod.add(rim);
139 rim.setName("ot.learner.data");
140 rim.setShape(byte [].class);
141
142 if(!authoring){
143 SessionDataService sessionDataService = sessionContext.getSessionDataService();
144 agentService = sessionDataService.getPersistenceService(sessionContext);
145
146 XMLDatabase sailLearnerDB = getLearnerDatabase();
147 try {
148 if(sailLearnerDB != null){
149 otViewerHelper.loadUserData(sailLearnerDB, null);
150 ((XMLDatabase)sailLearnerDB).setDirty(false);
151 } else {
152 otViewerHelper.newAnonUserData();
153 }
154
155 otrunkRoot = otrunk.getUserRuntimeObject(otrunkRoot, otViewerHelper.getCurrentUser());
156 } catch (Exception e1) {
157
158 e1.printStackTrace();
159 }
160
161 sessionContext.addSessionEventListener(new SessionEventListener(){
162
163 public void sessionInitiated(SessionEvent e) {
164 }
165
166 public void sessionStarted(SessionEvent e) {
167 }
168
169 public void sessionStopped(SessionEvent e) {
170 saveDataInternal();
171 }
172 });
173 }
174
175
176 otControllerService = otrunkRoot.getOTObjectService().createControllerService();
177 OTViewFactory viewFactory = otViewerHelper.getViewFactory();
178 if(viewFactory != null){
179 otControllerService.addService(OTViewContext.class, viewFactory.getViewContext());
180 }
181
182 } catch (Exception e) {
183
184 e.printStackTrace();
185 }
186
187
188
189
190 }
191
192 public OTDatabase getOTDatabase() throws Exception
193 {
194 if(otDb != null){
195 return otDb;
196 }
197
198 if(otmlUrl.getProtocol().startsWith("html")){
199 otDb = otViewerHelper.loadOTDatabaseXML(otmlUrl);
200 } else {
201 otDb = otViewerHelper.loadOTDatabase(otmlUrl);
202 }
203
204 return otDb;
205 }
206
207 public void previewCurnit(Component component) throws Exception {
208 if( otDb == null)
209 throw new Exception("Curnit has not been initialized");
210
211 otControllerService.saveRealObject(rootBean, otrunkRoot);
212
213
214 otViewerHelper.loadOTrunk(otDb, component);
215
216
217 JDialog popup = new JDialog();
218
219
220
221 OTViewContext viewContext =
222 (OTViewContext) otControllerService.getService(OTViewContext.class);
223
224 JPanel panel=new JPanel(new BorderLayout(5, 5));
225 OTViewContainerPanel otPanel = null;
226 OTFrameManager frameManager =
227 (OTFrameManager) viewContext.getViewService(OTFrameManager.class);
228 otPanel = new OTViewContainerPanel(frameManager);
229 otPanel.setTopLevelContainer(true);
230 OTViewFactory viewFactory = viewContext.createChildViewFactory();
231 otPanel.setOTViewFactory(viewFactory);
232
233 panel.setLayout(new BorderLayout());
234 otPanel.setCurrentObject(otrunkRoot);
235 panel.add(otPanel, BorderLayout.CENTER);
236
237
238 popup.setSize(PREVIEW_WINDOW_WIDTH, PREVIEW_WINDOW_HEIGHT);
239 popup.add(panel);
240 popup.setVisible(true);
241
242 }
243
244 public void saveCurnit(File file) throws FileNotFoundException, Exception {
245
246 if( otDb == null)
247 throw new Exception("Curnit has not been initialized");
248
249 otControllerService.saveRealObject(rootBean, otrunkRoot);
250 otViewerHelper.saveOTDatabase(otDb, new FileOutputStream(file));
251
252 }
253
254 public void setSessionContext(SessionContext sessionContext) {
255 this.sessionContext = sessionContext;
256 }
257
258 public Rim getWorkRim() {
259 return rim;
260 }
261
262 public ISock getWorkSock() {
263 if (workSock == null) {
264 workSock = getRimSock(getWorkRim());
265 }
266 return workSock;
267 }
268
269 protected ISock getRimSock(Rim rim)
270 {
271 ISock sock = null;
272 if (rim != null)
273 {
274 try {
275 IAgent agent = agentService.getAgentsInRole(Role.RUN_WORKGROUP).getSingle();
276 sock = agentService.getSock(rim, agent);
277 } catch (MismatchedAgentSetSizeException e) {
278
279 e.printStackTrace();
280 } catch (UnsupportedRimShapeException e) {
281
282 e.printStackTrace();
283 }
284 }
285 return sock;
286 }
287
288 private XMLDatabase getLearnerDatabase() {
289
290 ISock sock = getWorkSock();
291
292 Object lastEntry = null;
293 if(sock != null && sock.size() > 0){
294 lastEntry = sock.peek();
295 }
296
297 if(lastEntry == null){
298 return null;
299 }
300
301 try {
302 if(lastEntry instanceof byte[]){
303 ByteArrayInputStream inStream = new ByteArrayInputStream((byte[])lastEntry);
304
305 return (XMLDatabase) otViewerHelper.loadOTDatabase(inStream, null);
306 }
307
308 } catch (Exception exp) {
309 exp.printStackTrace();
310 }
311
312 return null;
313 }
314
315 private void saveDataInternal()
316 {
317 ISock sock = getWorkSock();
318
319 OTDatabase userDB = otViewerHelper.getUserOtDB();
320
321 if(userDB == null){
322
323 return;
324 }
325
326
327
328 if(userDB instanceof XMLDatabase &&
329 !((XMLDatabase)userDB).isDirty()){
330 return;
331 }
332
333 try {
334 if(sock != null && userDB != null){
335 ByteArrayOutputStream out = new ByteArrayOutputStream();
336 otViewerHelper.saveOTDatabase(userDB, out);
337 byte [] bytes = out.toByteArray();
338 sock.add(bytes);
339 if(userDB instanceof XMLDatabase){
340 ((XMLDatabase)userDB).setDirty(false);
341 }
342 } else {
343 System.err.println("No sock in OTrunkStep - cannot save data");
344 }
345
346 } catch (Exception e1) {
347
348 e1.printStackTrace();
349 }
350
351 }
352
353 /***
354 * This is false by default
355 *
356 * @param mode
357 */
358 public void setAuthoring(boolean mode)
359 {
360 authoring = mode;
361 }
362 }