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