001: /*
002: * Created on 03 mai. 2004 by the Message Center Team
003: *
004: */
005: package mc.formgenerator.bonita;
006:
007: import java.io.IOException;
008: import java.util.ArrayList;
009: import java.util.Collection;
010: import java.util.HashMap;
011: import java.util.Iterator;
012:
013: import org.w3c.dom.Document;
014:
015: //Bonita imports
016: import hero.interfaces.*;
017: import hero.interfaces.ProjectSessionHome;
018: import hero.interfaces.ProjectSession;
019:
020: /**
021: * The aim of this class is to transform Bonita activity data to a xml Document
022: */
023: public class BonitaActivityAdapter {
024:
025: private ArrayList projectKeys;
026: private ArrayList activityKeys;
027:
028: /**
029: * Default class constructor
030: */
031: public BonitaActivityAdapter() {
032: };
033:
034: /**
035: * Provides a Document output for Bonita data
036: * @param theHashMap : The table containing the properties.
037: * @param theProjectName : The project concerned.
038: * @param theProjectName the activity concerned in the project
039: * @return Document document with the activity information : project name & activity name & activity properties.
040: * @throws IOExecption
041: */
042: private Document generateBonitaDataDocument(HashMap projectHashMap,
043: HashMap activityHashMap, String theProjectName,
044: String theActivityName) throws IOException {
045:
046: //DOM
047: Document document = null;
048:
049: //a parser to generate the xml document from the dataProcess
050: DocumentParser parser = new DocumentParser();
051:
052: //Create the dataProcess structure with bonita key value
053: DataActivity data = new DataActivity(theProjectName,
054: theActivityName, projectHashMap, activityHashMap);
055:
056: //Generate the xml document representing the dataProcess
057: if (data != null)
058: document = parser.createDocument(data, projectKeys,
059: activityKeys);
060:
061: return document;
062: }
063:
064: /**
065: * Get the Bonita activity data identified by its name and put them in a DOM
066: * @param theProjectName the name of the project we have to get process informations
067: * @param theActivityName the name of the activityname concerned
068: * @return the document that represents bonita activity data
069: * @throws LoginException
070: * @throws IOException
071: */
072: public Document getActivityData(String theProjectName,
073: String theActivityName, String theProjectVersion)
074: throws IOException {
075:
076: //SET PROJECT AND ACTIVITY DATA IN HASHMAPS
077:
078: //H map of project data where 'Bonita key - value' will be put
079: HashMap projectMap = new HashMap();
080:
081: //H map of activity data where 'Bonita key - value' will be put
082: HashMap activityMap = new HashMap();
083:
084: //The local home
085: ProjectSessionHome projecth = null;
086:
087: //The bean
088: ProjectSession project = null;
089: ProjectSession model = null;
090:
091: //keys arrayLists
092: projectKeys = new ArrayList();
093: activityKeys = new ArrayList();
094:
095: try {
096: //Getting local home by using JNDI
097: projecth = ProjectSessionUtil.getHome();
098:
099: //Project creation
100: project = projecth.create();
101:
102: model = projecth.create(); // isAdmin of the model ?
103: int i = theProjectName.indexOf("_instance");
104: model.initModelWithVersion(theProjectName.substring(0, i),
105: theProjectVersion);
106: //model.initProject(theProjectName.substring(0, i));
107: //ORIG: model.initProject(theProjectName.substring(0, i));
108:
109: //Project initialisation
110: project.initProject(theProjectName);
111: //ORIG: project.initProject(theProjectName);
112:
113: if (!project.isUserInNodeRole(theActivityName)
114: && !model.isAdminOfProject())
115: throw new Exception(
116: "You are not allowed to execute this activity ! ");
117:
118: //properties collection
119: Collection collectionProperties;
120: //colelction iterator
121: Iterator it;
122: //The current key of the property value object
123: String currentKey = "";
124: //The current value of the property value object
125: String currentValue = "";
126: String possibleValue = "";
127:
128: //GET PROJECT PROPERTIES
129: //Project Property value of Bonita
130: BnProjectPropertyValue pv = null;
131: collectionProperties = project.getProperties();
132: //For all the keys we have to retrieve the associated value
133: it = collectionProperties.iterator();
134:
135: while (it.hasNext()) {
136: //Getting the values
137: pv = (BnProjectPropertyValue) it.next();
138:
139: //Retrieve the key
140: currentKey = pv.getTheKey();
141: currentValue = pv.getTheValue();
142: if (currentValue == null)
143: currentValue = "";
144: //And the value or possible values
145: Collection possibleValues = pv.getPossibleValues();
146: if (possibleValues != null) {
147: //Pb with the StringTokenizer if ""
148: if (currentValue.equals(""))
149: currentValue = " ";
150: //For all the keys we have to retrieve the associated value
151: Iterator itValues = possibleValues.iterator();
152: // each possible values are added after the value
153: while (itValues.hasNext()) {
154: // we add a pipe | after each possible values
155: currentValue = currentValue + "|"
156: + (String) itValues.next();
157: }
158: }
159: //Adding key to collection to mantain order
160: projectKeys.add(currentKey);
161: //Adding key and value in a hashMap
162: projectMap.put(currentKey, currentValue);
163: }
164:
165: //GET ACTIVITY PROPERTIES
166: //Project Property value of Bonita
167: BnNodePropertyValue npv = null;
168: collectionProperties = project
169: .getNodeProperties(theActivityName);
170:
171: //For all the keys we have to retrieve the associated value
172: it = collectionProperties.iterator();
173:
174: while (it.hasNext()) {
175: //Getting the values
176: npv = (BnNodePropertyValue) it.next();
177:
178: //Retrieve the key
179: currentKey = npv.getTheKey();
180: currentValue = npv.getTheValue();
181: if (currentValue == null)
182: currentValue = "";
183: //And the value or possible values
184: Collection possibleValues = npv.getPossibleValues();
185: if (possibleValues != null) {
186: //Pb with the StringTokenizer if ""
187: if (currentValue.equals(""))
188: currentValue = " ";
189: //For all the keys we have to retrieve the associated value
190: Iterator itValues = possibleValues.iterator();
191: // each possible values are added after the value
192: while (itValues.hasNext()) {
193: currentValue = currentValue + "|"
194: + (String) itValues.next();
195: }
196: }
197: //Adding key to collection to mantain order
198: activityKeys.add(currentKey);
199: //Adding key and value in a hashMap
200: activityMap.put(currentKey, currentValue);
201: currentValue = "";
202: }
203: project.remove();
204: model.remove();
205: } catch (Exception e) {
206: try {
207: if (project != null)
208: project.remove();
209: } catch (Exception re) {
210: System.out.println(re + " " + e.getMessage());
211: }
212: throw new IOException(e.getMessage());
213: }
214:
215: //Document generated
216: return this.generateBonitaDataDocument(projectMap, activityMap,
217: theProjectName, theActivityName);
218: }
219: }
|