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 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 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 userObj=dmtn.getUserObject(); 52 if (userObj instanceof Package ) { 53 allMyPkgs.add(((Package )userObj).get("Id").toString()); 54 } 55 } 56 return allMyPkgs; 58 } 59 60 public DefaultMutableTreeNode getCopyOfModelNodeForPackage (String 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 try { 78 boolean checkForPackageUpdates=checkForPackageUpdates(); 79 if (checkForPackageUpdates) { 80 clear(); 81 } 82 ArrayList oldOML=new ArrayList(openedPackageIds); 83 String [] opIds=SharkAdmin.getPackageAmin().getOpenedPackageIds(); 84 openedPackageIds=new ArrayList(Arrays.asList(opIds)); 85 87 ArrayList toAdd=new ArrayList(openedPackageIds); 88 toAdd.removeAll(oldOML); 89 ArrayList toRemove=new ArrayList(oldOML); 90 toRemove.removeAll(openedPackageIds); 91 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 ex) { 106 } 107 } 108 109 private void insertPackages (Collection xpdlIds) { 110 SharkConnection workflowService=workflowAdmin.getService(); 112 Set unsucsessfullPkgsIds1=new HashSet(); 114 Iterator it=xpdlIds.iterator(); 115 while (it.hasNext()) { 116 String xpdlId=(String )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 fileContStr=new String (fileCont,"UTF8"); 128 JaWE.getInstance().getXMLInterface().putPkgIdToFileContentMapping(xpdlId,fileContStr); 129 } 130 } catch (Exception ex) { 131 System.err.println("Problems while inserting package "+xpdlId); 132 ex.printStackTrace(); 133 } 134 } 135 Set unsucsessfullPkgsIds2=new HashSet(); 137 it=xpdlIds.iterator(); 138 while (it.hasNext()) { 139 String xmlFile=(String )it.next(); 140 if (!unsucsessfullPkgsIds1.contains(xmlFile)) { 141 Package 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 insertPackage(pkg); 160 WfProcessMgr[] mgrs=wpmgi.get_next_n_sequence(0); 161 insertManagers(mgrs); 162 } catch (Exception 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 it=unsucsessfullPkgsIds2.iterator(); 173 while (it.hasNext()) { 174 JaWE.getInstance().getXMLInterface().closePackage((String )it.next()); 175 } 176 } 177 178 private void removePackages (Collection pkgIds) { 179 Iterator it=pkgIds.iterator(); 180 while (it.hasNext()) { 181 String pkgId=(String )it.next(); 182 removePackage(pkgId); 183 JaWE.getInstance().getXMLInterface().closePackage(pkgId); 184 } 185 } 186 187 private void insertManagers (WfProcessMgr[] mgrs) { 188 if (mgrs!=null) { 190 for (int i=0; i<mgrs.length; i++) { 191 String pkgId=""; 192 try {pkgId=SharkClient.getAdminMiscUtilities().getProcessMgrPkgId(mgrs[i].name());}catch (Exception ex){} 193 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 ex) { 204 } 206 if (processes!=null) { 207 for (int j=0; j<processes.length; j++) { 209 insertProcess(pkgId,mgrs[i],processes[j]); 211 } 212 } 213 } 214 } 215 } 216 217 private void insertPackage (Package pkg) { 218 if (getModelNode(pkg.get("Id").toString())!=null) { 220 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 } 231 232 private void removePackage (String pkgId) { 233 removeNodeFromParent(getModelNode(pkgId)); 237 } 240 241 private void insertManager (String pkgId,WfProcessMgr newMgr) { 242 DefaultMutableTreeNode pn=getModelNode(pkgId); 244 DefaultMutableTreeNode mn=new DefaultMutableTreeNode(newMgr); 246 int hmc=pn.getChildCount(); 247 248 insertNodeInto(mn,pn,hmc); 249 } 251 252 private void insertProcess (String 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 } 261 262 274 275 private boolean checkForPackageUpdates () { 276 ExecutionAdministration ea=SharkAdmin.getExecAmin(); 277 DefaultMutableTreeNode root=(DefaultMutableTreeNode)getRoot(); 278 Enumeration pkgs=root.children(); 280 while (pkgs.hasMoreElements()) { 281 DefaultMutableTreeNode pkg=(DefaultMutableTreeNode)pkgs.nextElement(); 282 Object userObj=pkg.getUserObject(); 283 284 if (!(userObj instanceof Package )) continue; 285 286 String pkgId=((Package )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 ex) {ex.printStackTrace();} 298 } 299 return false; 300 } 301 302 303 307 private void updateProcesses () { 308 ArrayList toRemove=new ArrayList(); 309 ArrayList toAdd=new ArrayList(); 310 311 DefaultMutableTreeNode root=(DefaultMutableTreeNode)getRoot(); 312 Enumeration pkgs=root.children(); 314 while (pkgs.hasMoreElements()) { 315 DefaultMutableTreeNode pkg=(DefaultMutableTreeNode)pkgs.nextElement(); 316 Enumeration mgrs=pkg.children(); 318 while (mgrs.hasMoreElements()) { 319 DefaultMutableTreeNode mgrN=(DefaultMutableTreeNode)mgrs.nextElement(); 320 Enumeration processes=mgrN.children(); 323 WfProcessMgr mgr=(WfProcessMgr)mgrN.getUserObject(); 324 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 ex) { 348 } 350 while (processes.hasMoreElements()) { 352 DefaultMutableTreeNode processN=(DefaultMutableTreeNode)processes.nextElement(); 353 try { 359 String processKey=((WfProcess)processN.getUserObject()).key(); 360 realProcesses.remove(processKey); 361 362 if (!showFinishedProcesses && !openProcesses.contains(processKey)) { 363 toRemove.add(processN); 364 } 365 } catch(Exception ex) { 366 try { 367 toRemove.add(processN); 368 } catch (Exception ex2) {} 369 } 371 } 372 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 ex) { 381 ex.printStackTrace(); 382 } 383 } 384 } 385 } 386 Iterator rem=toRemove.iterator(); 388 while (rem.hasNext()) { 389 DefaultMutableTreeNode pr=(DefaultMutableTreeNode)rem.next(); 390 removeNodeFromParent(pr); 391 } 392 Iterator add=toAdd.iterator(); 394 while (add.hasNext()) { 395 WfProcess pr=(WfProcess)add.next(); 396 try { 397 WfProcessMgr mgr=pr.manager(); 398 insertProcess(SharkClient.getAdminMiscUtilities().getProcessMgrPkgId(mgr.name()),mgr,pr); 400 } catch (Exception ex) { 401 ex.printStackTrace(); 402 } 403 } 404 407 } 408 409 private DefaultMutableTreeNode getModelNode (String pkgId) { 410 DefaultMutableTreeNode root=(DefaultMutableTreeNode)getRoot(); 411 Enumeration pkgs=root.children(); 412 while (pkgs.hasMoreElements()) { 413 DefaultMutableTreeNode dmtn=(DefaultMutableTreeNode)pkgs.nextElement(); 414 Object userObj=dmtn.getUserObject(); 415 if ((userObj instanceof Package ) && ((Package )userObj).get("Id").toString().equals(pkgId)) { 416 return dmtn; 417 } 418 } 419 return null; 420 } 421 422 423 private DefaultMutableTreeNode getManagerNode (String 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 ex) { 435 return null; 436 } 437 } 438 return null; 440 } 441 442 private DefaultMutableTreeNode getProcessNode (String 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 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 |