⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xpdlreader.java

📁 国外的一套开源CRM
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/*
 * $Id: XpdlReader.java,v 1.1 2003/08/17 09:29:32 ajzeneski Exp $
 *
 * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
 * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package org.ofbiz.workflow.definition;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;

import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.StringUtil;
import org.ofbiz.base.util.UtilDateTime;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilURL;
import org.ofbiz.base.util.UtilXml;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.transaction.GenericTransactionException;
import org.ofbiz.entity.transaction.TransactionUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

/**
 * XpdlReader - Reads Process Definition objects from XPDL
 *
 * @author     <a href='mailto:jonesde@ofbiz.org'>David E. Jones</a>
 * @author     <a href='mailto:jaz@ofbiz.org'>Andy Zeneski</a>
 * @version    $Revision: 1.1 $
 * @since      2.0
 */
public class XpdlReader {

    protected GenericDelegator delegator = null;
    protected List values = null;

    public static final String module = XpdlReader.class.getName();

    public XpdlReader(GenericDelegator delegator) {
        this.delegator = delegator;
    }

    /** Imports an XPDL file at the given location and imports it into the
     * datasource through the given delegator */
    public static void importXpdl(URL location, GenericDelegator delegator) throws DefinitionParserException {
        List values = readXpdl(location, delegator);
        
        // attempt to start a transaction
        boolean beganTransaction = false;
        try {
            beganTransaction = TransactionUtil.begin();
        } catch (GenericTransactionException gte) {
            Debug.logError(gte, "Unable to begin transaction", module);
        }
        
        try {
            delegator.storeAll(values);
            TransactionUtil.commit(beganTransaction);
        } catch (GenericEntityException e) {
            try {
                // only rollback the transaction if we started one...
                TransactionUtil.rollback(beganTransaction);
            } catch (GenericEntityException e2) {                
                Debug.logError(e2, "Problems rolling back transaction", module);
            }                                  
            throw new DefinitionParserException("Could not store values", e);
        }
    }

    /** Gets an XML file from the specified location and reads it into
     * GenericValue objects from the given delegator and returns them in a
     * List; does not write to the database, just gets the entities. */
    public static List readXpdl(URL location, GenericDelegator delegator) throws DefinitionParserException {
        if (Debug.infoOn()) Debug.logInfo("Beginning XPDL File Parse: " + location.toString(), module);

        XpdlReader reader = new XpdlReader(delegator);

        try {
            Document document = UtilXml.readXmlDocument(location);

            return reader.readAll(document);
        } catch (ParserConfigurationException e) {
            Debug.logError(e, module);
            throw new DefinitionParserException("Could not configure XML reader", e);
        } catch (SAXException e) {
            Debug.logError(e, module);
            throw new DefinitionParserException("Could not parse XML (invalid?)", e);
        } catch (IOException e) {
            Debug.logError(e, module);
            throw new DefinitionParserException("Could not load file", e);
        }
    }

    public List readAll(Document document) throws DefinitionParserException {
        values = new LinkedList();
        Element docElement;

        docElement = document.getDocumentElement();
        // read the package element, and everything under it
        // puts everything in the values list for returning, etc later
        readPackage(docElement);

        return (values);
    }

    // ----------------------------------------------------------------
    // Package
    // ----------------------------------------------------------------

