001: /**
002: * Bonita
003: * Copyright (C) 1999 Bull S.A.
004: * Bull 68 route de versailles 78434 Louveciennes Cedex France
005: * Further information: bonita@objectweb.org
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
020: * USA
021: --------------------------------------------------------------------------
022: * BONITA Workflow Patterns sample
023: * Pattern 18 : Milestone
024: * Author : Jordi Anguela
025: * Date : 2006/03/01
026: --------------------------------------------------------------------------
027: * Example
028: * This exemple shows how you can use Bonita deadlines to control the period
029: * of execution of an activity.
030: * 'withdraw_order' is only enabled :
031: * - only after the execution of 'order_book'
032: * - only during 20 seconds
033: * - only if 'shipping_process' activity is not started
034: --------------------------------------------------------------------------
035: */package hero.client.samples.patterns.src;
036:
037: import hero.client.test.SimpleCallbackHandler;
038: import hero.interfaces.Constants;
039: import hero.interfaces.ProjectSession;
040: import hero.interfaces.ProjectSessionHome;
041: import hero.interfaces.ProjectSessionUtil;
042:
043: import javax.security.auth.login.LoginContext;
044:
045: public class Pattern18MilestoneAsDeadline {
046: final static long DEADLINE = 1000; // In miliseconds
047:
048: public static void main(String[] args) {
049: try {
050: // User Admin login
051: char[] password = { 't', 'o', 't', 'o' };
052: SimpleCallbackHandler handler = new SimpleCallbackHandler(
053: "admin", password);
054: LoginContext lc = new LoginContext("TestClient", handler);
055: lc.login();
056:
057: // Project Session Bean creation
058: ProjectSessionHome prjHome = (ProjectSessionHome) ProjectSessionUtil
059: .getHome();
060: ProjectSession prjSession = prjHome.create();
061:
062: // Model creation
063: System.out.print("\n Pattern 18 - Milestone as Deadline");
064: prjSession.initModel("Pattern 18 - Milestone as Deadline");
065:
066: // Add activities and set them to Non anticipable
067: prjSession.addNode("order_book",
068: hero.interfaces.Constants.Nd.AND_JOIN_NODE);
069: prjSession.addNode("withdraw_order",
070: hero.interfaces.Constants.Nd.AND_JOIN_NODE);
071: prjSession.addNode("ship_order",
072: hero.interfaces.Constants.Nd.AND_JOIN_NODE);
073: prjSession.setNodeTraditional("order_book");
074: prjSession.setNodeTraditional("withdraw_order");
075: prjSession.setNodeTraditional("ship_order");
076:
077: // Add transitions between activities
078: prjSession.addEdge("order_book", "ship_order");
079: prjSession.addEdge("order_book", "withdraw_order");
080:
081: // Add Hook that set the Deadline for 'withdraw_order'
082: String setDeadlineScript = "import hero.interfaces.*; \n"
083: + "beforeTerminate(Object engine, Object currentNode) { \n"
084: + " hero.interfaces.ProjectSessionHome pHome = (hero.interfaces.ProjectSessionHome) hero.interfaces.ProjectSessionUtil.getHome(); \n"
085: + " hero.interfaces.ProjectSession prjSession = pHome.create(); \n"
086: + " prjSession.initModel(currentNode.getBnProject().getName()); \n"
087: + " ArrayList deadlines = new ArrayList(); \n"
088: + " long date1 = System.currentTimeMillis() + (long)10000; \n"
089: + " deadlines.add(new Long(date1)); \n"
090: + " prjSession.setNodeDeadlines(\"withdraw_order\", deadlines); \n"
091: + "}";
092: prjSession.addNodeInterHook("order_book",
093: "order_book Hook",
094: hero.interfaces.Constants.Nd.BEFORETERMINATE,
095: hero.interfaces.Constants.Hook.BSINTERACTIVE,
096: setDeadlineScript);
097:
098: // Add Deadline Hook to 'withdraw_order' to cancel itself when the deadline expires
099: String onDeadlineScript = "import hero.interfaces.*; \n"
100: + "onDeadline(Object engine, Object currentNode) { \n"
101: + " System.out.println(\"\n\n --> Deadline hook <--\"); \n"
102: + " UserSessionHome userHome = UserSessionUtil.getHome(); \n"
103: + " UserSession userSession = userHome.create(); \n"
104: + " userSession.cancelActivity(currentNode.getBnProject().getName(), \"withdraw_order\"); \n "
105: + "}";
106: //prjSession.addNodeInterHook("withdraw_order", "withdraw_order Hook", hero.interfaces.Constants.Nd.ONDEADLINE, hero.interfaces.Constants.Hook.BSINTERACTIVE, onDeadlineScript);
107: prjSession.addNodeHook("withdraw_order",
108: "hero.hook.CancelActivity",
109: hero.interfaces.Constants.Nd.ONDEADLINE,
110: Constants.Hook.JAVA);
111:
112: // Add Hook to 'ship_order' to cancel the 'order_book if it has not executed yet
113: String cancelScript = "import hero.interfaces.*; \n"
114: + "beforeStart(Object engine, Object currentNode) { \n"
115: + " hero.interfaces.ProjectSessionHome pHome = (hero.interfaces.ProjectSessionHome) hero.interfaces.ProjectSessionUtil.getHome(); \n"
116: + " hero.interfaces.ProjectSession prjSession = pHome.create(); \n"
117: + " prjSession.initModel(currentNode.getBnProject().getName()); \n"
118: + " if (prjSession.getNodeState(\"withdraw_order\") == hero.interfaces.Constants.Nd.READY) { \n"
119: + " UserSessionHome userHome = UserSessionUtil.getHome(); \n"
120: + " UserSession userSession = userHome.create(); \n"
121: + " userSession.cancelActivity(currentNode.getBnProject().getName(), \"withdraw_order\"); \n "
122: + " } \n" + "}";
123: prjSession.addNodeInterHook("ship_order",
124: "ship_order Hook",
125: hero.interfaces.Constants.Nd.BEFORESTART,
126: hero.interfaces.Constants.Hook.BSINTERACTIVE,
127: cancelScript);
128:
129: // Add and set a role for the user admin to execute the activities
130: prjSession.addRole("Executor",
131: "Rol that enables to execute the activities");
132: prjSession.setUserRole("admin", "Executor");
133: prjSession.setNodeRole("order_book", "Executor");
134: prjSession.setNodeRole("withdraw_order", "Executor");
135: prjSession.setNodeRole("ship_order", "Executor");
136:
137: // Check model definition
138: prjSession.checkModelDefinition();
139:
140: System.out.println(" [ OK ]");
141: } catch (Exception e) {
142: System.out.println("\n\n [ ERROR ] : " + e);
143: e.printStackTrace();
144: } // Maybe something is wrong
145: }
146: }
|