Source Code Cross Referenced for EngineBean.java in  » Workflow-Engines » bonita-v3.1 » hero » session » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Workflow Engines » bonita v3.1 » hero.session 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        package hero.session;
0002:
0003:        /*
0004:         * 01/02/2004 - 17:04:13
0005:         *
0006:         * ProjectSessionEJB.java -
0007:         * Copyright (C) 2004 Ecoo Team
0008:         * valdes@loria.fr
0009:         *
0010:         *
0011:         * This program is free software; you can redistribute it and/or
0012:         * modify it under the terms of the GNU Lesser General Public License
0013:         * as published by the Free Software Foundation; either version 2
0014:         * of the License, or (at your option) any later version.
0015:         *
0016:         * This program is distributed in the hope that it will be useful,
0017:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
0018:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0019:         * GNU Lesser General Public License for more details.
0020:         *
0021:         * You should have received a copy of the GNU Lesser General Public License
0022:         * along with this program; if not, write to the Free Software
0023:         * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
0024:         */
0025:        import hero.entity.EdgeState;
0026:        import hero.entity.NodeState;
0027:        import hero.interfaces.BnAgentEdgeLocal;
0028:        import hero.interfaces.BnAgentLocal;
0029:        import hero.interfaces.BnEdgeLocal;
0030:        import hero.interfaces.BnIterationLocal;
0031:        import hero.interfaces.BnNodeLocal;
0032:        import hero.interfaces.BnNodePropertyLocal;
0033:        import hero.interfaces.BnProjectPropertyLocal;
0034:        import hero.interfaces.BnProjectLocal;
0035:        import hero.interfaces.BnProjectLocalHome;
0036:        import hero.interfaces.BnProjectUtil;
0037:        import hero.interfaces.BnNodePropertyValue;
0038:        import hero.interfaces.BnProjectPropertyValue;
0039:        import hero.interfaces.ProjectSessionLocal;
0040:        import hero.interfaces.ProjectSessionLocalHome;
0041:        import hero.interfaces.ProjectSessionUtil;
0042:        import hero.interfaces.UserSessionLocal;
0043:        import hero.interfaces.UserSessionLocalHome;
0044:        import hero.interfaces.UserSessionUtil;
0045:        import hero.util.BonitaConfig;
0046:        import hero.util.BonitaProjectLocator;
0047:        import hero.util.BonitaServiceException;
0048:        import hero.util.BonitaServiceValue;
0049:        import hero.util.EngineException;
0050:        import hero.util.EventConstants;
0051:        import hero.util.HeroException;
0052:        import hero.util.HeroHookException;
0053:        import hero.util.BonitaParsing;
0054:        import hero.interfaces.JMSServicesSessionUtil;
0055:        import hero.interfaces.JMSServicesSessionLocalHome;
0056:        import hero.interfaces.JMSServicesSessionLocal;
0057:
0058:        import hero.interfaces.BnNodePerformerAssignLocal;
0059:        import hero.util.values.BonitaEdgeValue;
0060:        import hero.util.values.BonitaHookValue;
0061:        import hero.util.values.BonitaInterHookValue;
0062:        import hero.util.values.BonitaIterationValue;
0063:        import hero.util.values.BonitaNodeValue;
0064:        import hero.util.values.BonitaProjectValue;
0065:        import hero.util.values.BonitaPropertyValue;
0066:        import hero.interfaces.Constants;
0067:        import hero.interfaces.Constants.Nd;
0068:
0069:        import java.util.Collection;
0070:        import java.util.ArrayList;
0071:        import java.util.Iterator;
0072:
0073:        import javax.ejb.CreateException;
0074:        import javax.ejb.FinderException;
0075:        import javax.ejb.SessionBean;
0076:        import javax.ejb.SessionContext;
0077:
0078:        import org.apache.log4j.Logger;
0079:
0080:        import bsh.Interpreter;
0081:
0082:        /**
0083:         * Session Bean Template
0084:         *
0085:         * @ejb:bean name="Engine"
0086:         *           display-name="Engine Bean"
0087:         *           type="Stateless"
0088:         *           transaction-type="Container"
0089:         *           jndi-name="ejb/hero/Engine"
0090:         *           local-jndi-name="ejb/hero/Engine_L"
0091:         *
0092:         * @ejb:ejb-ref ejb-name="BnProject"
0093:         *              ref-name="myhero/BnProject"
0094:         * @ejb:transaction type="Required"
0095:         * @ejb.security-identity run-as="SuperAdmin"
0096:         * @jonas.bean
0097:         *      ejb-name="Engine"
0098:         *      jndi-name="ejb/hero/Engine"
0099:         *
0100:         * @jboss.container-configuration name="Standard Stateless SessionBean for Bonita"
0101:         **/
0102:
0103:        public class EngineBean implements  SessionBean, Constants {
0104:
0105:            // -------------------------------------------------------------------------
0106:            // Static
0107:            // -------------------------------------------------------------------------
0108:            // Utility variable
0109:            private static final Logger log = Logger.getLogger("log");
0110:
0111:            // -------------------------------------------------------------------------
0112:            // Members
0113:            // -------------------------------------------------------------------------
0114:
0115:            private SessionContext mContext;
0116:
0117:            private JMSServicesSessionLocal jms;
0118:            private boolean raiseError = false;
0119:
0120:            private boolean endActivity = false;
0121:
0122:            // -------------------------------------------------------------------------
0123:            // Methods
0124:            // -------------------------------------------------------------------------
0125:            /**
0126:             * Terminates the project
0127:             * @ejb:interface-method view-type="both"
0128:             *
0129:             **/
0130:            public void terminate(String projectName) throws EngineException,
0131:                    HeroException {
0132:
0133:                log.debug("Start terminate the project = " + projectName
0134:                        + " started by "
0135:                        + mContext.getCallerPrincipal().getName());
0136:                BnProjectLocalHome pHome;
0137:                BnProjectLocal pLocal;
0138:                ProjectSessionLocalHome psHome;
0139:                ProjectSessionLocal mPSession;
0140:                try {
0141:                    psHome = ProjectSessionUtil.getLocalHome();
0142:                    pHome = BnProjectUtil.getLocalHome();
0143:                } catch (javax.naming.NamingException ne) {
0144:                    throw new HeroException(ne.getMessage());
0145:                }
0146:                try {
0147:                    mPSession = psHome.create();
0148:                    mPSession
0149:                            .setCaller(mContext.getCallerPrincipal().getName());
0150:                    pLocal = pHome.findByName(projectName);
0151:                    mPSession.initProject(projectName);
0152:                    if (pLocal.getType().equals(Constants.Pj.INSTANCE)) {
0153:                        if (pLocal.getStatus().equals(Constants.Pj.HIDDEN))
0154:                            throw new EngineException(
0155:                                    "Project is hidden. You cannot terminate an instance of a hidden model !!");
0156:                    } else if (pLocal.getStatus().equals(Constants.Pj.HIDDEN))
0157:                        throw new EngineException(
0158:                                "Project is hidden. You cannot terminate a hidden project !!");
0159:                    if (this .endActivity
0160:                            || nodesTerminated(pLocal.getBnNodes())) {
0161:                        pLocal
0162:                                .setState(hero.interfaces.Constants.Pj.TERMINATED);
0163:                        pLocal.setEndDate(new java.util.Date());
0164:                    } else {
0165:                        throw new EngineException(
0166:                                "Project cannot be terminated. Some nodes are still active");
0167:                    }
0168:                } catch (FinderException fe) {
0169:                    throw new HeroException("Project does not exist "
0170:                            + projectName);
0171:                } catch (CreateException he) {
0172:                    throw new HeroException("ProjectSession Error"
0173:                            + projectName);
0174:                }
0175:
0176:                log.debug(logProjectProperties("Properties after terminated: ",
0177:                        mPSession)
0178:                        + ", project = "
0179:                        + projectName
0180:                        + " started by "
0181:                        + mContext.getCallerPrincipal().getName());
0182:                log.debug("End terminate the project = " + projectName
0183:                        + " started by "
0184:                        + mContext.getCallerPrincipal().getName());
0185:                try {
0186:                    if (mPSession != null)
0187:                        mPSession.remove();
0188:                } catch (Exception re) {
0189:                    throw new HeroException(re.getMessage());
0190:                }
0191:            }
0192:
0193:            /**
0194:             * Starts the activity nodeName
0195:             * @param nodeName the name of the activity
0196:             * @ejb:interface-method view-type="both"
0197:             *
0198:             **/
0199:            public void startActivity(String projectName, String nodeName)
0200:                    throws EngineException, HeroException {
0201:
0202:                log.debug("Start startActivity = " + nodeName
0203:                        + " of the project = " + projectName + " started by "
0204:                        + mContext.getCallerPrincipal().getName());
0205:                ProjectSessionLocalHome psHome;
0206:                BnProjectLocalHome pHome;
0207:                ProjectSessionLocal mPSession;
0208:                BnProjectLocal mProject;
0209:                try {
0210:                    psHome = ProjectSessionUtil.getLocalHome();
0211:                    pHome = BnProjectUtil.getLocalHome();
0212:                } catch (javax.naming.NamingException ne) {
0213:                    throw new HeroException(ne.getMessage());
0214:                }
0215:                try {
0216:                    mProject = pHome.findByName(projectName);
0217:                    if (mProject.getType().equals(Constants.Pj.INSTANCE)) {
0218:                        if (mProject.getStatus().equals(Constants.Pj.HIDDEN))
0219:                            throw new EngineException(
0220:                                    "Project is hidden. You cannot start an activity of a hidden model !!");
0221:                    } else if (mProject.getStatus().equals(Constants.Pj.HIDDEN))
0222:                        throw new EngineException(
0223:                                "Project is hidden. You cannot start an activity of a hidden project !!");
0224:                    mPSession = psHome.create();
0225:                    mPSession
0226:                            .setCaller(mContext.getCallerPrincipal().getName());
0227:                    mPSession.initProject(projectName);
0228:                    /*if (!mPSession.isUserInNodeRole(nodeName) && !mPSession.isAdminOfProject(projectName))
0229:                    	throw new HeroException("You are not allowed to start this activity ! ");*/
0230:                } catch (FinderException fe) {
0231:                    throw new HeroException("Project does not exist "
0232:                            + projectName);
0233:                } catch (CreateException he) {
0234:                    throw new HeroException("ProjectSession Error"
0235:                            + projectName);
0236:                }
0237:                /*try {
0238:                	if (mProject.getType().equals(Constants.Pj.COOPERATIVE))
0239:                		mProject.setModel(BonitaProjectLocator.generateProjectValue(mProject.getName()));
0240:                } catch (BonitaServiceException e) {e.printStackTrace();
0241:                throw new HeroException(e.getMessage());
0242:                }*/
0243:
0244:                log.debug(logActivityProperties(nodeName,
0245:                        "Properties before start", mPSession)
0246:                        + ", activity = "
0247:                        + nodeName
0248:                        + "of project = "
0249:                        + projectName
0250:                        + " started by "
0251:                        + mContext.getCallerPrincipal().getName());
0252:                BnNodeLocal nd = mProject.getBnNode(nodeName);
0253:                //	if ( (nd.getActivityPerformer() != null  && ( nd.getActivityPerformer()).equals(mContext.getCallerPrincipal().getName())) || ! (nd.getActivityPerformer() != null))
0254:                //{
0255:                execute(nodeName, hero.interfaces.Constants.Nd.START, mProject,
0256:                        mPSession);
0257:                if (mProject.getState() == hero.interfaces.Constants.Pj.INITIAL)
0258:                    mProject.setState(hero.interfaces.Constants.Pj.STARTED);
0259:                if (raiseError == true) {
0260:                    raiseError = false;
0261:                    throw new HeroHookException(
0262:                            "An error is produced during the execution of a BeforeStart hook...");
0263:                }
0264:                //}
0265:
0266:                log.debug("End of start activity = " + nodeName
0267:                        + "of project = " + projectName + " started by "
0268:                        + mContext.getCallerPrincipal().getName());
0269:
0270:                log.debug(logActivityProperties(nodeName,
0271:                        "Properties after start", mPSession)
0272:                        + ", activity = "
0273:                        + nodeName
0274:                        + "of project = "
0275:                        + projectName
0276:                        + " started by "
0277:                        + mContext.getCallerPrincipal().getName());
0278:                try {
0279:                    if (mPSession != null)
0280:                        mPSession.remove();
0281:                } catch (Exception re) {
0282:                    throw new HeroException(re.getMessage());
0283:                }
0284:            }
0285:
0286:            /**
0287:             * Starts the activity nodeName
0288:             * @param nodeName the name of the activity
0289:             * @ejb:interface-method view-type="both"
0290:             *
0291:             **/
0292:            public boolean terminateActivity(String projectName, String nodeName)
0293:                    throws EngineException, HeroException {
0294:
0295:                log.debug("Start terminateActivity = " + nodeName
0296:                        + " of the project = " + projectName + " started by "
0297:                        + mContext.getCallerPrincipal().getName());
0298:                ProjectSessionLocalHome psHome;
0299:                BnProjectLocalHome pHome;
0300:                ProjectSessionLocal mPSession;
0301:                BnProjectLocal mProject;
0302:                this .endActivity = false;
0303:                boolean terminated = false;
0304:                try {
0305:                    psHome = ProjectSessionUtil.getLocalHome();
0306:                    pHome = BnProjectUtil.getLocalHome();
0307:                } catch (javax.naming.NamingException ne) {
0308:                    throw new HeroException(ne.getMessage());
0309:                }
0310:                try {
0311:                    mProject = pHome.findByName(projectName);
0312:                    if (mProject.getType().equals(Constants.Pj.INSTANCE)) {
0313:                        if (mProject.getStatus().equals(Constants.Pj.HIDDEN))
0314:                            throw new EngineException(
0315:                                    "Project is hidden. You cannot terminate an activity of a hidden model !!");
0316:                    } else if (mProject.getStatus().equals(Constants.Pj.HIDDEN))
0317:                        throw new EngineException(
0318:                                "Project is hidden. You cannot terminate an activity of a hidden project !!");
0319:                    mPSession = psHome.create();
0320:                    mPSession
0321:                            .setCaller(mContext.getCallerPrincipal().getName());
0322:                    mPSession.initProject(projectName);
0323:                } catch (FinderException fe) {
0324:                    throw new HeroException("Project does not exist "
0325:                            + projectName);
0326:                } catch (CreateException he) {
0327:                    throw new HeroException("ProjectSession Error"
0328:                            + projectName);
0329:                }
0330:                if (mProject.getBnNode(nodeName).getType() == Constants.Nd.SUB_PROCESS_NODE)
0331:                    mPSession.propagatesSubProcessProperties(mProject
0332:                            .getBnNode(nodeName));
0333:
0334:                log.debug(logActivityProperties(nodeName,
0335:                        "Properties before terminate", mPSession)
0336:                        + ", activity = "
0337:                        + nodeName
0338:                        + "of project = "
0339:                        + projectName
0340:                        + " started by "
0341:                        + mContext.getCallerPrincipal().getName());
0342:                execute(nodeName, hero.interfaces.Constants.Nd.TERMINATE,
0343:                        mProject, mPSession);
0344:                if (raiseError == true) {
0345:                    raiseError = false;
0346:                    throw new HeroHookException(
0347:                            "An error is produced during the execution of a hook...");
0348:                }
0349:
0350:                log.debug("End of terminate activity = " + nodeName
0351:                        + "of project = " + projectName + " started by "
0352:                        + mContext.getCallerPrincipal().getName());
0353:
0354:                log.debug(logActivityProperties(nodeName,
0355:                        "Properties after terminate", mPSession)
0356:                        + ", activity = "
0357:                        + nodeName
0358:                        + "of project = "
0359:                        + projectName
0360:                        + " started by "
0361:                        + mContext.getCallerPrincipal().getName());
0362:                // Automatically terminates subProcess activity
0363:                if (!mProject.getParent().equals(mProject.getName())) {
0364:                    if (mPSession.isTerminated()) {
0365:                        try {
0366:                            if (mPSession != null)
0367:                                mPSession.remove();
0368:                        } catch (Exception re) {
0369:                        }
0370:
0371:                        try {
0372:                            if (this .terminateActivity(
0373:                                    BonitaParsing
0374:                                            .getParentInstanceName(mProject
0375:                                                    .getParent()),
0376:                                    BonitaParsing.getParentNodeName(mProject
0377:                                            .getParent()))) {
0378:                                UserSessionLocalHome ulHome = UserSessionUtil
0379:                                        .getLocalHome();
0380:                                UserSessionLocal ul = ulHome.create();
0381:                                ul.terminate(BonitaParsing
0382:                                        .getParentInstanceName(mProject
0383:                                                .getParent()));
0384:                                ul.remove();
0385:                            }
0386:
0387:                        } catch (Exception ri) {
0388:                            throw new HeroException(
0389:                                    "Error when deleting the instance= "
0390:                                            + ri.getMessage());
0391:                        }
0392:
0393:                    }
0394:                    return terminated;
0395:                } else {
0396:                    try {
0397:                        if (mPSession != null) {
0398:                            // test if the instance is termined
0399:                            if (mPSession.isTerminated()
0400:                                    && mProject.getType().equals(
0401:                                            Constants.Pj.INSTANCE)) {
0402:                                mProject.setState(Constants.Pj.TERMINATED);
0403:                                this .terminate(projectName);
0404:                                terminated = true;
0405:                            }
0406:                            mPSession.remove();
0407:                        }
0408:                    } catch (Exception re) {
0409:                        re.printStackTrace();
0410:                    }
0411:                }
0412:                if (this .endActivity) {
0413:                    this .terminate(projectName);
0414:                    this .endActivity = false;
0415:                    terminated = true;
0416:                }
0417:                return terminated;
0418:            }
0419:
0420:            /**
0421:             * Starts the activity nodeName
0422:             * @param nodeName the name of the activity
0423:             * @ejb:interface-method view-type="both"
0424:             *
0425:             **/
0426:            public void cancelActivity(String projectName, String nodeName)
0427:                    throws EngineException, HeroException {
0428:
0429:                log.debug("Start cancelActivity = " + nodeName
0430:                        + " of the project = " + projectName + " started by "
0431:                        + mContext.getCallerPrincipal().getName());
0432:                ProjectSessionLocalHome psHome;
0433:                BnProjectLocalHome pHome;
0434:                ProjectSessionLocal mPSession;
0435:                BnProjectLocal mProject;
0436:                try {
0437:                    psHome = ProjectSessionUtil.getLocalHome();
0438:                    pHome = BnProjectUtil.getLocalHome();
0439:                } catch (javax.naming.NamingException ne) {
0440:                    throw new HeroException(ne.getMessage());
0441:                }
0442:                try {
0443:                    mProject = pHome.findByName(projectName);
0444:                    if (mProject.getType().equals(Constants.Pj.INSTANCE)) {
0445:                        if (mProject.getStatus().equals(Constants.Pj.HIDDEN))
0446:                            throw new EngineException(
0447:                                    "Project is hidden. You cannot cancel an activity of a hidden model !!");
0448:                    } else if (mProject.getStatus().equals(Constants.Pj.HIDDEN))
0449:                        throw new EngineException(
0450:                                "Project is hidden. You cannot cancel an activity of a hidden project !!");
0451:                    mPSession = psHome.create();
0452:                    mPSession
0453:                            .setCaller(mContext.getCallerPrincipal().getName());
0454:                    mPSession.initProject(projectName);
0455:                    //this.getProjectModel();
0456:                } catch (FinderException fe) {
0457:                    throw new HeroException("Project does not exist "
0458:                            + projectName);
0459:                } catch (CreateException he) {
0460:                    throw new HeroException("ProjectSession Error"
0461:                            + projectName);
0462:                }
0463:                execute(nodeName, hero.interfaces.Constants.Nd.CANCEL,
0464:                        mProject, mPSession);
0465:
0466:                log.debug("End of cancel activity = " + nodeName
0467:                        + "of project = " + projectName + " started by "
0468:                        + mContext.getCallerPrincipal().getName());
0469:                try {
0470:                    if (mPSession != null)
0471:                        mPSession.remove();
0472:                } catch (Exception re) {
0473:                    throw new HeroException(re.getMessage());
0474:                }
0475:            }
0476:
0477:            /**
0478:             * Suspend the activity nodeName
0479:             * @param nodeName the name of the activity
0480:             * @ejb:interface-method view-type="both"
0481:             *
0482:             **/
0483:            public void suspendActivity(String projectName, String nodeName)
0484:                    throws EngineException, HeroException {
0485:                //log.debug("suspendActivity: nodeName=" + nodeName);
0486:                ProjectSessionLocalHome psHome;
0487:                BnProjectLocalHome pHome;
0488:                ProjectSessionLocal mPSession;
0489:                BnProjectLocal mProject;
0490:                try {
0491:                    psHome = ProjectSessionUtil.getLocalHome();
0492:                    pHome = BnProjectUtil.getLocalHome();
0493:                } catch (javax.naming.NamingException ne) {
0494:                    throw new HeroException(ne.getMessage());
0495:                }
0496:                try {
0497:                    mProject = pHome.findByName(projectName);
0498:                    mPSession = psHome.create();
0499:                    mPSession
0500:                            .setCaller(mContext.getCallerPrincipal().getName());
0501:                    mPSession.initProject(projectName);
0502:                } catch (FinderException fe) {
0503:                    throw new HeroException("Project does not exist "
0504:                            + projectName);
0505:                } catch (CreateException he) {
0506:                    throw new HeroException("ProjectSession Error"
0507:                            + projectName);
0508:                }
0509:                execute(nodeName, hero.interfaces.Constants.Nd.SUSPEND,
0510:                        mProject, mPSession);
0511:                try {
0512:                    if (mPSession != null)
0513:                        mPSession.remove();
0514:                } catch (Exception re) {
0515:                    throw new HeroException(re.getMessage());
0516:                }
0517:            }
0518:
0519:            /**
0520:             * Non_Automatic operation method that resume specify activity
0521:             */
0522:
0523:            /**
0524:             * Resume the activity nodeName
0525:             * @param nodeName the name of the activity
0526:             * @ejb:interface-method view-type="both"
0527:             *
0528:             **/
0529:            public void resumeActivity(String projectName, String nodeName)
0530:                    throws EngineException, HeroException {
0531:                //log.debug("resumeActivity: nodeName=" + nodeName);
0532:                ProjectSessionLocalHome psHome;
0533:                BnProjectLocalHome pHome;
0534:                ProjectSessionLocal mPSession;
0535:                BnProjectLocal mProject;
0536:                try {
0537:                    psHome = ProjectSessionUtil.getLocalHome();
0538:                    pHome = BnProjectUtil.getLocalHome();
0539:                } catch (javax.naming.NamingException ne) {
0540:                    throw new HeroException(ne.getMessage());
0541:                }
0542:                try {
0543:                    mProject = pHome.findByName(projectName);
0544:                    mPSession = psHome.create();
0545:                    mPSession
0546:                            .setCaller(mContext.getCallerPrincipal().getName());
0547:                    mPSession.initProject(projectName);
0548:                } catch (FinderException fe) {
0549:                    throw new HeroException("Project does not exist "
0550:                            + projectName);
0551:                } catch (CreateException he) {
0552:                    throw new HeroException("ProjectSession Error"
0553:                            + projectName);
0554:                }
0555:                execute(nodeName, hero.interfaces.Constants.Nd.RESUME,
0556:                        mProject, mPSession);
0557:                try {
0558:                    if (mPSession != null)
0559:                        mPSession.remove();
0560:                } catch (Exception re) {
0561:                    throw new HeroException(re.getMessage());
0562:                }
0563:            }
0564:
0565:            /**
0566:             * Active an External Agent
0567:             * @param agentName the name of the external agent
0568:             * @ejb:interface-method view-type="both"
0569:             *
0570:             **/
0571:            public void activeAgent(String projectName, String agentName)
0572:                    throws EngineException, HeroException {
0573:                //log.debug("activeAgent: agentName=" + agentName);
0574:                BnAgentLocal ag;
0575:                ProjectSessionLocalHome psHome;
0576:                BnProjectLocalHome pHome;
0577:                ProjectSessionLocal mPSession;
0578:                BnProjectLocal mProject;
0579:                try {
0580:                    psHome = ProjectSessionUtil.getLocalHome();
0581:                    pHome = BnProjectUtil.getLocalHome();
0582:                } catch (javax.naming.NamingException ne) {
0583:                    throw new HeroException(ne.getMessage());
0584:                }
0585:                try {
0586:                    mProject = pHome.findByName(projectName);
0587:                    mPSession = psHome.create();
0588:                    mPSession
0589:                            .setCaller(mContext.getCallerPrincipal().getName());
0590:                    mPSession.initProject(projectName);
0591:                    ag = mProject.getBnAgent(agentName);
0592:                    mPSession.setAgentState(ag,
0593:                            hero.interfaces.Constants.Ag.ACTIVE);
0594:                    Collection edges = ag.getOutEdges();
0595:                    if (!edges.isEmpty())
0596:                        modifyAgentEdges(edges, mProject, mPSession);
0597:                } catch (FinderException fe) {
0598:                    throw new HeroException("Project does not exist "
0599:                            + projectName);
0600:                } catch (CreateException he) {
0601:                    throw new HeroException("ProjectSession Error"
0602:                            + projectName);
0603:                }
0604:                try {
0605:                    if (mPSession != null)
0606:                        mPSession.remove();
0607:                } catch (Exception re) {
0608:                    throw new HeroException(re.getMessage());
0609:                }
0610:            }
0611:
0612:            // Utility method
0613:            private void execute(String _node, int operation,
0614:                    BnProjectLocal mProject, ProjectSessionLocal mPSession)
0615:                    throws HeroException, EngineException {
0616:                boolean statePropagation = true;
0617:
0618:                //log.debug("execute: node="+ _node+" operation="+ hero.interfaces.Constants.Nd.operationName[operation]);
0619:
0620:                BnNodeLocal nd;
0621:                nd = mProject.getBnNode(_node);
0622:                NodeState nodeState = NodeState.make(nd.getType(), nd
0623:                        .getAnticipable());
0624:                int newState = nodeState.computeState(nd, operation);
0625:
0626:                if (newState == hero.interfaces.Constants.Nd.BAD_TRANSITION) {
0627:                    throw new EngineException("Bad state transition " + _node);
0628:                }
0629:
0630:                if (newState != nd.getState()) {
0631:
0632:                    Collection edges = nd.getOutBnEdges();
0633:                    if (edges.isEmpty() && isInstance(mProject.getName())
0634:                            && newState != Constants.Nd.INITIAL) {
0635:                        if (!instanceWasModified(nd, mProject)) {
0636:                            this .generateOutNodes(nd, mProject, mPSession);
0637:                            if (newState == Constants.Nd.DEAD)
0638:                                mPSession.propagateNodeProperties(_node);
0639:                            edges = nd.getOutBnEdges();
0640:                        }
0641:                    }
0642:
0643:                    if (newState == hero.interfaces.Constants.Nd.TERMINATED) {
0644:                        if (jms != null)
0645:                            jms.sendProjectEvent(EventConstants.TERMINATED,
0646:                                    mProject.getName(), _node, mContext
0647:                                            .getCallerPrincipal().getName());
0648:                        this .hookExecute(_node,
0649:                                hero.interfaces.Constants.Nd.BEFORETERMINATE,
0650:                                mProject);
0651:                        this .hookExecute(_node,
0652:                                hero.interfaces.Constants.Nd.AFTERTERMINATE,
0653:                                mProject);
0654:                        mPSession.propagateNodeProperties(_node);
0655:                    } else if (newState == hero.interfaces.Constants.Nd.EXECUTING) {
0656:                        if (nd.getType() == hero.interfaces.Constants.Nd.SUB_PROCESS_NODE)
0657:                            mPSession.propagatesParentProperties(_node);
0658:                        if (jms != null)
0659:                            jms.sendProjectEvent(EventConstants.START, mProject
0660:                                    .getName(), _node, mContext
0661:                                    .getCallerPrincipal().getName());
0662:                        this .hookExecute(_node,
0663:                                hero.interfaces.Constants.Nd.BEFORESTART,
0664:                                mProject);
0665:                    } else if (newState == hero.interfaces.Constants.Nd.ANTICIPATING) {
0666:                        if (jms != null)
0667:                            jms.sendProjectEvent(EventConstants.ANTICIPATING,
0668:                                    mProject.getName(), _node, mContext
0669:                                            .getCallerPrincipal().getName());
0670:                        this .hookExecute(_node,
0671:                                hero.interfaces.Constants.Nd.ANTICIPATE,
0672:                                mProject);
0673:                    } else if (newState == hero.interfaces.Constants.Nd.DEAD) {
0674:                        mPSession.propagateNodeProperties(_node);
0675:                        if (jms != null)
0676:                            jms.sendProjectEvent(EventConstants.CANCELED,
0677:                                    mProject.getName(), _node, mContext
0678:                                            .getCallerPrincipal().getName());
0679:                        this 
0680:                                .hookExecute(_node,
0681:                                        hero.interfaces.Constants.Nd.ONCANCEL,
0682:                                        mProject);
0683:                    }
0684:
0685:                    if (activityStarted(newState)) {
0686:                        this .hookExecute(_node,
0687:                                hero.interfaces.Constants.Nd.AFTERSTART,
0688:                                mProject);
0689:                    }
0690:
0691:                    // ---------- Activities iteration ----------
0692:                    if ((newState == hero.interfaces.Constants.Nd.TERMINATED)
0693:                            && (mPSession.getIterationExist(nd.getName()))) {
0694:                        Collection nodeIterations = mPSession
0695:                                .getIterations(_node);
0696:                        Iterator iterationsIt = nodeIterations.iterator();
0697:                        BnIterationLocal iteration;
0698:                        String iterationCondition;
0699:
0700:                        // Search the first condition of the iterations that is true
0701:                        do {
0702:                            iteration = (BnIterationLocal) iterationsIt.next();
0703:                            iterationCondition = iteration.getCondition();
0704:                        } while ((iterationsIt.hasNext())
0705:                                && ((evaluateCondition(iterationCondition, nd)) == Constants.CONDITION_FALSE));
0706:
0707:                        if ((evaluateCondition(iterationCondition, nd)) == Constants.CONDITION_TRUE) {
0708:                            // No state propagation thru transitions if an iteration starts
0709:                            statePropagation = false;
0710:
0711:                            BnNodeLocal toNode = mProject.getBnNode(iteration
0712:                                    .getToNode());
0713:
0714:                            this .setIterationPathInitialModel(nd, toNode,
0715:                                    mProject, mPSession);
0716:                            mPSession.setNodeState(toNode,
0717:                                    hero.interfaces.Constants.Nd.READY);
0718:
0719:                            if (toNode.getBnNodePerformerAssign() != null) {
0720:                                this .performAssign(toNode);
0721:                            }
0722:
0723:                            if ((toNode.getType() == Constants.Nd.AND_JOIN_NODE)
0724:                                    || (toNode.getType() == Constants.Nd.OR_JOIN_NODE)) {
0725:                                this .hookExecute(toNode.getName(),
0726:                                        hero.interfaces.Constants.Nd.ONREADY,
0727:                                        mProject);
0728:                            }
0729:
0730:                            if ((toNode.getType() == Constants.Nd.AND_JOIN_AUTOMATIC_NODE)
0731:                                    || (toNode.getType() == Constants.Nd.OR_JOIN_AUTOMATIC_NODE)
0732:                                    || (toNode.getType() == Constants.Nd.SUB_PROCESS_NODE)) {
0733:                                this 
0734:                                        .execute(toNode.getName(),
0735:                                                Constants.Nd.START, mProject,
0736:                                                mPSession);
0737:                            }
0738:
0739:                        } else {
0740:                            mPSession.setNodeState(nd, newState);
0741:                        }
0742:                    } else { // ---------- End activities iteration -----------
0743:                        mPSession.setNodeState(nd, newState);
0744:                        mPSession.setNodeExecutor(_node);
0745:                    }
0746:
0747:                    try {
0748:                        if (newState == hero.interfaces.Constants.Nd.READY
0749:                                && nd.getBnNodePerformerAssign() != null)
0750:                            this .performAssign(nd);
0751:                        if (newState == hero.interfaces.Constants.Nd.READY)
0752:                            this .hookExecute(_node,
0753:                                    hero.interfaces.Constants.Nd.ONREADY,
0754:                                    mProject);
0755:                    } catch (Exception e) {
0756:                        e.printStackTrace();
0757:                        throw new HeroException(
0758:                                "Error when calling the performAssign(): "
0759:                                        + e.getMessage());
0760:                    }
0761:
0762:                    if (statePropagation) {
0763:                        // Propagate the new state to the connected nodes. Subsequence automatic activities will be automatically
0764:                        //executed by the Engine
0765:                        edges = nd.getOutBnEdges();
0766:                        if (!instanceWasModified(nd, mProject)) {
0767:                            if (newState == Constants.Nd.DEAD)
0768:                                mPSession.propagateNodeProperties(_node);
0769:                            edges = nd.getOutBnEdges();
0770:                        }
0771:                        modifyNodeEdges(newState, edges, mProject, mPSession);
0772:                    }
0773:                }
0774:
0775:                if (newState == hero.interfaces.Constants.Nd.TERMINATED) {
0776:                    if (nd.getName().equals("BonitaEnd")) {
0777:                        this .endActivity = true;
0778:                    }
0779:                }
0780:
0781:                if ((newState == hero.interfaces.Constants.Nd.EXECUTING)
0782:                        && (nd.getType() == hero.interfaces.Constants.Nd.AND_JOIN_AUTOMATIC_NODE)) {
0783:                    this .execute(_node, hero.interfaces.Constants.Nd.TERMINATE,
0784:                            mProject, mPSession);
0785:                }
0786:            }
0787:
0788:            private void performAssign(BnNodeLocal nd) throws HeroException {
0789:                try {
0790:                    BnNodePerformerAssignLocal performerAssign = null;
0791:                    String creator = mContext.getCallerPrincipal().getName();
0792:                    performerAssign = nd.getBnNodePerformerAssign();
0793:                    hero.performerAssign.PerformerAssign h = hero.performerAssign.PerformerAssign
0794:                            .make(performerAssign.getName(), performerAssign
0795:                                    .getType());
0796:                    h.execute(this , performerAssign.getType(), nd, creator);
0797:
0798:                } catch (HeroException he) {
0799:                    he.printStackTrace();
0800:                    throw new HeroException("Error in performerAssignment. "
0801:                            + he.getMessage());
0802:                }
0803:            }
0804:
0805:            /**
0806:             * Method that consults new node state and then propagates the state
0807:             * through its out edges. Supposes that the model is defined correctly
0808:             *
0809:             * @param nodeState
0810:             * @param edges
0811:             * @param mPSession
0812:             * @throws HeroException
0813:             * @throws EngineException
0814:             */
0815:            private void modifyNodeEdges(int nodeState, Collection edges,
0816:                    BnProjectLocal mProject, ProjectSessionLocal mPSession)
0817:                    throws HeroException, EngineException {
0818:                int newEdgeState;
0819:                Iterator i = edges.iterator();
0820:
0821:                while (i.hasNext()) {
0822:                    BnEdgeLocal edge = (BnEdgeLocal) i.next();
0823:                    if ((nodeState != hero.interfaces.Constants.Nd.TERMINATED)
0824:                            && (nodeState != hero.interfaces.Constants.Nd.DEAD)) {
0825:                        newEdgeState = hero.interfaces.Constants.edgeTransition[Constants.CONDITION_TRUE][nodeState];
0826:
0827:                        if (edge.getState() != newEdgeState) {
0828:                            mPSession.setEdgeState(edge, newEdgeState);
0829:                            edgeChanged(edge, mProject, mPSession);
0830:                        }
0831:                    } else {
0832:                        newEdgeState = hero.interfaces.Constants.edgeTransition[evaluateCondition(
0833:                                edge.getCondition(), edge.getInBnNode())][nodeState];
0834:
0835:                        if (edge.getState() != newEdgeState) {
0836:                            mPSession.setEdgeState(edge, newEdgeState);
0837:
0838:                            edgeChanged(edge, mProject, mPSession);
0839:                        }
0840:                    }
0841:                }
0842:            }
0843:
0844:            /**
0845:             * Gets, in a recursive way, the nodes inside the iteration<br>
0846:             * Extra entry points are no returned (because they are not inside the iteration)<br>
0847:             * It works with model's defined objects because may be, some of them, have not been declared yet<br>
0848:             *
0849:             * @param nodesInIterationModel initially it is an empty list and, at the end, contains the nodes inside the iteration
0850:             * @param currentModelNode
0851:             * @param toIterationModelNode
0852:             * @return true if current node is inside the iteration and false otherwise
0853:             * @throws HeroException
0854:             */
0855:            private boolean getNodesInsideIterationModel(
0856:                    ArrayList nodesInIterationModel,
0857:                    BonitaNodeValue currentModelNode,
0858:                    BonitaNodeValue toIterationModelNode,
0859:                    BonitaProjectValue model) throws HeroException {
0860:                boolean pathExists = false;
0861:
0862:                if (currentModelNode.equals(toIterationModelNode)) {
0863:                    nodesInIterationModel.add(currentModelNode);
0864:                    return true;
0865:                } else {
0866:                    Collection inEdges = currentModelNode.getInEdges();
0867:                    Iterator it = inEdges.iterator();
0868:                    while (it.hasNext()) {
0869:                        BonitaEdgeValue nextEdge = (BonitaEdgeValue) it.next();
0870:                        BonitaNodeValue nextNode = BonitaServiceValue
0871:                                .getNodeFromCache(model, nextEdge.getInNode());
0872:                        if (nodesInIterationModel.contains(nextNode)) {
0873:                            pathExists = true;
0874:                        } else {
0875:                            if (getNodesInsideIterationModel(
0876:                                    nodesInIterationModel, nextNode,
0877:                                    toIterationModelNode, model)) {
0878:                                pathExists = true;
0879:                            }
0880:                        }
0881:                    }
0882:                }
0883:                if (pathExists) {
0884:                    if (!nodesInIterationModel.contains(currentModelNode)) {
0885:                        nodesInIterationModel.add(currentModelNode);
0886:                    }
0887:                    return true;
0888:                } else {
0889:                    return false;
0890:                }
0891:            }
0892:
0893:            /**
0894:             * Reinitializes 'instantiated' activities inside an iteration and their output edges as well<br>
0895:             *
0896:             * @param fromIterationNode
0897:             * @param toIterationNode
0898:             * @param mPSession
0899:             * @throws HeroException
0900:             */
0901:            private void setIterationPathInitialModel(
0902:                    BnNodeLocal fromIterationNode, BnNodeLocal toIterationNode,
0903:                    BnProjectLocal mProject, ProjectSessionLocal mPSession)
0904:                    throws HeroException {
0905:                ArrayList nodeListModel = new ArrayList();
0906:                ArrayList nodeListInstance = new ArrayList();
0907:                boolean correct;
0908:                String fromIterationNodeModelName;
0909:                String toIterationNodeModelName;
0910:                BonitaProjectValue model;
0911:
0912:                try {
0913:                    model = BonitaProjectLocator.getInstance().getModelValue(
0914:                            BonitaParsing.getModel(mProject.getName()),
0915:                            BonitaParsing.getModel(mProject.getVersion()));
0916:                } catch (BonitaServiceException e) {
0917:                    e.printStackTrace();
0918:                    throw new HeroException(e.getMessage());
0919:                }
0920:                // Get model names. (toIteration could not be instantiated yet -> work with model)
0921:                if (this .isInstance(fromIterationNode.getName())) {
0922:                    fromIterationNodeModelName = BonitaParsing
0923:                            .getModel(fromIterationNode.getName());
0924:                } else {
0925:                    fromIterationNodeModelName = fromIterationNode.getName();
0926:                }
0927:                if (this .isInstance(toIterationNode.getName())) {
0928:                    toIterationNodeModelName = BonitaParsing
0929:                            .getModel(toIterationNode.getName());
0930:                } else {
0931:                    toIterationNodeModelName = toIterationNode.getName();
0932:                }
0933:                BonitaNodeValue fromIterationNodeModel = BonitaServiceValue
0934:                        .getNodeFromCache(model, fromIterationNodeModelName);
0935:                BonitaNodeValue toIterationNodeModel = BonitaServiceValue
0936:                        .getNodeFromCache(model, toIterationNodeModelName);
0937:                correct = this .getNodesInsideIterationModel(nodeListModel,
0938:                        fromIterationNodeModel, toIterationNodeModel, model);
0939:                if (!correct) {
0940:                    throw new HeroException(
0941:                            "Doesn't exist a path in iteration : "
0942:                                    + fromIterationNode.getName() + "--->"
0943:                                    + toIterationNode.getName());
0944:                }
0945:
0946:                // Chek which nodes exist in the current mPSession
0947:                // Firstly we set everything inside the iteration to INITIAL (nodes & edges)
0948:                Iterator it = nodeListModel.iterator();
0949:                while (it.hasNext()) {
0950:
0951:                    BonitaNodeValue modelNode = (BonitaNodeValue) it.next();
0952:                    //String instanceNodeName = this.nodeConvertion(modelNode.getName(), mProject);
0953:
0954:                    String instanceNodeName = modelNode.getName();
0955:                    String version = this .nodeSubProcess(instanceNodeName,
0956:                            mProject);
0957:
0958:                    if ((modelNode.getType() == Nd.SUB_PROCESS_NODE)
0959:                            && (!version.equals(instanceNodeName)))
0960:                        this .initSubProcess(version);
0961:
0962:                    if ((modelNode.getType() == Nd.SUB_PROCESS_NODE)
0963:                            && (!version.equals(instanceNodeName))
0964:                            || (this .nodeExist(instanceNodeName, mPSession))) {
0965:                        BnNodeLocal nodeLocal = mProject
0966:                                .getBnNode(instanceNodeName);
0967:                        mPSession.setNodeState(nodeLocal, Constants.Nd.INITIAL);
0968:                        Collection outEdges = nodeLocal.getOutBnEdges();
0969:                        Iterator it2 = outEdges.iterator();
0970:                        while (it2.hasNext()) {
0971:                            BnEdgeLocal outEdge = (BnEdgeLocal) it2.next();
0972:                            mPSession.setEdgeState(outEdge,
0973:                                    Constants.Ed.INITIAL);
0974:                            BnNodeLocal outNode = outEdge.getOutBnNode();
0975:                            if ((outNode.getState() == Constants.Nd.DEAD)
0976:                                    || (outNode.getState() == Constants.Nd.ANTICIPABLE)
0977:                                    || (outNode.getState() == Constants.Nd.ANTICIPATING)) {
0978:                                this .propagateInitialStateThruNodes(outNode,
0979:                                        mPSession);
0980:                            }
0981:                        }
0982:                        nodeListInstance.add(nodeLocal);
0983:                    }
0984:                }
0985:
0986:                // After that, it is necessary to consider if there are another extra entry points in the iteration that changes some node's state
0987:                // This in done later because it needs be sure that any edge inside the iteration is ACTIVE
0988:                it = nodeListInstance.iterator();
0989:                int operation;
0990:                while (it.hasNext()) {
0991:                    BnNodeLocal node = (BnNodeLocal) it.next();
0992:
0993:                    // Computes next operation over the node
0994:                    EdgeState edgeState = EdgeState.make(node.getType());
0995:                    operation = edgeState.state(node);
0996:
0997:                    // If node type is: AND_JOIN or AND_JOIN_AUTOMATIC evaluates their in edge states to see if someone is DEAD -> executes a CANCEL over the node
0998:                    if ((node.getType() == Constants.Nd.AND_JOIN_NODE)
0999:                            || (node.getType() == Constants.Nd.AND_JOIN_AUTOMATIC_NODE)) {
1000:                        if (operation == Constants.Nd.CANCEL) {
1001:                            this .execute(node.getName(), Constants.Nd.CANCEL,
1002:                                    mProject, mPSession);
1003:                        }
1004:                    }
1005:
1006:                    // If node type is: OR_JOIN or OR_JOIN_AUTOMATIC evaluates their in edge states to see if someone is ACTIVE -> node is set to READY
1007:                    else if ((node.getType() == Constants.Nd.OR_JOIN_NODE)
1008:                            || (node.getType() == Constants.Nd.OR_JOIN_AUTOMATIC_NODE)) {
1009:                        if (operation == Constants.Nd.ACTIVE) {
1010:                            mPSession.setNodeState(node, Constants.Nd.READY);
1011:
1012:                            if (node.getType() == Constants.Nd.OR_JOIN_NODE) {
1013:                                this .hookExecute(node.getName(),
1014:                                        hero.interfaces.Constants.Nd.ONREADY,
1015:                                        mProject);
1016:                            } else { // It is an OR_JOIN_AUTOMATIC then it executes a START operation
1017:                                this 
1018:                                        .execute(node.getName(),
1019:                                                Constants.Nd.START, mProject,
1020:                                                mPSession);
1021:                            }
1022:                        }
1023:                    }
1024:                }
1025:            }
1026:
1027:            private void propagateInitialStateThruNodes(BnNodeLocal node,
1028:                    ProjectSessionLocal mPSession) throws HeroException {
1029:                mPSession.setNodeState(node, Constants.Nd.INITIAL);
1030:                Collection outEdges = node.getOutBnEdges();
1031:                Iterator it = outEdges.iterator();
1032:                while (it.hasNext()) {
1033:                    BnEdgeLocal outEdge = (BnEdgeLocal) it.next();
1034:                    mPSession.setEdgeState(outEdge, Constants.Ed.INITIAL);
1035:                    BnNodeLocal outNode = outEdge.getOutBnNode();
1036:                    if ((outNode.getState() == Constants.Nd.DEAD)
1037:                            || (outNode.getState() == Constants.Nd.ANTICIPABLE)
1038:                            || (outNode.getState() == Constants.Nd.ANTICIPATING)) {
1039:                        this .propagateInitialStateThruNodes(outNode, mPSession);
1040:                    }
1041:                }
1042:            }
1043:
1044:            /**
1045:             * Method that propagate the AgentEdges state
1046:             */
1047:            private void modifyAgentEdges(Collection edges,
1048:                    BnProjectLocal mProject, ProjectSessionLocal mPSession)
1049:                    throws HeroException, EngineException {
1050:                Iterator i = edges.iterator();
1051:                while (i.hasNext()) {
1052:                    BnAgentEdgeLocal edge = (BnAgentEdgeLocal) i.next();
1053:                    mPSession.setAgentEdgeState(edge,
1054:                            hero.interfaces.Constants.Ed.ANTICIPATING);
1055:                    BnNodeLocal nd = edge.getOutBnNode();
1056:                    EdgeState edgeState = EdgeState.make(nd.getType());
1057:                    int operation = edgeState.state(nd);
1058:                    execute(nd.getName(),
1059:                            hero.interfaces.Constants.Nd.ANTACTIVE, mProject,
1060:                            mPSession);
1061:                }
1062:            }
1063:
1064:            private void edgeChanged(BnEdgeLocal edge, BnProjectLocal mProject,
1065:                    ProjectSessionLocal mPSession) throws HeroException,
1066:                    EngineException {
1067:                BnNodeLocal nd = edge.getOutBnNode();
1068:                if (nd.getState() != hero.interfaces.Constants.Nd.DEAD) {
1069:                    EdgeState edgeState = EdgeState.make(nd.getType());
1070:                    int operation = edgeState.state(nd);
1071:                    execute(nd.getName(), operation, mProject, mPSession);
1072:                }
1073:            }
1074:
1075:            private boolean activityStarted(int state) throws HeroException {
1076:                return (state == hero.interfaces.Constants.Nd.ANTICIPATING || state == hero.interfaces.Constants.Nd.EXECUTING);
1077:            }
1078:
1079:            private void hookExecute(String nodeName, String eventName,
1080:                    BnProjectLocal mProject) throws HeroException,
1081:                    HeroHookException {
1082:                Collection hooks;
1083:                Collection interHooks;
1084:                BnNodeLocal node;
1085:                try {
1086:                    node = mProject.getBnNode(nodeName);
1087:                    BonitaProjectValue model = BonitaProjectLocator
1088:                            .getInstance().getModelValue(
1089:                                    BonitaParsing.getModel(mProject.getName()),
1090:                                    BonitaParsing.getModel(mProject
1091:                                            .getVersion()));
1092:
1093:                    //if (nodeName.matches(".*_instance.*"))
1094:                    //nodeName=nodeName.substring(0,nodeName.indexOf("_instance"));
1095:
1096:                    BonitaNodeValue nodeLocal;
1097:                    if (mProject.getType().equals(Constants.Pj.COOPERATIVE))
1098:                        nodeLocal = BonitaServiceValue.getNode(mProject,
1099:                                nodeName);
1100:                    else
1101:                        nodeLocal = BonitaServiceValue.getNodeFromCache(model,
1102:                                nodeName);
1103:
1104:                    hooks = nodeLocal.getHooks();
1105:                    for (Iterator i = hooks.iterator(); i.hasNext();) {
1106:                        BonitaHookValue hk = (BonitaHookValue) i.next();
1107:                        if (hk.getEvent().equalsIgnoreCase(eventName)) {
1108:                            hero.hook.Hook h = hero.hook.Hook.make(
1109:                                    hk.getName(), eventName, hk.getType());
1110:                            log.debug("hookExecute, eventName = " + eventName
1111:                                    + " nodeName = " + nodeName
1112:                                    + " projectName = " + mProject.getName());
1113:                            h.execute(this , eventName, node);
1114:                        }
1115:                    }
1116:
1117:                    interHooks = nodeLocal.getInterHooks();
1118:                    for (Iterator i = interHooks.iterator(); i.hasNext();) {
1119:                        BonitaInterHookValue hk = (BonitaInterHookValue) i
1120:                                .next();
1121:                        if (hk.getEvent().equalsIgnoreCase(eventName)) {
1122:                            hero.hook.Hook h = hero.hook.Hook.make(
1123:                                    hk.getName(), eventName, hk.getType(), hk
1124:                                            .getScript());
1125:                            log.debug("hookExecute, eventName = " + eventName
1126:                                    + " nodeName = " + nodeName
1127:                                    + " projectName = " + mProject.getName());
1128:                            h.execute(this , eventName, node);
1129:                        }
1130:                    }
1131:                } catch (HeroHookException hh) {
1132:                    log.error("hookExecute ERROR, eventName = " + eventName
1133:                            + " nodeName = " + nodeName + " projectName = "
1134:                            + mProject.getName() + " Error message: "
1135:                            + hh.getMessage());
1136:                    if (eventName
1137:                            .equals(hero.interfaces.Constants.Nd.AFTERSTART)
1138:                            || eventName
1139:                                    .equals(hero.interfaces.Constants.Nd.BEFORETERMINATE)
1140:                            || eventName
1141:                                    .equals(hero.interfaces.Constants.Nd.ANTICIPATE)
1142:                            || eventName
1143:                                    .equals(hero.interfaces.Constants.Nd.ONCANCEL)) {
1144:                        mContext.setRollbackOnly();
1145:                        throw new HeroHookException("Cannot execute hook "
1146:                                + hh.getMessage());
1147:                    } else
1148:                        raiseError = true;
1149:                } catch (Exception e) {
1150:                    e.printStackTrace();
1151:                }
1152:            }
1153:
1154:            private boolean nodesTerminated(Collection nodes) {
1155:                Iterator i = nodes.iterator();
1156:                while (i.hasNext()) {
1157:                    BnNodeLocal node = (BnNodeLocal) i.next();
1158:                    int state = node.getState();
1159:                    if (state != hero.interfaces.Constants.Nd.TERMINATED
1160:                            && state != hero.interfaces.Constants.Nd.EXPIRED
1161:                            && state != hero.interfaces.Constants.Nd.DEAD)
1162:                        return false;
1163:                }
1164:                return true;
1165:            }
1166:
1167:            public int evaluateCondition(String condition, BnNodeLocal node) {
1168:                try {
1169:                    if (condition != null) {
1170:                        condition = "import hero.interfaces.BnNodeLocal;\n"
1171:                                + "boolean evalCondition (Object node) {\n\n\n"
1172:                                + "return(" + condition + ");" + "}";
1173:                        Interpreter i = new Interpreter();
1174:
1175:                        // BnNode properties mapping
1176:
1177:                        Collection prop = node.getBnProperties();
1178:                        for (Iterator props = prop.iterator(); props.hasNext();) {
1179:                            BnNodePropertyLocal pl = (BnNodePropertyLocal) props
1180:                                    .next();
1181:                            i.set(pl.getTheKey(), pl.getTheValue());
1182:                        }
1183:
1184:                        //Project properties mapping
1185:                        BnProjectLocal project = node.getBnProject();
1186:                        Collection projectProp = project.getBnProperties();
1187:                        for (Iterator props = projectProp.iterator(); props
1188:                                .hasNext();) {
1189:                            BnProjectPropertyLocal pP = (BnProjectPropertyLocal) props
1190:                                    .next();
1191:                            i.set(pP.getTheKey(), pP.getTheValue());
1192:                        }
1193:                        i.set("node", node);
1194:                        i.eval(condition);
1195:                        i.eval("result=evalCondition(node)");
1196:                        if (((Boolean) i.get("result")).booleanValue() == true)
1197:                            return (Constants.CONDITION_TRUE);
1198:                        else
1199:                            return (Constants.CONDITION_FALSE);
1200:                    } else
1201:                        return (Constants.CONDITION_TRUE);
1202:                } catch (Exception t) {
1203:                    t.printStackTrace();
1204:                    return (Constants.CONDITION_FALSE);
1205:                }
1206:            }
1207:
1208:            private String logActivityProperties(String nodeName,
1209:                    String prelog, ProjectSessionLocal mPSession)
1210:                    throws HeroException {
1211:                String thelog = prelog;
1212:                synchronized (mPSession) {
1213:                    Collection np = mPSession.getNodeProperties(nodeName);
1214:                    Iterator i = np.iterator();
1215:                    if (np.size() != 0) {
1216:                        while (i.hasNext()) {
1217:                            thelog = thelog + " key= '";
1218:                            BnNodePropertyValue property = (BnNodePropertyValue) i
1219:                                    .next();
1220:                            thelog = thelog + property.getTheKey();
1221:                            thelog = thelog + "' value = '"
1222:                                    + property.getTheValue() + "'";
1223:                        }
1224:                    } else
1225:                        thelog = prelog + " No property";
1226:                }
1227:                return thelog;
1228:            }
1229:
1230:            private String logProjectProperties(String prelog,
1231:                    ProjectSessionLocal mPSession) throws HeroException {
1232:                String thelog = prelog;
1233:                synchronized (mPSession) {
1234:                    Collection np = mPSession.getProperties();
1235:                    Iterator i = np.iterator();
1236:                    if (np.size() != 0) {
1237:                        while (i.hasNext()) {
1238:                            thelog = thelog + " key= '";
1239:                            BnProjectPropertyValue property = (BnProjectPropertyValue) i
1240:                                    .next();
1241:                            thelog = thelog + property.getTheKey();
1242:                            thelog = thelog + "' value = '"
1243:                                    + property.getTheValue() + "'";
1244:                        }
1245:                    } else
1246:                        thelog = prelog + " No property";
1247:                }
1248:                return thelog;
1249:            }
1250:
1251:            // Init activities of the subProcess project
1252:            private void initSubProcess(String subProcess) throws HeroException {
1253:                ProjectSessionLocalHome psh;
1254:                ProjectSessionLocal psj;
1255:                BnProjectLocalHome ph;
1256:                BnProjectLocal pj;
1257:                try {
1258:                    psh = ProjectSessionUtil.getLocalHome();
1259:                    psj = psh.create();
1260:                    psj.initProject(subProcess);
1261:
1262:                    ph = BnProjectUtil.getLocalHome();
1263:                    pj = ph.findByName(subProcess);
1264:                    Collection nodes = pj.getBnNodes();
1265:                    Iterator nds = nodes.iterator();
1266:                    while (nds.hasNext()) {
1267:                        BnNodeLocal node = (BnNodeLocal) nds.next();
1268:                        if (node.getType() == Nd.SUB_PROCESS_NODE)
1269:                            this .initSubProcess(node.getReference());
1270:                        psj.setNodeState(node,
1271:                                hero.interfaces.Constants.Nd.INITIAL);
1272:
1273:                        Collection outEdges = node.getOutBnEdges();
1274:                        Iterator it2 = outEdges.iterator();
1275:                        while (it2.hasNext()) {
1276:                            BnEdgeLocal edge = (BnEdgeLocal) it2.next();
1277:                            psj.setEdgeState(edge, Constants.Ed.INITIAL);
1278:                        }
1279:                    }
1280:                } catch (Exception e) {
1281:                    throw new HeroException(e.getMessage());
1282:                }
1283:                try {
1284:                    psj.remove();
1285:                } catch (Exception re) {
1286:                    throw new HeroException(re.getMessage());
1287:                }
1288:            }
1289:
1290:            private void generateOutNodes(BnNodeLocal nd,
1291:                    BnProjectLocal mProject, ProjectSessionLocal mPSession)
1292:                    throws HeroException {
1293:                // Searches the current node in the model to know their out edges and nodes
1294:                BonitaNodeValue parentNode;
1295:                String instName = null;
1296:
1297:                BonitaProjectValue model;
1298:                // Getting the model data from the cache, the version number is passed as a parameter
1299:                try {
1300:                    model = BonitaProjectLocator.getInstance().getModelValue(
1301:                            BonitaParsing.getModel(mProject.getName()),
1302:                            mProject.getVersion());
1303:                } catch (BonitaServiceException e1) {
1304:                    e1.printStackTrace();
1305:                    throw new HeroException(e1.getMessage());
1306:                }
1307:
1308:                // Getting the node Value Object from cache
1309:                parentNode = BonitaServiceValue.getNodeFromCache(model, nd
1310:                        .getName());
1311:
1312:                // Outgoing nodes creation
1313:                Collection outEdges = parentNode.getOutEdges();
1314:                Iterator edges = outEdges.iterator();
1315:                while (edges.hasNext()) {
1316:                    BonitaEdgeValue edge = (BonitaEdgeValue) edges.next();
1317:                    BonitaNodeValue node = BonitaServiceValue.getNodeFromCache(
1318:                            model, edge.getOutNode());
1319:                    String nodeName = node.getName();
1320:
1321:                    if (node.getType() == Nd.SUB_PROCESS_NODE
1322:                            && mProject.getType().equals(Constants.Pj.INSTANCE)) // Node is a subprocess
1323:                    {
1324:                        String subProcessName = BonitaParsing
1325:                                .getSubProcessName(node.getReference());
1326:                        String subProcessVersion = BonitaParsing
1327:                                .getSubProcessVersion(node.getReference());
1328:                        try {
1329:                            // Creates an instance of the subProcess pointed by the node to be created
1330:                            BnProjectLocalHome opHome = BnProjectUtil
1331:                                    .getLocalHome();
1332:                            instName = mPSession.instantiateProject(
1333:                                    subProcessName, subProcessVersion);
1334:                            this .initSubProcess(instName);
1335:
1336:                            BnProjectLocal newInstance = opHome
1337:                                    .findByName(instName);
1338:                            // The new instance refers to the instance in which the node subProcess is going to be created
1339:                            // The format of the parent column is InstanceName+"_node-"+nodeName
1340:                            newInstance.setParent(nd.getBnProject().getName()
1341:                                    + "_node-" + nodeName);
1342:                            mPSession.initProject(nd.getBnProject().getName());
1343:
1344:                        } catch (Exception e) {
1345:                            throw new HeroException(e.getMessage());
1346:                        }
1347:                    }
1348:                    if (!this .nodeExist(nodeName, mPSession)) {
1349:                        mPSession.copyNode(nodeName, node);
1350:                        // Copy Node Properties (default value)
1351:                        Collection nodeProp = node.getProperties();
1352:                        Iterator nodeProps = nodeProp.iterator();
1353:                        while (nodeProps.hasNext()) {
1354:                            BonitaPropertyValue ndProp = (BonitaPropertyValue) nodeProps
1355:                                    .next();
1356:                            mPSession
1357:                                    .setNodePropertyPossibleValues(nodeName,
1358:                                            ndProp.getKey(), ndProp
1359:                                                    .getPossibleValues());
1360:                            mPSession.setNodeProperty(nodeName,
1361:                                    ndProp.getKey(), ndProp.getValue(), ndProp
1362:                                            .getPropagate());
1363:                        }
1364:                        if (node.getType() == Nd.SUB_PROCESS_NODE) {
1365:                            BnNodeLocal newNode = mProject.getBnNode(nodeName);
1366:                            if (!newNode.getBnProject().getType().equals(
1367:                                    Constants.Pj.COOPERATIVE))
1368:                                newNode.setReference(instName);
1369:                        }
1370:                    }
1371:
1372:                    mPSession.copyEdge(nodeName, nd, edge);
1373:                }
1374:
1375:                // Iterations between nodes
1376:                Collection modelIterations = model.getIterations();
1377:                Iterator iter = modelIterations.iterator();
1378:                while (iter.hasNext()) {
1379:                    BonitaIterationValue modelIteration = (BonitaIterationValue) iter
1380:                            .next();
1381:                    String modelFromNodeName = modelIteration.getFromNode();
1382:
1383:                    String instanceFromNodeName = modelFromNodeName;
1384:                    if (instanceFromNodeName.equals(nd.getName())) {
1385:                        BonitaNodeValue modelToNode = BonitaServiceValue
1386:                                .getNodeFromCache(model, modelIteration
1387:                                        .getToNode());
1388:                        String instanceToNodeName = modelToNode.getName();
1389:                        // Adds toNode to the mPSession if it has not been defined yet and it is not a SUB_PROCESS
1390:                        if (!this .nodeExist(instanceToNodeName, mPSession)) {
1391:                            if ((modelToNode.getType() == Nd.SUB_PROCESS_NODE)) {
1392:                                try {
1393:                                    String subProcessName = BonitaParsing
1394:                                            .getSubProcessName(modelToNode
1395:                                                    .getReference());
1396:                                    String subProcessVersion = BonitaParsing
1397:                                            .getSubProcessVersion(modelToNode
1398:                                                    .getReference());
1399:
1400:                                    BnProjectLocalHome opHome = BnProjectUtil
1401:                                            .getLocalHome();
1402:                                    instName = mPSession.instantiateProject(
1403:                                            subProcessName, subProcessVersion);
1404:                                    this .initSubProcess(instName);
1405:                                    BnProjectLocal newInstance = opHome
1406:                                            .findByName(instName);
1407:                                    newInstance.setParent(nd.getBnProject()
1408:                                            .getName()
1409:                                            + "_node-" + modelToNode);
1410:                                    mPSession.initProject(nd.getBnProject()
1411:                                            .getName());
1412:                                } catch (Exception e) {
1413:                                    throw new HeroException(e.getMessage());
1414:                                }
1415:                            }
1416:                            mPSession.copyNode(instanceToNodeName, modelToNode);
1417:                        }
1418:                        try {
1419:                            mPSession.copyIteration(instanceFromNodeName,
1420:                                    instanceToNodeName, modelIteration
1421:                                            .getCondition());
1422:                        } catch (Exception it) {
1423:                            //throw new HeroException("Error in generateOutNodes(): copyIteration() fail\n   Message: " + it.getMessage());
1424:                        } // maybe the iteration was already created
1425:                    }
1426:                }
1427:            }
1428:
1429:            private boolean isInstance(String name) {
1430:                return (name.matches(".*_instance.*"));
1431:            }
1432:
1433:            private String nodeSubProcess(String name, BnProjectLocal mProject) {
1434:                Collection nodes = mProject.getBnNodes();
1435:                Iterator nds = nodes.iterator();
1436:                while (nds.hasNext()) {
1437:                    BnNodeLocal node = (BnNodeLocal) nds.next();
1438:                    if ((node.getName()).matches(name))
1439:                        return node.getReference();
1440:                }
1441:                return name;
1442:            }
1443:
1444:            private boolean nodeExist(String node, ProjectSessionLocal mPSession)
1445:                    throws HeroException {
1446:                return (mPSession.getNodesNames().contains(node));
1447:            }
1448:
1449:            private boolean instanceWasModified(BnNodeLocal node,
1450:                    BnProjectLocal mProject) throws HeroException {
1451:                try {
1452:                    BonitaProjectValue model = BonitaProjectLocator
1453:                            .getInstance().getModelValue(
1454:                                    BonitaParsing.getModel(mProject.getName()),
1455:                                    BonitaParsing.getModel(mProject
1456:                                            .getVersion()));
1457:                    String name = node.getName();
1458:                    if (this .isInstance(name))
1459:                        name = BonitaParsing.getModel(node.getName());
1460:                    if (BonitaServiceValue.getNodeFromCache(model, name) == null)
1461:                        return true;
1462:                    return false;
1463:                } catch (Exception e) {
1464:                    e.printStackTrace();
1465:                    throw new HeroException(e.getMessage());
1466:                }
1467:            }
1468:
1469:            /**
1470:             *
1471:             * @throws CreateException
1472:             *
1473:             * @ejb:create-method view-type="both"
1474:             **/
1475:            public void ejbCreate() throws CreateException {
1476:                try {
1477:                    BonitaConfig bonitaConfig = BonitaConfig.getInstance();
1478:                    if (bonitaConfig.getJms()) {
1479:                        JMSServicesSessionLocalHome home = JMSServicesSessionUtil
1480:                                .getLocalHome();
1481:                        jms = home.create();
1482:                    }
1483:                } catch (Exception e) {
1484:                    throw new CreateException(e.getMessage());
1485:                }
1486:            }
1487:
1488:            public void setSessionContext(final javax.ejb.SessionContext context) {
1489:                mContext = context;
1490:            }
1491:
1492:            public void ejbRemove() {
1493:            }
1494:
1495:            public void ejbActivate() {
1496:            }
1497:
1498:            public void ejbPassivate() {
1499:            }
1500:
1501:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.