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 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 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 userObj=dmtn.getUserObject(); 56 if (userObj instanceof Package ) { 57 allMyPkgs.add(((Package )userObj).get("Id").toString()); 58 } 59 } 60 return allMyPkgs; 62 } 63 64 public DefaultMutableTreeNode getCopyOfModelNodeForPackage (String 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 try { 82 boolean checkForPackageUpdates=checkForPackageUpdates(); 83 if (checkForPackageUpdates) { 84 clear(); 86 } 87 ArrayList oldOML=new ArrayList(openedPackageIds); 90 String [] opIds=SharkAdmin.getPackageAmin().getOpenedPackageIds(); 91 openedPackageIds=new ArrayList(Arrays.asList(opIds)); 92 94 ArrayList toAdd=new ArrayList(openedPackageIds); 95 toAdd.removeAll(oldOML); 96 ArrayList toRemove=new ArrayList(oldOML); 97 toRemove.removeAll(openedPackageIds); 98 insertPackages(toAdd); 102 removePackages(toRemove); 104 updateProcesses(); 106 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 ex) { 118 } 119 } 120 121 private void insertPackages (Collection xpdlIds) { 122 SharkConnection workflowService=workflowAdmin.getService(); 124 Set unsucsessfullPkgsIds1=new HashSet(); 126 Iterator it=xpdlIds.iterator(); 127 while (it.hasNext()) { 128 String xpdlId=(String )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 fileContStr=new String (fileCont,"UTF8"); 140 JaWE.getInstance().getXMLInterface().putPkgIdToFileContentMapping(xpdlId,fileContStr); 141 } 142 } catch (Exception ex) { 143 System.err.println("Problems while inserting package "+xpdlId); 144 ex.printStackTrace(); 145 } 146 } 147 Set unsucsessfullPkgsIds2=new HashSet(); 149 it=xpdlIds.iterator(); 150 while (it.hasNext()) { 151 String xmlFile=(String )it.next(); 152 if (!unsucsessfullPkgsIds1.contains(xmlFile)) { 153 Package 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 insertPackage(pkg); 172 WfProcessMgr[] mgrs=wpmgi.get_next_n_sequence(0); 173 insertManagers(mgrs); 174 } catch (Exception 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 it=unsucsessfullPkgsIds2.iterator(); 185 while (it.hasNext()) { 186 JaWE.getInstance().getXMLInterface().closePackage((String )it.next()); 187 } 188 } 189 190 private void removePackages (Collection pkgIds) { 191 Iterator it=pkgIds.iterator(); 192 while (it.hasNext()) { 193 String pkgId=(String )it.next(); 194 removePackage(pkgId); 195 JaWE.getInstance().getXMLInterface().closePackage(pkgId); 196 } 197 } 198 199 private void insertManagers (WfProcessMgr[] mgrs) { 200 if (mgrs!=null) { 202 for (int i=0; i<mgrs.length; i++) { 203 String pkgId=""; 204 try {pkgId=SharkClient.getAdminMiscUtilities().getProcessMgrPkgId(mgrs[i].name());}catch (Exception ex){} 205 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 ex) { 216 } 218 if (processes!=null) { 219 for (int j=0; j<processes.length; j++) { 221 insertProcess(pkgId,mgrs[i],processes[j]); 223 } 224 } 225 } 226 } 227 } 228 229 private void insertPackage (Package pkg) { 230 if (getModelNode(pkg.get("Id").toString())!=null) { 232 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 } 243 244 private void removePackage (String pkgId) { 245 removeNodeFromParent(getModelNode(pkgId)); 249 } 252 253 private void insertManager (String pkgId,WfProcessMgr newMgr) { 254 DefaultMutableTreeNode pn=getModelNode(pkgId); 256 DefaultMutableTreeNode mn=new DefaultMutableTreeNode(newMgr); 258 int hmc=pn.getChildCount(); 259 260 insertNodeInto(mn,pn,hmc); 261 } 263 264 private void insertProcess (String 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 } 273 274 286 287 private boolean checkForPackageUpdates () { 288 ExecutionAdministration ea=SharkAdmin.getExecAmin(); 289 DefaultMutableTreeNode root=(DefaultMutableTreeNode)getRoot(); 290 Enumeration pkgs=root.children(); 292 while (pkgs.hasMoreElements()) { 293 DefaultMutableTreeNode pkg=(DefaultMutableTreeNode)pkgs.nextElement(); 294 Object userObj=pkg.getUserObject(); 295 296 if (!(userObj instanceof Package )) continue; 297 298 String pkgId=((Package )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 ex) {ex.printStackTrace();} 310 } 311 return false; 312 } 313 314 315 319 private void updateProcesses () { 320 ArrayList toRemove=new ArrayList(); 321 ArrayList toAdd=new ArrayList(); 322 323 DefaultMutableTreeNode root=(DefaultMutableTreeNode)getRoot(); 325 Enumeration pkgs=root.children(); 327 while (pkgs.hasMoreElements()) { 328 DefaultMutableTreeNode pkg=(DefaultMutableTreeNode)pkgs.nextElement(); 329 Enumeration mgrs=pkg.children(); 331 while (mgrs.hasMoreElements()) { 332 DefaultMutableTreeNode mgrN=(DefaultMutableTreeNode)mgrs.nextElement(); 334 Enumeration processes=mgrN.children(); 337 WfProcessMgr mgr=(WfProcessMgr)mgrN.getUserObject(); 338 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 ex) { 363 } 365 368 while (processes.hasMoreElements()) { 371 373 DefaultMutableTreeNode processN=(DefaultMutableTreeNode)processes.nextElement(); 374 try { 382 String processKey=((WfProcess)processN.getUserObject()).key(); 383 realProcesses.remove(processKey); 384 385 if (!showFinishedProcesses && !openProcesses.contains(processKey)) { 388 toRemove.add(processN); 389 } 390 } catch(Exception ex) { 393 try { 394 toRemove.add(processN); 395 } catch (Exception ex2) {} 396 } 398 401 } 402 Iterator it=realProcesses.values().iterator(); 405 while (it.hasNext()) { 406 WfProcess process=(WfProcess)it.next(); 408 try { 409 if (showFinishedProcesses || openProcesses.contains(process.key())) { 410 toAdd.add(process); 411 } 412 } catch(Exception ex) { 413 ex.printStackTrace(); 414 } 415 418 } 419 } 420 } 421 Iterator rem=toRemove.iterator(); 424 while (rem.hasNext()) { 425 DefaultMutableTreeNode pr=(DefaultMutableTreeNode)rem.next(); 426 removeNodeFromParent(pr); 427 } 428 430 Iterator add=toAdd.iterator(); 432 while (add.hasNext()) { 433 WfProcess pr=(WfProcess)add.next(); 434 try { 435 WfProcessMgr mgr=pr.manager(); 436 insertProcess(SharkClient.getAdminMiscUtilities().getProcessMgrPkgId(mgr.name()),mgr,pr); 438 } catch (Exception ex) { 439 ex.printStackTrace(); 440 } 441 } 442 444 } 448 449 private DefaultMutableTreeNode getModelNode (String pkgId) { 450 DefaultMutableTreeNode root=(DefaultMutableTreeNode)getRoot(); 451 Enumeration pkgs=root.children(); 452 while (pkgs.hasMoreElements()) { 453 DefaultMutableTreeNode dmtn=(DefaultMutableTreeNode)pkgs.nextElement(); 454 Object userObj=dmtn.getUserObject(); 455 if ((userObj instanceof Package ) && ((Package )userObj).get("Id").toString().equals(pkgId)) { 456 return dmtn; 457 } 458 } 459 return null; 460 } 461 462 463 private DefaultMutableTreeNode getManagerNode (String 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 ex) { 475 return null; 476 } 477 } 478 return null; 480 } 481 482 private DefaultMutableTreeNode getProcessNode (String 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 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 |