
org.ow2.bonita.parsing.binding.PackageBinding Maven / Gradle / Ivy
/**
* Copyright (C) 2007 Bull S. A. S.
* Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation
* deploymentId 2.1 of the License.
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
* Floor, Boston, MA 02110-1301, USA.
**/
package org.ow2.bonita.parsing.binding;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.ow2.bonita.pvm.internal.util.XmlUtil;
import org.ow2.bonita.pvm.internal.xml.Parse;
import org.ow2.bonita.pvm.internal.xml.Parser;
import org.ow2.bonita.facade.def.dataType.DataTypeDefinition;
import org.ow2.bonita.facade.def.element.BusinessArchive;
import org.ow2.bonita.facade.def.element.DeadlineDefinition;
import org.ow2.bonita.facade.def.element.ExternalPackageDefinition;
import org.ow2.bonita.facade.def.element.HookDefinition;
import org.ow2.bonita.facade.def.element.TypeDeclarationDefinition;
import org.ow2.bonita.facade.def.element.impl.ExternalPackageDefinitionImpl;
import org.ow2.bonita.facade.def.element.impl.TypeDeclarationDefinitionImpl;
import org.ow2.bonita.facade.def.majorElement.ActivityDefinition;
import org.ow2.bonita.facade.def.majorElement.ActivitySetDefinition;
import org.ow2.bonita.facade.def.majorElement.ApplicationDefinition;
import org.ow2.bonita.facade.def.majorElement.DataFieldDefinition;
import org.ow2.bonita.facade.def.majorElement.PackageDefinition;
import org.ow2.bonita.facade.def.majorElement.ParticipantDefinition;
import org.ow2.bonita.facade.def.majorElement.ProcessDefinition;
import org.ow2.bonita.facade.def.majorElement.ProcessFullDefinition;
import org.ow2.bonita.facade.def.majorElement.PackageDefinition.PackageState;
import org.ow2.bonita.facade.def.majorElement.impl.PackageFullDefinitionImpl;
import org.ow2.bonita.facade.uuid.PackageDefinitionUUID;
import org.ow2.bonita.parsing.XpdlParserException;
import org.ow2.bonita.services.util.ServiceEnvTool;
import org.ow2.bonita.util.XmlConstants;
import org.w3c.dom.Element;
/**
* @author Marc Blachon, Guillaume Porcher, Charles Souillard, Miguel Valdes, Pierre Vigneras
*/
public class PackageBinding extends MajorElementBinding {
private static final Logger LOG = Logger.getLogger(PackageBinding.class.getName());
public PackageBinding() {
super("Package");
}
static class PackageDescription {
private final String version;
private final String packageName;
public PackageDescription(final String packageName, final String version) {
super();
this.packageName = packageName;
this.version = version;
}
public String getVersion() {
return this.version;
}
public String getPackageName() {
return this.packageName;
}
}
public Object parse(final Element packageElement, final Parse parse, final Parser parser) {
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("parsing element = " + packageElement);
}
final String id = getId(packageElement);
final String name = getName(packageElement);
final Element packageHeaderElement = XmlUtil.element(packageElement, "PackageHeader");
final String xpdlVersion = getChildTextContent(packageHeaderElement, "XPDLVersion");
final String vendor = getChildTextContent(packageHeaderElement, "Vendor");
final String created = getChildTextContent(packageHeaderElement, "Created");
final String description = getChildTextContent(packageHeaderElement, "Description");
final String documentation = getChildTextContent(packageHeaderElement, "Documentation");
final String priorityUnit = getChildTextContent(packageHeaderElement, "PriorityUnit");
final String costUnit = getChildTextContent(packageHeaderElement, "CostUnit");
final String packageNS = packageElement.getNamespaceURI();
if (xpdlVersion == null || "".equals(xpdlVersion)) {
parse.addProblem("XPDLVersion attribute must be specified at package level.");
} else if (!"1.0".equals(xpdlVersion)) {
parse.addProblem("Unsupported XPDLVersion : " + xpdlVersion);
} else if ("1.0".equals(xpdlVersion) && !packageNS.equals(XmlConstants.XPDL_1_0_NS)) {
final String incoherentValuesMessage =
"XPDLVersion is " + xpdlVersion + ", and package namespace is " + packageNS + ". These two values are not coherents.";
parse.addProblem(incoherentValuesMessage);
}
final Element redefinableHeaderElement = XmlUtil.element(packageElement, "RedefinableHeader");
final String author = getChildTextContent(redefinableHeaderElement, "Author");
final String version = getChildTextContent(redefinableHeaderElement, "Version");
if (version == null || "".equals(version)) {
parse.addProblem("Version attribute is mandatory at Package level (in RedefinableHeader). Package "
+ id + " does not define this attribute.");
}
final String codepage = getChildTextContent(redefinableHeaderElement, "Codepage");
final String countrykey = getChildTextContent(redefinableHeaderElement, "Countrykey");
String pubStatusAttr = null;
final PackageDefinitionUUID recordUUID = ServiceEnvTool.getUUIDGenerator().getPackageDefinitionUUID(name, version);
parse.pushObject(recordUUID);
if (redefinableHeaderElement != null) {
pubStatusAttr = XmlUtil.attribute(redefinableHeaderElement, "PublicationStatus");
}
final PackageDefinition.PublicationStatus publicationStatus =
getEnumValue(PackageDefinition.PublicationStatus.class, pubStatusAttr, null);
Set responsibles = null;
if (redefinableHeaderElement != null) {
final Element responsiblesElement = XmlUtil.element(redefinableHeaderElement, "Responsibles");
if (responsiblesElement != null) {
final List responsibleElements = XmlUtil.elements(responsiblesElement, "Responsible");
responsibles = new HashSet();
for (final Element responsibleElement : responsibleElements) {
responsibles.add(responsibleElement.getTextContent());
}
}
}
final PackageDefinition.GraphConformance conformanceClass = parseConformanceClass(packageElement, parse);
final Element scriptElement = XmlUtil.element(packageElement, "Script");
String scriptType = "bsh";
String scriptVersion = "2.0b1";
String scriptGrammar = null;
if (scriptElement != null) {
scriptGrammar = XmlUtil.attribute(scriptElement, "Grammar");
scriptType = XmlUtil.attribute(scriptElement, "Type");
scriptVersion = XmlUtil.attribute(scriptElement, "Version");
if (!"bsh".equals(scriptType) || !"2.0b1".equals(scriptVersion)) {
parse.addProblem("Unsupported script : " + scriptType + "-" + scriptVersion + "-" + scriptGrammar);
}
}
parse.pushObject(new PackageDescription(name, version));
final Set externalPackages = parseExternalPackages(packageElement, parse);
final Set typeDeclarations = parseTypeDeclarations(packageElement, parse, parser);
final Set participants = parseXpdlMajorElementList(packageElement,
"Participants", "Participant", parse, parser);
final Set applications = parseXpdlMajorElementList(packageElement,
"Applications", "Application", parse, parser);
final Set dataFields =
parseXpdlMajorElementList(packageElement,"DataFields", "DataField", parse, parser);
final Set processes = parseXpdlMajorElementList(packageElement,
"WorkflowProcesses", "WorkflowProcess", parse, parser);
final Set classDependencies = getClassDependencies(processes, participants);
final Set packageDependencies = getPackageDependencies(id, processes);
final Date deployedDate = new Date();
final String deployedBy = ServiceEnvTool.getUserId();
BusinessArchive businessArchice = parse.findObject(BusinessArchive.class);
final PackageDefinition packageDefinition = new PackageFullDefinitionImpl(recordUUID, deployedDate, deployedBy,
id, name, author, classDependencies, codepage, conformanceClass, costUnit,
countrykey, created, description,documentation, externalPackages, packageDependencies, priorityUnit,
publicationStatus, responsibles, scriptType, scriptVersion, scriptGrammar,
typeDeclarations, vendor, version, xpdlVersion, businessArchice, participants, applications,
dataFields, processes, PackageState.DEPLOYED);
//pop packageVersion
parse.popObject();
parse.popObject();
return packageDefinition;
}
protected Set getPackageDependencies(final String packageId, final Set processes) {
Set dependencies = new HashSet();
if (processes != null) {
for (final ProcessFullDefinition process : processes) {
if (process.getActivities() != null) {
for (final ActivityDefinition activity : process.getActivities()) {
if (activity.getSubFlow() != null) {
final String subProcessId = activity.getSubFlow().getProcessId();
if (!containsProcess(processes, subProcessId)) {
ProcessFullDefinition subProcess =
ServiceEnvTool.getJournalQueriers().getLastDeployedProcess(packageId, activity.getSubFlow().getProcessId(),
ProcessDefinition.ProcessState.DEPLOYED);
if (subProcess == null) {
final Set prs = ServiceEnvTool.getJournalQueriers().getProcesses(
activity.getSubFlow().getProcessId(), ProcessDefinition.ProcessState.DEPLOYED);
if (prs != null && !prs.isEmpty() && prs.size() >= 1) {
subProcess = prs.iterator().next();
}
}
if (subProcess == null) {
throw new XpdlParserException("Unable to find process \"" + subProcessId + "\" used in subFlow. "
+ "If the subFlow process is in an external package, "
+ "please check that this package has already been deployed");
}
final PackageDefinition packageDefinition =
ServiceEnvTool.getJournalQueriers().getPackage(subProcess.getPackageDefinitionUUID());
if (packageDefinition.getUndeployedBy() != null) {
throw new XpdlParserException("Unable to find process \"" + subProcessId + "\" used in subFlow. ");
}
dependencies.add(packageDefinition.getUUID().toString());
}
}
}
}
if (process.getActivitySets() != null) {
for (final ActivitySetDefinition activitySet : process.getActivitySets()) {
dependencies.addAll(getActivitiesDependencies(activitySet.getActivities()));
}
}
}
}
if (dependencies.isEmpty()) {
dependencies = null;
}
return dependencies;
}
protected boolean containsProcess(final Set processes, final String processId) {
if (processes == null) {
return false;
}
for (final ProcessFullDefinition process : processes) {
if (process.getProcessId().equals(processId)) {
return true;
}
}
return false;
}
protected Set getClassDependencies(final Set processes,
final Set participants) {
Set dependencies = new HashSet();
dependencies.addAll(getParticipantsDependencies(participants));
if (processes != null) {
for (final ProcessFullDefinition process : processes) {
dependencies.addAll(getActivitiesDependencies(process.getActivities()));
dependencies.addAll(getParticipantsDependencies(process.getParticipants()));
if (process.getActivitySets() != null) {
for (final ActivitySetDefinition activitySet : process.getActivitySets()) {
dependencies.addAll(getActivitiesDependencies(activitySet.getActivities()));
}
}
}
}
if (dependencies.isEmpty()) {
dependencies = null;
}
return dependencies;
}
protected Set getActivitiesDependencies(final Set activities) {
final Set dependencies = new HashSet();
if (activities != null) {
for (final ActivityDefinition activity : activities) {
if (activity.getPerformerAssign() != null) {
dependencies.add(activity.getPerformerAssign().getClassName());
}
if (activity.getHooks() != null) {
for (final HookDefinition hook : activity.getHooks()) {
dependencies.add(hook.getClassName());
}
}
if (activity.getDeadlines() != null) {
for (final DeadlineDefinition deadline : activity.getDeadlines()) {
dependencies.add(deadline.getExceptionName());
}
}
if (activity.getMultiInstantiationDefinition() != null) {
dependencies.add(activity.getMultiInstantiationDefinition().getClassName());
}
}
}
return dependencies;
}
protected Set getParticipantsDependencies(final Set participants) {
final Set dependencies = new HashSet();
if (participants != null) {
for (final ParticipantDefinition participant : participants) {
if (participant.getRoleMapper() != null) {
dependencies.add(participant.getRoleMapper().getClassName());
}
}
}
return dependencies;
}
protected PackageDefinition.GraphConformance parseConformanceClass(final Element packageElement, final Parse parse) {
final Element conformanceClassElement = XmlUtil.element(packageElement, "ConformanceClass");
if (conformanceClassElement != null) {
final PackageDefinition.GraphConformance conformanceClass = getEnumValue(PackageDefinition.GraphConformance.class,
XmlUtil.attribute(conformanceClassElement, "GraphConformance"), null);
if (conformanceClass == null) {
parse.addProblem("ConformanceClass element is defined on package but the attribute GraphConformance is not specified.");
} else {
if (!PackageDefinition.GraphConformance.NON_BLOCKED.equals(conformanceClass)) {
parse.addProblem("Only " + PackageDefinition.GraphConformance.NON_BLOCKED
+ " value is supported for conformanceClass on package element.");
} else {
return conformanceClass;
}
}
}
return null;
}
protected Set parseExternalPackages(final Element packageElement, final Parse parse) {
final Element externalPackagesElement = XmlUtil.element(packageElement, "ExternalPackages");
Set externalPackages = null;
if (externalPackagesElement != null) {
final List externalPackageElements = XmlUtil.elements(externalPackagesElement, "ExternalPackage");
if (externalPackageElements != null) {
parse.addProblem("ExternalPackage element not yet supported.");
externalPackages = new HashSet();
for (final Element externalPackageElement : externalPackageElements) {
final String href = XmlUtil.attribute(externalPackageElement, "href");
final ExternalPackageDefinition externalPackageDefinition = new ExternalPackageDefinitionImpl(href);
externalPackages.add(externalPackageDefinition);
}
}
}
return externalPackages;
}
protected Set parseTypeDeclarations(final Element packageElement,
final Parse parse, final Parser parser) {
final Element typeDeclarationsElement = XmlUtil.element(packageElement, "TypeDeclarations");
Set typeDeclarations = null;
if (typeDeclarationsElement != null) {
final List typeDeclarationElements = XmlUtil.elements(typeDeclarationsElement, "TypeDeclaration");
if (typeDeclarationElements != null) {
typeDeclarations = new HashSet();
final Set ids = new HashSet();
final Set names = new HashSet();
for (final Element typeDeclarationElement : typeDeclarationElements) {
final Element dataTypesElement = XmlUtil.element(typeDeclarationElement, "DataTypes");
Set dataTypes = null;
if (dataTypesElement != null) {
final List dataTypeElements = XmlUtil.elements(dataTypesElement);
if (dataTypeElements != null) {
dataTypes = new HashSet();
for (final Element dataTypeElement : dataTypeElements) {
final DataTypeDefinition dataType = (DataTypeDefinition) parser.parseElement(dataTypeElement, parse, "dataTypes");
dataTypes.add(dataType);
}
}
}
final String description = getChildTextContent(typeDeclarationElement, "Description");
final String id = XmlUtil.attribute(typeDeclarationElement, "Id");
String name = XmlUtil.attribute(typeDeclarationElement, "Name");
if (name == null) {
name = id;
}
if (!ids.add(id)) {
throw new XpdlParserException("Error: Duplicate packageDefinitionUUID " + id + " for typeDeclaration");
}
if (!names.add(name)) {
throw new XpdlParserException("Error: Duplicate name " + name + " for typeDeclaration");
}
final TypeDeclarationDefinition typeDeclarationDefinition = new TypeDeclarationDefinitionImpl(dataTypes, description,
id, name);
typeDeclarations.add(typeDeclarationDefinition);
}
}
}
return typeDeclarations;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy