001: /*
002: * Created on 19 avr. 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: import hero.interfaces.BnProjectPropertyValue;
016: import hero.interfaces.ProjectSessionHome;
017: import hero.interfaces.ProjectSession;
018: import hero.interfaces.ProjectSessionUtil;
019:
020: /**
021: * The aim of this class is to transform data from a Bonita project to a xml Document
022: */
023: public class BonitaProjectAdapter {
024:
025: private ArrayList keys;
026:
027: /**
028: * Default class constructor
029: */
030: public BonitaProjectAdapter() {
031: };
032:
033: /**
034: * Get the bonita properties as key value hash map
035: * @param theProjectName : The project concerned.
036: * @return HashMap the table containing the properties.
037: */
038: private HashMap getHBonitaData(String theProjectName,
039: String theProjectVersion) {
040:
041: //Hash map where 'Bonita key - value' will be put
042: HashMap hMap = new HashMap();
043:
044: //The local home
045: ProjectSessionHome projecth = null;
046:
047: //The bean
048: ProjectSession project = null;
049:
050: //keys arrayList
051: keys = new ArrayList();
052:
053: try {
054: //Getting local home by using JNDI
055: projecth = ProjectSessionUtil.getHome();
056:
057: //Project creation
058: project = projecth.create();
059:
060: project.initModelWithVersion(theProjectName,
061: theProjectVersion);
062: project.executeProcessHook();
063:
064: //We are now able to acess Bonita api and get properties
065: Collection collectionProperties = project.getProperties();
066:
067: //Number of bean instances responding
068:
069: //Property value of Bonita
070: BnProjectPropertyValue pv = null;
071:
072: //The current key of the property value object
073: String currentKey = "";
074:
075: //The current value of the property value object
076: String currentValue = "";
077: String possibleValue = "";
078:
079: //For all the keys we have to retrieve the associated value
080: Iterator it = collectionProperties.iterator();
081:
082: while (it.hasNext()) {
083: //Getting the values
084: pv = (BnProjectPropertyValue) it.next();
085:
086: //Retrieve the key
087: currentKey = pv.getTheKey();
088: currentValue = pv.getTheValue();
089: if (currentValue == null)
090: currentValue = "";
091: //And the value or possible values
092: Collection possibleValues = pv.getPossibleValues();
093: if (possibleValues != null) {
094: //Pb with the StringTokenizer if ""
095: if (currentValue.equals(""))
096: currentValue = " ";
097: //For all the keys we have to retrieve the associated value
098: Iterator itValues = possibleValues.iterator();
099: // each possible values are added after the value
100: while (itValues.hasNext()) {
101: // we add a pipe | after each possible values
102: currentValue = currentValue + "|"
103: + (String) itValues.next();
104: }
105: }
106: //Adding key to collection to mantain order
107: keys.add(currentKey);
108: //Adding key and value in a hashMap
109: hMap.put(currentKey, currentValue);
110: }
111: project.remove();
112: //Returns the hash map
113: return hMap;
114: } catch (Exception e) {
115: try {
116: if (project != null)
117: project.remove();
118: } catch (Exception re) {
119: System.out.println(re + " " + e.getMessage());
120: }
121: System.out.println(e + " " + e.getMessage());
122: return null;
123: }
124: }
125:
126: /**
127: * Provides a Document output for Bonita data
128: * @param theHashMap : The table containing the properties.
129: * @param theProjectName : The project concerned.
130: * @return Document document with the process information : name & properties.
131: * @throws IOExecption
132: */
133: private Document generateBonitaDataDocument(HashMap theHashMap,
134: String theProjectName) throws IOException {
135:
136: //DOM
137: Document document = null;
138:
139: //a parser to generate the xml document from the dataProcess
140: DocumentParser parser = new DocumentParser();
141:
142: //Create the dataProcess structure with bonita key value
143: DataProject data = new DataProject(theProjectName, theHashMap);
144:
145: //Generate the xml document representing the dataProcess
146: if (data != null)
147: document = parser.createDocument(data, keys);
148:
149: return document;
150: }
151:
152: /**
153: * Get the Bonita project data identified by its name and put them in a DOM
154: * @param theProjectName : The name of the project we have to get process informations
155: * @return the document that represents bonita data
156: * @throws LoginException
157: * @throws IOException
158: */
159: public Document getProjectData(String theProjectName,
160: String theProjectVersion) throws IOException {
161:
162: //H map of Bonita data
163: HashMap bonitaMap = new HashMap();
164:
165: //Set Bonita data in H map
166: bonitaMap = this .getHBonitaData(theProjectName,
167: theProjectVersion);
168:
169: //Document generated
170: return this.generateBonitaDataDocument(bonitaMap,
171: theProjectName);
172: }
173: }
|