0001: /*******************************************************************************
0002: * Licensed to the Apache Software Foundation (ASF) under one
0003: * or more contributor license agreements. See the NOTICE file
0004: * distributed with this work for additional information
0005: * regarding copyright ownership. The ASF licenses this file
0006: * to you under the Apache License, Version 2.0 (the
0007: * "License"); you may not use this file except in compliance
0008: * with the License. You may obtain a copy of the License at
0009: *
0010: * http://www.apache.org/licenses/LICENSE-2.0
0011: *
0012: * Unless required by applicable law or agreed to in writing,
0013: * software distributed under the License is distributed on an
0014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0015: * KIND, either express or implied. See the License for the
0016: * specific language governing permissions and limitations
0017: * under the License.
0018: *******************************************************************************/package org.ofbiz.workflow.definition;
0019:
0020: import java.io.IOException;
0021: import java.net.URL;
0022: import java.util.HashMap;
0023: import java.util.Iterator;
0024: import java.util.LinkedList;
0025: import java.util.List;
0026: import java.util.Map;
0027:
0028: import javax.xml.parsers.ParserConfigurationException;
0029:
0030: import org.ofbiz.base.util.Debug;
0031: import org.ofbiz.base.util.StringUtil;
0032: import org.ofbiz.base.util.UtilDateTime;
0033: import org.ofbiz.base.util.UtilMisc;
0034: import org.ofbiz.base.util.UtilURL;
0035: import org.ofbiz.base.util.UtilXml;
0036: import org.ofbiz.entity.GenericDelegator;
0037: import org.ofbiz.entity.GenericEntityException;
0038: import org.ofbiz.entity.GenericValue;
0039: import org.ofbiz.entity.transaction.GenericTransactionException;
0040: import org.ofbiz.entity.transaction.TransactionUtil;
0041: import org.w3c.dom.Document;
0042: import org.w3c.dom.Element;
0043: import org.xml.sax.SAXException;
0044:
0045: /**
0046: * XpdlReader - Reads Process Definition objects from XPDL
0047: */
0048: public class XpdlReader {
0049:
0050: protected GenericDelegator delegator = null;
0051: protected List values = null;
0052:
0053: public static final String module = XpdlReader.class.getName();
0054:
0055: public XpdlReader(GenericDelegator delegator) {
0056: this .delegator = delegator;
0057: }
0058:
0059: /** Imports an XPDL file at the given location and imports it into the
0060: * datasource through the given delegator */
0061: public static void importXpdl(URL location,
0062: GenericDelegator delegator)
0063: throws DefinitionParserException {
0064: List values = readXpdl(location, delegator);
0065:
0066: // attempt to start a transaction
0067: boolean beganTransaction = false;
0068: try {
0069: beganTransaction = TransactionUtil.begin();
0070: } catch (GenericTransactionException gte) {
0071: Debug.logError(gte, "Unable to begin transaction", module);
0072: }
0073:
0074: try {
0075: delegator.storeAll(values);
0076: TransactionUtil.commit(beganTransaction);
0077: } catch (GenericEntityException e) {
0078: try {
0079: // only rollback the transaction if we started one...
0080: TransactionUtil.rollback(beganTransaction,
0081: "Error importing XPDL", e);
0082: } catch (GenericEntityException e2) {
0083: Debug.logError(e2, "Problems rolling back transaction",
0084: module);
0085: }
0086: throw new DefinitionParserException(
0087: "Could not store values", e);
0088: }
0089: }
0090:
0091: /** Gets an XML file from the specified location and reads it into
0092: * GenericValue objects from the given delegator and returns them in a
0093: * List; does not write to the database, just gets the entities. */
0094: public static List readXpdl(URL location, GenericDelegator delegator)
0095: throws DefinitionParserException {
0096: if (Debug.infoOn())
0097: Debug.logInfo("Beginning XPDL File Parse: "
0098: + location.toString(), module);
0099:
0100: XpdlReader reader = new XpdlReader(delegator);
0101:
0102: try {
0103: Document document = UtilXml.readXmlDocument(location);
0104:
0105: return reader.readAll(document);
0106: } catch (ParserConfigurationException e) {
0107: Debug.logError(e, module);
0108: throw new DefinitionParserException(
0109: "Could not configure XML reader", e);
0110: } catch (SAXException e) {
0111: Debug.logError(e, module);
0112: throw new DefinitionParserException(
0113: "Could not parse XML (invalid?)", e);
0114: } catch (IOException e) {
0115: Debug.logError(e, module);
0116: throw new DefinitionParserException("Could not load file",
0117: e);
0118: }
0119: }
0120:
0121: public List readAll(Document document)
0122: throws DefinitionParserException {
0123: values = new LinkedList();
0124: Element docElement;
0125:
0126: docElement = document.getDocumentElement();
0127: // read the package element, and everything under it
0128: // puts everything in the values list for returning, etc later
0129: readPackage(docElement);
0130:
0131: return (values);
0132: }
0133:
0134: // ----------------------------------------------------------------
0135: // Package
0136: // ----------------------------------------------------------------
0137:
0138: protected void readPackage(Element packageElement)
0139: throws DefinitionParserException {
0140: if (packageElement == null)
0141: return;
0142: if (!"Package".equals(packageElement.getTagName()))
0143: throw new DefinitionParserException(
0144: "Tried to make Package from element not named Package");
0145:
0146: GenericValue packageValue = delegator.makeValue(
0147: "WorkflowPackage", null);
0148:
0149: values.add(packageValue);
0150:
0151: String packageId = packageElement.getAttribute("Id");
0152:
0153: packageValue.set("packageId", packageId);
0154: packageValue.set("packageName", packageElement
0155: .getAttribute("Name"));
0156:
0157: // PackageHeader
0158: Element packageHeaderElement = UtilXml.firstChildElement(
0159: packageElement, "PackageHeader");
0160:
0161: if (packageHeaderElement != null) {
0162: packageValue.set("specificationId", "XPDL");
0163: packageValue.set("specificationVersion", UtilXml
0164: .childElementValue(packageHeaderElement,
0165: "XPDLVersion"));
0166: packageValue.set("sourceVendorInfo", UtilXml
0167: .childElementValue(packageHeaderElement, "Vendor"));
0168: String createdStr = UtilXml.childElementValue(
0169: packageHeaderElement, "Created");
0170:
0171: if (createdStr != null) {
0172: try {
0173: packageValue.set("creationDateTime",
0174: java.sql.Timestamp.valueOf(createdStr));
0175: } catch (IllegalArgumentException e) {
0176: throw new DefinitionParserException(
0177: "Invalid Date-Time format in Package->Created: "
0178: + createdStr, e);
0179: }
0180: }
0181: packageValue.set("description", UtilXml.childElementValue(
0182: packageHeaderElement, "Description"));
0183: packageValue.set("documentationUrl", UtilXml
0184: .childElementValue(packageHeaderElement,
0185: "Documentation"));
0186: packageValue.set("priorityUomId", UtilXml
0187: .childElementValue(packageHeaderElement,
0188: "PriorityUnit"));
0189: packageValue.set("costUomId", UtilXml.childElementValue(
0190: packageHeaderElement, "CostUnit"));
0191: }
0192:
0193: // RedefinableHeader?
0194: Element redefinableHeaderElement = UtilXml.firstChildElement(
0195: packageElement, "RedefinableHeader");
0196: boolean packageOk = readRedefinableHeader(
0197: redefinableHeaderElement, packageValue, "package");
0198: String packageVersion = packageValue
0199: .getString("packageVersion");
0200:
0201: // Only do these if the package hasn't been imported.
0202: if (packageOk) {
0203: // ConformanceClass?
0204: Element conformanceClassElement = UtilXml
0205: .firstChildElement(packageElement,
0206: "ConformanceClass");
0207:
0208: if (conformanceClassElement != null) {
0209: packageValue.set("graphConformanceEnumId", "WGC_"
0210: + conformanceClassElement
0211: .getAttribute("GraphConformance"));
0212: }
0213:
0214: // Participants?
0215: Element participantsElement = UtilXml.firstChildElement(
0216: packageElement, "Participants");
0217: List participants = UtilXml.childElementList(
0218: participantsElement, "Participant");
0219:
0220: readParticipants(participants, packageId, packageVersion,
0221: "_NA_", "_NA_", packageValue);
0222:
0223: // ExternalPackages?
0224: Element externalPackagesElement = UtilXml
0225: .firstChildElement(packageElement,
0226: "ExternalPackages");
0227: List externalPackages = UtilXml.childElementList(
0228: externalPackagesElement, "ExternalPackage");
0229:
0230: readExternalPackages(externalPackages, packageId,
0231: packageVersion);
0232:
0233: // TypeDeclarations?
0234: Element typeDeclarationsElement = UtilXml
0235: .firstChildElement(packageElement,
0236: "TypeDeclarations");
0237: List typeDeclarations = UtilXml.childElementList(
0238: typeDeclarationsElement, "TypeDeclaration");
0239:
0240: readTypeDeclarations(typeDeclarations, packageId,
0241: packageVersion);
0242:
0243: // Applications?
0244: Element applicationsElement = UtilXml.firstChildElement(
0245: packageElement, "Applications");
0246: List applications = UtilXml.childElementList(
0247: applicationsElement, "Application");
0248:
0249: readApplications(applications, packageId, packageVersion,
0250: "_NA_", "_NA_");
0251:
0252: // DataFields?
0253: Element dataFieldsElement = UtilXml.firstChildElement(
0254: packageElement, "DataFields");
0255: List dataFields = UtilXml.childElementList(
0256: dataFieldsElement, "DataField");
0257:
0258: readDataFields(dataFields, packageId, packageVersion,
0259: "_NA_", "_NA_");
0260: } else {
0261: values = new LinkedList();
0262: }
0263:
0264: // WorkflowProcesses?
0265: Element workflowProcessesElement = UtilXml.firstChildElement(
0266: packageElement, "WorkflowProcesses");
0267: List workflowProcesses = UtilXml.childElementList(
0268: workflowProcessesElement, "WorkflowProcess");
0269:
0270: readWorkflowProcesses(workflowProcesses, packageId,
0271: packageVersion);
0272: }
0273:
0274: protected boolean readRedefinableHeader(
0275: Element redefinableHeaderElement, GenericValue valueObject,
0276: String prefix) throws DefinitionParserException {
0277: if (redefinableHeaderElement == null) {
0278: valueObject.set(prefix + "Version", UtilDateTime
0279: .nowDateString());
0280: return checkVersion(valueObject, prefix);
0281: }
0282:
0283: valueObject.set("author", UtilXml.childElementValue(
0284: redefinableHeaderElement, "Author"));
0285: valueObject.set(prefix + "Version", UtilXml.childElementValue(
0286: redefinableHeaderElement, "Version", UtilDateTime
0287: .nowDateString()));
0288: valueObject.set("codepage", UtilXml.childElementValue(
0289: redefinableHeaderElement, "Codepage"));
0290: valueObject.set("countryGeoId", UtilXml.childElementValue(
0291: redefinableHeaderElement, "Countrykey"));
0292: valueObject.set("publicationStatusId", "WPS_"
0293: + redefinableHeaderElement
0294: .getAttribute("PublicationStatus"));
0295:
0296: if (!checkVersion(valueObject, prefix))
0297: return false;
0298:
0299: // Responsibles?
0300: Element responsiblesElement = UtilXml.firstChildElement(
0301: redefinableHeaderElement, "Responsibles");
0302: List responsibles = UtilXml.childElementList(
0303: responsiblesElement, "Responsible");
0304:
0305: readResponsibles(responsibles, valueObject, prefix);
0306: return true;
0307: }
0308:
0309: private boolean checkVersion(GenericValue valueObject, String prefix) {
0310: // Test if the object already exists. If so throw an exception.
0311: try {
0312: String message = new String();
0313:
0314: if (prefix.equals("package")) {
0315: GenericValue gvCheck = valueObject
0316: .getDelegator()
0317: .findByPrimaryKey(
0318: "WorkflowPackage",
0319: UtilMisc
0320: .toMap(
0321: "packageId",
0322: valueObject
0323: .getString("packageId"),
0324: "packageVersion",
0325: valueObject
0326: .getString("packageVersion")));
0327:
0328: if (gvCheck != null) {
0329: message = "[xpdl] Package: "
0330: + valueObject.getString("packageId")
0331: + " (ver "
0332: + valueObject.getString("packageVersion")
0333: + ") has already been imported. Will not update/import.";
0334: }
0335: } else if (prefix.equals("process")) {
0336: GenericValue gvCheck = valueObject
0337: .getDelegator()
0338: .findByPrimaryKey(
0339: "WorkflowProcess",
0340: UtilMisc
0341: .toMap(
0342: "packageId",
0343: valueObject
0344: .getString("packageId"),
0345: "packageVersion",
0346: valueObject
0347: .getString("packageVersion"),
0348: "processId",
0349: valueObject
0350: .getString("processId"),
0351: "processVersion",
0352: valueObject
0353: .getString("processVersion")));
0354:
0355: if (gvCheck != null) {
0356: message = "[xpdl] Process: "
0357: + valueObject.getString("processId")
0358: + " (ver "
0359: + valueObject.getString("processVersion")
0360: + ") has already been imported. Not importing.";
0361: }
0362: }
0363: if (message.length() > 0) {
0364: StringBuffer lines = new StringBuffer();
0365:
0366: for (int i = 0; i < message.length(); i++) {
0367: lines.append("-");
0368: }
0369: Debug.logWarning(lines.toString(), module);
0370: Debug.logWarning(message, module);
0371: Debug.logWarning(lines.toString(), module);
0372: return false;
0373: }
0374: } catch (GenericEntityException e) {
0375: return false;
0376: }
0377: return true;
0378: }
0379:
0380: protected void readResponsibles(List responsibles,
0381: GenericValue valueObject, String prefix)
0382: throws DefinitionParserException {
0383: if (responsibles == null || responsibles.size() == 0) {
0384: return;
0385: }
0386:
0387: String responsibleListId = delegator
0388: .getNextSeqId("WorkflowParticipantList");
0389: valueObject.set("responsibleListId", responsibleListId);
0390:
0391: Iterator responsibleIter = responsibles.iterator();
0392: int responsibleIndex = 1;
0393:
0394: while (responsibleIter.hasNext()) {
0395: Element responsibleElement = (Element) responsibleIter
0396: .next();
0397: String responsibleId = UtilXml
0398: .elementValue(responsibleElement);
0399: GenericValue participantListValue = delegator.makeValue(
0400: "WorkflowParticipantList", null);
0401:
0402: participantListValue.set("packageId", valueObject
0403: .getString("packageId"));
0404: participantListValue.set("packageVersion", valueObject
0405: .getString("packageVersion"));
0406: participantListValue.set("participantListId",
0407: responsibleListId);
0408: participantListValue.set("participantId", responsibleId);
0409: participantListValue.set("participantIndex", new Long(
0410: responsibleIndex));
0411: if (prefix.equals("process")) {
0412: participantListValue.set("processId", valueObject
0413: .getString("processId"));
0414: participantListValue.set("processVersion", valueObject
0415: .getString("processVersion"));
0416: } else {
0417: participantListValue.set("processId", "_NA_");
0418: participantListValue.set("processVersion", "_NA_");
0419: }
0420: values.add(participantListValue);
0421: responsibleIndex++;
0422: }
0423: }
0424:
0425: protected void readExternalPackages(List externalPackages,
0426: String packageId, String packageVersion) {
0427: if (externalPackages == null || externalPackages.size() == 0)
0428: return;
0429: Iterator externalPackageIter = externalPackages.iterator();
0430:
0431: while (externalPackageIter.hasNext()) {
0432: Element externalPackageElement = (Element) externalPackageIter
0433: .next();
0434: GenericValue externalPackageValue = delegator.makeValue(
0435: "WorkflowPackageExternal", null);
0436:
0437: values.add(externalPackageValue);
0438: externalPackageValue.set("packageId", packageId);
0439: externalPackageValue.set("packageVersion", packageVersion);
0440: externalPackageValue.set("externalPackageId",
0441: externalPackageElement.getAttribute("href"));
0442: }
0443: }
0444:
0445: protected void readTypeDeclarations(List typeDeclarations,
0446: String packageId, String packageVersion)
0447: throws DefinitionParserException {
0448: if (typeDeclarations == null || typeDeclarations.size() == 0)
0449: return;
0450: Iterator typeDeclarationsIter = typeDeclarations.iterator();
0451:
0452: while (typeDeclarationsIter.hasNext()) {
0453: Element typeDeclarationElement = (Element) typeDeclarationsIter
0454: .next();
0455: GenericValue typeDeclarationValue = delegator.makeValue(
0456: "WorkflowTypeDeclaration", null);
0457:
0458: values.add(typeDeclarationValue);
0459:
0460: typeDeclarationValue.set("packageId", packageId);
0461: typeDeclarationValue.set("packageVersion", packageVersion);
0462: typeDeclarationValue.set("typeId", typeDeclarationElement
0463: .getAttribute("Id"));
0464: typeDeclarationValue.set("typeName", typeDeclarationElement
0465: .getAttribute("Name"));
0466:
0467: // (%Type;)
0468: readType(typeDeclarationElement, typeDeclarationValue);
0469:
0470: // Description?
0471: typeDeclarationValue.set("description", UtilXml
0472: .childElementValue(typeDeclarationElement,
0473: "Description"));
0474: }
0475: }
0476:
0477: // ----------------------------------------------------------------
0478: // Process
0479: // ----------------------------------------------------------------
0480:
0481: protected void readWorkflowProcesses(List workflowProcesses,
0482: String packageId, String packageVersion)
0483: throws DefinitionParserException {
0484: if (workflowProcesses == null || workflowProcesses.size() == 0)
0485: return;
0486: Iterator workflowProcessIter = workflowProcesses.iterator();
0487:
0488: while (workflowProcessIter.hasNext()) {
0489: Element workflowProcessElement = (Element) workflowProcessIter
0490: .next();
0491:
0492: readWorkflowProcess(workflowProcessElement, packageId,
0493: packageVersion);
0494: }
0495: }
0496:
0497: protected void readWorkflowProcess(Element workflowProcessElement,
0498: String packageId, String packageVersion)
0499: throws DefinitionParserException {
0500: GenericValue workflowProcessValue = delegator.makeValue(
0501: "WorkflowProcess", null);
0502:
0503: values.add(workflowProcessValue);
0504:
0505: String processId = workflowProcessElement.getAttribute("Id");
0506:
0507: workflowProcessValue.set("packageId", packageId);
0508: workflowProcessValue.set("packageVersion", packageVersion);
0509: workflowProcessValue.set("processId", processId);
0510: workflowProcessValue.set("objectName", workflowProcessElement
0511: .getAttribute("Name"));
0512:
0513: // ProcessHeader
0514: Element processHeaderElement = UtilXml.firstChildElement(
0515: workflowProcessElement, "ProcessHeader");
0516:
0517: if (processHeaderElement != null) {
0518: // TODO: add prefix to duration Unit or map it to make it a real uomId
0519: workflowProcessValue.set("durationUomId",
0520: processHeaderElement.getAttribute("DurationUnit"));
0521: String createdStr = UtilXml.childElementValue(
0522: processHeaderElement, "Created");
0523:
0524: if (createdStr != null) {
0525: try {
0526: workflowProcessValue.set("creationDateTime",
0527: java.sql.Timestamp.valueOf(createdStr));
0528: } catch (IllegalArgumentException e) {
0529: throw new DefinitionParserException(
0530: "Invalid Date-Time format in WorkflowProcess->ProcessHeader->Created: "
0531: + createdStr, e);
0532: }
0533: }
0534: workflowProcessValue.set("description", UtilXml
0535: .childElementValue(processHeaderElement,
0536: "Description"));
0537:
0538: String priorityStr = UtilXml.childElementValue(
0539: processHeaderElement, "Priority");
0540:
0541: if (priorityStr != null) {
0542: try {
0543: workflowProcessValue.set("objectPriority", Long
0544: .valueOf(priorityStr));
0545: } catch (NumberFormatException e) {
0546: throw new DefinitionParserException(
0547: "Invalid whole number format in WorkflowProcess->ProcessHeader->Priority: "
0548: + priorityStr, e);
0549: }
0550: }
0551: String limitStr = UtilXml.childElementValue(
0552: processHeaderElement, "Limit");
0553:
0554: if (limitStr != null) {
0555: try {
0556: workflowProcessValue.set("timeLimit", Double
0557: .valueOf(limitStr));
0558: } catch (NumberFormatException e) {
0559: throw new DefinitionParserException(
0560: "Invalid decimal number format in WorkflowProcess->ProcessHeader->Limit: "
0561: + limitStr, e);
0562: }
0563: }
0564:
0565: String validFromStr = UtilXml.childElementValue(
0566: processHeaderElement, "ValidFrom");
0567:
0568: if (validFromStr != null) {
0569: try {
0570: workflowProcessValue.set("validFromDate",
0571: java.sql.Timestamp.valueOf(validFromStr));
0572: } catch (IllegalArgumentException e) {
0573: throw new DefinitionParserException(
0574: "Invalid Date-Time format in WorkflowProcess->ProcessHeader->ValidFrom: "
0575: + validFromStr, e);
0576: }
0577: }
0578: String validToStr = UtilXml.childElementValue(
0579: processHeaderElement, "ValidTo");
0580:
0581: if (validToStr != null) {
0582: try {
0583: workflowProcessValue.set("validToDate",
0584: java.sql.Timestamp.valueOf(validToStr));
0585: } catch (IllegalArgumentException e) {
0586: throw new DefinitionParserException(
0587: "Invalid Date-Time format in WorkflowProcess->ProcessHeader->ValidTo: "
0588: + validToStr, e);
0589: }
0590: }
0591:
0592: // TimeEstimation?
0593: Element timeEstimationElement = UtilXml.firstChildElement(
0594: processHeaderElement, "TimeEstimation");
0595:
0596: if (timeEstimationElement != null) {
0597: String waitingTimeStr = UtilXml.childElementValue(
0598: timeEstimationElement, "WaitingTime");
0599:
0600: if (waitingTimeStr != null) {
0601: try {
0602: workflowProcessValue.set("waitingTime", Double
0603: .valueOf(waitingTimeStr));
0604: } catch (NumberFormatException e) {
0605: throw new DefinitionParserException(
0606: "Invalid decimal number format in WorkflowProcess->ProcessHeader->TimeEstimation->WaitingTime: "
0607: + waitingTimeStr, e);
0608: }
0609: }
0610: String workingTimeStr = UtilXml.childElementValue(
0611: timeEstimationElement, "WorkingTime");
0612:
0613: if (workingTimeStr != null) {
0614: try {
0615: workflowProcessValue.set("waitingTime", Double
0616: .valueOf(workingTimeStr));
0617: } catch (NumberFormatException e) {
0618: throw new DefinitionParserException(
0619: "Invalid decimal number format in WorkflowProcess->ProcessHeader->TimeEstimation->WorkingTime: "
0620: + workingTimeStr, e);
0621: }
0622: }
0623: String durationStr = UtilXml.childElementValue(
0624: timeEstimationElement, "Duration");
0625:
0626: if (durationStr != null) {
0627: try {
0628: workflowProcessValue.set("duration", Double
0629: .valueOf(durationStr));
0630: } catch (NumberFormatException e) {
0631: throw new DefinitionParserException(
0632: "Invalid decimal number format in WorkflowProcess->ProcessHeader->TimeEstimation->Duration: "
0633: + durationStr, e);
0634: }
0635: }
0636: }
0637: }
0638:
0639: // RedefinableHeader?
0640: Element redefinableHeaderElement = UtilXml.firstChildElement(
0641: workflowProcessElement, "RedefinableHeader");
0642: boolean processOk = readRedefinableHeader(
0643: redefinableHeaderElement, workflowProcessValue,
0644: "process");
0645: String processVersion = workflowProcessValue
0646: .getString("processVersion");
0647:
0648: if (!processOk) {
0649: values.remove(workflowProcessValue);
0650: return;
0651: }
0652:
0653: // FormalParameters?
0654: Element formalParametersElement = UtilXml.firstChildElement(
0655: workflowProcessElement, "FormalParameters");
0656: List formalParameters = UtilXml.childElementList(
0657: formalParametersElement, "FormalParameter");
0658:
0659: readFormalParameters(formalParameters, packageId,
0660: packageVersion, processId, processVersion, "_NA_");
0661:
0662: // (%Type;)* TODO
0663:
0664: // DataFields?
0665: Element dataFieldsElement = UtilXml.firstChildElement(
0666: workflowProcessElement, "DataFields");
0667: List dataFields = UtilXml.childElementList(dataFieldsElement,
0668: "DataField");
0669:
0670: readDataFields(dataFields, packageId, packageVersion,
0671: processId, processVersion);
0672:
0673: // Participants?
0674: Element participantsElement = UtilXml.firstChildElement(
0675: workflowProcessElement, "Participants");
0676: List participants = UtilXml.childElementList(
0677: participantsElement, "Participant");
0678:
0679: readParticipants(participants, packageId, packageVersion,
0680: processId, processVersion, workflowProcessValue);
0681:
0682: // Applications?
0683: Element applicationsElement = UtilXml.firstChildElement(
0684: workflowProcessElement, "Applications");
0685: List applications = UtilXml.childElementList(
0686: applicationsElement, "Application");
0687:
0688: readApplications(applications, packageId, packageVersion,
0689: processId, processVersion);
0690:
0691: // Activities
0692: Element activitiesElement = UtilXml.firstChildElement(
0693: workflowProcessElement, "Activities");
0694: List activities = UtilXml.childElementList(activitiesElement,
0695: "Activity");
0696:
0697: readActivities(activities, packageId, packageVersion,
0698: processId, processVersion, workflowProcessValue);
0699:
0700: // Transitions
0701: Element transitionsElement = UtilXml.firstChildElement(
0702: workflowProcessElement, "Transitions");
0703: List transitions = UtilXml.childElementList(transitionsElement,
0704: "Transition");
0705:
0706: readTransitions(transitions, packageId, packageVersion,
0707: processId, processVersion);
0708:
0709: // ExtendedAttributes?
0710: workflowProcessValue.set("defaultStartActivityId",
0711: getExtendedAttributeValue(workflowProcessElement,
0712: "defaultStartActivityId", workflowProcessValue
0713: .getString("defaultStartActivityId")));
0714: workflowProcessValue.set("sourceReferenceField",
0715: getExtendedAttributeValue(workflowProcessElement,
0716: "sourceReferenceField", "sourceReferenceId"));
0717: }
0718:
0719: // ----------------------------------------------------------------
0720: // Activity
0721: // ----------------------------------------------------------------
0722:
0723: protected void readActivities(List activities, String packageId,
0724: String packageVersion, String processId,
0725: String processVersion, GenericValue processValue)
0726: throws DefinitionParserException {
0727: if (activities == null || activities.size() == 0)
0728: return;
0729: Iterator activitiesIter = activities.iterator();
0730:
0731: // do the first one differently because it will be the defaultStart activity
0732: if (activitiesIter.hasNext()) {
0733: Element activityElement = (Element) activitiesIter.next();
0734: String activityId = activityElement.getAttribute("Id");
0735:
0736: processValue.set("defaultStartActivityId", activityId);
0737: readActivity(activityElement, packageId, packageVersion,
0738: processId, processVersion);
0739: }
0740:
0741: while (activitiesIter.hasNext()) {
0742: Element activityElement = (Element) activitiesIter.next();
0743:
0744: readActivity(activityElement, packageId, packageVersion,
0745: processId, processVersion);
0746: }
0747: }
0748:
0749: protected void readActivity(Element activityElement,
0750: String packageId, String packageVersion, String processId,
0751: String processVersion) throws DefinitionParserException {
0752: if (activityElement == null)
0753: return;
0754:
0755: GenericValue activityValue = delegator.makeValue(
0756: "WorkflowActivity", null);
0757:
0758: values.add(activityValue);
0759:
0760: String activityId = activityElement.getAttribute("Id");
0761:
0762: activityValue.set("packageId", packageId);
0763: activityValue.set("packageVersion", packageVersion);
0764: activityValue.set("processId", processId);
0765: activityValue.set("processVersion", processVersion);
0766: activityValue.set("activityId", activityId);
0767: activityValue.set("objectName", activityElement
0768: .getAttribute("Name"));
0769:
0770: activityValue.set("description", UtilXml.childElementValue(
0771: activityElement, "Description"));
0772: String limitStr = UtilXml.childElementValue(activityElement,
0773: "Limit");
0774:
0775: if (limitStr != null) {
0776: try {
0777: activityValue
0778: .set("timeLimit", Double.valueOf(limitStr));
0779: } catch (NumberFormatException e) {
0780: throw new DefinitionParserException(
0781: "Invalid decimal number format in Activity->Limit: "
0782: + limitStr, e);
0783: }
0784: }
0785:
0786: // (Route | Implementation)
0787: Element routeElement = UtilXml.firstChildElement(
0788: activityElement, "Route");
0789: Element implementationElement = UtilXml.firstChildElement(
0790: activityElement, "Implementation");
0791:
0792: if (routeElement != null) {
0793: activityValue.set("activityTypeEnumId", "WAT_ROUTE");
0794: } else if (implementationElement != null) {
0795: Element noElement = UtilXml.firstChildElement(
0796: implementationElement, "No");
0797: Element subFlowElement = UtilXml.firstChildElement(
0798: implementationElement, "SubFlow");
0799: Element loopElement = UtilXml.firstChildElement(
0800: implementationElement, "Loop");
0801: List tools = UtilXml.childElementList(
0802: implementationElement, "Tool");
0803:
0804: if (noElement != null) {
0805: activityValue.set("activityTypeEnumId", "WAT_NO");
0806: } else if (subFlowElement != null) {
0807: activityValue.set("activityTypeEnumId", "WAT_SUBFLOW");
0808: readSubFlow(subFlowElement, packageId, packageVersion,
0809: processId, processVersion, activityId);
0810: } else if (loopElement != null) {
0811: activityValue.set("activityTypeEnumId", "WAT_LOOP");
0812: readLoop(loopElement, packageId, packageVersion,
0813: processId, processVersion, activityId);
0814: } else if (tools != null && tools.size() > 0) {
0815: activityValue.set("activityTypeEnumId", "WAT_TOOL");
0816: readTools(tools, packageId, packageVersion, processId,
0817: processVersion, activityId);
0818: } else {
0819: throw new DefinitionParserException(
0820: "No, SubFlow, Loop or one or more Tool elements must exist under the Implementation element of Activity with ID "
0821: + activityId
0822: + " in Process with ID "
0823: + processId);
0824: }
0825: } else {
0826: throw new DefinitionParserException(
0827: "Route or Implementation must exist for Activity with ID "
0828: + activityId + " in Process with ID "
0829: + processId);
0830: }
0831:
0832: // Performer?
0833: activityValue.set("performerParticipantId", UtilXml
0834: .childElementValue(activityElement, "Performer"));
0835:
0836: // StartMode?
0837: Element startModeElement = UtilXml.firstChildElement(
0838: activityElement, "StartMode");
0839:
0840: if (startModeElement != null) {
0841: if (UtilXml
0842: .firstChildElement(startModeElement, "Automatic") != null)
0843: activityValue.set("startModeEnumId", "WAM_AUTOMATIC");
0844: else if (UtilXml.firstChildElement(startModeElement,
0845: "Manual") != null)
0846: activityValue.set("startModeEnumId", "WAM_MANUAL");
0847: else
0848: throw new DefinitionParserException(
0849: "Could not find Mode under StartMode");
0850: }
0851:
0852: // FinishMode?
0853: Element finishModeElement = UtilXml.firstChildElement(
0854: activityElement, "FinishMode");
0855:
0856: if (finishModeElement != null) {
0857: if (UtilXml.firstChildElement(finishModeElement,
0858: "Automatic") != null)
0859: activityValue.set("finishModeEnumId", "WAM_AUTOMATIC");
0860: else if (UtilXml.firstChildElement(finishModeElement,
0861: "Manual") != null)
0862: activityValue.set("finishModeEnumId", "WAM_MANUAL");
0863: else
0864: throw new DefinitionParserException(
0865: "Could not find Mode under FinishMode");
0866: }
0867:
0868: // Priority?
0869: String priorityStr = UtilXml.childElementValue(activityElement,
0870: "Priority");
0871:
0872: if (priorityStr != null) {
0873: try {
0874: activityValue.set("objectPriority", Long
0875: .valueOf(priorityStr));
0876: } catch (NumberFormatException e) {
0877: throw new DefinitionParserException(
0878: "Invalid whole number format in Activity->Priority: "
0879: + priorityStr, e);
0880: }
0881: }
0882:
0883: // SimulationInformation?
0884: Element simulationInformationElement = UtilXml
0885: .firstChildElement(activityElement,
0886: "SimulationInformation");
0887:
0888: if (simulationInformationElement != null) {
0889: if (simulationInformationElement
0890: .getAttribute("Instantiation") != null)
0891: activityValue.set("instantiationLimitEnumId", "WFI_"
0892: + simulationInformationElement
0893: .getAttribute("Instantiation"));
0894: String costStr = UtilXml.childElementValue(
0895: simulationInformationElement, "Cost");
0896:
0897: if (costStr != null) {
0898: try {
0899: activityValue.set("cost", Double.valueOf(costStr));
0900: } catch (NumberFormatException e) {
0901: throw new DefinitionParserException(
0902: "Invalid decimal number format in Activity->SimulationInformation->Cost: "
0903: + costStr, e);
0904: }
0905: }
0906:
0907: // TimeEstimation
0908: Element timeEstimationElement = UtilXml.firstChildElement(
0909: simulationInformationElement, "TimeEstimation");
0910:
0911: if (timeEstimationElement != null) {
0912: String waitingTimeStr = UtilXml.childElementValue(
0913: timeEstimationElement, "WaitingTime");
0914:
0915: if (waitingTimeStr != null) {
0916: try {
0917: activityValue.set("waitingTime", Double
0918: .valueOf(waitingTimeStr));
0919: } catch (NumberFormatException e) {
0920: throw new DefinitionParserException(
0921: "Invalid decimal number format in Activity->SimulationInformation->TimeEstimation->WaitingTime: "
0922: + waitingTimeStr, e);
0923: }
0924: }
0925: String workingTimeStr = UtilXml.childElementValue(
0926: timeEstimationElement, "WorkingTime");
0927:
0928: if (workingTimeStr != null) {
0929: try {
0930: activityValue.set("waitingTime", Double
0931: .valueOf(workingTimeStr));
0932: } catch (NumberFormatException e) {
0933: throw new DefinitionParserException(
0934: "Invalid decimal number format in Activity->SimulationInformation->TimeEstimation->WorkingTime: "
0935: + workingTimeStr, e);
0936: }
0937: }
0938: String durationStr = UtilXml.childElementValue(
0939: timeEstimationElement, "Duration");
0940:
0941: if (durationStr != null) {
0942: try {
0943: activityValue.set("duration", Double
0944: .valueOf(durationStr));
0945: } catch (NumberFormatException e) {
0946: throw new DefinitionParserException(
0947: "Invalid decimal number format in Activity->SimulationInformation->TimeEstimation->Duration: "
0948: + durationStr, e);
0949: }
0950: }
0951: }
0952: }
0953:
0954: activityValue.set("iconUrl", UtilXml.childElementValue(
0955: activityElement, "Icon"));
0956: activityValue.set("documentationUrl", UtilXml
0957: .childElementValue(activityElement, "Documentation"));
0958:
0959: // TransitionRestrictions?
0960: Element transitionRestrictionsElement = UtilXml
0961: .firstChildElement(activityElement,
0962: "TransitionRestrictions");
0963: List transitionRestrictions = UtilXml.childElementList(
0964: transitionRestrictionsElement, "TransitionRestriction");
0965:
0966: readTransitionRestrictions(transitionRestrictions,
0967: activityValue);
0968:
0969: // ExtendedAttributes?
0970: activityValue.set("acceptAllAssignments",
0971: getExtendedAttributeValue(activityElement,
0972: "acceptAllAssignments", "N"));
0973: activityValue.set("completeAllAssignments",
0974: getExtendedAttributeValue(activityElement,
0975: "completeAllAssignments", "N"));
0976: activityValue.set("limitService", getExtendedAttributeValue(
0977: activityElement, "limitService", null), false);
0978: activityValue.set("limitAfterStart", getExtendedAttributeValue(
0979: activityElement, "limitAfterStart", "Y"));
0980: activityValue.set("restartOnDelegate",
0981: getExtendedAttributeValue(activityElement,
0982: "restartOnDelegate", "N"));
0983: activityValue.set("delegateAfterStart",
0984: getExtendedAttributeValue(activityElement,
0985: "delegateAfterStart", "Y"));
0986: activityValue.set("inheritPriority", getExtendedAttributeValue(
0987: activityElement, "inheritPriority", "N"));
0988: activityValue.set("canStart", getExtendedAttributeValue(
0989: activityElement, "canStart", "Y"));
0990: }
0991:
0992: protected void readSubFlow(Element subFlowElement,
0993: String packageId, String packageVersion, String processId,
0994: String processVersion, String activityId)
0995: throws DefinitionParserException {
0996: if (subFlowElement == null)
0997: return;
0998:
0999: GenericValue subFlowValue = delegator.makeValue(
1000: "WorkflowActivitySubFlow", null);
1001:
1002: values.add(subFlowValue);
1003:
1004: subFlowValue.set("packageId", packageId);
1005: subFlowValue.set("packageVersion", packageVersion);
1006: subFlowValue.set("processId", processId);
1007: subFlowValue.set("processVersion", processVersion);
1008: subFlowValue.set("activityId", activityId);
1009: subFlowValue.set("subFlowProcessId", subFlowElement
1010: .getAttribute("Id"));
1011:
1012: if (subFlowElement.getAttribute("Execution") != null)
1013: subFlowValue.set("executionEnumId", "WSE_"
1014: + subFlowElement.getAttribute("Execution"));
1015: else
1016: subFlowValue.set("executionEnumId", "WSE_ASYNCHR");
1017:
1018: // ActualParameters?
1019: Element actualParametersElement = UtilXml.firstChildElement(
1020: subFlowElement, "ActualParameters");
1021: List actualParameters = UtilXml.childElementList(
1022: actualParametersElement, "ActualParameter");
1023:
1024: subFlowValue.set("actualParameters",
1025: readActualParameters(actualParameters), false);
1026: }
1027:
1028: protected void readLoop(Element loopElement, String packageId,
1029: String packageVersion, String processId,
1030: String processVersion, String activityId)
1031: throws DefinitionParserException {
1032: if (loopElement == null)
1033: return;
1034:
1035: GenericValue loopValue = delegator.makeValue(
1036: "WorkflowActivityLoop", null);
1037:
1038: values.add(loopValue);
1039:
1040: loopValue.set("packageId", packageId);
1041: loopValue.set("packageVersion", packageVersion);
1042: loopValue.set("processId", processId);
1043: loopValue.set("processVersion", processVersion);
1044: loopValue.set("activityId", activityId);
1045:
1046: if (loopElement.getAttribute("Kind") != null)
1047: loopValue.set("loopKindEnumId", "WLK_"
1048: + loopElement.getAttribute("Kind"));
1049: else
1050: loopValue.set("loopKindEnumId", "WLK_WHILE");
1051:
1052: // Condition?
1053: loopValue.set("conditionExpr", UtilXml.childElementValue(
1054: loopElement, "Condition"));
1055: }
1056:
1057: protected void readTools(List tools, String packageId,
1058: String packageVersion, String processId,
1059: String processVersion, String activityId)
1060: throws DefinitionParserException {
1061: if (tools == null || tools.size() == 0)
1062: return;
1063: Iterator toolsIter = tools.iterator();
1064:
1065: while (toolsIter.hasNext()) {
1066: Element toolElement = (Element) toolsIter.next();
1067:
1068: readTool(toolElement, packageId, packageVersion, processId,
1069: processVersion, activityId);
1070: }
1071: }
1072:
1073: protected void readTool(Element toolElement, String packageId,
1074: String packageVersion, String processId,
1075: String processVersion, String activityId)
1076: throws DefinitionParserException {
1077: if (toolElement == null)
1078: return;
1079:
1080: GenericValue toolValue = delegator.makeValue(
1081: "WorkflowActivityTool", null);
1082:
1083: values.add(toolValue);
1084:
1085: toolValue.set("packageId", packageId);
1086: toolValue.set("packageVersion", packageVersion);
1087: toolValue.set("processId", processId);
1088: toolValue.set("processVersion", processVersion);
1089: toolValue.set("activityId", activityId);
1090: toolValue.set("toolId", toolElement.getAttribute("Id"));
1091:
1092: if (toolElement.getAttribute("Type") != null)
1093: toolValue.set("toolTypeEnumId", "WTT_"
1094: + toolElement.getAttribute("Type"));
1095: else
1096: toolValue.set("toolTypeEnumId", "WTT_PROCEDURE");
1097:
1098: // Description?
1099: toolValue.set("description", UtilXml.childElementValue(
1100: toolElement, "Description"));
1101:
1102: // ActualParameters/ExtendedAttributes?
1103: Element actualParametersElement = UtilXml.firstChildElement(
1104: toolElement, "ActualParameters");
1105: Element extendedAttributesElement = UtilXml.firstChildElement(
1106: toolElement, "ExtendedAttributes");
1107: List actualParameters = UtilXml.childElementList(
1108: actualParametersElement, "ActualParameter");
1109: List extendedAttributes = UtilXml.childElementList(
1110: extendedAttributesElement, "ExtendedAttribute");
1111:
1112: toolValue.set("actualParameters",
1113: readActualParameters(actualParameters), false);
1114: toolValue.set("extendedAttributes",
1115: readExtendedAttributes(extendedAttributes), false);
1116: }
1117:
1118: protected String readActualParameters(List actualParameters) {
1119: if (actualParameters == null || actualParameters.size() == 0)
1120: return null;
1121: StringBuffer actualParametersBuf = new StringBuffer();
1122: Iterator actualParametersIter = actualParameters.iterator();
1123:
1124: while (actualParametersIter.hasNext()) {
1125: Element actualParameterElement = (Element) actualParametersIter
1126: .next();
1127:
1128: actualParametersBuf.append(UtilXml
1129: .elementValue(actualParameterElement));
1130: if (actualParametersIter.hasNext())
1131: actualParametersBuf.append(',');
1132: }
1133: return actualParametersBuf.toString();
1134: }
1135:
1136: protected String readExtendedAttributes(List extendedAttributes) {
1137: if (extendedAttributes == null
1138: || extendedAttributes.size() == 0)
1139: return null;
1140: Map ea = new HashMap();
1141: Iterator i = extendedAttributes.iterator();
1142:
1143: while (i.hasNext()) {
1144: Element e = (Element) i.next();
1145:
1146: ea.put(e.getAttribute("Name"), e.getAttribute("Value"));
1147: }
1148: return StringUtil.mapToStr(ea);
1149: }
1150:
1151: // ----------------------------------------------------------------
1152: // Transition
1153: // ----------------------------------------------------------------
1154:
1155: protected void readTransitions(List transitions, String packageId,
1156: String packageVersion, String processId,
1157: String processVersion) throws DefinitionParserException {
1158: if (transitions == null || transitions.size() == 0)
1159: return;
1160: Iterator transitionsIter = transitions.iterator();
1161:
1162: while (transitionsIter.hasNext()) {
1163: Element transitionElement = (Element) transitionsIter
1164: .next();
1165:
1166: readTransition(transitionElement, packageId,
1167: packageVersion, processId, processVersion);
1168: }
1169: }
1170:
1171: protected void readTransition(Element transitionElement,
1172: String packageId, String packageVersion, String processId,
1173: String processVersion) throws DefinitionParserException {
1174: if (transitionElement == null)
1175: return;
1176:
1177: GenericValue transitionValue = delegator.makeValue(
1178: "WorkflowTransition", null);
1179:
1180: values.add(transitionValue);
1181:
1182: String transitionId = transitionElement.getAttribute("Id");
1183:
1184: transitionValue.set("packageId", packageId);
1185: transitionValue.set("packageVersion", packageVersion);
1186: transitionValue.set("processId", processId);
1187: transitionValue.set("processVersion", processVersion);
1188: transitionValue.set("transitionId", transitionId);
1189: transitionValue.set("fromActivityId", transitionElement
1190: .getAttribute("From"));
1191: transitionValue.set("toActivityId", transitionElement
1192: .getAttribute("To"));
1193:
1194: if (transitionElement.getAttribute("Loop") != null
1195: && transitionElement.getAttribute("Loop").length() > 0)
1196: transitionValue.set("loopTypeEnumId", "WTL_"
1197: + transitionElement.getAttribute("Loop"));
1198: else
1199: transitionValue.set("loopTypeEnumId", "WTL_NOLOOP");
1200:
1201: transitionValue.set("transitionName", transitionElement
1202: .getAttribute("Name"));
1203:
1204: // Condition?
1205: Element conditionElement = UtilXml.firstChildElement(
1206: transitionElement, "Condition");
1207:
1208: if (conditionElement != null) {
1209: if (conditionElement.getAttribute("Type") != null)
1210: transitionValue.set("conditionTypeEnumId", "WTC_"
1211: + conditionElement.getAttribute("Type"));
1212: else
1213: transitionValue.set("conditionTypeEnumId",
1214: "WTC_CONDITION");
1215:
1216: // a Condition will have either a list of XPression elements, or plain PCDATA
1217: List xPressions = UtilXml.childElementList(
1218: conditionElement, "XPression");
1219:
1220: if (xPressions != null && xPressions.size() > 0) {
1221: throw new DefinitionParserException(
1222: "XPression elements under Condition not yet supported, just use text inside Condition with the expression");
1223: } else {
1224: transitionValue.set("conditionExpr", UtilXml
1225: .elementValue(conditionElement));
1226: }
1227: }
1228:
1229: // Description?
1230: transitionValue.set("description", UtilXml.childElementValue(
1231: transitionElement, "Description"));
1232:
1233: // ExtendedAttributes?
1234: Element extendedAttributesElement = UtilXml.firstChildElement(
1235: transitionElement, "ExtendedAttributes");
1236: List extendedAttributes = UtilXml.childElementList(
1237: extendedAttributesElement, "ExtendedAttribute");
1238: transitionValue.set("extendedAttributes",
1239: readExtendedAttributes(extendedAttributes), false);
1240: }
1241:
1242: protected void readTransitionRestrictions(
1243: List transitionRestrictions, GenericValue activityValue)
1244: throws DefinitionParserException {
1245: if (transitionRestrictions == null
1246: || transitionRestrictions.size() == 0)
1247: return;
1248: Iterator transitionRestrictionsIter = transitionRestrictions
1249: .iterator();
1250:
1251: if (transitionRestrictionsIter.hasNext()) {
1252: Element transitionRestrictionElement = (Element) transitionRestrictionsIter
1253: .next();
1254:
1255: readTransitionRestriction(transitionRestrictionElement,
1256: activityValue);
1257: }
1258: if (transitionRestrictionsIter.hasNext()) {
1259: throw new DefinitionParserException(
1260: "Multiple TransitionRestriction elements found, this is not currently supported. Please remove extras.");
1261: }
1262: }
1263:
1264: protected void readTransitionRestriction(
1265: Element transitionRestrictionElement,
1266: GenericValue activityValue)
1267: throws DefinitionParserException {
1268: String packageId = activityValue.getString("packageId");
1269: String packageVersion = activityValue
1270: .getString("packageVersion");
1271: String processId = activityValue.getString("processId");
1272: String processVersion = activityValue
1273: .getString("processVersion");
1274: String activityId = activityValue.getString("activityId");
1275:
1276: // InlineBlock?
1277: Element inlineBlockElement = UtilXml.firstChildElement(
1278: transitionRestrictionElement, "InlineBlock");
1279:
1280: if (inlineBlockElement != null) {
1281: activityValue.set("isInlineBlock", "Y");
1282: activityValue.set("blockName", UtilXml.childElementValue(
1283: inlineBlockElement, "BlockName"));
1284: activityValue.set("blockDescription", UtilXml
1285: .childElementValue(inlineBlockElement,
1286: "Description"));
1287: activityValue.set("blockIconUrl", UtilXml
1288: .childElementValue(inlineBlockElement, "Icon"));
1289: activityValue.set("blockDocumentationUrl", UtilXml
1290: .childElementValue(inlineBlockElement,
1291: "Documentation"));
1292:
1293: activityValue.set("blockBeginActivityId",
1294: inlineBlockElement.getAttribute("Begin"));
1295: activityValue.set("blockEndActivityId", inlineBlockElement
1296: .getAttribute("End"));
1297: }
1298:
1299: // Join?
1300: Element joinElement = UtilXml.firstChildElement(
1301: transitionRestrictionElement, "Join");
1302:
1303: if (joinElement != null) {
1304: String joinType = joinElement.getAttribute("Type");
1305:
1306: if (joinType != null && joinType.length() > 0) {
1307: activityValue.set("joinTypeEnumId", "WJT_" + joinType);
1308: }
1309: }
1310:
1311: // Split?
1312: Element splitElement = UtilXml.firstChildElement(
1313: transitionRestrictionElement, "Split");
1314:
1315: if (splitElement != null) {
1316: String splitType = splitElement.getAttribute("Type");
1317:
1318: if (splitType != null && splitType.length() > 0) {
1319: activityValue
1320: .set("splitTypeEnumId", "WST_" + splitType);
1321: }
1322:
1323: // TransitionRefs
1324: Element transitionRefsElement = UtilXml.firstChildElement(
1325: splitElement, "TransitionRefs");
1326: List transitionRefs = UtilXml.childElementList(
1327: transitionRefsElement, "TransitionRef");
1328:
1329: readTransitionRefs(transitionRefs, packageId,
1330: packageVersion, processId, processVersion,
1331: activityId);
1332: }
1333: }
1334:
1335: protected void readTransitionRefs(List transitionRefs,
1336: String packageId, String packageVersion, String processId,
1337: String processVersion, String activityId)
1338: throws DefinitionParserException {
1339: if (transitionRefs == null || transitionRefs.size() == 0)
1340: return;
1341: Iterator transitionRefsIter = transitionRefs.iterator();
1342:
1343: while (transitionRefsIter.hasNext()) {
1344: Element transitionRefElement = (Element) transitionRefsIter
1345: .next();
1346: GenericValue transitionRefValue = delegator.makeValue(
1347: "WorkflowTransitionRef", null);
1348:
1349: values.add(transitionRefValue);
1350:
1351: transitionRefValue.set("packageId", packageId);
1352: transitionRefValue.set("packageVersion", packageVersion);
1353: transitionRefValue.set("processId", processId);
1354: transitionRefValue.set("processVersion", processVersion);
1355: transitionRefValue.set("activityId", activityId);
1356: transitionRefValue.set("transitionId", transitionRefElement
1357: .getAttribute("Id"));
1358: }
1359: }
1360:
1361: // ----------------------------------------------------------------
1362: // Others
1363: // ----------------------------------------------------------------
1364:
1365: protected void readParticipants(List participants,
1366: String packageId, String packageVersion, String processId,
1367: String processVersion, GenericValue valueObject)
1368: throws DefinitionParserException {
1369: if (participants == null || participants.size() == 0)
1370: return;
1371: Iterator participantsIter = participants.iterator();
1372:
1373: while (participantsIter.hasNext()) {
1374: Element participantElement = (Element) participantsIter
1375: .next();
1376: String participantId = participantElement
1377: .getAttribute("Id");
1378: GenericValue participantValue = delegator.makeValue(
1379: "WorkflowParticipant", null);
1380:
1381: values.add(participantValue);
1382:
1383: participantValue.set("packageId", packageId);
1384: participantValue.set("packageVersion", packageVersion);
1385: participantValue.set("processId", processId);
1386: participantValue.set("processVersion", processVersion);
1387: participantValue.set("participantId", participantId);
1388: participantValue.set("participantName", participantElement
1389: .getAttribute("Name"));
1390:
1391: // ParticipantType
1392: Element participantTypeElement = UtilXml.firstChildElement(
1393: participantElement, "ParticipantType");
1394:
1395: if (participantTypeElement != null) {
1396: participantValue.set("participantTypeId",
1397: participantTypeElement.getAttribute("Type"));
1398: }
1399:
1400: // Description?
1401: participantValue.set("description", UtilXml
1402: .childElementValue(participantElement,
1403: "Description"));
1404:
1405: // ExtendedAttributes
1406: participantValue.set("partyId", getExtendedAttributeValue(
1407: participantElement, "partyId", null), false);
1408: participantValue.set("roleTypeId",
1409: getExtendedAttributeValue(participantElement,
1410: "roleTypeId", null), false);
1411: }
1412: }
1413:
1414: /*
1415: protected void readParticipants(List participants, String packageId, String packageVersion, String processId, String processVersion, GenericValue valueObject) throws DefinitionParserException {
1416: if (participants == null || participants.size() == 0)
1417: return;
1418:
1419: Long nextSeqId = delegator.getNextSeqId("WorkflowParticipantList");
1420:
1421: if (nextSeqId == null)
1422: throw new DefinitionParserException("Could not get next sequence id from data source");
1423: String participantListId = nextSeqId.toString();
1424:
1425: valueObject.set("participantListId", participantListId);
1426:
1427: Iterator participantsIter = participants.iterator();
1428: long index = 1;
1429:
1430: while (participantsIter.hasNext()) {
1431: Element participantElement = (Element) participantsIter.next();
1432: String participantId = participantElement.getAttribute("Id");
1433:
1434: // if participant doesn't exist, create it; don't do an update because if settings are manually changed it would be annoying as all get out
1435: GenericValue testValue = null;
1436:
1437: try {
1438: testValue = delegator.findByPrimaryKey("WorkflowParticipant", UtilMisc.toMap("participantId", participantId));
1439: } catch (GenericEntityException e) {
1440: Debug.logWarning(e, module);
1441: }
1442: if (testValue == null) {
1443: GenericValue participantValue = delegator.makeValue("WorkflowParticipant", null);
1444:
1445: values.add(participantValue);
1446: participantValue.set("packageId", packageId);
1447: participantValue.set("packageVersion", packageVersion);
1448: participantValue.set("processId", processId);
1449: participantValue.set("processVersion", processVersion);
1450: participantValue.set("participantId", participantId);
1451: participantValue.set("participantName", participantElement.getAttribute("Name"));
1452:
1453: // ParticipantType
1454: Element participantTypeElement = UtilXml.firstChildElement(participantElement, "ParticipantType");
1455:
1456: if (participantTypeElement != null) {
1457: participantValue.set("participantTypeId", participantTypeElement.getAttribute("Type"));
1458: }
1459:
1460: // Description?
1461: participantValue.set("description", UtilXml.childElementValue(participantElement, "Description"));
1462:
1463: // ExtendedAttributes
1464: participantValue.set("partyId", getExtendedAttributeValue(participantElement, "partyId", null), false);
1465: participantValue.set("roleTypeId", getExtendedAttributeValue(participantElement, "roleTypeId", null), false);
1466: }
1467:
1468: // regardless of whether the participant was created, create a participant list entry
1469: GenericValue participantListValue = delegator.makeValue("WorkflowParticipantList", null);
1470:
1471: values.add(participantListValue);
1472: participantListValue.set("participantListId", participantListId);
1473: participantListValue.set("participantId", participantId);
1474: participantListValue.set("participantIndex", new Long(index));
1475: index++;
1476: }
1477: }
1478: */
1479:
1480: protected void readApplications(List applications,
1481: String packageId, String packageVersion, String processId,
1482: String processVersion) throws DefinitionParserException {
1483: if (applications == null || applications.size() == 0)
1484: return;
1485: Iterator applicationsIter = applications.iterator();
1486:
1487: while (applicationsIter.hasNext()) {
1488: Element applicationElement = (Element) applicationsIter
1489: .next();
1490: GenericValue applicationValue = delegator.makeValue(
1491: "WorkflowApplication", null);
1492:
1493: values.add(applicationValue);
1494:
1495: String applicationId = applicationElement
1496: .getAttribute("Id");
1497:
1498: applicationValue.set("packageId", packageId);
1499: applicationValue.set("packageVersion", packageVersion);
1500: applicationValue.set("processId", processId);
1501: applicationValue.set("processVersion", processVersion);
1502: applicationValue.set("applicationId", applicationId);
1503: applicationValue.set("applicationName", applicationElement
1504: .getAttribute("Name"));
1505:
1506: // Description?
1507: applicationValue.set("description", UtilXml
1508: .childElementValue(applicationElement,
1509: "Description"));
1510:
1511: // FormalParameters?
1512: Element formalParametersElement = UtilXml
1513: .firstChildElement(applicationElement,
1514: "FormalParameters");
1515: List formalParameters = UtilXml.childElementList(
1516: formalParametersElement, "FormalParameter");
1517:
1518: readFormalParameters(formalParameters, packageId,
1519: packageVersion, processId, processVersion,
1520: applicationId);
1521: }
1522: }
1523:
1524: protected void readDataFields(List dataFields, String packageId,
1525: String packageVersion, String processId,
1526: String processVersion) throws DefinitionParserException {
1527: if (dataFields == null || dataFields.size() == 0)
1528: return;
1529: Iterator dataFieldsIter = dataFields.iterator();
1530:
1531: while (dataFieldsIter.hasNext()) {
1532: Element dataFieldElement = (Element) dataFieldsIter.next();
1533: GenericValue dataFieldValue = delegator.makeValue(
1534: "WorkflowDataField", null);
1535:
1536: values.add(dataFieldValue);
1537:
1538: String dataFieldId = dataFieldElement.getAttribute("Id");
1539: String dataFieldName = dataFieldElement
1540: .getAttribute("Name");
1541: if (dataFieldName == null || dataFieldName.length() == 0)
1542: dataFieldName = dataFieldId;
1543:
1544: dataFieldValue.set("packageId", packageId);
1545: dataFieldValue.set("packageVersion", packageVersion);
1546: dataFieldValue.set("processId", processId);
1547: dataFieldValue.set("processVersion", processVersion);
1548: dataFieldValue.set("dataFieldId", dataFieldId);
1549: dataFieldValue.set("dataFieldName", dataFieldName);
1550:
1551: // IsArray attr
1552: dataFieldValue.set("isArray",
1553: ("TRUE".equals(dataFieldElement
1554: .getAttribute("IsArray")) ? "Y" : "N"));
1555:
1556: // DataType
1557: Element dataTypeElement = UtilXml.firstChildElement(
1558: dataFieldElement, "DataType");
1559:
1560: if (dataTypeElement != null) {
1561: // (%Type;)
1562: readType(dataTypeElement, dataFieldValue);
1563: }
1564:
1565: // InitialValue?
1566: dataFieldValue.set("initialValue",
1567: UtilXml.childElementValue(dataFieldElement,
1568: "InitialValue"));
1569:
1570: // Length?
1571: String lengthStr = UtilXml.childElementValue(
1572: dataFieldElement, "Length");
1573:
1574: if (lengthStr != null && lengthStr.length() > 0) {
1575: try {
1576: dataFieldValue.set("lengthBytes", Long
1577: .valueOf(lengthStr));
1578: } catch (NumberFormatException e) {
1579: throw new DefinitionParserException(
1580: "Invalid whole number format in DataField->Length: "
1581: + lengthStr, e);
1582: }
1583: }
1584:
1585: // Description?
1586: dataFieldValue
1587: .set("description", UtilXml.childElementValue(
1588: dataFieldElement, "Description"));
1589: }
1590: }
1591:
1592: protected void readFormalParameters(List formalParameters,
1593: String packageId, String packageVersion, String processId,
1594: String processVersion, String applicationId)
1595: throws DefinitionParserException {
1596: if (formalParameters == null || formalParameters.size() == 0)
1597: return;
1598: Iterator formalParametersIter = formalParameters.iterator();
1599: long index = 1;
1600:
1601: while (formalParametersIter.hasNext()) {
1602: Element formalParameterElement = (Element) formalParametersIter
1603: .next();
1604: GenericValue formalParameterValue = delegator.makeValue(
1605: "WorkflowFormalParam", null);
1606:
1607: values.add(formalParameterValue);
1608:
1609: String formalParamId = formalParameterElement
1610: .getAttribute("Id");
1611:
1612: formalParameterValue.set("packageId", packageId);
1613: formalParameterValue.set("packageVersion", packageVersion);
1614: formalParameterValue.set("processId", processId);
1615: formalParameterValue.set("processVersion", processVersion);
1616: formalParameterValue.set("applicationId", applicationId);
1617: formalParameterValue.set("formalParamId", formalParamId);
1618: formalParameterValue.set("modeEnumId", "WPM_"
1619: + formalParameterElement.getAttribute("Mode"));
1620:
1621: String indexStr = formalParameterElement
1622: .getAttribute("Index");
1623:
1624: if (indexStr != null && indexStr.length() > 0) {
1625: try {
1626: formalParameterValue.set("indexNumber", Long
1627: .valueOf(indexStr));
1628: } catch (NumberFormatException e) {
1629: throw new DefinitionParserException(
1630: "Invalid decimal number format in FormalParameter->Index: "
1631: + indexStr, e);
1632: }
1633: } else
1634: formalParameterValue
1635: .set("indexNumber", new Long(index));
1636: index++;
1637:
1638: // DataType
1639: Element dataTypeElement = UtilXml.firstChildElement(
1640: formalParameterElement, "DataType");
1641:
1642: if (dataTypeElement != null) {
1643: // (%Type;)
1644: readType(dataTypeElement, formalParameterValue);
1645: }
1646:
1647: // Description?
1648: formalParameterValue.set("description", UtilXml
1649: .childElementValue(formalParameterElement,
1650: "Description"));
1651: }
1652: }
1653:
1654: /** Reads information about "Type" entity member sub-elements; the value
1655: * object passed must have two fields to contain Type information:
1656: * <code>dataTypeEnumId</code> and <code>complexTypeInfoId</code>.
1657: */
1658: protected void readType(Element element, GenericValue value) {
1659: // (%Type;) - (RecordType | UnionType | EnumerationType | ArrayType | ListType | BasicType | PlainType | DeclaredType)
1660: Element typeElement = null;
1661:
1662: if ((typeElement = UtilXml.firstChildElement(element,
1663: "RecordType")) != null) {// TODO: write code for complex type
1664: } else if ((typeElement = UtilXml.firstChildElement(element,
1665: "UnionType")) != null) {// TODO: write code for complex type
1666: } else if ((typeElement = UtilXml.firstChildElement(element,
1667: "EnumerationType")) != null) {// TODO: write code for complex type
1668: } else if ((typeElement = UtilXml.firstChildElement(element,
1669: "ArrayType")) != null) {// TODO: write code for complex type
1670: } else if ((typeElement = UtilXml.firstChildElement(element,
1671: "ListType")) != null) {// TODO: write code for complex type
1672: } else if ((typeElement = UtilXml.firstChildElement(element,
1673: "BasicType")) != null) {
1674: value.set("dataTypeEnumId", "WDT_"
1675: + typeElement.getAttribute("Type"));
1676: } else if ((typeElement = UtilXml.firstChildElement(element,
1677: "PlainType")) != null) {
1678: value.set("dataTypeEnumId", "WDT_"
1679: + typeElement.getAttribute("Type"));
1680: } else if ((typeElement = UtilXml.firstChildElement(element,
1681: "DeclaredType")) != null) {
1682: // For DeclaredTypes complexTypeInfoId will actually be the type id
1683: value.set("dataTypeEnumId", "WDT_DECLARED");
1684: value.set("complexTypeInfoId", typeElement
1685: .getAttribute("Id"));
1686: }
1687:
1688: /*
1689: <entity entity-name="WorkflowComplexTypeInfo"
1690: <field name="complexTypeInfoId" type="id-ne"></field>
1691: <field name="memberParentInfoId" type="id"></field>
1692: <field name="dataTypeEnumId" type="id"></field>
1693: <field name="subTypeEnumId" type="id"></field>
1694: <field name="arrayLowerIndex" type="numeric"></field>
1695: <field name="arrayUpperIndex" type="numeric"></field>
1696: */
1697: }
1698:
1699: protected String getExtendedAttributeValue(Element element,
1700: String name, String defaultValue) {
1701: if (element == null || name == null)
1702: return defaultValue;
1703:
1704: Element extendedAttributesElement = UtilXml.firstChildElement(
1705: element, "ExtendedAttributes");
1706:
1707: if (extendedAttributesElement == null)
1708: return defaultValue;
1709: List extendedAttributes = UtilXml.childElementList(
1710: extendedAttributesElement, "ExtendedAttribute");
1711:
1712: if (extendedAttributes == null
1713: || extendedAttributes.size() == 0)
1714: return defaultValue;
1715:
1716: Iterator iter = extendedAttributes.iterator();
1717:
1718: while (iter.hasNext()) {
1719: Element extendedAttribute = (Element) iter.next();
1720: String elementName = extendedAttribute.getAttribute("Name");
1721:
1722: if (name.equals(elementName)) {
1723: return extendedAttribute.getAttribute("Value");
1724: }
1725: }
1726: return defaultValue;
1727: }
1728:
1729: // ---------------------------------------------------------
1730: // RUNTIME, TEST, AND SAMPLE METHODS
1731: // ---------------------------------------------------------
1732:
1733: public static void main(String[] args) throws Exception {
1734: String sampleFileName = "../../docs/examples/sample.xpdl";
1735:
1736: if (args.length > 0)
1737: sampleFileName = args[0];
1738: List values = readXpdl(UtilURL.fromFilename(sampleFileName),
1739: GenericDelegator.getGenericDelegator("default"));
1740: Iterator viter = values.iterator();
1741:
1742: while (viter.hasNext())
1743: System.out.println(viter.next().toString());
1744: }
1745: }
|