    protected void readPackage(Element packageElement) throws DefinitionParserException {
        if (packageElement == null)
            return;
        if (!"Package".equals(packageElement.getTagName()))
            throw new DefinitionParserException("Tried to make Package from element not named Package");

        GenericValue packageValue = delegator.makeValue("WorkflowPackage", null);

        values.add(packageValue);

        String packageId = packageElement.getAttribute("Id");

        packageValue.set("packageId", packageId);
        packageValue.set("packageName", packageElement.getAttribute("Name"));

        // PackageHeader
        Element packageHeaderElement = UtilXml.firstChildElement(packageElement, "PackageHeader");

        if (packageHeaderElement != null) {
            packageValue.set("specificationId", "XPDL");
            packageValue.set("specificationVersion", UtilXml.childElementValue(packageHeaderElement, "XPDLVersion"));
            packageValue.set("sourceVendorInfo", UtilXml.childElementValue(packageHeaderElement, "Vendor"));
            String createdStr = UtilXml.childElementValue(packageHeaderElement, "Created");

            if (createdStr != null) {
                try {
                    packageValue.set("creationDateTime", java.sql.Timestamp.valueOf(createdStr));
                } catch (IllegalArgumentException e) {
                    throw new DefinitionParserException("Invalid Date-Time format in Package->Created: " + createdStr, e);
                }
            }
            packageValue.set("description", UtilXml.childElementValue(packageHeaderElement, "Description"));
            packageValue.set("documentationUrl", UtilXml.childElementValue(packageHeaderElement, "Documentation"));
            packageValue.set("priorityUomId", UtilXml.childElementValue(packageHeaderElement, "PriorityUnit"));
            packageValue.set("costUomId", UtilXml.childElementValue(packageHeaderElement, "CostUnit"));
        }

        // RedefinableHeader?
        Element redefinableHeaderElement = UtilXml.firstChildElement(packageElement, "RedefinableHeader");
        boolean packageOk = readRedefinableHeader(redefinableHeaderElement, packageValue, "package");
        String packageVersion = packageValue.getString("packageVersion");       

        // Only do these if the package hasn't been imported.
        if (packageOk) {
            // ConformanceClass?
            Element conformanceClassElement = UtilXml.firstChildElement(packageElement, "ConformanceClass");

            if (conformanceClassElement != null) {
                packageValue.set("graphConformanceEnumId", "WGC_" + conformanceClassElement.getAttribute("GraphConformance"));
            }

            // Participants?
            Element participantsElement = UtilXml.firstChildElement(packageElement, "Participants");
            List participants = UtilXml.childElementList(participantsElement, "Participant");

            readParticipants(participants, packageId, packageVersion, "_NA_", "_NA_", packageValue);

            // ExternalPackages?
            Element externalPackagesElement = UtilXml.firstChildElement(packageElement, "ExternalPackages");
            List externalPackages = UtilXml.childElementList(externalPackagesElement, "ExternalPackage");

            readExternalPackages(externalPackages, packageId, packageVersion);

            // TypeDeclarations?
            Element typeDeclarationsElement = UtilXml.firstChildElement(packageElement, "TypeDeclarations");
            List typeDeclarations = UtilXml.childElementList(typeDeclarationsElement, "TypeDeclaration");

            readTypeDeclarations(typeDeclarations, packageId, packageVersion);

            // Applications?
            Element applicationsElement = UtilXml.firstChildElement(packageElement, "Applications");
            List applications = UtilXml.childElementList(applicationsElement, "Application");

            readApplications(applications, packageId, packageVersion, "_NA_", "_NA_");

            // DataFields?
            Element dataFieldsElement = UtilXml.firstChildElement(packageElement, "DataFields");
            List dataFields = UtilXml.childElementList(dataFieldsElement, "DataField");

            readDataFields(dataFields, packageId, packageVersion, "_NA_", "_NA_");
        } else {
            values = new LinkedList();
        }

        // WorkflowProcesses?
        Element workflowProcessesElement = UtilXml.firstChildElement(packageElement, "WorkflowProcesses");
        List workflowProcesses = UtilXml.childElementList(workflowProcessesElement, "WorkflowProcess");

        readWorkflowProcesses(workflowProcesses, packageId, packageVersion);
    }

    protected boolean readRedefinableHeader(Element redefinableHeaderElement, GenericValue valueObject, String prefix) throws DefinitionParserException {
        if (redefinableHeaderElement == null) {
            valueObject.set(prefix + "Version", UtilDateTime.nowDateString());
            return checkVersion(valueObject, prefix);
        }

        valueObject.set("author", UtilXml.childElementValue(redefinableHeaderElement, "Author"));
        valueObject.set(prefix + "Version", UtilXml.childElementValue(redefinableHeaderElement, "Version", UtilDateTime.nowDateString()));
        valueObject.set("codepage", UtilXml.childElementValue(redefinableHeaderElement, "Codepage"));
        valueObject.set("countryGeoId", UtilXml.childElementValue(redefinableHeaderElement, "Countrykey"));
        valueObject.set("publicationStatusId", "WPS_" + redefinableHeaderElement.getAttribute("PublicationStatus"));

        if (!checkVersion(valueObject, prefix)) return false;

        // Responsibles?
        Element responsiblesElement = UtilXml.firstChildElement(redefinableHeaderElement, "Responsibles");
        List responsibles = UtilXml.childElementList(responsiblesElement, "Responsible");

        readResponsibles(responsibles, valueObject, prefix);
        return true;
    }

    private boolean checkVersion(GenericValue valueObject, String prefix) {
        // Test if the object already exists. If so throw an exception.
        try {
            String message = new String();

            if (prefix.equals("package")) {
                GenericValue gvCheck = valueObject.getDelegator().findByPrimaryKey("WorkflowPackage",
                        UtilMisc.toMap("packageId", valueObject.getString("packageId"),
                            "packageVersion", valueObject.getString("packageVersion")));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -