001: /*
002: * JFolder, Copyright 2001-2006 Gary Steinmetz
003: *
004: * Distributable under LGPL license.
005: * See terms of license at gnu.org.
006: */
007:
008: package org.jfolder.engines;
009:
010: //base classes
011: import java.io.IOException;
012: import java.math.BigDecimal;
013: import javax.xml.parsers.ParserConfigurationException;
014: import org.w3c.dom.Document;
015: import org.xml.sax.SAXException;
016:
017: //project specific classes
018: import org.jfolder.common.UnexpectedSystemException;
019: import org.jfolder.common.tagging.ConceptTagCharacteristic;
020: import org.jfolder.common.tagging.ConceptTagPreferences;
021: import org.jfolder.common.tagging.DetermineValueAndClassConceptTagContext;
022: import org.jfolder.common.tagging.DetermineValueAndClassConceptTagException;
023: import org.jfolder.common.tagging.RootConceptTagHolder;
024: import org.jfolder.common.tagging.ValueAndClassForConceptTag;
025: import org.jfolder.common.utils.misc.MiscHelper;
026: import org.jfolder.common.utils.xml.XMLHelper;
027: import org.jfolder.security.audit.SecurityAudit;
028: import org.jfolder.services.workflow.WorkflowServiceCaller;
029: import org.jfolder.workflow.model.attributes.AttributeSet;
030: import org.jfolder.workflow.model.attributes.WaitHelper;
031: import org.jfolder.workflow.model.history.HistoryAudit;
032: import org.jfolder.workflow.model.history.TraceAudit;
033: import org.jfolder.workflow.model.template.WorkflowTemplate;
034: import org.jfolder.workflow.model.template.WorkflowTemplateStart;
035: import org.jfolder.workflow.model.trigger.WorkflowTrigger;
036:
037: //other classes
038:
039: public class TriggerEngine {
040:
041: public final static void triggerWorkflow(String inHandle,
042: WorkflowServiceCaller inWsc) {
043:
044: throw UnexpectedSystemException.notImplemented();
045: //TO DO: determine how to use local interfaces with JBoss
046:
047: //MiscHelper.println("TriggerEngine inHandle = " + inHandle);
048: //TriggerData td = inWl.retrieveTriggerData(inHandle);
049: //MiscHelper.println("TriggerEngine content = " + td.getDataAsString());
050: //MiscHelper.println("TriggerEngine status = " + td.getAuditStatus());
051: //td.setAuditStatus(TriggerData.PROCESSED);
052:
053: //if (true) {
054: // throw UnexpectedSystemException.notImplemented();
055: //}
056:
057: //inWl.updateTriggerDataStatus(inHandle, td);
058:
059: //td = inWl.retrieveTriggerData(inHandle);
060: //MiscHelper.println("TriggerEngine status2 = " + td.getAuditStatus());
061:
062: /*
063: try {
064:
065: //core variables
066: HistoryAudit history = null;
067: TriggerData trigger = inWl.retrieveTriggerData(inHandle);
068: WorkflowTemplate processMap = null;
069: RootConceptTagHolder rootTagHolder = null;
070: AttributeSet attributeSet = null;
071: WorkflowTemplateStart start = null;
072: //DetermineValueAndClassConceptTagContext rvacfctc = null;
073: String appName = null;
074:
075: //String triggerData = trigger.getDataAsString();
076:
077: //if (trigger.getWorkflowId() == null) {
078: // trigger = TriggerDataFactory.newTrigger(triggerData, inTc);
079: //}
080: //else {
081: // WorkflowInstance w =
082: // inWl.retrieveWorkflowInstance(inTc.getWorkflowId());
083: // if (!w.getStateCode().equals(inTc.getWorkflowStateCode())) {
084: // throw new UnexpectedSystemException(
085: // "Trigger for workflow '" + inTc.getWorkflowId()
086: // + "' has already been resubmitted");
087: // }
088: // trigger = TriggerDataFactory.newTrigger(triggerData,
089: // w.getTriggerData(), inTc);
090: //}
091:
092: if (trigger.getWorkflowId() != null) {
093: throw new UnexpectedSystemException(
094: "Trigger for workflow '" + trigger.getWorkflowId()
095: + "' has already been resubmitted");
096: }
097:
098: WorkflowInstancePrecursor wp = null;
099:
100: if (!trigger.isAuditExceptionPresent()) {
101: ProjectScript ws[] = inWl.getDeployedScripts();
102:
103: selectProcessMapAndStart: for (int i = 0; i < ws.length; i++) {
104:
105: ProjectScript nextScript = ws[i];
106: Document doc = XMLHelper.loadDocument(
107: nextScript.getContent());
108:
109: RootConceptTagHolder rth =
110: WorkflowTemplateTagHelper.getRootConceptTagHolder(
111: doc, null, trigger, nextScript.getName());
112: ConceptTagPreferences localCtp =
113: rth.getConceptTagPreferences();
114: DetermineValueAndClassConceptTagContext rvacfctc =
115: localCtp.getDetermineValueAndClassConceptTagContext();
116: ConceptTagCharacteristic tc =
117: rth.getConceptTagCharacteristic();
118: Object ccto = tc.getValueAsCoreConceptTag();
119: WorkflowTemplateScriptTag pm =
120: (WorkflowTemplateScriptTag)ccto;
121: //StartsTag starts = (StartsTag)pm.getStarts();
122:
123: for (int j = 0; j < pm.getStartCount(); j++) {
124: StartScriptTag currentStart = pm.getStart(j);
125: //TO DO: trigger, attribute set part of rvacc
126: //rvacc =
127: //ScriptTagHelper.createdetermineValueAndClassContext(
128: // null, trigger, nextScript.getName());
129: boolean result =
130: isStartActive(currentStart, rvacfctc, trigger);
131: if (trigger.isAuditExceptionPresent()) {
132: break selectProcessMapAndStart;
133: }
134: else if (result) {
135: processMap = pm;
136: rootTagHolder = rth;
137: start = currentStart;
138: //set application name
139: appName = nextScript.getName();
140: break selectProcessMapAndStart;
141: }
142: }
143: }
144: if (trigger.isAuditExceptionPresent() || processMap == null
145: || start == null) {
146: trigger.setAuditException(
147: new UnexpectedSystemException(
148: "Unable to find active 'start'"));
149: }
150:
151: ConceptTagPreferences localCtp =
152: rootTagHolder.getConceptTagPreferences();
153: DetermineValueAndClassConceptTagContext rvacfctc =
154: localCtp.getDetermineValueAndClassConceptTagContext();
155:
156: attributeSet = AttributeSetFactory.newAttributeSet();
157: initializeAttributes(
158: processMap, attributeSet, rvacfctc, trigger);
159: }
160:
161: if (!trigger.isAuditExceptionPresent()) {
162:
163: ConceptTagPreferences localCtp =
164: rootTagHolder.getConceptTagPreferences();
165: DetermineValueAndClassConceptTagContext rvacfctc =
166: localCtp.getDetermineValueAndClassConceptTagContext();
167:
168: SecurityAudit wa = SecurityAuditFactory.newSecurityAudit(
169: trigger.getAuditUser(), SecurityAudit.INITIAL_ID,
170: "Trigger Engine Initialized", TraceAudit.STATUS_ACTIVE,
171: MiscHelper.currentGMT());
172: //initialize history
173: history = HistoryAuditFactory.createHistoryAudit(start,
174: rvacfctc, wa);
175:
176: //set workflow precursor attributes here
177:
178: //TO DO: try to use below function instead at some point
179: //VersionHelper.initializeSystemAttributes(wp);
180:
181: //initialize all system parameters, except ID
182:
183: TraceAudit currentTrace =
184: history.getOneTraceByStatus(TraceAudit.STATUS_ACTIVE);
185: //MiscHelper.println("currentTrace = " + currentTrace);
186: //MiscHelper.println("currentTrace,id = "
187: // + currentTrace.getId());
188:
189: attributeSet.addPublicSysAttr(AttributeSet.STATUS,
190: ValueAndClassForConceptTag.newInstance(
191: TraceAudit.STATUS_ACTIVE, String.class));
192: attributeSet.addPublicSysAttr(AttributeSet.APP,
193: ValueAndClassForConceptTag.newInstance(
194: appName, String.class));
195: //as.addPublicSysAttr(AttributeSet.APP_VERSION,
196: // ValueAndClassFactory.newValueAndClass("",String.class));
197: attributeSet.addPublicSysAttr(AttributeSet.WAIT,
198: ValueAndClassForConceptTag.newInstance(
199: WaitHelper.NO_WAIT, String.class));
200: attributeSet.addPublicSysAttr(AttributeSet.CURRENT_TRACE,
201: ValueAndClassForConceptTag.newInstance(
202: new BigDecimal(currentTrace.getId()),
203: BigDecimal.class));
204: attributeSet.addPublicSysAttr(AttributeSet.STEP_COUNT,
205: ValueAndClassForConceptTag.newInstance(
206: new BigDecimal(history.getTotalRecordCount()),
207: BigDecimal.class));
208:
209: wp = WorkflowInstancePrecursorFactory.newWorkflowPrecursor(
210: trigger, attributeSet, rootTagHolder, history);
211: }
212: else {
213: throw UnexpectedSystemException.notImplemented();
214: //wp = WorkflowInstancePrecursorFactory.newWorkflowPrecursor(
215: // inTc, trigger);
216: }
217:
218:
219: inWl.startWorkflowInstance(wp);
220: trigger.setAuditStatus(TriggerData.PROCESSED);
221: inWl.updateTriggerDataStatus(inHandle, trigger);
222: }
223: catch (ParserConfigurationException pce) {
224: throw new UnexpectedSystemException(pce);
225: }
226: catch (SAXException saxe) {
227: throw new UnexpectedSystemException(saxe);
228: }
229: catch (IOException ioe) {
230: throw new UnexpectedSystemException(ioe);
231: }
232: */
233: }
234:
235: //private final static boolean isStartActive(StartScriptTag inSt,
236: // DetermineValueAndClassConceptTagContext inRvacfctc,
237: // TriggerData inTrigger) {
238: //
239: // boolean outValue = false;
240: //
241: // try {
242: // outValue = inSt.isTriggerConditionActive(inRvacfctc);
243: // }
244: // catch (DetermineValueAndClassConceptTagException rste) {
245: // outValue = false;
246: // inTrigger.setAuditException(rste);
247: // }
248: //
249: // return outValue;
250: //}
251:
252: //private final static void initializeAttributes(
253: // WorkflowTemplateScriptTag inPmt, AttributeSet inAs,
254: // DetermineValueAndClassConceptTagContext inRvacfctc,
255: // TriggerData inTrigger) {
256: //
257: // try {
258: // inPmt.initializeApplicationAttributeSet(inAs, inRvacfctc);
259: // }
260: // catch (DetermineValueAndClassConceptTagException rste) {
261: // inTrigger.setAuditException(rste);
262: // }
263: //}
264: }
|