KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > swingclient > workflowadmin > EngineTreeModel


1 package org.enhydra.shark.swingclient.workflowadmin;
2
3 import java.util.*;
4
5 import javax.swing.*;
6 import javax.swing.tree.*;
7
8 import org.enhydra.shark.swingclient.*;
9
10 import org.enhydra.shark.api.client.wfbase.*;
11
12 import org.enhydra.shark.api.client.wfmodel.*;
13
14 import org.enhydra.shark.api.client.wfservice.*;
15 import org.enhydra.jawe.*;
16 import org.enhydra.jawe.xml.*;
17 import org.enhydra.jawe.xml.elements.Package;
18 import org.enhydra.shark.swingclient.ResourceManager;
19
20 /**
21  * Represents the tree model of engine. The first level nodes of the
22  * tree represents the packages imported into engine, the second level
23  * nodes are the workflow definitions within given package, and the third
24  * level nodes are instances of corredponding process definitions. The
25  * tree is periodically refreshed to retrieve changes from engine.
26  */

27 public class EngineTreeModel extends DefaultTreeModel {
28
29    private boolean showFinishedProcesses;
30
31    private SharkAdmin workflowAdmin;
32    private JaWE jawe=JaWE.getInstance();
33
34    private ArrayList openedPackageIds=new ArrayList();
35
36    public EngineTreeModel (SharkAdmin wa) {
37       super(new DefaultMutableTreeNode(
38                ResourceManager.getLanguageDependentString("OpenedPackagesKey")));
39       this.workflowAdmin=wa;
40    }
41
42    public void clear () {
43       // here the new Set must be created, otherwise
44
// the concurent modification exception will occur
45
removePackages(getMyPackagesIds());
46       openedPackageIds.clear();
47    }
48
49    private Collection getMyPackagesIds () {
50       ArrayList allMyPkgs=new ArrayList();
51       DefaultMutableTreeNode root=(DefaultMutableTreeNode)getRoot();
52       Enumeration pkgs=root.children();
53       while (pkgs.hasMoreElements()) {
54          DefaultMutableTreeNode dmtn=(DefaultMutableTreeNode)pkgs.nextElement();
55          Object JavaDoc userObj=dmtn.getUserObject();
56          if (userObj instanceof Package JavaDoc) {
57             allMyPkgs.add(((Package JavaDoc)userObj).get("Id").toString());
58          }
59       }
60       //System.out.println("AllMyPkgs="+allMyPkgs);
61
return allMyPkgs;
62    }
63
64    public DefaultMutableTreeNode getCopyOfModelNodeForPackage (String JavaDoc pkgId) {
65       DefaultMutableTreeNode orig=getModelNode(pkgId);
66       DefaultMutableTreeNode cpy=new DefaultMutableTreeNode(orig.getUserObject());
67       Enumeration children=orig.children();
68       while (children.hasMoreElements()) {
69          cpy.add(new DefaultMutableTreeNode(
70                                               ((DefaultMutableTreeNode)children.nextElement()).getUserObject()));
71       }
72       return cpy;
73    }
74
75    public synchronized void refresh (boolean mandatoryRefreshing) {
76       if (!mandatoryRefreshing &&
77           !workflowAdmin.getPackageManagement().isShowing() &&
78           !workflowAdmin.getProcessInstantiationManagement().isShowing() &&
79           !workflowAdmin.getProcessMonitor().isShowing()) return;
80       //System.out.println("REFRESHING ETM");
81
try {
82          boolean checkForPackageUpdates=checkForPackageUpdates();
83          if (checkForPackageUpdates) {
84 //System.out.println("CFPUPDTS");
85
clear();
86          }
87 //long t1, t2, t3, t4, t5;
88
//t1=System.currentTimeMillis();
89
ArrayList oldOML=new ArrayList(openedPackageIds);
90          String JavaDoc[] opIds=SharkAdmin.getPackageAmin().getOpenedPackageIds();
91          openedPackageIds=new ArrayList(Arrays.asList(opIds));
92          //System.out.println("REFRESH: OLDPKGIds="+oldOML+", NEWPKGIds="+openedPackageIds);
93

94          ArrayList toAdd=new ArrayList(openedPackageIds);
95          toAdd.removeAll(oldOML);
96          ArrayList toRemove=new ArrayList(oldOML);
97          toRemove.removeAll(openedPackageIds);
98          //System.out.println("toAdd="+toAdd+", toRem="+toRemove);
99
// NOTE: first insert to the monitor to get the correct mapping
100
//t2=System.currentTimeMillis();
101
insertPackages(toAdd);
102 //t3=System.currentTimeMillis();
103
removePackages(toRemove);
104 //t4=System.currentTimeMillis();
105
updateProcesses();
106 //t5=System.currentTimeMillis();
107
//System.out.println("REFET: t1="+(t2-t1)+", t2="+(t3-t2)+", t3="+(t4-t3)+", t4="+(t5-t4));
108
if (checkForPackageUpdates) {
109             workflowAdmin.getProcessInstantiationManagement().clear();
110             workflowAdmin.getProcessInstantiationManagement().refresh(true);
111             workflowAdmin.getProcessMonitor().clear();
112          }
113       } catch (BaseException be) {
114          JOptionPane.showMessageDialog(workflowAdmin.getFrame(),
115                                        be.getMessage(),
116                                        workflowAdmin.getAppTitle(),JOptionPane.ERROR_MESSAGE);
117       } catch (Exception JavaDoc ex) {
118       }
119    }
120
121    private void insertPackages (Collection xpdlIds) {
122       //System.out.println("Inserting packages...");
123
SharkConnection workflowService=workflowAdmin.getService();
124       // First put all new url to file content mapping
125
Set unsucsessfullPkgsIds1=new HashSet();
126       Iterator it=xpdlIds.iterator();
127       while (it.hasNext()) {
128          String JavaDoc xpdlId=(String JavaDoc)it.next();
129          try {
130             byte[] fileCont=null;
131             try {
132                fileCont=SharkClient.getPackageAmin().getPackageContent(xpdlId);
133             } catch (BaseException be){
134             }
135             if (fileCont==null) {
136                openedPackageIds.remove(xpdlId);
137                unsucsessfullPkgsIds1.add(xpdlId);
138             } else {
139                String JavaDoc fileContStr=new String JavaDoc(fileCont,"UTF8");
140                JaWE.getInstance().getXMLInterface().putPkgIdToFileContentMapping(xpdlId,fileContStr);
141             }
142          } catch (Exception JavaDoc ex) {
143             System.err.println("Problems while inserting package "+xpdlId);
144             ex.printStackTrace();
145          }
146       }
147       // open all documents using method openDocument of JaWE
148
Set unsucsessfullPkgsIds2=new HashSet();
149       it=xpdlIds.iterator();
150       while (it.hasNext()) {
151          String JavaDoc xmlFile=(String JavaDoc)it.next();
152          if (!unsucsessfullPkgsIds1.contains(xmlFile)) {
153             Package JavaDoc pkg=jawe.openDocument(xmlFile,false,true);
154             if (pkg!=null) {
155                jawe.getPackageEditor().setNewPackage(pkg,false);
156                pkg.setReadOnly(true);
157                try {
158                   WfProcessMgrIterator wpmgi=null;
159                   boolean sucsessfull=true;
160                   try {
161                      wpmgi=SharkClient.getCommonExpressionBuilder().getManagersForPackage(pkg.get("Id").toString());
162                   } catch (BaseException be){
163                      unsucsessfullPkgsIds2.add(xmlFile);
164                      sucsessfull=false;
165                   }
166
167                   if (!sucsessfull) {
168                      continue;
169                   }
170                   //System.out.println("Inserting package "+xmlFile);
171
insertPackage(pkg);
172                   WfProcessMgr[] mgrs=wpmgi.get_next_n_sequence(0);
173                   insertManagers(mgrs);
174                } catch (Exception JavaDoc ex) {
175                   ex.printStackTrace();
176                   System.err.println("Cannot get process managers to insert");
177                }
178             } else {
179                System.err.println("Document "+xmlFile+" can not be opened !!!");
180             }
181          }
182       }
183       // Close all packages which mrs retrieveing were unsucsessful
184
it=unsucsessfullPkgsIds2.iterator();
185       while (it.hasNext()) {
186          JaWE.getInstance().getXMLInterface().closePackage((String JavaDoc)it.next());
187       }
188    }
189
190    private void removePackages (Collection pkgIds) {
191       Iterator it=pkgIds.iterator();
192       while (it.hasNext()) {
193          String JavaDoc pkgId=(String JavaDoc)it.next();
194          removePackage(pkgId);
195          JaWE.getInstance().getXMLInterface().closePackage(pkgId);
196       }
197    }
198
199    private void insertManagers (WfProcessMgr[] mgrs) {
200       //System.out.println("Inserting "+mgrs.length+" managers...");
201
if (mgrs!=null) {
202          for (int i=0; i<mgrs.length; i++) {
203             String JavaDoc pkgId="";
204             try {pkgId=SharkClient.getAdminMiscUtilities().getProcessMgrPkgId(mgrs[i].name());}catch (Exception JavaDoc ex){}
205             //try{System.out.println("Inserting manager "+mgrs[i].name());}catch(Exception ex){}
206
insertManager(pkgId,mgrs[i]);
207             WfProcess[] processes=null;
208             try {
209                if (showFinishedProcesses) {
210                   processes=mgrs[i].get_sequence_process(0);
211                } else {
212                   WfProcessIterator pi=SharkClient.getCommonExpressionBuilder().getOpenProcessesForManager(mgrs[i].name());
213                   processes=pi.get_next_n_sequence(0);
214                }
215             } catch (Exception JavaDoc ex) {
216                //System.out.println("There are no retrieved processes for mgr "+mgrs[i]);
217
}
218             if (processes!=null) {
219                //System.out.println("Inserting processes...");
220
for (int j=0; j<processes.length; j++) {
221                   //try{System.out.println("Inserting process "+processes[j].name()+" - "+processes[j].key());}catch(Exception ex){}
222
insertProcess(pkgId,mgrs[i],processes[j]);
223                }
224             }
225          }
226       }
227    }
228
229    private void insertPackage (Package JavaDoc pkg) {
230       //System.out.println("Inserting pkg "+pkg);
231
if (getModelNode(pkg.get("Id").toString())!=null) {
232          //System.out.println("There is already inserted pkg "+pkg);
233
return;
234       }
235       DefaultMutableTreeNode pn=new DefaultMutableTreeNode(pkg);
236       DefaultMutableTreeNode root=(DefaultMutableTreeNode)getRoot();
237       int hmc=root.getChildCount();
238
239       insertNodeInto(pn,root,hmc);
240       //System.out.println("PB="+hmc+", PA="+root.getChildCount());
241
//reload();
242
}
243
244    private void removePackage (String JavaDoc pkgId) {
245       //System.out.println("Removing pkg "+pkgId);
246
// DefaultMutableTreeNode root=(DefaultMutableTreeNode)getRoot();
247
// int hmcb=root.getChildCount();
248
removeNodeFromParent(getModelNode(pkgId));
249       // int hmca=root.getChildCount();
250
//System.out.println("PB="+hmcb+", PA="+hmca);
251
}
252
253    private void insertManager (String JavaDoc pkgId,WfProcessMgr newMgr) {
254       //System.out.println("Inserting mgr "+newMgr);
255
DefaultMutableTreeNode pn=getModelNode(pkgId);
256       //System.out.println("PNHC="+pn.hashCode());
257
DefaultMutableTreeNode mn=new DefaultMutableTreeNode(newMgr);
258       int hmc=pn.getChildCount();
259
260       insertNodeInto(mn,pn,hmc);
261       //reload();
262
}
263
264    private void insertProcess (String JavaDoc pkgId,WfProcessMgr mgr,WfProcess newProcess) {
265
266       DefaultMutableTreeNode mn=getManagerNode(pkgId,mgr);
267       DefaultMutableTreeNode pn=new DefaultMutableTreeNode(newProcess);
268       int hmc=mn.getChildCount();
269
270       insertNodeInto(pn,mn,hmc);
271       //reload();
272
}
273
274    /* private void removeProcess (
275     Package pkg,
276     WfProcessMgr mgr,
277     WfProcess newProcess) {
278
279     DefaultMutableTreeNode mn=getManagerNode(pkg,mgr);
280     DefaultMutableTreeNode pn=new DefaultMutableTreeNode(newProcess);
281     int hmc=mn.getChildCount();
282
283     insertNodeInto(pn,mn,hmc);
284     //reload();
285     }*/

286
287    private boolean checkForPackageUpdates () {
288       ExecutionAdministration ea=SharkAdmin.getExecAmin();
289       DefaultMutableTreeNode root=(DefaultMutableTreeNode)getRoot();
290       // iterate through all packages within the tree
291
Enumeration pkgs=root.children();
292       while (pkgs.hasMoreElements()) {
293          DefaultMutableTreeNode pkg=(DefaultMutableTreeNode)pkgs.nextElement();
294          Object JavaDoc userObj=pkg.getUserObject();
295
296          if (!(userObj instanceof Package JavaDoc)) continue;
297
298          String JavaDoc pkgId=((Package JavaDoc)userObj).get("Id").toString().trim();
299          int hmMgrs=pkg.getChildCount();
300          try {
301             WfProcessMgrIterator mgri=SharkClient.getCommonExpressionBuilder().getManagersForPackage(pkgId);
302             WfProcessMgr[] mgrs=mgri.get_next_n_sequence(0);
303             int hmOld=0;
304             if (mgrs!=null) {
305                hmOld=mgrs.length;
306             }
307             if (hmMgrs!=hmOld) return true;
308
309          } catch (Exception JavaDoc ex) {ex.printStackTrace();}
310       }
311       return false;
312    }
313
314
315    /**
316     * Adds newly started processes to the tree, and removes the finished
317     * processes from the tree.
318     */

319    private void updateProcesses () {
320       ArrayList toRemove=new ArrayList();
321       ArrayList toAdd=new ArrayList();
322             
323 //long t1, t2, t3, t4, t5, t6, t7, t8, t9, td1=0, td2=0, td3=0, ltd1=0, ltd2=0, ltd3=0;
324
DefaultMutableTreeNode root=(DefaultMutableTreeNode)getRoot();
325       // iterate through all packages within the tree
326
Enumeration pkgs=root.children();
327       while (pkgs.hasMoreElements()) {
328          DefaultMutableTreeNode pkg=(DefaultMutableTreeNode)pkgs.nextElement();
329          // iterate through all process definitions within package
330
Enumeration mgrs=pkg.children();
331          while (mgrs.hasMoreElements()) {
332 // t1=System.currentTimeMillis();
333
DefaultMutableTreeNode mgrN=(DefaultMutableTreeNode)mgrs.nextElement();
334             // Iterate through all instantied processes within process definition
335
// to remove all that are in the close state (and are contained in the tree)
336
Enumeration processes=mgrN.children();
337             WfProcessMgr mgr=(WfProcessMgr)mgrN.getUserObject();
338             // refresh the list of all processes started within the engine for
339
// the given process definition - if the list of started processes
340
// from that list differs from the processes within the tree, we must
341
// add that newly created processes
342
Map realProcesses=new HashMap();
343             List openProcesses=new ArrayList();
344             List closedProcesses=new ArrayList();
345             try {
346                WfProcess[] procs;
347                WfProcessIterator pi=SharkClient.getCommonExpressionBuilder().getOpenProcessesForManager(mgr.name());
348                procs=pi.get_next_n_sequence(0);
349                for (int i=0; i<procs.length; i++) {
350                   openProcesses.add(procs[i].key());
351                   realProcesses.put(procs[i].key(),procs[i]);
352                }
353                if (showFinishedProcesses) {
354                   pi=SharkClient.getCommonExpressionBuilder().getClosedProcessesForManager(mgr.name());
355                   procs=pi.get_next_n_sequence(0);
356                   for (int i=0; i<procs.length; i++) {
357                      closedProcesses.add(procs[i].key());
358                      realProcesses.put(procs[i].key(),procs[i]);
359                   }
360                }
361                                 
362             } catch (Exception JavaDoc ex) {
363                //System.out.println("There are no retrieved processes for mgr "+mgrs[i]);
364
}
365 // t2=System.currentTimeMillis();
366
// td1+=(t2-t1);
367

368             // begin iteration
369
// long lt1, lt2, lt3;
370
while (processes.hasMoreElements()) {
371 // t3=System.currentTimeMillis();
372

373                DefaultMutableTreeNode processN=(DefaultMutableTreeNode)processes.nextElement();
374                // remove the processes that are contained within the tree from
375
// the list of all processes within the engine -> the processes
376
// that will be left in the list and which state is "open" (if showing
377
// only opened processes), are
378
// the newly created processes that must be added to the tree
379
// lt1=System.currentTimeMillis();
380
// ltd1+=(lt1-t3);
381
try {
382                   String JavaDoc processKey=((WfProcess)processN.getUserObject()).key();
383                   realProcesses.remove(processKey);
384
385 // lt2=System.currentTimeMillis();
386
// ltd2+=(lt2-lt1);
387
if (!showFinishedProcesses && !openProcesses.contains(processKey)) {
388                      toRemove.add(processN);
389                   }
390 // lt3=System.currentTimeMillis();
391
// ltd3+=(lt3-lt2);
392
} catch(Exception JavaDoc ex) {
393                   try {
394                      toRemove.add(processN);
395                   } catch (Exception JavaDoc ex2) {}
396                   //ex.printStackTrace();
397
}
398 // t4=System.currentTimeMillis();
399
// td2+=(t4-t3);
400

401             }
402 // System.out.println("LT1="+ltd1+", LT2="+ltd2+", LT3="+ltd3);
403
// adding the newly started processes to the list for insertion
404
Iterator it=realProcesses.values().iterator();
405             while (it.hasNext()) {
406 // t5=System.currentTimeMillis();
407
WfProcess process=(WfProcess)it.next();
408                try {
409                   if (showFinishedProcesses || openProcesses.contains(process.key())) {
410                      toAdd.add(process);
411                   }
412                } catch(Exception JavaDoc ex) {
413                   ex.printStackTrace();
414                }
415 // t6=System.currentTimeMillis();
416
// td3+=(t6-t5);
417

418             }
419          }
420       }
421       // remove the closed process instances
422
// t7=System.currentTimeMillis();
423
Iterator rem=toRemove.iterator();
424       while (rem.hasNext()) {
425          DefaultMutableTreeNode pr=(DefaultMutableTreeNode)rem.next();
426          removeNodeFromParent(pr);
427       }
428 // t8=System.currentTimeMillis();
429

430       //insert newly created process instances
431
Iterator add=toAdd.iterator();
432       while (add.hasNext()) {
433          WfProcess pr=(WfProcess)add.next();
434          try {
435             WfProcessMgr mgr=pr.manager();
436             //System.out.println("Inserting "+mgr.package_id()+" - "+mgr.name()+" - "+pr.name()+" - "+pr.key());
437
insertProcess(SharkClient.getAdminMiscUtilities().getProcessMgrPkgId(mgr.name()),mgr,pr);
438          } catch (Exception JavaDoc ex) {
439             ex.printStackTrace();
440          }
441       }
442 // t9=System.currentTimeMillis();
443

444       //System.out.println(toRemove);
445
//System.out.println(toAdd);
446
// System.out.println("UP: t1="+td1+", t2="+td2+", t3="+td3+", t4="+(t8-t7)+", t5="+(t9-t8));
447
}
448
449    private DefaultMutableTreeNode getModelNode (String JavaDoc pkgId) {
450       DefaultMutableTreeNode root=(DefaultMutableTreeNode)getRoot();
451       Enumeration pkgs=root.children();
452       while (pkgs.hasMoreElements()) {
453          DefaultMutableTreeNode dmtn=(DefaultMutableTreeNode)pkgs.nextElement();
454          Object JavaDoc userObj=dmtn.getUserObject();
455          if ((userObj instanceof Package JavaDoc) && ((Package JavaDoc)userObj).get("Id").toString().equals(pkgId)) {
456             return dmtn;
457          }
458       }
459       return null;
460    }
461
462
463    private DefaultMutableTreeNode getManagerNode (String JavaDoc pkgId,WfProcessMgr mgr) {
464
465       DefaultMutableTreeNode pkgnode=getModelNode(pkgId);
466       Enumeration mgrs=pkgnode.children();
467       while (mgrs.hasMoreElements()) {
468          DefaultMutableTreeNode dmtn=(DefaultMutableTreeNode)mgrs.nextElement();
469          WfProcessMgr treeMgr=(WfProcessMgr)dmtn.getUserObject();
470          try {
471             if (treeMgr.name().equals(mgr.name())) {
472                return dmtn;
473             }
474          } catch (Exception JavaDoc ex) {
475             return null;
476          }
477       }
478       //System.out.println("Returning null MGR NODE");
479
return null;
480    }
481
482    private DefaultMutableTreeNode getProcessNode (String JavaDoc pkgId,WfProcessMgr mgr,
483                                                   WfProcess proc) {
484
485       DefaultMutableTreeNode mgrnode=getManagerNode(pkgId,mgr);
486       Enumeration procs=mgrnode.children();
487       while (procs.hasMoreElements()) {
488          DefaultMutableTreeNode dmtn=(DefaultMutableTreeNode)procs.nextElement();
489          WfProcess treeProc=(WfProcess)dmtn.getUserObject();
490          try {
491             if (treeProc.key().equals(proc.key())) {
492                return dmtn;
493             }
494          } catch (Exception JavaDoc ex) {
495             return null;
496          }
497       }
498       return null;
499    }
500
501    public void setShowFinishedProcesses (boolean show) {
502       showFinishedProcesses=show;
503    }
504
505    public boolean getShowFinishedProcesses () {
506       return showFinishedProcesses;
507    }
508
509 }
510
511
512
Popular Tags