KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

307    private void updateProcesses () {
308       ArrayList toRemove=new ArrayList();
309       ArrayList toAdd=new ArrayList();
310
311       DefaultMutableTreeNode root=(DefaultMutableTreeNode)getRoot();
312       // iterate through all packages within the tree
313
Enumeration pkgs=root.children();
314       while (pkgs.hasMoreElements()) {
315          DefaultMutableTreeNode pkg=(DefaultMutableTreeNode)pkgs.nextElement();
316          // iterate through all process definitions within package
317
Enumeration mgrs=pkg.children();
318          while (mgrs.hasMoreElements()) {
319             DefaultMutableTreeNode mgrN=(DefaultMutableTreeNode)mgrs.nextElement();
320             // Iterate through all instantied processes within process definition
321
// to remove all that are in the close state (and are contained in the tree)
322
Enumeration processes=mgrN.children();
323             WfProcessMgr mgr=(WfProcessMgr)mgrN.getUserObject();
324             // refresh the list of all processes started within the engine for
325
// the given process definition - if the list of started processes
326
// from that list differs from the processes within the tree, we must
327
// add that newly created processes
328
Map realProcesses=new HashMap();
329             List openProcesses=new ArrayList();
330             List closedProcesses=new ArrayList();
331             try {
332                WfProcess[] procs;
333                   WfProcessIterator pi=SharkClient.getCommonExpressionBuilder().getOpenProcessesForManager(mgr.name());
334                   procs=pi.get_next_n_sequence(0);
335                for (int i=0; i<procs.length; i++) {
336                   openProcesses.add(procs[i].key());
337                   realProcesses.put(procs[i].key(),procs[i]);
338                }
339                if (showFinishedProcesses) {
340                   pi=SharkClient.getCommonExpressionBuilder().getClosedProcessesForManager(mgr.name());
341                   procs=pi.get_next_n_sequence(0);
342                   for (int i=0; i<procs.length; i++) {
343                      closedProcesses.add(procs[i].key());
344                      realProcesses.put(procs[i].key(),procs[i]);
345                   }
346                }
347             } catch (Exception JavaDoc ex) {
348                //System.out.println("There are no retrieved processes for mgr "+mgrs[i]);
349
}
350             // begin iteration
351
while (processes.hasMoreElements()) {
352                DefaultMutableTreeNode processN=(DefaultMutableTreeNode)processes.nextElement();
353                // remove the processes that are contained within the tree from
354
// the list of all processes within the engine -> the processes
355
// that will be left in the list and which state is "open" (if showing
356
// only opened processes), are
357
// the newly created processes that must be added to the tree
358
try {
359                   String JavaDoc processKey=((WfProcess)processN.getUserObject()).key();
360                   realProcesses.remove(processKey);
361
362                   if (!showFinishedProcesses && !openProcesses.contains(processKey)) {
363                      toRemove.add(processN);
364                   }
365                } catch(Exception JavaDoc ex) {
366                   try {
367                      toRemove.add(processN);
368                   } catch (Exception JavaDoc ex2) {}
369                   //ex.printStackTrace();
370
}
371             }
372             // adding the newly started processes to the list for insertion
373
Iterator it=realProcesses.values().iterator();
374             while (it.hasNext()) {
375                WfProcess process=(WfProcess)it.next();
376                try {
377                   if (showFinishedProcesses || openProcesses.contains(process.key())) {
378                      toAdd.add(process);
379                   }
380                } catch(Exception JavaDoc ex) {
381                   ex.printStackTrace();
382                }
383             }
384          }
385       }
386       // remove the closed process instances
387
Iterator rem=toRemove.iterator();
388       while (rem.hasNext()) {
389          DefaultMutableTreeNode pr=(DefaultMutableTreeNode)rem.next();
390          removeNodeFromParent(pr);
391       }
392       //insert newly created process instances
393
Iterator add=toAdd.iterator();
394       while (add.hasNext()) {
395          WfProcess pr=(WfProcess)add.next();
396          try {
397             WfProcessMgr mgr=pr.manager();
398             //System.out.println("Inserting "+mgr.name()+" - "+pr.name()+" - "+pr.key());
399
insertProcess(SharkClient.getAdminMiscUtilities().getProcessMgrPkgId(mgr.name()),mgr,pr);
400          } catch (Exception JavaDoc ex) {
401             ex.printStackTrace();
402          }
403       }
404       //System.out.println(toRemove);
405
//System.out.println(toAdd);
406

407    }
408
409    private DefaultMutableTreeNode getModelNode (String JavaDoc pkgId) {
410       DefaultMutableTreeNode root=(DefaultMutableTreeNode)getRoot();
411       Enumeration pkgs=root.children();
412       while (pkgs.hasMoreElements()) {
413          DefaultMutableTreeNode dmtn=(DefaultMutableTreeNode)pkgs.nextElement();
414          Object JavaDoc userObj=dmtn.getUserObject();
415          if ((userObj instanceof Package JavaDoc) && ((Package JavaDoc)userObj).get("Id").toString().equals(pkgId)) {
416             return dmtn;
417          }
418       }
419       return null;
420    }
421
422
423    private DefaultMutableTreeNode getManagerNode (String JavaDoc pkgId,WfProcessMgr mgr) {
424
425       DefaultMutableTreeNode pkgnode=getModelNode(pkgId);
426       Enumeration mgrs=pkgnode.children();
427       while (mgrs.hasMoreElements()) {
428          DefaultMutableTreeNode dmtn=(DefaultMutableTreeNode)mgrs.nextElement();
429          WfProcessMgr treeMgr=(WfProcessMgr)dmtn.getUserObject();
430          try {
431             if (treeMgr.name().equals(mgr.name())) {
432                return dmtn;
433             }
434          } catch (Exception JavaDoc ex) {
435             return null;
436          }
437       }
438       //System.out.println("Returning null MGR NODE");
439
return null;
440    }
441
442    private DefaultMutableTreeNode getProcessNode (String JavaDoc pkgId,WfProcessMgr mgr,
443                                                   WfProcess proc) {
444
445       DefaultMutableTreeNode mgrnode=getManagerNode(pkgId,mgr);
446       Enumeration procs=mgrnode.children();
447       while (procs.hasMoreElements()) {
448          DefaultMutableTreeNode dmtn=(DefaultMutableTreeNode)procs.nextElement();
449          WfProcess treeProc=(WfProcess)dmtn.getUserObject();
450          try {
451             if (treeProc.key().equals(proc.key())) {
452                return dmtn;
453             }
454          } catch (Exception JavaDoc ex) {
455             return null;
456          }
457       }
458       return null;
459    }
460
461    public void setShowFinishedProcesses (boolean show) {
462       showFinishedProcesses=show;
463    }
464
465    public boolean getShowFinishedProcesses () {
466       return showFinishedProcesses;
467    }
468
469 }
470
471
Popular Tags