1 package hero.session; 2 24 import hero.interfaces.BnAgentEdgeLocal; 25 import hero.interfaces.BnAgentLocal; 26 import hero.interfaces.BnEdgeLocal; 27 import hero.interfaces.BnNodeLocal; 28 import hero.interfaces.BnNodePropertyLocal; 29 import hero.interfaces.BnProjectPropertyLocal; 30 import hero.interfaces.BnProjectLocal; 31 import hero.interfaces.BnProjectLocalHome; 32 import hero.interfaces.BnProjectUtil; 33 import hero.interfaces.BnNodePropertyValue; 34 import hero.interfaces.BnProjectPropertyValue; 35 import hero.interfaces.ProjectSessionLocal; 36 import hero.interfaces.ProjectSessionLocalHome; 37 import hero.interfaces.ProjectSessionUtil; 38 import hero.util.BonitaConfig; 39 import hero.util.BonitaProjectLocator; 40 import hero.util.BonitaServiceException; 41 import hero.util.BonitaServiceValue; 42 import hero.util.EngineException; 43 import hero.util.EventConstants; 44 import hero.util.HeroException; 45 import hero.util.HeroHookException; 46 import hero.util.BonitaDeadline; 47 import hero.interfaces.JMSServicesSessionUtil; 48 import hero.interfaces.JMSServicesSessionLocalHome; 49 import hero.interfaces.JMSServicesSessionLocal; 50 import hero.interfaces.UserServiceLocalHome; 51 import hero.interfaces.UserServiceLocal; 52 53 import hero.interfaces.BnNodePerformerAssignLocal; 54 import hero.interfaces.BnUserLocal; 55 import hero.interfaces.BnUserLocalHome; 56 import hero.util.MailNotification; 57 import hero.util.values.BonitaEdgeValue; 58 import hero.util.values.BonitaHookValue; 59 import hero.util.values.BonitaInterHookValue; 60 import hero.util.values.BonitaIterationValue; 61 import hero.util.values.BonitaNodeValue; 62 import hero.util.values.BonitaProjectValue; 63 import hero.util.values.BonitaPropertyValue; 64 import hero.interfaces.Constants; 65 66 import java.util.Collection ; 67 import java.util.ArrayList ; 68 import java.util.Iterator ; 69 import java.util.Map ; 70 import java.util.Date ; 71 72 import javax.ejb.CreateException ; 73 import javax.ejb.EJBException ; 74 import javax.ejb.FinderException ; 75 import javax.ejb.SessionBean ; 76 import javax.ejb.SessionContext ; 77 78 import org.apache.log4j.Logger; 79 80 import bsh.Interpreter; 81 82 100 101 public class EngineBean implements SessionBean , Constants { 102 103 private static final Logger log = Logger.getLogger("log"); 108 109 113 private SessionContext mContext; 114 115 private BnProjectLocal mProject; 116 private ProjectSessionLocal mPSession; 117 private JMSServicesSessionLocal jms; 118 private boolean raiseError = false; 119 private ArrayList visited = new ArrayList (); 120 private Collection iterationPath = new ArrayList (); 121 private BonitaConfig bonitaConfig; 122 123 private BonitaProjectValue model=null; 124 125 133 public void terminate(String projectName) 134 throws EngineException, HeroException { 135 if (this.getProjectConfigLog(projectName)) 136 log.debug("Start terminate the project = " + projectName + " started by " + mContext.getCallerPrincipal().getName()); 137 BnProjectLocalHome pHome; 138 BnProjectLocal pLocal; 139 ProjectSessionLocalHome psHome; 140 ProjectSessionLocal temp = mPSession; try { 142 psHome = ProjectSessionUtil.getLocalHome(); 143 pHome = BnProjectUtil.getLocalHome(); 144 } catch (javax.naming.NamingException ne) { 145 throw new HeroException(ne.getMessage()); 146 } 147 try { 148 mPSession = psHome.create(); 149 mPSession.setCaller(mContext.getCallerPrincipal().getName()); 150 mPSession.initProject(projectName); 151 pLocal = pHome.findByName(projectName); 152 if (mProject.getType().equals(Constants.Pj.INSTANCE)) 153 { 154 if (mProject.getStatus().equals(Constants.Pj.HIDDEN)) 155 throw new EngineException("Project is hidden. You cannot terminate an instance of a hidden model !!"); 156 } 157 else if(mProject.getStatus().equals(Constants.Pj.HIDDEN)) 158 throw new EngineException("Project is hidden. You cannot terminate a hidden project !!"); 159 if (nodesTerminated(pLocal.getBnNodes())) { 160 pLocal.setState(hero.interfaces.Constants.Pj.TERMINATED); 161 pLocal.setEndDate(new java.util.Date ()); 162 } else { 163 throw new EngineException("Project cannot be terminated. Some nodes are still active"); 164 } 165 } catch (FinderException fe) { 166 throw new HeroException("Project does not exist " + projectName); 167 } catch (CreateException he) { 168 throw new HeroException("ProjectSession Error" + projectName); 169 } 170 if (this.getProjectConfigLog(projectName)) 171 log.debug(logProjectProperties("Properties after terminated: ")+ ", project = " + projectName+" started by " + mContext.getCallerPrincipal().getName()); 172 mPSession = temp; 173 if (this.getProjectConfigLog(projectName)) 174 log.debug("End terminate the project = " + projectName+" started by " + mContext.getCallerPrincipal().getName()); 175 } 176 177 183 public void startActivity(String projectName, String nodeName) 184 throws EngineException, HeroException { 185 if (this.getProjectConfigLog(projectName)) 186 log.debug("Start startActivity = " + nodeName +" of the project = " + projectName + " started by " + mContext.getCallerPrincipal().getName()); 187 ProjectSessionLocalHome psHome; 188 BnProjectLocalHome pHome; 189 try { 190 psHome = ProjectSessionUtil.getLocalHome(); 191 pHome = BnProjectUtil.getLocalHome(); 192 } catch (javax.naming.NamingException ne) { 193 throw new HeroException(ne.getMessage()); 194 } 195 try { 196 mProject = pHome.findByName(projectName); 197 if (mProject.getType().equals(Constants.Pj.INSTANCE)) 198 { 199 if (mProject.getStatus().equals(Constants.Pj.HIDDEN)) 200 throw new EngineException("Project is hidden. You cannot start an activity of a hidden model !!"); 201 } 202 else if(mProject.getStatus().equals(Constants.Pj.HIDDEN)) 203 throw new EngineException("Project is hidden. You cannot start an activity of a hidden project !!"); 204 mPSession = psHome.create(); 205 mPSession.setCaller(mContext.getCallerPrincipal().getName()); 206 mPSession.initProject(projectName); 207 } catch (FinderException fe) { 208 throw new HeroException("Project does not exist " + projectName); 209 } catch (CreateException he) { 210 throw new HeroException("ProjectSession Error" + projectName); 211 } 212 try { 213 if (mProject.getType().equals(Constants.Pj.COOPERATIVE)) 214 mProject.setModel(BonitaProjectLocator.generateProjectValue(mProject.getName())); 215 } catch (BonitaServiceException e) {e.printStackTrace(); 216 throw new HeroException(e.getMessage()); 217 } 218 if (this.getProjectConfigLog(projectName)) 219 log.debug(logActivityProperties(nodeName, "Properties before start")+ ", activity = " + nodeName + "of project = " +projectName+" started by " + mContext.getCallerPrincipal().getName()); 220 BnNodeLocal nd = mProject.getBnNode(nodeName); 221 if ( (nd.getActivityPerformer() != null && ( nd.getActivityPerformer()).equals(mContext.getCallerPrincipal().getName())) || ! (nd.getActivityPerformer() != null)) 222 { 223 execute(nodeName, hero.interfaces.Constants.Nd.START); 224 if (mProject.getState()== hero.interfaces.Constants.Pj.INITIAL) 225 mProject.setState(hero.interfaces.Constants.Pj.STARTED); 226 if (raiseError == true) 227 { 228 raiseError = false; 229 throw new HeroHookException("An error is produced during the execution of a BeforeStart hook..."); 230 } 231 } 232 else 233 throw new HeroException("Only activity performer can execute this activity."); 234 if (this.getProjectConfigLog(projectName)) 235 log.debug("End of start activity = " + nodeName + "of project = " +projectName+" started by " + mContext.getCallerPrincipal().getName()); 236 if (this.getProjectConfigLog(projectName)) 237 log.debug(logActivityProperties(nodeName, "Properties after start")+ ", activity = " + nodeName + "of project = " +projectName+" started by " + mContext.getCallerPrincipal().getName()); 238 239 } 240 241 247 public void terminateActivity(String projectName, String nodeName) 248 throws EngineException, HeroException { 249 if (this.getProjectConfigLog(projectName)) 250 log.debug("Start terminateActivity = " + nodeName +" of the project = " + projectName + " started by " + mContext.getCallerPrincipal().getName()); 251 ProjectSessionLocalHome psHome; 252 BnProjectLocalHome pHome; 253 try { 254 psHome = ProjectSessionUtil.getLocalHome(); 255 pHome = BnProjectUtil.getLocalHome(); 256 } catch (javax.naming.NamingException ne) { 257 throw new HeroException(ne.getMessage()); 258 } 259 try { 260 mProject = pHome.findByName(projectName); 261 if (mProject.getType().equals(Constants.Pj.INSTANCE)) 262 { 263 if (mProject.getStatus().equals(Constants.Pj.HIDDEN)) 264 throw new EngineException("Project is hidden. You cannot terminate an activity of a hidden model !!"); 265 } 266 else if(mProject.getStatus().equals(Constants.Pj.HIDDEN)) 267 throw new EngineException("Project is hidden. You cannot terminate an activity of a hidden project !!"); 268 mPSession = psHome.create(); 269 mPSession.setCaller(mContext.getCallerPrincipal().getName()); 270 mPSession.initProject(projectName); 271 } catch (FinderException fe) { 273 throw new HeroException("Project does not exist " + projectName); 274 } catch (CreateException he) { 275 throw new HeroException("ProjectSession Error" + projectName); 276 } 277 if (this.getProjectConfigLog(projectName)) 278 log.debug(logActivityProperties(nodeName, "Properties before terminate")+ ", activity = " + nodeName + "of project = " +projectName+" started by " + mContext.getCallerPrincipal().getName()); 279 execute(nodeName, hero.interfaces.Constants.Nd.TERMINATE); 280 if (raiseError == true) 281 { 282 raiseError = false; 283 throw new HeroHookException("An error is produced during the execution of a hook..."); 284 } 285 if (this.getProjectConfigLog(projectName)) 286 log.debug("End of terminate activity = " + nodeName + "of project = " +projectName+" started by " + mContext.getCallerPrincipal().getName()); 287 if (this.getProjectConfigLog(projectName)) 288 log.debug(logActivityProperties(nodeName, "Properties after terminate")+ ", activity = " + nodeName + "of project = " +projectName+" started by " + mContext.getCallerPrincipal().getName()); 289 if (!mProject.getParent().equals(mProject.getName())) 291 { 292 if (mPSession.isTerminated()) 293 this.terminateActivity(mProject.getParent(),mProject.getName()); 294 } 295 } 296 297 303 public void cancelActivity(String projectName, String nodeName) 304 throws EngineException, HeroException { 305 if (this.getProjectConfigLog(projectName)) 306 log.debug("Start cancelActivity = " + nodeName +" of the project = " + projectName + " started by " + mContext.getCallerPrincipal().getName()); 307 ProjectSessionLocalHome psHome; 308 BnProjectLocalHome pHome; 309 try { 310 psHome = ProjectSessionUtil.getLocalHome(); 311 pHome = BnProjectUtil.getLocalHome(); 312 } catch (javax.naming.NamingException ne) { 313 throw new HeroException(ne.getMessage()); 314 } 315 try { 316 mProject = pHome.findByName(projectName); 317 if (mProject.getType().equals(Constants.Pj.INSTANCE)) 318 { 319 if (mProject.getStatus().equals(Constants.Pj.HIDDEN)) 320 throw new EngineException("Project is hidden. You cannot cancel an activity of a hidden model !!"); 321 } 322 else if(mProject.getStatus().equals(Constants.Pj.HIDDEN)) 323 throw new EngineException("Project is hidden. You cannot cancel an activity of a hidden project !!"); 324 mPSession = psHome.create(); 325 mPSession.setCaller(mContext.getCallerPrincipal().getName()); 326 mPSession.initProject(projectName); 327 } catch (FinderException fe) { 329 throw new HeroException("Project does not exist " + projectName); 330 } catch (CreateException he) { 331 throw new HeroException("ProjectSession Error" + projectName); 332 } 333 execute(nodeName, hero.interfaces.Constants.Nd.CANCEL); 334 if (this.getProjectConfigLog(projectName)) 335 log.debug("End of cancel activity = " + nodeName + "of project = " +projectName+" started by " + mContext.getCallerPrincipal().getName()); 336 } 337 338 344 public void suspendActivity(String projectName, String nodeName) 345 throws EngineException, HeroException { 346 ProjectSessionLocalHome psHome; 348 BnProjectLocalHome pHome; 349 try { 350 psHome = ProjectSessionUtil.getLocalHome(); 351 pHome = BnProjectUtil.getLocalHome(); 352 } catch (javax.naming.NamingException ne) { 353 throw new HeroException(ne.getMessage()); 354 } 355 try { 356 mProject = pHome.findByName(projectName); 357 mPSession = psHome.create(); 358 mPSession.setCaller(mContext.getCallerPrincipal().getName()); 359 mPSession.initProject(projectName); 360 } catch (FinderException fe) { 361 throw new HeroException("Project does not exist " + projectName); 362 } catch (CreateException he) { 363 throw new HeroException("ProjectSession Error" + projectName); 364 } 365 execute(nodeName, hero.interfaces.Constants.Nd.SUSPEND); 366 } 367 368 371 372 378 public void resumeActivity(String projectName, String nodeName) 379 throws EngineException, HeroException { 380 ProjectSessionLocalHome psHome; 382 BnProjectLocalHome pHome; 383 try { 384 psHome = ProjectSessionUtil.getLocalHome(); 385 pHome = BnProjectUtil.getLocalHome(); 386 } catch (javax.naming.NamingException ne) { 387 throw new HeroException(ne.getMessage()); 388 } 389 try { 390 mProject = pHome.findByName(projectName); 391 mPSession = psHome.create(); 392 mPSession.setCaller(mContext.getCallerPrincipal().getName()); 393 mPSession.initProject(projectName); 394 } catch (FinderException fe) { 395 throw new HeroException("Project does not exist " + projectName); 396 } catch (CreateException he) { 397 throw new HeroException("ProjectSession Error" + projectName); 398 } 399 execute(nodeName, hero.interfaces.Constants.Nd.RESUME); 400 } 401 402 408 public void activeAgent(String projectName, String agentName) 409 throws EngineException, HeroException { 410 BnAgentLocal ag; 412 ProjectSessionLocalHome psHome; 413 BnProjectLocalHome pHome; 414 try { 415 psHome = ProjectSessionUtil.getLocalHome(); 416 pHome = BnProjectUtil.getLocalHome(); 417 } catch (javax.naming.NamingException ne) { 418 throw new HeroException(ne.getMessage()); 419 } 420 try { 421 mProject = pHome.findByName(projectName); 422 mPSession = psHome.create(); 423 mPSession.setCaller(mContext.getCallerPrincipal().getName()); 424 mPSession.initProject(projectName); 425 ag = mProject.getBnAgent(agentName); 426 mPSession.setAgentState(ag, hero.interfaces.Constants.Ag.ACTIVE); 427 Collection edges = ag.getOutEdges(); 428 if (!edges.isEmpty()) 429 modifyAgentEdges(edges); 430 } catch (FinderException fe) { 431 throw new HeroException("Project does not exist " + projectName); 432 } catch (CreateException he) { 433 throw new HeroException("ProjectSession Error" + projectName); 434 } 435 } 436 437 private void execute(String _node, int operation) throws HeroException, EngineException { 439 441 BnNodeLocal nd; 442 nd = mProject.getBnNode(_node); 443 444 int newState = nd.getTransition().computeState(nd, operation); 445 448 if (newState == hero.interfaces.Constants.Nd.BAD_TRANSITION) 449 throw new EngineException("Bad state transition " + _node); 450 451 if (newState != nd.getState()) { 452 if (newState == hero.interfaces.Constants.Nd.TERMINATED) { 453 if (nd.getType()== hero.interfaces.Constants.Nd.SUB_PROCESS_NODE) 454 { 455 this.terminate(_node); 456 mPSession.propagatesSubProcessProperties(_node); 457 } 458 jms.sendProjectEvent(EventConstants.TERMINATED,mProject.getName(),_node,mContext.getCallerPrincipal().getName()); 459 this.hookExecute(_node, hero.interfaces.Constants.Nd.BEFORETERMINATE); 460 mPSession.propagateNodeProperties(_node); 461 } else if (newState == hero.interfaces.Constants.Nd.EXECUTING) { 462 if (nd.getType()== hero.interfaces.Constants.Nd.SUB_PROCESS_NODE) 463 mPSession.propagatesParentProperties(_node); 464 jms.sendProjectEvent(EventConstants.START,mProject.getName(),_node,mContext.getCallerPrincipal().getName()); 465 this.hookExecute(_node, hero.interfaces.Constants.Nd.BEFORESTART); 466 } else if (newState == hero.interfaces.Constants.Nd.ANTICIPATING) { 467 jms.sendProjectEvent(EventConstants.ANTICIPATING,mProject.getName(),_node,mContext.getCallerPrincipal().getName()); 468 this.hookExecute(_node,hero.interfaces.Constants.Nd.ANTICIPATE); 469 } else if (newState == hero.interfaces.Constants.Nd.DEAD) { 470 mPSession.propagateNodeProperties(_node); 471 jms.sendProjectEvent(EventConstants.CANCELED,mProject.getName(),_node,mContext.getCallerPrincipal().getName()); 472 this.hookExecute(_node, hero.interfaces.Constants.Nd.ONCANCEL); 473 } 474 475 if (activityStarted(newState)) { 476 this.hookExecute(_node,hero.interfaces.Constants.Nd.AFTERSTART); 477 } 478 479 if ((newState == hero.interfaces.Constants.Nd.TERMINATED || newState == hero.interfaces.Constants.Nd.DEAD) && mPSession.getIterationExist(nd.getName())) 481 { 482 mPSession.setNodeState(nd,hero.interfaces.Constants.Nd.INITIAL); 483 String preNode = mPSession.getToIteration(nd.getName()); 484 String condition = mPSession.getIterationCondition(nd.getName()); 485 486 if (preNode != null && (evaluateCondition(condition, nd))==Constants.CONDITION_TRUE) 487 { 488 this.setIterationPathInitial(nd.getName(),preNode); 489 BnNodeLocal preNodeLocal = mProject.getBnNode(preNode); 490 mPSession.setNodeState(preNodeLocal,hero.interfaces.Constants.Nd.READY); 492 493 if (preNodeLocal.getType() == Constants.Nd.AND_JOIN_AUTOMATIC_NODE || preNodeLocal.getType() == Constants.Nd.OR_JOIN_AUTOMATIC_NODE || preNodeLocal.getType() == Constants.Nd.SUB_PROCESS_NODE) 495 this.execute(preNode,Constants.Nd.START); 496 this.hookExecute(preNode, hero.interfaces.Constants.Nd.ONREADY); if ( preNodeLocal.getBnNodePerformerAssign() != null) 498 this.performAssign(preNodeLocal); 499 } 500 else if (preNode != null) 501 { 502 if (newState == hero.interfaces.Constants.Nd.TERMINATED) 503 mPSession.setNodeState(nd,hero.interfaces.Constants.Nd.TERMINATED); 504 else 505 mPSession.setNodeState(nd,hero.interfaces.Constants.Nd.DEAD); 506 activeStandByEdges(nd.getName(),preNode); 507 } 508 } 509 else 510 { 511 mPSession.setNodeState(nd, newState); 512 mPSession.setNodeExecutor(_node); 513 } 514 if (newState == hero.interfaces.Constants.Nd.READY && (nd.getDeadlines().size() != 0 || nd.getRelativeDeadlines().size() != 0)) { 515 BonitaDeadline.getInstance().startDeadlines(_node,nd.getBnProject().getId()); 516 } else { 517 if ((newState == hero.interfaces.Constants.Nd.TERMINATED || newState == hero.interfaces.Constants.Nd.DEAD) 519 && nd.getDeadlines().size() != 0) { 520 Collection co = nd.getDeadlines(); 521 Iterator it = co.iterator(); 522 while (it.hasNext()) { 523 long date = ((java.sql.Date ) it.next()).getTime(); 524 if (date > new Date ().getTime()) 525 ; 526 BonitaDeadline.getInstance().removeNodeNotification(mProject.getName(),_node, date); 527 } 528 } 529 if ((newState == hero.interfaces.Constants.Nd.TERMINATED || newState == hero.interfaces.Constants.Nd.DEAD) 531 && nd.getRelativeDeadlines().size() != 0) { 532 Collection co = nd.getRelativeDeadlines(); 533 Iterator it = co.iterator(); 534 while (it.hasNext()) { 535 long date = ((java.sql.Date ) it.next()).getTime(); 536 if (new Date ().getTime() + date > new Date ().getTime()); 537 BonitaDeadline.getInstance().removeNodeNotification(mProject.getName(), _node, date); 538 } 539 } 540 } 541 try { 542 if (newState == hero.interfaces.Constants.Nd.READY && nd.getBnNodePerformerAssign() != null) 543 this.performAssign(nd); 544 else if (newState==hero.interfaces.Constants.Nd.READY) 545 this.hookExecute(_node, hero.interfaces.Constants.Nd.ONREADY); 546 } catch (Exception e){ 547 e.printStackTrace(); 548 throw new HeroException("Error when call of performAssign()!!!"); 549 } 550 Collection edges = nd.getOutBnEdges(); 551 if (edges.isEmpty() && isInstance(mProject.getName()) && newState != Constants.Nd.INITIAL){ 552 if (!instanceWasModified(nd)) 553 { 554 this.generateOutNodes(nd); 555 if (newState == Constants.Nd.DEAD) 556 mPSession.propagateNodeProperties(_node); 557 edges = nd.getOutBnEdges(); 558 } 559 } 560 modifyNodeEdges(newState, edges); 561 } 562 564 if (newState == hero.interfaces.Constants.Nd.TERMINATED) 565 this.hookExecute(_node, hero.interfaces.Constants.Nd.AFTERTERMINATE); 566 567 if (newState == hero.interfaces.Constants.Nd.EXECUTING && nd.getType() == hero.interfaces.Constants.Nd.AND_JOIN_AUTOMATIC_NODE) 568 this.execute(_node,hero.interfaces.Constants.Nd.TERMINATE); 569 } 570 571 private void performAssign(BnNodeLocal nd) throws HeroException { 572 try { 573 BnNodePerformerAssignLocal performerAssign = null; 574 BnUserLocal mUser; 575 UserServiceLocalHome ushome; 576 UserServiceLocal ul; 577 String creator = mContext.getCallerPrincipal().getName(); 578 performerAssign = nd.getBnNodePerformerAssign(); 579 hero.performerAssign.PerformerAssign h = hero.performerAssign.PerformerAssign.make(performerAssign.getName(), performerAssign.getType()); 580 h.execute(this, performerAssign.getType(), nd, creator); 581 582 String activityPerformerName = nd.getActivityPerformer(); 584 585 try { 586 BnUserLocalHome userhome=hero.interfaces.BnUserUtil.getLocalHome(); 587 ushome=hero.interfaces.UserServiceUtil.getLocalHome(); 588 mUser=userhome.findByName(activityPerformerName); 589 590 } catch (javax.naming.NamingException ne) { 591 throw new HeroException(ne.getMessage()); 592 } catch (javax.ejb.FinderException fe) { 593 throw new HeroException(activityPerformerName+" does not exists"); 594 } 595 try{ 596 ul= ushome.create(); 597 Map infos = ul.getUserInfos(mUser.getName()); 598 if (infos.get("email")!=null) { 599 MailNotification mailNotif = new MailNotification(); 600 mailNotif.initMailService(); 601 mailNotif.sendMail((String )infos.get("email"),"Bonita Notification: activity assigned to you","Hello,\n\n For the Project: " + mProject.getName()+ ",\nthe activity for the node : " + nd.getName() + "\nis ready to be started by you.\n\nPlease connect to the Bonita manager application." ); 602 } 603 } catch (Exception e){ 604 throw new HeroException("Activity assign Notification fails" + e.getMessage()); 605 } 606 607 } catch (HeroException he) { 608 he.printStackTrace(); 609 throw new HeroException("Error in performerAssignment" + he.getMessage()); 610 } 611 } 612 613 617 618 private void modifyNodeEdges(int nodeState, Collection edges) 619 throws HeroException, EngineException { 620 int newEdgeState; 621 Iterator i = edges.iterator(); 622 while (i.hasNext()) { 623 BnEdgeLocal edge = (BnEdgeLocal) i.next(); 624 boolean inNode = nodeExistInIteration(edge.getInBnNode().getName()); 625 boolean outNode = nodeExistInIteration(edge.getOutBnNode().getName()); 626 if ((inNode && outNode) || (!inNode && !outNode) ||(!inNode && outNode)) { 628 if (nodeState != hero.interfaces.Constants.Nd.TERMINATED && nodeState != hero.interfaces.Constants.Nd.DEAD) { 629 newEdgeState =hero.interfaces.Constants.edgeTransition[Constants.CONDITION_TRUE][nodeState]; 630 mPSession.setEdgeState(edge, newEdgeState); 631 edgeChanged(edge); 632 } else { 633 newEdgeState =hero.interfaces.Constants.edgeTransition[evaluateCondition(edge.getCondition(),edge.getInBnNode())][nodeState]; 634 if (edge.getState() != newEdgeState) { 635 mPSession.setEdgeState(edge, newEdgeState); 636 edgeChanged(edge); 637 } 638 } 639 } 640 } 641 } 642 643 646 647 private void activeStandByEdges(String lastNode, String firstNode) throws HeroException { 648 ArrayList nodes = new ArrayList (); 649 boolean stop = false; int index=0; 650 651 Collection activeEdges = mPSession.getNodeOutEdges(lastNode); 652 Iterator j = activeEdges.iterator(); 653 while (j.hasNext()) 654 { 655 String aEdge = (String )j.next(); 656 if (!nodes.contains(mPSession.getEdgeOutNode(aEdge))) 657 { 658 BnEdgeLocal ed = mProject.getBnEdge(aEdge); 659 int newEdgeState =hero.interfaces.Constants.edgeTransition[evaluateCondition(ed.getCondition(),ed.getInBnNode())][mPSession.getNodeState(lastNode)]; 660 661 mPSession.setEdgeState(ed, newEdgeState); 662 edgeChanged(ed); 663 } 664 } 665 while (!stop) 666 { 667 Collection edges = mPSession.getNodeInEdges(lastNode); 668 Iterator i = edges.iterator(); 669 while (i.hasNext()) 670 { 671 String edgeName = (String )i.next(); 672 if (!nodes.contains(mPSession.getEdgeInNode(edgeName))) 673 nodes.add(mPSession.getEdgeInNode(edgeName)); 674 } 675 if (nodes.size()==0) 676 nodes.add(firstNode); 677 String actNode = (String )nodes.get(index); 678 activeEdges = mPSession.getNodeOutEdges(actNode); 679 j = activeEdges.iterator(); 680 while (j.hasNext()) 681 { 682 String aEdge = (String )j.next(); 683 if (!nodes.contains(mPSession.getEdgeOutNode(aEdge))) 684 { 685 BnEdgeLocal ed = mProject.getBnEdge(aEdge); 686 int newEdgeState =hero.interfaces.Constants.edgeTransition[evaluateCondition(ed.getCondition(), ed.getInBnNode())][mPSession.getNodeState(actNode)]; 687 688 mPSession.setEdgeState(ed, newEdgeState); 689 edgeChanged(ed); 690 } 691 } 692 lastNode = (String )nodes.get(index); 693 if (lastNode.equals(firstNode)) 694 { 695 stop=true; 696 nodes.remove(lastNode); 697 } 698 index++; 699 } 700 } 701 702 705 private boolean nodeExistInIteration(String node) throws HeroException { 706 707 Collection ite = mProject.getModel().getIterations(); 708 Iterator iter = ite.iterator(); 709 710 while (iter.hasNext()) 711 { 712 BonitaIterationValue iv = (BonitaIterationValue)iter.next(); 713 if (isInstance(node)) 714 node = getModel(node); 715 if (isNodeInIterationPath(iv.getFromNode(), iv.getToNode(),node) || node.equals(iv.getFromNode())) 716 { 717 mPSession.initProject(mProject.getName()); 718 return true; 719 } 720 } 721 mPSession.initProject(mProject.getName()); 722 return false; 723 } 724 725 private boolean isNodeInIterationPath(String in, String out, String node) throws HeroException { 726 if (isInstance(in)) 727 in = getModel(in); 728 if (isInstance(out)) 729 out = getModel(out); 730 if (isInstance(node)) 731 node = getModel(node); 732 733 BonitaNodeValue nd = BonitaServiceValue.getNodeFromCache(mProject.getModel(),in); 734 Collection edges = nd.getInEdges(); 735 Iterator eds = edges.iterator(); 736 while (eds.hasNext()) 737 { 738 BonitaEdgeValue ed = (BonitaEdgeValue)eds.next(); 739 String edgeNode = ed.getInNode(); 740 if (edgeNode.equals(node)) 741 { 742 iterationPath.add(node); 743 return true; 744 } 745 if (edgeNode.equals(out)) 746 return false; 747 else 748 if (isNodeInIterationPath(edgeNode,out,node)) 749 return true; 750 } 751 return false; 752 } 753 754 private void setIterationPathInitial(String in, String out) throws HeroException 755 { 756 if (!in.equals(out)) { 757 ArrayList nodes = new ArrayList (); 758 BnNodeLocal actualNode = mProject.getBnNode(in); 759 if (actualNode.getType() == Nd.SUB_PROCESS_NODE) 760 initSubProcess(actualNode.getName()); 761 nodes.add(actualNode); 762 while (!(actualNode.getName()).equals(out) || !nodes.isEmpty()) { 763 nodes.remove(actualNode); 764 Collection edges = actualNode.getInBnEdges(); 765 Iterator eds = edges.iterator(); 766 while (eds.hasNext()) { 767 BnEdgeLocal ed = (BnEdgeLocal) eds.next(); 768 BnNodeLocal node = ed.getInBnNode(); 769 if (isNodeInIterationPath(in, out, node.getName())) { 770 mPSession.setNodeState(node, Constants.Nd.INITIAL); 771 if (node.getType() == Nd.SUB_PROCESS_NODE) 772 initSubProcess(node.getName()); 773 mPSession.setEdgeState(ed, Constants.Ed.INITIAL); 774 nodes.add(node); 775 } 776 } 777 if (!nodes.isEmpty()) 778 actualNode = (BnNodeLocal) nodes.get(0); 779 780 } 781 } 782 } 783 784 787 private void modifyAgentEdges(Collection edges) 788 throws HeroException, EngineException { 789 Iterator i = edges.iterator(); 790 while (i.hasNext()) { 791 BnAgentEdgeLocal edge = (BnAgentEdgeLocal) i.next(); 792 mPSession.setAgentEdgeState(edge,hero.interfaces.Constants.Ed.ANTICIPATING); 793 BnNodeLocal nd = edge.getOutBnNode(); 794 int operation = nd.getActivation().state(nd); 795 execute(nd.getName(), hero.interfaces.Constants.Nd.ANTACTIVE); 796 } 797 } 798 799 private void edgeChanged(BnEdgeLocal edge) 800 throws HeroException, EngineException { 801 BnNodeLocal nd = edge.getOutBnNode(); 802 if (nd.getState() != hero.interfaces.Constants.Nd.DEAD) { 803 int operation = nd.getActivation().state(nd); 804 execute(nd.getName(), operation); 805 } 806 } 807 808 private Collection getNodeInEdges(BnNodeLocal nd ){ 809 Collection edgeValues = new ArrayList (); 810 Collection edges = nd.getInBnEdges(); 811 Iterator nodeEdges = edges.iterator(); 812 while (nodeEdges.hasNext()) 813 edgeValues.add(((BnEdgeLocal)nodeEdges.next()).getBnEdgeValue()); 814 return edgeValues; 815 816 } 817 818 private boolean activityStarted(int state) throws HeroException { 819 return ( 820 state == hero.interfaces.Constants.Nd.ANTICIPATING 821 || state == hero.interfaces.Constants.Nd.EXECUTING); 822 } 823 824 private void hookExecute(String nodeName, String eventName) 825 throws HeroException, HeroHookException { 826 Collection hooks; 827 Collection interHooks; 828 BnNodeLocal node; 829 BonitaProjectValue currentProject; 830 try { 831 node = mProject.getBnNode(nodeName); 832 model = mProject.getModel(); 833 834 if (nodeName.matches(".*_instance.*")) 835 nodeName=nodeName.substring(0,nodeName.indexOf("_instance")); 836 837 BonitaNodeValue nodeLocal; 838 if (mProject.getType().equals(Constants.Pj.COOPERATIVE)) 839 nodeLocal = BonitaServiceValue.getNode(mProject,nodeName); 840 else 841 nodeLocal = BonitaServiceValue.getNodeFromCache(model,nodeName); 842 843 hooks = nodeLocal.getHooks(); 844 for (Iterator i = hooks.iterator(); i.hasNext();) { 845 BonitaHookValue hk = (BonitaHookValue) i.next(); 846 if (hk.getEvent().equalsIgnoreCase(eventName)) { 847 hero.hook.Hook h = hero.hook.Hook.make(hk.getName(), eventName, hk.getType()); 848 if (this.getProjectConfigLog(mPSession.getName())) 849 log.debug("hookExecute, eventName = " + eventName+" nodeName = "+nodeName+" projectName = "+mProject.getName()); 850 h.execute(this, eventName, node); 851 } 852 } 853 854 interHooks = nodeLocal.getInterHooks(); 855 for (Iterator i = interHooks.iterator(); i.hasNext();) { 856 BonitaInterHookValue hk = (BonitaInterHookValue)i.next(); 857 if (hk.getEvent().equalsIgnoreCase(eventName)) { 858 hero.hook.Hook h = hero.hook.Hook.make(hk.getName(), eventName, hk.getType(), hk.getScript()); 859 if (this.getProjectConfigLog(mPSession.getName())) 860 log.debug("hookExecute, eventName = " + eventName+" nodeName = "+nodeName+" projectName = "+mProject.getName()); 861 h.execute(this, eventName, node); 862 } 863 } 864 865 if (mProject.getType().equals(Constants.Pj.COOPERATIVE)) 866 hooks = BonitaServiceValue.getProjectHooks(mProject); 867 else 868 hooks = model.getHooks(); 869 for (Iterator i = hooks.iterator(); i.hasNext();) { 870 BonitaHookValue hk = (BonitaHookValue) i.next(); 871 if (hk.getEvent().equalsIgnoreCase(eventName)) { 872 hero.hook.Hook h = hero.hook.Hook.make(hk.getName(), eventName, hk.getType()); 873 if (this.getProjectConfigLog(mPSession.getName())) 874 log.debug("hookExecute, eventName = " + eventName+" nodeName = "+nodeName+" projectName = "+mProject.getName()); 875 h.execute(this, eventName, node); 876 } 877 } 878 879 if (mProject.getType().equals(Constants.Pj.COOPERATIVE)) 880 hooks = BonitaServiceValue.getProjectInterHooks(mProject); 881 else 882 interHooks = model.getInterHooks(); 883 for (Iterator i = interHooks.iterator(); i.hasNext();) { 884 BonitaInterHookValue hk = (BonitaInterHookValue) i.next(); 885 if (hk.getEvent().equalsIgnoreCase(eventName)) { 886 hero.hook.Hook h = hero.hook.Hook.make(hk.getName(), eventName, hk.getType(),hk.getScript()); 887 if (this.getProjectConfigLog(mPSession.getName())) 888 log.debug("hookExecute, eventName = " + eventName+" nodeName = "+nodeName+" projectName = "+mProject.getName()); 889 h.execute(this, eventName, node); 890 } 891 } 892 }catch(HeroHookException hh){ 893 if (this.getProjectConfigLog(mPSession.getName())) 894 log.error("hookExecute ERROR, eventName = " + eventName+" nodeName = "+nodeName+" projectName = "+mProject.getName()+" Error message: "+hh.getMessage()); 895 if (eventName.equals(hero.interfaces.Constants.Nd.AFTERSTART) || eventName.equals(hero.interfaces.Constants.Nd.BEFORETERMINATE) 896 || eventName.equals(hero.interfaces.Constants.Nd.ANTICIPATE) || eventName.equals(hero.interfaces.Constants.Nd.ONCANCEL)) { 897 mContext.setRollbackOnly(); 898 throw new HeroHookException("Cannot execute hook "+hh.getMessage()); 899 } 900 else 901 raiseError = true; 902 }; 903 } 904 905 private boolean nodesTerminated(Collection nodes) { 906 Iterator i = nodes.iterator(); 907 while (i.hasNext()) { 908 BnNodeLocal node = (BnNodeLocal) i.next(); 909 int state = node.getState(); 910 if (state != hero.interfaces.Constants.Nd.TERMINATED 911 && state != hero.interfaces.Constants.Nd.EXPIRED 912 && state != hero.interfaces.Constants.Nd.DEAD) 913 return false; 914 } 915 return true; 916 } 917 918 public int evaluateCondition(String condition, BnNodeLocal node) { 919 try { 920 if (condition != null) { 921 condition = "import hero.interfaces.BnNodeLocal;\n" 922 + "boolean evalCondition (Object node) {\n\n\n" 923 + "return(" 924 + condition 925 + ");" 926 + "}"; 927 Interpreter i = new Interpreter(); 928 929 931 Collection prop = node.getBnProperties(); 932 for (Iterator props = prop.iterator(); props.hasNext();) { 933 BnNodePropertyLocal pl = (BnNodePropertyLocal) props.next(); 934 i.set(pl.getTheKey(), pl.getTheValue()); 935 } 936 937 BnProjectLocal project = node.getBnProject(); 939 Collection projectProp = project.getBnProperties(); 940 for (Iterator props=projectProp.iterator();props.hasNext();) { 941 BnProjectPropertyLocal pP=(BnProjectPropertyLocal)props.next(); 942 i.set(pP.getTheKey(),pP.getTheValue()); 943 } 944 i.set("node",node); 945 i.eval(condition); 946 i.eval("result=evalCondition(node)"); 947 if (((Boolean ) i.get("result")).booleanValue() == true) 948 return (Constants.CONDITION_TRUE); 949 else 950 return (Constants.CONDITION_FALSE); 951 } else 952 return (Constants.CONDITION_TRUE); 953 } catch (Exception t) { 954 t.printStackTrace(); 955 return (Constants.CONDITION_FALSE); 956 } 957 } 958 959 private String logActivityProperties(String nodeName, String prelog) 960 throws HeroException { 961 Collection np = mPSession.getNodeProperties(nodeName); 962 Iterator i = np.iterator(); 963 String thelog = prelog; 964 if (np.size() != 0) { 965 while (i.hasNext()) { 966 thelog = thelog + " key= '"; 967 BnNodePropertyValue property = (BnNodePropertyValue) i.next(); 968 thelog = thelog + property.getTheKey(); 969 thelog = thelog + "' value = '" + property.getTheValue() + "'"; 970 } 971 } else 972 thelog = prelog + " No property"; 973 return thelog; 974 } 975 private String logProjectProperties(String prelog) throws HeroException { 976 Collection np = mPSession.getProperties(); 977 Iterator i = np.iterator(); 978 String thelog = prelog; 979 if (np.size() != 0) { 980 while (i.hasNext()) { 981 thelog = thelog + " key= '"; 982 BnProjectPropertyValue property = 983 (BnProjectPropertyValue) i.next(); 984 thelog = thelog + property.getTheKey(); 985 thelog = thelog + "' value = '" + property.getTheValue() + "'"; 986 } 987 } else 988 thelog = prelog + " No property"; 989 return thelog; 990 } 991 992 private void initSubProcess(String subProcess) throws HeroException { 994 ProjectSessionLocalHome psh; 995 ProjectSessionLocal psj; 996 BnProjectLocalHome ph; 997 BnProjectLocal pj; 998 try{ 999 psh = ProjectSessionUtil.getLocalHome(); 1000 psj = psh.create(); 1001 psj.initProject(subProcess); 1002 1003 ph = BnProjectUtil.getLocalHome(); 1004 pj = ph.findByName(subProcess); 1005 Collection nodes = pj.getBnNodes(); 1006 Iterator nds = nodes.iterator(); 1007 while (nds.hasNext()) 1008 { 1009 BnNodeLocal node = (BnNodeLocal)nds.next(); 1010 if (node.getType() == Nd.SUB_PROCESS_NODE) 1011 this.initSubProcess(node.getName()); 1012 psj.setNodeState(node,hero.interfaces.Constants.Nd.INITIAL); 1013 } 1014 } catch (Exception e) { 1015 throw new HeroException(e.getMessage()); 1016 } 1017 } 1018 1019 private void generateOutNodes(BnNodeLocal nd) throws HeroException { 1020 BonitaNodeValue parentNode; 1021 model = mProject.getModel(); 1022 if (nd.getName().matches(".*_instance.*")) parentNode = BonitaServiceValue.getNodeFromCache(model,this.getModel(nd.getName())); 1024 else 1025 parentNode = BonitaServiceValue.getNodeFromCache(model,nd.getName()); 1026 Collection outEdges = parentNode.getOutEdges(); 1027 Iterator edges = outEdges.iterator(); 1028 while (edges.hasNext()) { 1029 BonitaEdgeValue edge = (BonitaEdgeValue) edges.next(); 1030 BonitaNodeValue node = BonitaServiceValue.getNodeFromCache(model,edge.getOutNode()); 1031 String nodeName = node.getName(); 1032 1033 if (node.getType() == Nd.SUB_PROCESS_NODE) { 1035 try { 1036 BnProjectLocal temp = mProject; 1037 BnProjectLocalHome opHome = BnProjectUtil.getLocalHome(); 1039 nodeName = mPSession.instantiateProject(nodeName); 1040 mProject = opHome.findByName(nodeName); 1042 mProject.setParent(nd.getBnProject().getName()); 1043 mProject = temp; mPSession.initProject(mProject.getName()); 1046 } catch (Exception e) { 1047 throw new HeroException(e.getMessage()); 1048 } 1049 } 1050 if (!this.nodeExist(nodeName)) 1051 mPSession.copyNode(nodeName,node); 1052 1053 Collection nodeProp = node.getProperties(); 1055 Iterator nodeProps = nodeProp.iterator(); 1056 while (nodeProps.hasNext()) 1057 { 1058 BonitaPropertyValue ndProp = (BonitaPropertyValue)nodeProps.next(); 1059 mPSession.setNodePropertyPossibleValues(nodeName,ndProp.getKey(),ndProp.getPossibleValues()); 1060 mPSession.setNodeProperty(nodeName,ndProp.getKey(),ndProp.getValue(),ndProp.getPropagate()); 1061 } 1062 mPSession.copyEdge(nodeName,nd, edge); 1063 1064 if (nd.getType() == Constants.Nd.AND_JOIN_AUTOMATIC_NODE || nd.getType() == Constants.Nd.OR_JOIN_AUTOMATIC_NODE) 1066 mPSession.propagateNodeProperties(nd.getName()); 1067 1068 Collection iterations = model.getIterations(); 1070 Iterator iter = iterations.iterator(); 1071 while (iter.hasNext()) 1072 { 1073 BonitaIterationValue iteration = (BonitaIterationValue) iter.next(); 1074 String from = iteration.getFromNode(); 1075 if (nodeName.matches(from+"_instance.*") || nodeName.equals(from)) 1076 { 1077 try{ 1078 mPSession.addIteration(nodeName, this.nodeConvertion(iteration.getToNode()),iteration.getCondition()); 1079 }catch(Exception it){} } 1081 } 1082 } 1083 } 1084 1085 private String getModel(String instanceName) { 1087 int i = instanceName.indexOf("_instance"); 1088 return (instanceName.substring(0, i)); 1089 } 1090 1091 private boolean isInstance(String name) { 1092 return (name.matches(".*_instance.*")); 1093 } 1094 1095 private String nodeConvertion(String name) { 1096 Collection nodes = mProject.getBnNodes(); 1097 Iterator nds = nodes.iterator(); 1098 while (nds.hasNext()) 1099 { 1100 String node = ((BnNodeLocal)nds.next()).getName(); 1101 if (node.matches(name+"_.*")) 1102 return node; 1103 } 1104 return name; 1105 } 1106 1107 private boolean nodeExist(String node) throws HeroException{ 1108 return(mPSession.getNodesNames().contains(node)); 1109 } 1110 1111 private boolean getProjectConfigLog(String projectName) throws HeroException{ 1112 return (bonitaConfig.getProcessLog(projectName)); 1113 } 1114 1115 private boolean instanceWasModified(BnNodeLocal node) throws HeroException{ 1116 model = mProject.getModel(); 1117 String name = node.getName(); 1118 if (this.isInstance(name)) 1119 name = this.getModel(node.getName()); 1120 if (BonitaServiceValue.getNodeFromCache(model,name)==null) 1121 return true; 1122 return false; 1123 } 1124 1125 1131 public void ejbCreate() throws CreateException { 1132 try{ 1133 JMSServicesSessionLocalHome home = JMSServicesSessionUtil.getLocalHome(); 1134 jms=home.create(); 1135 }catch(Exception e){throw new CreateException (e.getMessage());} 1136 } 1137 1138 public void setSessionContext(final javax.ejb.SessionContext context) { 1139 mContext = context; 1140 try{ 1141 this.bonitaConfig = new BonitaConfig(); 1142 }catch(Exception e){throw new EJBException (e.getMessage());} 1143 } 1144 1145 public void ejbRemove() { 1146 } 1147 1148 public void ejbActivate() { 1149 } 1150 1151 public void ejbPassivate() { 1152 } 1153 1154} 1155 | Popular Tags |