001: /*
002: * Created on 3 mai 2004
003: *
004: */
005: package mc.formgenerator.bonita;
006:
007: import hero.interfaces.ProjectSession;
008: import hero.interfaces.ProjectSessionHome;
009: import hero.interfaces.ProjectSessionUtil;
010: import hero.interfaces.UserSession;
011: import hero.interfaces.UserSessionHome;
012: import hero.interfaces.UserSessionUtil;
013:
014: import java.util.*;
015:
016: import org.w3c.dom.Document;
017:
018: import hero.util.HeroException;
019:
020: /**
021: * Given instance document with properties, starts and ends an activity
022: */
023: public class BonitaActivityExecutor {
024:
025: /**
026: * Default constructor
027: */
028: public BonitaActivityExecutor() {
029:
030: }
031:
032: /**
033: * Allows to find, initialise and terminate an activity.
034: * @param document Document with the activity information (name, attributes and project name).
035: * @param mode the application mode: consumer or exploitant
036: * @throws LoginException
037: */
038: public void actOnActivity(Document document, Vector req_parameters,
039: boolean formsExists, String mode, String version)
040: throws HeroException {
041:
042: //'parse' transforms a Document
043: DocumentParser parse = new DocumentParser();
044:
045: DataActivity data = parse.parseActivity(document,
046: req_parameters, formsExists);
047:
048: //get the project name to wich it belongs
049: String projectName = data.getProjectName();
050:
051: //get the activity name
052: String activityName = data.getActivityName();
053:
054: //get the process properties
055: HashMap projectTable = data.getProjectProperties();
056:
057: //get activity properties
058: HashMap activityTable = data.getActivityProperties();
059:
060: //terminate the activity thanks to its name and properties
061: this .startActivity(projectName, activityName, projectTable,
062: activityTable, mode, version);
063: }
064:
065: /**
066: * Thanks to its name, the activity is found and stopped.
067: * Before, its attributes are set with the HashMap values.
068: * @param activityName Name of the activity to stop.
069: * @param projectName Name of the project to which it belongs.
070: * @param table HashMap with the activity attributes associated to their values
071: * @param mode the application mode: consumer or exploitant
072: * @throws LoginException
073: */
074: private void startActivity(String projectName, String activityName,
075: HashMap projectTable, HashMap activityTable, String mode,
076: String version) throws HeroException {
077:
078: //The local home
079: ProjectSessionHome projecth = null;
080:
081: //The bean
082: ProjectSession project = null;
083:
084: //All the keys will be saved in a set
085: Set collec;
086:
087: try {
088: //Getting local home by using JNDI
089: projecth = ProjectSessionUtil.getHome();
090:
091: //Creation of the project
092: project = projecth.create();
093:
094: //project.initProjectWithVersion(projectName,version);
095: project.initProject(projectName);
096:
097: //**************************************************************************
098: // SET THE PROJECT PROPERTIES
099: //**************************************************************************
100:
101: collec = projectTable.keySet();
102:
103: //Set properties if they exist
104: if (collec.size() > 0) {
105:
106: //For all the keys we have to retrieve the associated value
107: Iterator it = collec.iterator();
108:
109: //variable which will stock the property name
110: String cle = "";
111:
112: while (it.hasNext()) {
113:
114: //Getting the values
115: cle = (String) it.next();
116: //Set the current process property
117: project.setProperty(cle, (String) projectTable
118: .get(cle));
119: }
120: }
121:
122: //**************************************************************************
123: // SET ACTIVITY PROPERTIES
124: //**************************************************************************
125:
126: collec = activityTable.keySet();
127:
128: //Set properties if they exist
129: if (collec.size() > 0) {
130:
131: //For all the keys we have to retrieve the associated value
132: Iterator it = collec.iterator();
133:
134: //variable which will stock the property name
135: String cle = "";
136:
137: while (it.hasNext()) {
138:
139: //Getting the values
140: cle = (String) it.next();
141: //Set the current process property
142: project.setNodeProperty(activityName, cle,
143: (String) activityTable.get(cle));
144: }
145: }
146:
147: //**************************************************************************
148: // START THE ACTIVITY
149: //**************************************************************************
150:
151: //The local home
152: UserSessionHome userh = UserSessionUtil.getHome();
153: //The bean
154: UserSession user = userh.create();
155: //the session starts the activity
156: user.startActivity(projectName, activityName);
157: //the session terminate the activity
158: user.terminateActivity(projectName, activityName);
159:
160: //**************************************************************************
161: // STOP THE ACTIVITY
162: //**************************************************************************
163:
164: //if(mode.equals("consumer"))
165: //this.endActivity(activityName, projectName);
166: project.remove();
167: } catch (Exception e) {
168: try {
169: if (project != null)
170: project.remove();
171: } catch (Exception re) {
172: System.out.println(re + " " + e.getMessage());
173: }
174: //System.out.println(e.getMessage());
175: throw new HeroException(e.getMessage());
176: }
177: }
178:
179: public void endActivity(String activityName, String projectName) {
180: try {
181:
182: //The local home
183: UserSessionHome userh = UserSessionUtil.getHome();
184:
185: //The bean
186: UserSession user = userh.create();
187:
188: //the session terminates the activity
189: user.terminateActivity(projectName, activityName);
190:
191: } catch (Exception e) {
192: System.out.println(e.getMessage());
193: }
194: }
195:
196: }
|