001: package hero.session;
002:
003: import hero.user.ReadEnv;
004: import hero.util.BonitaConfig;
005: import hero.util.EventConstants;
006: import hero.util.HeroException;
007: import java.util.Vector;
008:
009: import hero.historic.ProjectHistoric;
010: import hero.historic.NodeHistoric;
011: import hero.historic.PropertyHistoric;
012:
013: import java.io.File;
014: import java.util.ArrayList;
015: import java.util.Collection;
016:
017: import javax.ejb.CreateException;
018: import javax.ejb.SessionBean;
019: import javax.ejb.SessionContext;
020:
021: import org.apache.log4j.Logger;
022:
023: import org.apache.xerces.parsers.DOMParser;
024:
025: import org.w3c.dom.Document;
026: import org.w3c.dom.Element;
027: import org.w3c.dom.NodeList;
028: import org.w3c.dom.NodeList;
029:
030: /**
031: *
032: * The History Session Bean, is an stateless session bean that provides the user API to get information on
033: * historic instance and activity properties.<br>
034: *<br><br>
035: * <strong>The following lines shows a sample code to use this API in your application:<br><br></strong>
036: * <br>
037: * First of all you have to import the History Session files:<br>
038: * <br>
039: * import hero.interfaces.HistorySessionLocalHome;<br>
040: * import hero.interfaces.HistorySessionLocal;<br>
041: * import hero.interfaces.HistorySessionHome;<br>
042: * import hero.interfaces.HistorySession;<br>
043: * import hero.interfaces.HistorySessionUtil;<br>
044: * <br>
045: * Now, it is time to create the History Session instance:<br>
046: * <br>
047: * Like this if you want to use local interfaces:<br><br>
048: * HistorySessionLocalHome historyh = (HistorySessionLocalHome)hero.interfaces.HistorySessionUtil.getLocalHome();<br>
049: * HistorySessionLocal historysession = historyh.create();<br>
050: * <br>
051: * or like this if you use remote interfaces:<br>
052: * <br>
053: * HistorySessionHome historyh = (HistorySessionHome)hero.interfaces.HistorySessionUtil.getHome();<br>
054: * HistorySession historysession = historyh.create();<br>
055: * <br>
056: * <br>
057: * Now you can call all History Sessions methods...
058: *
059: *
060: * @ejb:bean name="HistorySession"
061: * display-name="HistorySession Bean"
062: * type="Stateless"
063: * transaction-type="Container"
064: * jndi-name="ejb/hero/HistorySession"
065: * local-jndi-name="ejb/hero/HistorySession_L"
066: *
067: * @ejb.permission role-name="BONITAUSER,user,SuperAdmin"
068: *
069: *
070: * @jonas.bean
071: * ejb-name="HistorySession"
072: * jndi-name="ejb/hero/HistorySession"
073: *
074: * @jboss.container-configuration name="Standard Stateless SessionBean for Bonita"
075: *
076: * @copyright (C) 2007 Bull S.A.
077: * @author D. Parisek
078: **/
079:
080: public class HistorySessionBean implements SessionBean, EventConstants {
081:
082: // -------------------------------------------------------------------------
083: // Static
084: // -------------------------------------------------------------------------
085: // Utility variable
086: private static final Logger trace = Logger
087: .getLogger(HistorySessionBean.class);
088:
089: // -------------------------------------------------------------------------
090: // Members
091: // -------------------------------------------------------------------------
092:
093: private SessionContext mContext;
094: private BonitaConfig bonitaConfig;
095:
096: // -------------------------------------------------------------------------
097: // Methods
098: // -------------------------------------------------------------------------
099:
100: /**
101: * Get list of project histories by project name w/o pagination.
102: * @return a Collection of projects that have history
103: * @throws HeroException
104: * @ejb:interface-method view-type="both"
105: * @ejb:transaction type="Supports"
106: *
107: **/
108: public Collection getHistoryProjectList() throws HeroException {
109: try {
110: return (getHistoryProjectList(0, 0));
111: } catch (Exception e) {
112: throw new HeroException("Error getting history projects: "
113: + e.getMessage());
114: }
115: }
116:
117: /**
118: * Get list of project histories by project name w/ pagination.
119: * @return a Collection of projects that have history
120: * @param startindex
121: * @param nrows
122: * @throws HeroException
123: * @ejb:interface-method view-type="both"
124: * @ejb:transaction type="Supports"
125: *
126: **/
127: public Collection getHistoryProjectList(int startindex, int nrows)
128: throws HeroException {
129: ArrayList historyProjects = new ArrayList();
130: try {
131: String historyDir = initHistoryLocation();
132: if (historyDir.equals(""))
133: return null;
134: File historicDirectory = new File(historyDir);
135: trace.debug("start by "
136: + mContext.getCallerPrincipal().getName());
137:
138: if (historicDirectory.isDirectory()) {
139: File[] processDirectories = historicDirectory
140: .listFiles();
141: int limit = processDirectories.length;
142: if (nrows == 0)
143: startindex = 0;
144: else if ((startindex + nrows) < limit)
145: limit = startindex + nrows;
146: for (int i = startindex; i < limit; i++) {
147: File processDirectory = processDirectories[i];
148: String processName = processDirectory.getName();
149: historyProjects.add(processName);
150: }
151: }
152: return historyProjects;
153: } catch (Exception e) {
154: throw new HeroException("Error getting history projects: "
155: + e.getMessage());
156: }
157: }
158:
159: /**
160: * Get instance history attributes.
161: * @return a ProjectHistoric object - get project attrs
162: * @parsm projectName
163: * @param instanceName
164: * @throws HeroException
165: * @ejb:interface-method view-type="both"
166: * @ejb:transaction type="Supports"
167: *
168: **/
169: public Object getHistoryInstance(String instanceName)
170: throws HeroException {
171: try {
172: int delim = instanceName.indexOf("_instance");
173: if (delim < 0)
174: return null;
175: String projectName = instanceName.substring(0, delim);
176: Vector nodelist = new Vector();
177: String historyDir = initHistoryLocation();
178: if (historyDir.equals(""))
179: return null;
180: String instancePath = historyDir + File.separator
181: + projectName + File.separator + instanceName
182: + ".xml";
183:
184: ProjectHistoric ph = new ProjectHistoric();
185:
186: DOMParser parser = new DOMParser();
187: parser.parse(instancePath);
188: Document document = parser.getDocument();
189: Element element = document.getDocumentElement();
190: if (element.hasAttributes()) {
191: ph.setName(element.getAttribute("name"));
192: ph.setVersion(element.getAttribute("version"));
193: ph
194: .setCreationDate(element
195: .getAttribute("creationDate"));
196: ph.setEndDate(element.getAttribute("endDate"));
197: ph.setInitiator(element.getAttribute("initiator"));
198: }
199: // set nodes (activities)
200: NodeList nodes = document.getElementsByTagName("Node");
201: for (int i = 0; i < nodes.getLength(); i++) {
202: NodeHistoric nh = new NodeHistoric();
203: Element nodeElement = (Element) nodes.item(i);
204: if (nodeElement.hasAttributes()) {
205: nh.setName(nodeElement.getAttribute("name"));
206: nh.setState(nodeElement.getAttribute("state"));
207: nh.setRole(nodeElement.getAttribute("role"));
208: nh
209: .setExecutor(nodeElement
210: .getAttribute("executor"));
211: nh.setType(nodeElement.getAttribute("type"));
212: if (nodeElement.getAttribute("anticipable").equals(
213: "true"))
214: nh.setAnticipable(true);
215: else
216: nh.setAnticipable(false);
217: nh.setDescription(nodeElement
218: .getAttribute("description"));
219: nh.setStartDate(nodeElement
220: .getAttribute("startDate"));
221: nh.setEndDate(nodeElement.getAttribute("endDate"));
222: nodelist.add(nh);
223: }
224: // set nodes (activities) properties
225: Vector nproplist = new Vector();
226: NodeList nprops = nodeElement
227: .getElementsByTagName("Property");
228: for (int i2 = 0; i2 < nprops.getLength(); i2++) {
229: PropertyHistoric pph = new PropertyHistoric();
230: Element propElement = (Element) nprops.item(i2);
231: if (propElement.hasAttributes()) {
232: pph.setKey(propElement.getAttribute("key"));
233: pph.setValue(propElement.getAttribute("value"));
234: nproplist.add(pph);
235: }
236: }
237: if (nproplist.size() > 0)
238: nh.setProperties(nproplist);
239: else
240: nh.setProperties(null);
241: }
242: if (nodelist.size() > 0)
243: ph.setNodes(nodelist);
244: else
245: ph.setNodes(null);
246:
247: // set project properties
248: Vector pproplist = new Vector();
249: NodeList pprops = element.getElementsByTagName("Property");
250: for (int i = 0; i < pprops.getLength(); i++) {
251: PropertyHistoric pph = new PropertyHistoric();
252: Element propElement = (Element) pprops.item(i);
253: if (propElement.getParentNode().getNodeName().equals(
254: "Instance")
255: && propElement.hasAttributes()) {
256: pph.setKey(propElement.getAttribute("key"));
257: pph.setValue(propElement.getAttribute("value"));
258: pproplist.add(pph);
259: }
260: }
261: if (pproplist.size() > 0)
262: ph.setProperties(pproplist);
263: else
264: ph.setProperties(null);
265: return (ph);
266: } catch (Exception e) {
267: throw new HeroException(
268: "Error getting history instance properties: "
269: + e.getMessage());
270: }
271: }
272:
273: private Vector getHistoryNodes(String instanceName,
274: String instancePath) throws HeroException {
275: try {
276: Vector result = new Vector();
277: NodeHistoric nh = new NodeHistoric();
278:
279: DOMParser parser = new DOMParser();
280: parser.parse(instancePath);
281: Document document = parser.getDocument();
282: NodeList nodes = document.getElementsByTagName("Instance");
283: for (int i = 0; i < nodes.getLength(); i++) {
284: Element nodeElement = (Element) nodes.item(i);
285: nh.setName(nodeElement.getAttribute("name"));
286: nh.setState(nodeElement.getAttribute("state"));
287: nh.setRole(nodeElement.getAttribute("role"));
288: nh.setExecutor(nodeElement.getAttribute("executor"));
289: nh.setType(nodeElement.getAttribute("type"));
290: if (nodeElement.getAttribute("anticipable").equals(
291: "true"))
292: nh.setAnticipable(true);
293: else
294: nh.setAnticipable(false);
295: nh.setDescription(nodeElement
296: .getAttribute("description"));
297: nh.setStartDate(nodeElement.getAttribute("startDate"));
298: nh.setEndDate(nodeElement.getAttribute("endDate"));
299: result.add(nh);
300: }
301: return (result);
302: } catch (Exception e) {
303: throw new HeroException(
304: "Error getting history instance activity nodes: "
305: + e.getMessage());
306: }
307: }
308:
309: /**
310: * Get instance activity history list.
311: * @return a Collection of activity names
312: * @param projectName
313: * @param instanceName
314: * @throws HeroException
315: * @ejb:interface-method view-type="both"
316: * @ejb:transaction type="Supports"
317: *
318: **/
319: public Collection getHistoryInstanceActivityList(String instanceName)
320: throws HeroException {
321: ArrayList nodeList = new ArrayList();
322: try {
323: int delim = instanceName.indexOf("_instance");
324: if (delim < 0)
325: return null;
326: String projectName = instanceName.substring(0, delim);
327: String historyDir = initHistoryLocation();
328: if (historyDir.equals(""))
329: return null;
330: String instancePath = historyDir + File.separator
331: + projectName + File.separator + instanceName
332: + ".xml";
333:
334: DOMParser parser = new DOMParser();
335: parser.parse(instancePath);
336: Document document = parser.getDocument();
337: NodeList nodes = document.getElementsByTagName("Node");
338: for (int i = 0; i < nodes.getLength(); i++) {
339: Element nodeElement = (Element) nodes.item(i);
340: nodeList.add(nodeElement.getAttribute("name"));
341: }
342: return (nodeList);
343: } catch (Exception e) {
344: throw new HeroException(
345: "Error getting history instance activity list: "
346: + e.getMessage());
347: }
348: }
349:
350: /**
351: * Get instance activity history list w/o pagination.
352: * @return a Collection of activity names
353: * @param projectName
354: * @throws HeroException
355: * @ejb:interface-method view-type="both"
356: * @ejb:transaction type="Supports"
357: *
358: **/
359: public Collection getHistoryInstancesList(String projectName)
360: throws HeroException {
361: try {
362: return getHistoryInstancesList(projectName, 0, 0);
363: } catch (Exception e) {
364: throw new HeroException(
365: "Error getting history instance activity list: "
366: + e.getMessage());
367: }
368: }
369:
370: /**
371: * Get instance activity history list w/ pagination.
372: * @return a Collection of activity names
373: * @param projectName
374: * @param startindex
375: * @param nrows
376: * @throws HeroException
377: * @ejb:interface-method view-type="both"
378: * @ejb:transaction type="Supports"
379: *
380: **/
381: public Collection getHistoryInstancesList(String projectName,
382: int startindex, int nrows) throws HeroException {
383: ArrayList instList = new ArrayList();
384: try {
385: String historyDir = initHistoryLocation();
386: if (historyDir.equals(""))
387: return null;
388: String instancePath = historyDir + File.separator
389: + projectName;
390: File processDirectory = new File(instancePath);
391: if (processDirectory.isDirectory()) {
392: File[] instancesFiles = processDirectory.listFiles();
393: int limit = instancesFiles.length;
394: if (nrows == 0)
395: startindex = 0;
396: else if ((startindex + nrows) < limit)
397: limit = startindex + nrows;
398: for (int i = startindex; i < limit; i++) {
399: File instanceFile = instancesFiles[i];
400: String instanceName = instanceFile.getName();
401: instList.add(instanceName);
402: }
403: }
404: return (instList);
405: } catch (Exception e) {
406: throw new HeroException(
407: "Error getting history instance activity list: "
408: + e.getMessage());
409: }
410: }
411:
412: private String initHistoryLocation() throws HeroException {
413: String HISTORIC_DIR = "";
414: try {
415: ReadEnv renv = new ReadEnv();
416: HISTORIC_DIR = renv.getVariable("BONITA_HOME")
417: + File.separator + "bonita-historic";
418:
419: if (!(new File(HISTORIC_DIR)).exists())
420: return ("");
421: } catch (Exception e) {
422: throw new HeroException(
423: "Error getting history instance activity list: "
424: + e.getMessage());
425: }
426: return (HISTORIC_DIR);
427: }
428:
429: /**
430: * Creates the History Session Bean. This method is the first one to invoke in order to
431: * use HistorySession API. If the user is not authorized this method throws an exception.
432: *
433: * @throws CreateException
434: *
435: * @ejb:create-method view-type="both"
436: **/
437: public void ejbCreate() throws CreateException {
438: }
439:
440: /**
441: * Internal Enterprise Java Beans method.
442: **/
443:
444: public void setSessionContext(final javax.ejb.SessionContext context) {
445: mContext = context;
446: }
447:
448: /**
449: * Internal Enterprise Java Beans method.
450: **/
451: public void ejbRemove() {
452: }
453:
454: /**
455: * Internal Enterprise Java Beans method.
456: **/
457: public void ejbActivate() {
458: }
459:
460: /**
461: * Internal Enterprise Java Beans method.
462: **/
463: public void ejbPassivate() {
464: }
465: }
|