KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > workflow > definition > XpdlReader


1 /*
2  * $Id: XpdlReader.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.workflow.definition;
26
27 import java.io.IOException JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.LinkedList JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import javax.xml.parsers.ParserConfigurationException JavaDoc;
36
37 import org.ofbiz.base.util.Debug;
38 import org.ofbiz.base.util.StringUtil;
39 import org.ofbiz.base.util.UtilDateTime;
40 import org.ofbiz.base.util.UtilMisc;
41 import org.ofbiz.base.util.UtilURL;
42 import org.ofbiz.base.util.UtilXml;
43 import org.ofbiz.entity.GenericDelegator;
44 import org.ofbiz.entity.GenericEntityException;
45 import org.ofbiz.entity.GenericValue;
46 import org.ofbiz.entity.transaction.GenericTransactionException;
47 import org.ofbiz.entity.transaction.TransactionUtil;
48 import org.w3c.dom.Document JavaDoc;
49 import org.w3c.dom.Element JavaDoc;
50 import org.xml.sax.SAXException JavaDoc;
51
52 /**
53  * XpdlReader - Reads Process Definition objects from XPDL
54  *
55  * @author <a HREF='mailto:jonesde@ofbiz.org'>David E. Jones</a>
56  * @author <a HREF='mailto:jaz@ofbiz.org'>Andy Zeneski</a>
57  * @version $Rev: 5462 $
58  * @since 2.0
59  */

60 public class XpdlReader {
61
62     protected GenericDelegator delegator = null;
63     protected List JavaDoc values = null;
64
65     public static final String JavaDoc module = XpdlReader.class.getName();
66
67     public XpdlReader(GenericDelegator delegator) {
68         this.delegator = delegator;
69     }
70
71     /** Imports an XPDL file at the given location and imports it into the
72      * datasource through the given delegator */

73     public static void importXpdl(URL JavaDoc location, GenericDelegator delegator) throws DefinitionParserException {
74         List JavaDoc values = readXpdl(location, delegator);
75         
76         // attempt to start a transaction
77
boolean beganTransaction = false;
78         try {
79             beganTransaction = TransactionUtil.begin();
80         } catch (GenericTransactionException gte) {
81             Debug.logError(gte, "Unable to begin transaction", module);
82         }
83         
84         try {
85             delegator.storeAll(values);
86             TransactionUtil.commit(beganTransaction);
87         } catch (GenericEntityException e) {
88             try {
89                 // only rollback the transaction if we started one...
90
TransactionUtil.rollback(beganTransaction, "Error importing XPDL", e);
91             } catch (GenericEntityException e2) {
92                 Debug.logError(e2, "Problems rolling back transaction", module);
93             }
94             throw new DefinitionParserException("Could not store values", e);
95         }
96     }
97
98     /** Gets an XML file from the specified location and reads it into
99      * GenericValue objects from the given delegator and returns them in a
100      * List; does not write to the database, just gets the entities. */

101     public static List JavaDoc readXpdl(URL JavaDoc location, GenericDelegator delegator) throws DefinitionParserException {
102         if (Debug.infoOn()) Debug.logInfo("Beginning XPDL File Parse: " + location.toString(), module);
103
104         XpdlReader reader = new XpdlReader(delegator);
105
106         try {
107             Document JavaDoc document = UtilXml.readXmlDocument(location);
108
109             return reader.readAll(document);
110         } catch (ParserConfigurationException JavaDoc e) {
111             Debug.logError(e, module);
112             throw new DefinitionParserException("Could not configure XML reader", e);
113         } catch (SAXException JavaDoc e) {
114             Debug.logError(e, module);
115             throw new DefinitionParserException("Could not parse XML (invalid?)", e);
116         } catch (IOException JavaDoc e) {
117             Debug.logError(e, module);
118             throw new DefinitionParserException("Could not load file", e);
119         }
120     }
121
122     public List JavaDoc readAll(Document JavaDoc document) throws DefinitionParserException {
123         values = new LinkedList JavaDoc();
124         Element JavaDoc docElement;
125
126         docElement = document.getDocumentElement();
127         // read the package element, and everything under it
128
// puts everything in the values list for returning, etc later
129
readPackage(docElement);
130
131         return (values);
132     }
133
134     // ----------------------------------------------------------------
135
// Package
136
// ----------------------------------------------------------------
137

138     protected void readPackage(Element JavaDoc packageElement) throws DefinitionParserException {
139         if (packageElement == null)
140             return;
141         if (!"Package".equals(packageElement.getTagName()))
142             throw new DefinitionParserException("Tried to make Package from element not named Package");
143
144         GenericValue packageValue = delegator.makeValue("WorkflowPackage", null);
145
146         values.add(packageValue);
147
148         String JavaDoc packageId = packageElement.getAttribute("Id");
149
150         packageValue.set("packageId", packageId);
151         packageValue.set("packageName", packageElement.getAttribute("Name"));
152
153         // PackageHeader
154
Element JavaDoc packageHeaderElement = UtilXml.firstChildElement(packageElement, "PackageHeader");
155
156         if (packageHeaderElement != null) {
157             packageValue.set("specificationId", "XPDL");
158             packageValue.set("specificationVersion", UtilXml.childElementValue(packageHeaderElement, "XPDLVersion"));
159             packageValue.set("sourceVendorInfo", UtilXml.childElementValue(packageHeaderElement, "Vendor"));
160             String JavaDoc createdStr = UtilXml.childElementValue(packageHeaderElement, "Created");
161
162             if (createdStr != null) {
163                 try {
164                     packageValue.set("creationDateTime", java.sql.Timestamp.valueOf(createdStr));
165                 } catch (IllegalArgumentException JavaDoc e) {
166                     throw new DefinitionParserException("Invalid Date-Time format in Package->Created: " + createdStr, e);
167                 }
168             }
169             packageValue.set("description", UtilXml.childElementValue(packageHeaderElement, "Description"));
170             packageValue.set("documentationUrl", UtilXml.childElementValue(packageHeaderElement, "Documentation"));
171             packageValue.set("priorityUomId", UtilXml.childElementValue(packageHeaderElement, "PriorityUnit"));
172             packageValue.set("costUomId", UtilXml.childElementValue(packageHeaderElement, "CostUnit"));
173         }
174
175         // RedefinableHeader?
176
Element JavaDoc redefinableHeaderElement = UtilXml.firstChildElement(packageElement, "RedefinableHeader");
177         boolean packageOk = readRedefinableHeader(redefinableHeaderElement, packageValue, "package");
178         String JavaDoc packageVersion = packageValue.getString("packageVersion");
179
180         // Only do these if the package hasn't been imported.
181
if (packageOk) {
182             // ConformanceClass?
183
Element JavaDoc conformanceClassElement = UtilXml.firstChildElement(packageElement, "ConformanceClass");
184
185             if (conformanceClassElement != null) {
186                 packageValue.set("graphConformanceEnumId", "WGC_" + conformanceClassElement.getAttribute("GraphConformance"));
187             }
188
189             // Participants?
190
Element JavaDoc participantsElement = UtilXml.firstChildElement(packageElement, "Participants");
191             List JavaDoc participants = UtilXml.childElementList(participantsElement, "Participant");
192
193             readParticipants(participants, packageId, packageVersion, "_NA_", "_NA_", packageValue);
194
195             // ExternalPackages?
196
Element JavaDoc externalPackagesElement = UtilXml.firstChildElement(packageElement, "ExternalPackages");
197             List JavaDoc externalPackages = UtilXml.childElementList(externalPackagesElement, "ExternalPackage");
198
199             readExternalPackages(externalPackages, packageId, packageVersion);
200
201             // TypeDeclarations?
202
Element JavaDoc typeDeclarationsElement = UtilXml.firstChildElement(packageElement, "TypeDeclarations");
203             List JavaDoc typeDeclarations = UtilXml.childElementList(typeDeclarationsElement, "TypeDeclaration");
204
205             readTypeDeclarations(typeDeclarations, packageId, packageVersion);
206
207             // Applications?
208
Element JavaDoc applicationsElement = UtilXml.firstChildElement(packageElement, "Applications");
209             List JavaDoc applications = UtilXml.childElementList(applicationsElement, "Application");
210
211             readApplications(applications, packageId, packageVersion, "_NA_", "_NA_");
212
213             // DataFields?
214
Element JavaDoc dataFieldsElement = UtilXml.firstChildElement(packageElement, "DataFields");
215             List JavaDoc dataFields = UtilXml.childElementList(dataFieldsElement, "DataField");
216
217             readDataFields(dataFields, packageId, packageVersion, "_NA_", "_NA_");
218         } else {
219             values = new LinkedList JavaDoc();
220         }
221
222         // WorkflowProcesses?
223
Element JavaDoc workflowProcessesElement = UtilXml.firstChildElement(packageElement, "WorkflowProcesses");
224         List JavaDoc workflowProcesses = UtilXml.childElementList(workflowProcessesElement, "WorkflowProcess");
225
226         readWorkflowProcesses(workflowProcesses, packageId, packageVersion);
227     }
228
229     protected boolean readRedefinableHeader(Element JavaDoc redefinableHeaderElement, GenericValue valueObject, String JavaDoc prefix) throws DefinitionParserException {
230         if (redefinableHeaderElement == null) {
231             valueObject.set(prefix + "Version", UtilDateTime.nowDateString());
232             return checkVersion(valueObject, prefix);
233         }
234
235         valueObject.set("author", UtilXml.childElementValue(redefinableHeaderElement, "Author"));
236         valueObject.set(prefix + "Version", UtilXml.childElementValue(redefinableHeaderElement, "Version", UtilDateTime.nowDateString()));
237         valueObject.set("codepage", UtilXml.childElementValue(redefinableHeaderElement, "Codepage"));
238         valueObject.set("countryGeoId", UtilXml.childElementValue(redefinableHeaderElement, "Countrykey"));
239         valueObject.set("publicationStatusId", "WPS_" + redefinableHeaderElement.getAttribute("PublicationStatus"));
240
241         if (!checkVersion(valueObject, prefix)) return false;
242
243         // Responsibles?
244
Element JavaDoc responsiblesElement = UtilXml.firstChildElement(redefinableHeaderElement, "Responsibles");
245         List JavaDoc responsibles = UtilXml.childElementList(responsiblesElement, "Responsible");
246
247         readResponsibles(responsibles, valueObject, prefix);
248         return true;
249     }
250
251     private boolean checkVersion(GenericValue valueObject, String JavaDoc prefix) {
252         // Test if the object already exists. If so throw an exception.
253
try {
254             String JavaDoc message = new String JavaDoc();
255
256             if (prefix.equals("package")) {
257                 GenericValue gvCheck = valueObject.getDelegator().findByPrimaryKey("WorkflowPackage",
258                         UtilMisc.toMap("packageId", valueObject.getString("packageId"),
259                             "packageVersion", valueObject.getString("packageVersion")));
260
261                 if (gvCheck != null) {
262                     message = "[xpdl] Package: " + valueObject.getString("packageId") +
263                             " (ver " + valueObject.getString("packageVersion") +
264                             ") has already been imported. Will not update/import.";
265                 }
266             } else if (prefix.equals("process")) {
267                 GenericValue gvCheck = valueObject.getDelegator().findByPrimaryKey("WorkflowProcess",
268                         UtilMisc.toMap("packageId", valueObject.getString("packageId"),
269                             "packageVersion", valueObject.getString("packageVersion"),
270                             "processId", valueObject.getString("processId"),
271                             "processVersion", valueObject.getString("processVersion")));
272
273                 if (gvCheck != null) {
274                     message = "[xpdl] Process: " + valueObject.getString("processId") +
275                             " (ver " + valueObject.getString("processVersion") +
276                             ") has already been imported. Not importing.";
277                 }
278             }
279             if (message.length() > 0) {
280                 StringBuffer JavaDoc lines = new StringBuffer JavaDoc();
281
282                 for (int i = 0; i < message.length(); i++) {
283                     lines.append("-");
284                 }
285                 Debug.logWarning(lines.toString(), module);
286                 Debug.logWarning(message, module);
287                 Debug.logWarning(lines.toString(), module);
288                 return false;
289             }
290         } catch (GenericEntityException e) {
291             return false;
292         }
293         return true;
294     }
295
296     protected void readResponsibles(List JavaDoc responsibles, GenericValue valueObject, String JavaDoc prefix) throws DefinitionParserException {
297         if (responsibles == null || responsibles.size() == 0) {
298             return;
299         }
300
301         String JavaDoc responsibleListId = delegator.getNextSeqId("WorkflowParticipantList");
302         valueObject.set("responsibleListId", responsibleListId);
303
304         Iterator JavaDoc responsibleIter = responsibles.iterator();
305         int responsibleIndex = 1;
306
307         while (responsibleIter.hasNext()) {
308             Element JavaDoc responsibleElement = (Element JavaDoc) responsibleIter.next();
309             String JavaDoc responsibleId = UtilXml.elementValue(responsibleElement);
310             GenericValue participantListValue = delegator.makeValue("WorkflowParticipantList", null);
311
312             participantListValue.set("packageId", valueObject.getString("packageId"));
313             participantListValue.set("packageVersion", valueObject.getString("packageVersion"));
314             participantListValue.set("participantListId", responsibleListId);
315             participantListValue.set("participantId", responsibleId);
316             participantListValue.set("participantIndex", new Long JavaDoc(responsibleIndex));
317             if (prefix.equals("process")) {
318                 participantListValue.set("processId", valueObject.getString("processId"));
319                 participantListValue.set("processVersion", valueObject.getString("processVersion"));
320             } else {
321                 participantListValue.set("processId", "_NA_");
322                 participantListValue.set("processVersion", "_NA_");
323             }
324             values.add(participantListValue);
325             responsibleIndex++;
326         }
327     }
328
329     protected void readExternalPackages(List JavaDoc externalPackages, String JavaDoc packageId, String JavaDoc packageVersion) {
330         if (externalPackages == null || externalPackages.size() == 0)
331             return;
332         Iterator JavaDoc externalPackageIter = externalPackages.iterator();
333
334         while (externalPackageIter.hasNext()) {
335             Element JavaDoc externalPackageElement = (Element JavaDoc) externalPackageIter.next();
336             GenericValue externalPackageValue = delegator.makeValue("WorkflowPackageExternal", null);
337
338             values.add(externalPackageValue);
339             externalPackageValue.set("packageId", packageId);
340             externalPackageValue.set("packageVersion", packageVersion);
341             externalPackageValue.set("externalPackageId", externalPackageElement.getAttribute("href"));
342         }
343     }
344
345     protected void readTypeDeclarations(List JavaDoc typeDeclarations, String JavaDoc packageId, String JavaDoc packageVersion) throws DefinitionParserException {
346         if (typeDeclarations == null || typeDeclarations.size() == 0)
347             return;
348         Iterator JavaDoc typeDeclarationsIter = typeDeclarations.iterator();
349
350         while (typeDeclarationsIter.hasNext()) {
351             Element JavaDoc typeDeclarationElement = (Element JavaDoc) typeDeclarationsIter.next();
352             GenericValue typeDeclarationValue = delegator.makeValue("WorkflowTypeDeclaration", null);
353
354             values.add(typeDeclarationValue);
355
356             typeDeclarationValue.set("packageId", packageId);
357             typeDeclarationValue.set("packageVersion", packageVersion);
358             typeDeclarationValue.set("typeId", typeDeclarationElement.getAttribute("Id"));
359             typeDeclarationValue.set("typeName", typeDeclarationElement.getAttribute("Name"));
360
361             // (%Type;)
362
readType(typeDeclarationElement, typeDeclarationValue);
363
364             // Description?
365
typeDeclarationValue.set("description", UtilXml.childElementValue(typeDeclarationElement, "Description"));
366         }
367     }
368
369     // ----------------------------------------------------------------
370
// Process
371
// ----------------------------------------------------------------
372

373     protected void readWorkflowProcesses(List JavaDoc workflowProcesses, String JavaDoc packageId, String JavaDoc packageVersion) throws DefinitionParserException {
374         if (workflowProcesses == null || workflowProcesses.size() == 0)
375             return;
376         Iterator JavaDoc workflowProcessIter = workflowProcesses.iterator();
377
378         while (workflowProcessIter.hasNext()) {
379             Element JavaDoc workflowProcessElement = (Element JavaDoc) workflowProcessIter.next();
380
381             readWorkflowProcess(workflowProcessElement, packageId, packageVersion);
382         }
383     }
384
385     protected void readWorkflowProcess(Element JavaDoc workflowProcessElement, String JavaDoc packageId, String JavaDoc packageVersion) throws DefinitionParserException {
386         GenericValue workflowProcessValue = delegator.makeValue("WorkflowProcess", null);
387
388         values.add(workflowProcessValue);
389
390         String JavaDoc processId = workflowProcessElement.getAttribute("Id");
391
392         workflowProcessValue.set("packageId", packageId);
393         workflowProcessValue.set("packageVersion", packageVersion);
394         workflowProcessValue.set("processId", processId);
395         workflowProcessValue.set("objectName", workflowProcessElement.getAttribute("Name"));
396
397         // ProcessHeader
398
Element JavaDoc processHeaderElement = UtilXml.firstChildElement(workflowProcessElement, "ProcessHeader");
399
400         if (processHeaderElement != null) {
401             // TODO: add prefix to duration Unit or map it to make it a real uomId
402
workflowProcessValue.set("durationUomId", processHeaderElement.getAttribute("DurationUnit"));
403             String JavaDoc createdStr = UtilXml.childElementValue(processHeaderElement, "Created");
404
405             if (createdStr != null) {
406                 try {
407                     workflowProcessValue.set("creationDateTime", java.sql.Timestamp.valueOf(createdStr));
408                 } catch (IllegalArgumentException JavaDoc e) {
409                     throw new DefinitionParserException("Invalid Date-Time format in WorkflowProcess->ProcessHeader->Created: " + createdStr, e);
410                 }
411             }
412             workflowProcessValue.set("description", UtilXml.childElementValue(processHeaderElement, "Description"));
413
414             String JavaDoc priorityStr = UtilXml.childElementValue(processHeaderElement, "Priority");
415
416             if (priorityStr != null) {
417                 try {
418                     workflowProcessValue.set("objectPriority", Long.valueOf(priorityStr));
419                 } catch (NumberFormatException JavaDoc e) {
420                     throw new DefinitionParserException("Invalid whole number format in WorkflowProcess->ProcessHeader->Priority: " + priorityStr, e);
421                 }
422             }
423             String JavaDoc limitStr = UtilXml.childElementValue(processHeaderElement, "Limit");
424
425             if (limitStr != null) {
426                 try {
427                     workflowProcessValue.set("timeLimit", Double.valueOf(limitStr));
428                 } catch (NumberFormatException JavaDoc e) {
429                     throw new DefinitionParserException("Invalid decimal number format in WorkflowProcess->ProcessHeader->Limit: " + limitStr, e);
430                 }
431             }
432
433             String JavaDoc validFromStr = UtilXml.childElementValue(processHeaderElement, "ValidFrom");
434
435             if (validFromStr != null) {
436                 try {
437                     workflowProcessValue.set("validFromDate", java.sql.Timestamp.valueOf(validFromStr));
438                 } catch (IllegalArgumentException JavaDoc e) {
439                     throw new DefinitionParserException("Invalid Date-Time format in WorkflowProcess->ProcessHeader->ValidFrom: " + validFromStr, e);
440                 }
441             }
442             String JavaDoc validToStr = UtilXml.childElementValue(processHeaderElement, "ValidTo");
443
444             if (validToStr != null) {
445                 try {
446                     workflowProcessValue.set("validToDate", java.sql.Timestamp.valueOf(validToStr));
447                 } catch (IllegalArgumentException JavaDoc e) {
448                     throw new DefinitionParserException("Invalid Date-Time format in WorkflowProcess->ProcessHeader->ValidTo: " + validToStr, e);
449                 }
450             }
451
452             // TimeEstimation?
453
Element JavaDoc timeEstimationElement = UtilXml.firstChildElement(processHeaderElement, "TimeEstimation");
454
455             if (timeEstimationElement != null) {
456                 String JavaDoc waitingTimeStr = UtilXml.childElementValue(timeEstimationElement, "WaitingTime");
457
458                 if (waitingTimeStr != null) {
459                     try {
460                         workflowProcessValue.set("waitingTime", Double.valueOf(waitingTimeStr));
461                     } catch (NumberFormatException JavaDoc e) {
462                         throw new DefinitionParserException("Invalid decimal number format in WorkflowProcess->ProcessHeader->TimeEstimation->WaitingTime: " + waitingTimeStr, e);
463                     }
464                 }
465                 String JavaDoc workingTimeStr = UtilXml.childElementValue(timeEstimationElement, "WorkingTime");
466
467                 if (workingTimeStr != null) {
468                     try {
469                         workflowProcessValue.set("waitingTime", Double.valueOf(workingTimeStr));
470                     } catch (NumberFormatException JavaDoc e) {
471                         throw new DefinitionParserException("Invalid decimal number format in WorkflowProcess->ProcessHeader->TimeEstimation->WorkingTime: " + workingTimeStr, e);
472                     }
473                 }
474                 String JavaDoc durationStr = UtilXml.childElementValue(timeEstimationElement, "Duration");
475
476                 if (durationStr != null) {
477                     try {
478                         workflowProcessValue.set("duration", Double.valueOf(durationStr));
479                     } catch (NumberFormatException JavaDoc e) {
480                         throw new DefinitionParserException("Invalid decimal number format in WorkflowProcess->ProcessHeader->TimeEstimation->Duration: " + durationStr, e);
481                     }
482                 }
483             }
484         }
485
486         // RedefinableHeader?
487
Element JavaDoc redefinableHeaderElement = UtilXml.firstChildElement(workflowProcessElement, "RedefinableHeader");
488         boolean processOk = readRedefinableHeader(redefinableHeaderElement, workflowProcessValue, "process");
489         String JavaDoc processVersion = workflowProcessValue.getString("processVersion");
490
491         if (!processOk) {
492             values.remove(workflowProcessValue);
493             return;
494         }
495
496         // FormalParameters?
497
Element JavaDoc formalParametersElement = UtilXml.firstChildElement(workflowProcessElement, "FormalParameters");
498         List JavaDoc formalParameters = UtilXml.childElementList(formalParametersElement, "FormalParameter");
499
500         readFormalParameters(formalParameters, packageId, packageVersion, processId, processVersion, "_NA_");
501
502         // (%Type;)* TODO
503

504         // DataFields?
505
Element JavaDoc dataFieldsElement = UtilXml.firstChildElement(workflowProcessElement, "DataFields");
506         List JavaDoc dataFields = UtilXml.childElementList(dataFieldsElement, "DataField");
507
508         readDataFields(dataFields, packageId, packageVersion, processId, processVersion);
509
510         // Participants?
511
Element JavaDoc participantsElement = UtilXml.firstChildElement(workflowProcessElement, "Participants");
512         List JavaDoc participants = UtilXml.childElementList(participantsElement, "Participant");
513
514         readParticipants(participants, packageId, packageVersion, processId, processVersion, workflowProcessValue);
515
516         // Applications?
517
Element JavaDoc applicationsElement = UtilXml.firstChildElement(workflowProcessElement, "Applications");
518         List JavaDoc applications = UtilXml.childElementList(applicationsElement, "Application");
519
520         readApplications(applications, packageId, packageVersion, processId, processVersion);
521
522         // Activities
523
Element JavaDoc activitiesElement = UtilXml.firstChildElement(workflowProcessElement, "Activities");
524         List JavaDoc activities = UtilXml.childElementList(activitiesElement, "Activity");
525
526         readActivities(activities, packageId, packageVersion, processId, processVersion, workflowProcessValue);
527
528         // Transitions
529
Element JavaDoc transitionsElement = UtilXml.firstChildElement(workflowProcessElement, "Transitions");
530         List JavaDoc transitions = UtilXml.childElementList(transitionsElement, "Transition");
531
532         readTransitions(transitions, packageId, packageVersion, processId, processVersion);
533         
534         // ExtendedAttributes?
535
workflowProcessValue.set("defaultStartActivityId", getExtendedAttributeValue(workflowProcessElement, "defaultStartActivityId", workflowProcessValue.getString("defaultStartActivityId")));
536         workflowProcessValue.set("sourceReferenceField", getExtendedAttributeValue(workflowProcessElement, "sourceReferenceField", "sourceReferenceId"));
537     }
538
539     // ----------------------------------------------------------------
540
// Activity
541
// ----------------------------------------------------------------
542

543     protected void readActivities(List JavaDoc activities, String JavaDoc packageId, String JavaDoc packageVersion, String JavaDoc processId,
544         String JavaDoc processVersion, GenericValue processValue) throws DefinitionParserException {
545         if (activities == null || activities.size() == 0)
546             return;
547         Iterator JavaDoc activitiesIter = activities.iterator();
548
549         // do the first one differently because it will be the defaultStart activity
550
if (activitiesIter.hasNext()) {
551             Element JavaDoc activityElement = (Element JavaDoc) activitiesIter.next();
552             String JavaDoc activityId = activityElement.getAttribute("Id");
553
554             processValue.set("defaultStartActivityId", activityId);
555             readActivity(activityElement, packageId, packageVersion, processId, processVersion);
556         }
557
558         while (activitiesIter.hasNext()) {
559             Element JavaDoc activityElement = (Element JavaDoc) activitiesIter.next();
560
561             readActivity(activityElement, packageId, packageVersion, processId, processVersion);
562         }
563     }
564
565     protected void readActivity(Element JavaDoc activityElement, String JavaDoc packageId, String JavaDoc packageVersion, String JavaDoc processId,
566         String JavaDoc processVersion) throws DefinitionParserException {
567         if (activityElement == null)
568             return;
569
570         GenericValue activityValue = delegator.makeValue("WorkflowActivity", null);
571
572         values.add(activityValue);
573
574         String JavaDoc activityId = activityElement.getAttribute("Id");
575
576         activityValue.set("packageId", packageId);
577         activityValue.set("packageVersion", packageVersion);
578         activityValue.set("processId", processId);
579         activityValue.set("processVersion", processVersion);
580         activityValue.set("activityId", activityId);
581         activityValue.set("objectName", activityElement.getAttribute("Name"));
582
583         activityValue.set("description", UtilXml.childElementValue(activityElement, "Description"));
584         String JavaDoc limitStr = UtilXml.childElementValue(activityElement, "Limit");
585
586         if (limitStr != null) {
587             try {
588                 activityValue.set("timeLimit", Double.valueOf(limitStr));
589             } catch (NumberFormatException JavaDoc e) {
590                 throw new DefinitionParserException("Invalid decimal number format in Activity->Limit: " + limitStr, e);
591             }
592         }
593
594         // (Route | Implementation)
595
Element JavaDoc routeElement = UtilXml.firstChildElement(activityElement, "Route");
596         Element JavaDoc implementationElement = UtilXml.firstChildElement(activityElement, "Implementation");
597
598         if (routeElement != null) {
599             activityValue.set("activityTypeEnumId", "WAT_ROUTE");
600         } else if (implementationElement != null) {
601             Element JavaDoc noElement = UtilXml.firstChildElement(implementationElement, "No");
602             Element JavaDoc subFlowElement = UtilXml.firstChildElement(implementationElement, "SubFlow");
603             Element JavaDoc loopElement = UtilXml.firstChildElement(implementationElement, "Loop");
604             List JavaDoc tools = UtilXml.childElementList(implementationElement, "Tool");
605
606             if (noElement != null) {
607                 activityValue.set("activityTypeEnumId", "WAT_NO");
608             } else if (subFlowElement != null) {
609                 activityValue.set("activityTypeEnumId", "WAT_SUBFLOW");
610                 readSubFlow(subFlowElement, packageId, packageVersion, processId, processVersion, activityId);
611             } else if (loopElement != null) {
612                 activityValue.set("activityTypeEnumId", "WAT_LOOP");
613                 readLoop(loopElement, packageId, packageVersion, processId, processVersion, activityId);
614             } else if (tools != null && tools.size() > 0) {
615                 activityValue.set("activityTypeEnumId", "WAT_TOOL");
616                 readTools(tools, packageId, packageVersion, processId, processVersion, activityId);
617             } else {
618                 throw new DefinitionParserException(
619                         "No, SubFlow, Loop or one or more Tool elements must exist under the Implementation element of Activity with ID " + activityId +
620                         " in Process with ID " + processId);
621             }
622         } else {
623             throw new DefinitionParserException("Route or Implementation must exist for Activity with ID " + activityId + " in Process with ID " + processId);
624         }
625
626         // Performer?
627
activityValue.set("performerParticipantId", UtilXml.childElementValue(activityElement, "Performer"));
628
629         // StartMode?
630
Element JavaDoc startModeElement = UtilXml.firstChildElement(activityElement, "StartMode");
631
632         if (startModeElement != null) {
633             if (UtilXml.firstChildElement(startModeElement, "Automatic") != null)
634                 activityValue.set("startModeEnumId", "WAM_AUTOMATIC");
635             else if (UtilXml.firstChildElement(startModeElement, "Manual") != null)
636                 activityValue.set("startModeEnumId", "WAM_MANUAL");
637             else
638                 throw new DefinitionParserException("Could not find Mode under StartMode");
639         }
640
641         // FinishMode?
642
Element JavaDoc finishModeElement = UtilXml.firstChildElement(activityElement, "FinishMode");
643
644         if (finishModeElement != null) {
645             if (UtilXml.firstChildElement(finishModeElement, "Automatic") != null)
646                 activityValue.set("finishModeEnumId", "WAM_AUTOMATIC");
647             else if (UtilXml.firstChildElement(finishModeElement, "Manual") != null)
648                 activityValue.set("finishModeEnumId", "WAM_MANUAL");
649             else
650                 throw new DefinitionParserException("Could not find Mode under FinishMode");
651         }
652
653         // Priority?
654
String JavaDoc priorityStr = UtilXml.childElementValue(activityElement, "Priority");
655
656         if (priorityStr != null) {
657             try {
658                 activityValue.set("objectPriority", Long.valueOf(priorityStr));
659             } catch (NumberFormatException JavaDoc e) {
660                 throw new DefinitionParserException("Invalid whole number format in Activity->Priority: " + priorityStr, e);
661             }
662         }
663
664         // SimulationInformation?
665
Element JavaDoc simulationInformationElement = UtilXml.firstChildElement(activityElement, "SimulationInformation");
666
667         if (simulationInformationElement != null) {
668             if (simulationInformationElement.getAttribute("Instantiation") != null)
669                 activityValue.set("instantiationLimitEnumId", "WFI_" + simulationInformationElement.getAttribute("Instantiation"));
670             String JavaDoc costStr = UtilXml.childElementValue(simulationInformationElement, "Cost");
671
672             if (costStr != null) {
673                 try {
674                     activityValue.set("cost", Double.valueOf(costStr));
675                 } catch (NumberFormatException JavaDoc e) {
676                     throw new DefinitionParserException("Invalid decimal number format in Activity->SimulationInformation->Cost: " + costStr, e);
677                 }
678             }
679
680             // TimeEstimation
681
Element JavaDoc timeEstimationElement = UtilXml.firstChildElement(simulationInformationElement, "TimeEstimation");
682
683             if (timeEstimationElement != null) {
684                 String JavaDoc waitingTimeStr = UtilXml.childElementValue(timeEstimationElement, "WaitingTime");
685
686                 if (waitingTimeStr != null) {
687                     try {
688                         activityValue.set("waitingTime", Double.valueOf(waitingTimeStr));
689                     } catch (NumberFormatException JavaDoc e) {
690                         throw new DefinitionParserException("Invalid decimal number format in Activity->SimulationInformation->TimeEstimation->WaitingTime: " + waitingTimeStr, e);
691                     }
692                 }
693                 String JavaDoc workingTimeStr = UtilXml.childElementValue(timeEstimationElement, "WorkingTime");
694
695                 if (workingTimeStr != null) {
696                     try {
697                         activityValue.set("waitingTime", Double.valueOf(workingTimeStr));
698                     } catch (NumberFormatException JavaDoc e) {
699                         throw new DefinitionParserException("Invalid decimal number format in Activity->SimulationInformation->TimeEstimation->WorkingTime: " + workingTimeStr, e);
700                     }
701                 }
702                 String JavaDoc durationStr = UtilXml.childElementValue(timeEstimationElement, "Duration");
703
704                 if (durationStr != null) {
705                     try {
706                         activityValue.set("duration", Double.valueOf(durationStr));
707                     } catch (NumberFormatException JavaDoc e) {
708                         throw new DefinitionParserException("Invalid decimal number format in Activity->SimulationInformation->TimeEstimation->Duration: " + durationStr, e);
709                     }
710                 }
711             }
712         }
713
714         activityValue.set("iconUrl", UtilXml.childElementValue(activityElement, "Icon"));
715         activityValue.set("documentationUrl", UtilXml.childElementValue(activityElement, "Documentation"));
716
717         // TransitionRestrictions?
718
Element JavaDoc transitionRestrictionsElement = UtilXml.firstChildElement(activityElement, "TransitionRestrictions");
719         List JavaDoc transitionRestrictions = UtilXml.childElementList(transitionRestrictionsElement, "TransitionRestriction");
720
721         readTransitionRestrictions(transitionRestrictions, activityValue);
722
723         // ExtendedAttributes?
724
activityValue.set("acceptAllAssignments", getExtendedAttributeValue(activityElement, "acceptAllAssignments", "N"));
725         activityValue.set("completeAllAssignments", getExtendedAttributeValue(activityElement, "completeAllAssignments", "N"));
726         activityValue.set("limitService", getExtendedAttributeValue(activityElement, "limitService", null), false);
727         activityValue.set("limitAfterStart", getExtendedAttributeValue(activityElement, "limitAfterStart", "Y"));
728         activityValue.set("restartOnDelegate", getExtendedAttributeValue(activityElement, "restartOnDelegate", "N"));
729         activityValue.set("delegateAfterStart", getExtendedAttributeValue(activityElement, "delegateAfterStart", "Y"));
730         activityValue.set("inheritPriority", getExtendedAttributeValue(activityElement, "inheritPriority", "N"));
731         activityValue.set("canStart", getExtendedAttributeValue(activityElement, "canStart", "Y"));
732     }
733
734     protected void readSubFlow(Element JavaDoc subFlowElement, String JavaDoc packageId, String JavaDoc packageVersion, String JavaDoc processId,
735         String JavaDoc processVersion, String JavaDoc activityId) throws DefinitionParserException {
736         if (subFlowElement == null)
737             return;
738
739         GenericValue subFlowValue = delegator.makeValue("WorkflowActivitySubFlow", null);
740
741         values.add(subFlowValue);
742
743         subFlowValue.set("packageId", packageId);
744         subFlowValue.set("packageVersion", packageVersion);
745         subFlowValue.set("processId", processId);
746         subFlowValue.set("processVersion", processVersion);
747         subFlowValue.set("activityId", activityId);
748         subFlowValue.set("subFlowProcessId", subFlowElement.getAttribute("Id"));
749
750         if (subFlowElement.getAttribute("Execution") != null)
751             subFlowValue.set("executionEnumId", "WSE_" + subFlowElement.getAttribute("Execution"));
752         else
753             subFlowValue.set("executionEnumId", "WSE_ASYNCHR");
754
755         // ActualParameters?
756
Element JavaDoc actualParametersElement = UtilXml.firstChildElement(subFlowElement, "ActualParameters");
757         List JavaDoc actualParameters = UtilXml.childElementList(actualParametersElement, "ActualParameter");
758
759         subFlowValue.set("actualParameters", readActualParameters(actualParameters), false);
760     }
761
762     protected void readLoop(Element JavaDoc loopElement, String JavaDoc packageId, String JavaDoc packageVersion, String JavaDoc processId,
763         String JavaDoc processVersion, String JavaDoc activityId) throws DefinitionParserException {
764         if (loopElement == null)
765             return;
766
767         GenericValue loopValue = delegator.makeValue("WorkflowActivityLoop", null);
768
769         values.add(loopValue);
770
771         loopValue.set("packageId", packageId);
772         loopValue.set("packageVersion", packageVersion);
773         loopValue.set("processId", processId);
774         loopValue.set("processVersion", processVersion);
775         loopValue.set("activityId", activityId);
776
777         if (loopElement.getAttribute("Kind") != null)
778             loopValue.set("loopKindEnumId", "WLK_" + loopElement.getAttribute("Kind"));
779         else
780             loopValue.set("loopKindEnumId", "WLK_WHILE");
781
782         // Condition?
783
loopValue.set("conditionExpr", UtilXml.childElementValue(loopElement, "Condition"));
784     }
785
786     protected void readTools(List JavaDoc tools, String JavaDoc packageId, String JavaDoc packageVersion, String JavaDoc processId,
787         String JavaDoc processVersion, String JavaDoc activityId) throws DefinitionParserException {
788         if (tools == null || tools.size() == 0)
789             return;
790         Iterator JavaDoc toolsIter = tools.iterator();
791
792         while (toolsIter.hasNext()) {
793             Element JavaDoc toolElement = (Element JavaDoc) toolsIter.next();
794
795             readTool(toolElement, packageId, packageVersion, processId, processVersion, activityId);
796         }
797     }
798
799     protected void readTool(Element JavaDoc toolElement, String JavaDoc packageId, String JavaDoc packageVersion, String JavaDoc processId,
800         String JavaDoc processVersion, String JavaDoc activityId) throws DefinitionParserException {
801         if (toolElement == null)
802             return;
803
804         GenericValue toolValue = delegator.makeValue("WorkflowActivityTool", null);
805
806         values.add(toolValue);
807
808         toolValue.set("packageId", packageId);
809         toolValue.set("packageVersion", packageVersion);
810         toolValue.set("processId", processId);
811         toolValue.set("processVersion", processVersion);
812         toolValue.set("activityId", activityId);
813         toolValue.set("toolId", toolElement.getAttribute("Id"));
814
815         if (toolElement.getAttribute("Type") != null)
816             toolValue.set("toolTypeEnumId", "WTT_" + toolElement.getAttribute("Type"));
817         else
818             toolValue.set("toolTypeEnumId", "WTT_PROCEDURE");
819
820         // Description?
821
toolValue.set("description", UtilXml.childElementValue(toolElement, "Description"));
822
823         // ActualParameters/ExtendedAttributes?
824
Element JavaDoc actualParametersElement = UtilXml.firstChildElement(toolElement, "ActualParameters");
825         Element JavaDoc extendedAttributesElement = UtilXml.firstChildElement(toolElement, "ExtendedAttributes");
826         List JavaDoc actualParameters = UtilXml.childElementList(actualParametersElement, "ActualParameter");
827         List JavaDoc extendedAttributes = UtilXml.childElementList(extendedAttributesElement, "ExtendedAttribute");
828
829         toolValue.set("actualParameters", readActualParameters(actualParameters), false);
830         toolValue.set("extendedAttributes", readExtendedAttributes(extendedAttributes), false);
831     }
832
833     protected String JavaDoc readActualParameters(List JavaDoc actualParameters) {
834         if (actualParameters == null || actualParameters.size() == 0) return null;
835         StringBuffer JavaDoc actualParametersBuf = new StringBuffer JavaDoc();
836         Iterator JavaDoc actualParametersIter = actualParameters.iterator();
837
838         while (actualParametersIter.hasNext()) {
839             Element JavaDoc actualParameterElement = (Element JavaDoc) actualParametersIter.next();
840
841             actualParametersBuf.append(UtilXml.elementValue(actualParameterElement));
842             if (actualParametersIter.hasNext())
843                 actualParametersBuf.append(',');
844         }
845         return actualParametersBuf.toString();
846     }
847
848     protected String JavaDoc readExtendedAttributes(List JavaDoc extendedAttributes) {
849         if (extendedAttributes == null || extendedAttributes.size() == 0) return null;
850         Map JavaDoc ea = new HashMap JavaDoc();
851         Iterator JavaDoc i = extendedAttributes.iterator();
852
853         while (i.hasNext()) {
854             Element JavaDoc e = (Element JavaDoc) i.next();
855
856             ea.put(e.getAttribute("Name"), e.getAttribute("Value"));
857         }
858         return StringUtil.mapToStr(ea);
859     }
860
861     // ----------------------------------------------------------------
862
// Transition
863
// ----------------------------------------------------------------
864

865     protected void readTransitions(List JavaDoc transitions, String JavaDoc packageId, String JavaDoc packageVersion, String JavaDoc processId,
866         String JavaDoc processVersion) throws DefinitionParserException {
867         if (transitions == null || transitions.size() == 0)
868             return;
869         Iterator JavaDoc transitionsIter = transitions.iterator();
870
871         while (transitionsIter.hasNext()) {
872             Element JavaDoc transitionElement = (Element JavaDoc) transitionsIter.next();
873
874             readTransition(transitionElement, packageId, packageVersion, processId, processVersion);
875         }
876     }
877
878     protected void readTransition(Element JavaDoc transitionElement, String JavaDoc packageId, String JavaDoc packageVersion,
879         String JavaDoc processId, String JavaDoc processVersion) throws DefinitionParserException {
880         if (transitionElement == null)
881             return;
882
883         GenericValue transitionValue = delegator.makeValue("WorkflowTransition", null);
884
885         values.add(transitionValue);
886
887         String JavaDoc transitionId = transitionElement.getAttribute("Id");
888
889         transitionValue.set("packageId", packageId);
890         transitionValue.set("packageVersion", packageVersion);
891         transitionValue.set("processId", processId);
892         transitionValue.set("processVersion", processVersion);
893         transitionValue.set("transitionId", transitionId);
894         transitionValue.set("fromActivityId", transitionElement.getAttribute("From"));
895         transitionValue.set("toActivityId", transitionElement.getAttribute("To"));
896
897         if (transitionElement.getAttribute("Loop") != null && transitionElement.getAttribute("Loop").length() > 0)
898             transitionValue.set("loopTypeEnumId", "WTL_" + transitionElement.getAttribute("Loop"));
899         else
900             transitionValue.set("loopTypeEnumId", "WTL_NOLOOP");
901
902         transitionValue.set("transitionName", transitionElement.getAttribute("Name"));
903
904         // Condition?
905
Element JavaDoc conditionElement = UtilXml.firstChildElement(transitionElement, "Condition");
906
907         if (conditionElement != null) {
908             if (conditionElement.getAttribute("Type") != null)
909                 transitionValue.set("conditionTypeEnumId", "WTC_" + conditionElement.getAttribute("Type"));
910             else
911                 transitionValue.set("conditionTypeEnumId", "WTC_CONDITION");
912
913             // a Condition will have either a list of XPression elements, or plain PCDATA
914
List JavaDoc xPressions = UtilXml.childElementList(conditionElement, "XPression");
915
916             if (xPressions != null && xPressions.size() > 0) {
917                 throw new DefinitionParserException("XPression elements under Condition not yet supported, just use text inside Condition with the expression");
918             } else {
919                 transitionValue.set("conditionExpr", UtilXml.elementValue(conditionElement));
920             }
921         }
922
923         // Description?
924
transitionValue.set("description", UtilXml.childElementValue(transitionElement, "Description"));
925         
926         // ExtendedAttributes?
927
Element JavaDoc extendedAttributesElement = UtilXml.firstChildElement(transitionElement, "ExtendedAttributes");
928         List JavaDoc extendedAttributes = UtilXml.childElementList(extendedAttributesElement, "ExtendedAttribute");
929         transitionValue.set("extendedAttributes", readExtendedAttributes(extendedAttributes), false);
930     }
931
932     protected void readTransitionRestrictions(List JavaDoc transitionRestrictions, GenericValue activityValue) throws DefinitionParserException {
933         if (transitionRestrictions == null || transitionRestrictions.size() == 0)
934             return;
935         Iterator JavaDoc transitionRestrictionsIter = transitionRestrictions.iterator();
936
937         if (transitionRestrictionsIter.hasNext()) {
938             Element JavaDoc transitionRestrictionElement = (Element JavaDoc) transitionRestrictionsIter.next();
939
940             readTransitionRestriction(transitionRestrictionElement, activityValue);
941         }
942         if (transitionRestrictionsIter.hasNext()) {
943             throw new DefinitionParserException("Multiple TransitionRestriction elements found, this is not currently supported. Please remove extras.");
944         }
945     }
946
947     protected void readTransitionRestriction(Element JavaDoc transitionRestrictionElement, GenericValue activityValue) throws DefinitionParserException {
948         String JavaDoc packageId = activityValue.getString("packageId");
949         String JavaDoc packageVersion = activityValue.getString("packageVersion");
950         String JavaDoc processId = activityValue.getString("processId");
951         String JavaDoc processVersion = activityValue.getString("processVersion");
952         String JavaDoc activityId = activityValue.getString("activityId");
953
954         // InlineBlock?
955
Element JavaDoc inlineBlockElement = UtilXml.firstChildElement(transitionRestrictionElement, "InlineBlock");
956
957         if (inlineBlockElement != null) {
958             activityValue.set("isInlineBlock", "Y");
959             activityValue.set("blockName", UtilXml.childElementValue(inlineBlockElement, "BlockName"));
960             activityValue.set("blockDescription", UtilXml.childElementValue(inlineBlockElement, "Description"));
961             activityValue.set("blockIconUrl", UtilXml.childElementValue(inlineBlockElement, "Icon"));
962             activityValue.set("blockDocumentationUrl", UtilXml.childElementValue(inlineBlockElement, "Documentation"));
963
964             activityValue.set("blockBeginActivityId", inlineBlockElement.getAttribute("Begin"));
965             activityValue.set("blockEndActivityId", inlineBlockElement.getAttribute("End"));
966         }
967
968         // Join?
969
Element JavaDoc joinElement = UtilXml.firstChildElement(transitionRestrictionElement, "Join");
970
971         if (joinElement != null) {
972             String JavaDoc joinType = joinElement.getAttribute("Type");
973
974             if (joinType != null && joinType.length() > 0) {
975                 activityValue.set("joinTypeEnumId", "WJT_" + joinType);
976             }
977         }
978
979         // Split?
980
Element JavaDoc splitElement = UtilXml.firstChildElement(transitionRestrictionElement, "Split");
981
982         if (splitElement != null) {
983             String JavaDoc splitType = splitElement.getAttribute("Type");
984
985             if (splitType != null && splitType.length() > 0) {
986                 activityValue.set("splitTypeEnumId", "WST_" + splitType);
987             }
988
989             // TransitionRefs
990
Element JavaDoc transitionRefsElement = UtilXml.firstChildElement(splitElement, "TransitionRefs");
991             List JavaDoc transitionRefs = UtilXml.childElementList(transitionRefsElement, "TransitionRef");
992
993             readTransitionRefs(transitionRefs, packageId, packageVersion, processId, processVersion, activityId);
994         }
995     }
996
997     protected void readTransitionRefs(List JavaDoc transitionRefs, String JavaDoc packageId, String JavaDoc packageVersion, String JavaDoc processId, String JavaDoc processVersion, String JavaDoc activityId) throws DefinitionParserException {
998         if (transitionRefs == null || transitionRefs.size() == 0)
999             return;
1000        Iterator JavaDoc transitionRefsIter = transitionRefs.iterator();
1001
1002        while (transitionRefsIter.hasNext()) {
1003            Element JavaDoc transitionRefElement = (Element JavaDoc) transitionRefsIter.next();
1004            GenericValue transitionRefValue = delegator.makeValue("WorkflowTransitionRef", null);
1005
1006            values.add(transitionRefValue);
1007
1008            transitionRefValue.set("packageId", packageId);
1009            transitionRefValue.set("packageVersion", packageVersion);
1010            transitionRefValue.set("processId", processId);
1011            transitionRefValue.set("processVersion", processVersion);
1012            transitionRefValue.set("activityId", activityId);
1013            transitionRefValue.set("transitionId", transitionRefElement.getAttribute("Id"));
1014        }
1015    }
1016
1017    // ----------------------------------------------------------------
1018
// Others
1019
// ----------------------------------------------------------------
1020

1021    protected void readParticipants(List JavaDoc participants, String JavaDoc packageId, String JavaDoc packageVersion, String JavaDoc processId, String JavaDoc processVersion, GenericValue valueObject) throws DefinitionParserException {
1022        if (participants == null || participants.size() == 0)
1023            return;
1024        Iterator JavaDoc participantsIter = participants.iterator();
1025        
1026        while (participantsIter.hasNext()) {
1027            Element JavaDoc participantElement = (Element JavaDoc) participantsIter.next();
1028            String JavaDoc participantId = participantElement.getAttribute("Id");
1029            GenericValue participantValue = delegator.makeValue("WorkflowParticipant", null);
1030            
1031            values.add(participantValue);
1032            
1033            participantValue.set("packageId", packageId);
1034            participantValue.set("packageVersion", packageVersion);
1035            participantValue.set("processId", processId);
1036            participantValue.set("processVersion", processVersion);
1037            participantValue.set("participantId", participantId);
1038            participantValue.set("participantName", participantElement.getAttribute("Name"));
1039            
1040            // ParticipantType
1041
Element JavaDoc participantTypeElement = UtilXml.firstChildElement(participantElement, "ParticipantType");
1042
1043            if (participantTypeElement != null) {
1044                participantValue.set("participantTypeId", participantTypeElement.getAttribute("Type"));
1045            }
1046
1047            // Description?
1048
participantValue.set("description", UtilXml.childElementValue(participantElement, "Description"));
1049
1050            // ExtendedAttributes
1051
participantValue.set("partyId", getExtendedAttributeValue(participantElement, "partyId", null), false);
1052            participantValue.set("roleTypeId", getExtendedAttributeValue(participantElement, "roleTypeId", null), false);
1053        }
1054    }
1055    
1056    /*
1057    protected void readParticipants(List participants, String packageId, String packageVersion, String processId, String processVersion, GenericValue valueObject) throws DefinitionParserException {
1058        if (participants == null || participants.size() == 0)
1059            return;
1060
1061        Long nextSeqId = delegator.getNextSeqId("WorkflowParticipantList");
1062
1063        if (nextSeqId == null)
1064            throw new DefinitionParserException("Could not get next sequence id from data source");
1065        String participantListId = nextSeqId.toString();
1066
1067        valueObject.set("participantListId", participantListId);
1068
1069        Iterator participantsIter = participants.iterator();
1070        long index = 1;
1071
1072        while (participantsIter.hasNext()) {
1073            Element participantElement = (Element) participantsIter.next();
1074            String participantId = participantElement.getAttribute("Id");
1075
1076            // 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
1077            GenericValue testValue = null;
1078
1079            try {
1080                testValue = delegator.findByPrimaryKey("WorkflowParticipant", UtilMisc.toMap("participantId", participantId));
1081            } catch (GenericEntityException e) {
1082                Debug.logWarning(e, module);
1083            }
1084            if (testValue == null) {
1085                GenericValue participantValue = delegator.makeValue("WorkflowParticipant", null);
1086
1087                values.add(participantValue);
1088                participantValue.set("packageId", packageId);
1089                participantValue.set("packageVersion", packageVersion);
1090                participantValue.set("processId", processId);
1091                participantValue.set("processVersion", processVersion);
1092                participantValue.set("participantId", participantId);
1093                participantValue.set("participantName", participantElement.getAttribute("Name"));
1094
1095                // ParticipantType
1096                Element participantTypeElement = UtilXml.firstChildElement(participantElement, "ParticipantType");
1097
1098                if (participantTypeElement != null) {
1099                    participantValue.set("participantTypeId", participantTypeElement.getAttribute("Type"));
1100                }
1101
1102                // Description?
1103                participantValue.set("description", UtilXml.childElementValue(participantElement, "Description"));
1104
1105                // ExtendedAttributes
1106                participantValue.set("partyId", getExtendedAttributeValue(participantElement, "partyId", null), false);
1107                participantValue.set("roleTypeId", getExtendedAttributeValue(participantElement, "roleTypeId", null), false);
1108            }
1109
1110            // regardless of whether the participant was created, create a participant list entry
1111            GenericValue participantListValue = delegator.makeValue("WorkflowParticipantList", null);
1112
1113            values.add(participantListValue);
1114            participantListValue.set("participantListId", participantListId);
1115            participantListValue.set("participantId", participantId);
1116            participantListValue.set("participantIndex", new Long(index));
1117            index++;
1118        }
1119    }
1120    */

1121
1122    protected void readApplications(List JavaDoc applications, String JavaDoc packageId, String JavaDoc packageVersion, String JavaDoc processId,
1123            String JavaDoc processVersion) throws DefinitionParserException {
1124        if (applications == null || applications.size() == 0)
1125            return;
1126        Iterator JavaDoc applicationsIter = applications.iterator();
1127
1128        while (applicationsIter.hasNext()) {
1129            Element JavaDoc applicationElement = (Element JavaDoc) applicationsIter.next();
1130            GenericValue applicationValue = delegator.makeValue("WorkflowApplication", null);
1131
1132            values.add(applicationValue);
1133
1134            String JavaDoc applicationId = applicationElement.getAttribute("Id");
1135
1136            applicationValue.set("packageId", packageId);
1137            applicationValue.set("packageVersion", packageVersion);
1138            applicationValue.set("processId", processId);
1139            applicationValue.set("processVersion", processVersion);
1140            applicationValue.set("applicationId", applicationId);
1141            applicationValue.set("applicationName", applicationElement.getAttribute("Name"));
1142
1143            // Description?
1144
applicationValue.set("description", UtilXml.childElementValue(applicationElement, "Description"));
1145
1146            // FormalParameters?
1147
Element JavaDoc formalParametersElement = UtilXml.firstChildElement(applicationElement, "FormalParameters");
1148            List JavaDoc formalParameters = UtilXml.childElementList(formalParametersElement, "FormalParameter");
1149
1150            readFormalParameters(formalParameters, packageId, packageVersion, processId, processVersion, applicationId);
1151        }
1152    }
1153
1154    protected void readDataFields(List JavaDoc dataFields, String JavaDoc packageId, String JavaDoc packageVersion, String JavaDoc processId,
1155            String JavaDoc processVersion) throws DefinitionParserException {
1156        if (dataFields == null || dataFields.size() == 0)
1157            return;
1158        Iterator JavaDoc dataFieldsIter = dataFields.iterator();
1159
1160        while (dataFieldsIter.hasNext()) {
1161            Element JavaDoc dataFieldElement = (Element JavaDoc) dataFieldsIter.next();
1162            GenericValue dataFieldValue = delegator.makeValue("WorkflowDataField", null);
1163
1164            values.add(dataFieldValue);
1165
1166            String JavaDoc dataFieldId = dataFieldElement.getAttribute("Id");
1167            String JavaDoc dataFieldName = dataFieldElement.getAttribute("Name");
1168            if (dataFieldName == null || dataFieldName.length() == 0)
1169                dataFieldName = dataFieldId;
1170
1171            dataFieldValue.set("packageId", packageId);
1172            dataFieldValue.set("packageVersion", packageVersion);
1173            dataFieldValue.set("processId", processId);
1174            dataFieldValue.set("processVersion", processVersion);
1175            dataFieldValue.set("dataFieldId", dataFieldId);
1176            dataFieldValue.set("dataFieldName", dataFieldName);
1177
1178            // IsArray attr
1179
dataFieldValue.set("isArray", ("TRUE".equals(dataFieldElement.getAttribute("IsArray")) ? "Y" : "N"));
1180
1181            // DataType
1182
Element JavaDoc dataTypeElement = UtilXml.firstChildElement(dataFieldElement, "DataType");
1183
1184            if (dataTypeElement != null) {
1185                // (%Type;)
1186
readType(dataTypeElement, dataFieldValue);
1187            }
1188
1189            // InitialValue?
1190
dataFieldValue.set("initialValue", UtilXml.childElementValue(dataFieldElement, "InitialValue"));
1191
1192            // Length?
1193
String JavaDoc lengthStr = UtilXml.childElementValue(dataFieldElement, "Length");
1194
1195            if (lengthStr != null && lengthStr.length() > 0) {
1196                try {
1197                    dataFieldValue.set("lengthBytes", Long.valueOf(lengthStr));
1198                } catch (NumberFormatException JavaDoc e) {
1199                    throw new DefinitionParserException("Invalid whole number format in DataField->Length: " + lengthStr, e);
1200                }
1201            }
1202
1203            // Description?
1204
dataFieldValue.set("description", UtilXml.childElementValue(dataFieldElement, "Description"));
1205        }
1206    }
1207
1208    protected void readFormalParameters(List JavaDoc formalParameters, String JavaDoc packageId, String JavaDoc packageVersion,
1209        String JavaDoc processId, String JavaDoc processVersion, String JavaDoc applicationId) throws DefinitionParserException {
1210        if (formalParameters == null || formalParameters.size() == 0)
1211            return;
1212        Iterator JavaDoc formalParametersIter = formalParameters.iterator();
1213        long index = 1;
1214
1215        while (formalParametersIter.hasNext()) {
1216            Element JavaDoc formalParameterElement = (Element JavaDoc) formalParametersIter.next();
1217            GenericValue formalParameterValue = delegator.makeValue("WorkflowFormalParam", null);
1218
1219            values.add(formalParameterValue);
1220
1221            String JavaDoc formalParamId = formalParameterElement.getAttribute("Id");
1222
1223            formalParameterValue.set("packageId", packageId);
1224            formalParameterValue.set("packageVersion", packageVersion);
1225            formalParameterValue.set("processId", processId);
1226            formalParameterValue.set("processVersion", processVersion);
1227            formalParameterValue.set("applicationId", applicationId);
1228            formalParameterValue.set("formalParamId", formalParamId);
1229            formalParameterValue.set("modeEnumId", "WPM_" + formalParameterElement.getAttribute("Mode"));
1230
1231            String JavaDoc indexStr = formalParameterElement.getAttribute("Index");
1232
1233            if (indexStr != null && indexStr.length() > 0) {
1234                try {
1235                    formalParameterValue.set("indexNumber", Long.valueOf(indexStr));
1236                } catch (NumberFormatException JavaDoc e) {
1237                    throw new DefinitionParserException("Invalid decimal number format in FormalParameter->Index: " + indexStr, e);
1238                }
1239            } else
1240                formalParameterValue.set("indexNumber", new Long JavaDoc(index));
1241            index++;
1242
1243            // DataType
1244
Element JavaDoc dataTypeElement = UtilXml.firstChildElement(formalParameterElement, "DataType");
1245
1246            if (dataTypeElement != null) {
1247                // (%Type;)
1248
readType(dataTypeElement, formalParameterValue);
1249            }
1250
1251            // Description?
1252
formalParameterValue.set("description", UtilXml.childElementValue(formalParameterElement, "Description"));
1253        }
1254    }
1255
1256    /** Reads information about "Type" entity member sub-elements; the value
1257     * object passed must have two fields to contain Type information:
1258     * <code>dataTypeEnumId</code> and <code>complexTypeInfoId</code>.
1259     */

1260    protected void readType(Element JavaDoc element, GenericValue value) {
1261        // (%Type;) - (RecordType | UnionType | EnumerationType | ArrayType | ListType | BasicType | PlainType | DeclaredType)
1262
Element JavaDoc typeElement = null;
1263
1264        if ((typeElement = UtilXml.firstChildElement(element, "RecordType")) != null) {// TODO: write code for complex type
1265
} else if ((typeElement = UtilXml.firstChildElement(element, "UnionType")) != null) {// TODO: write code for complex type
1266
} else if ((typeElement = UtilXml.firstChildElement(element, "EnumerationType")) != null) {// TODO: write code for complex type
1267
} else if ((typeElement = UtilXml.firstChildElement(element, "ArrayType")) != null) {// TODO: write code for complex type
1268
} else if ((typeElement = UtilXml.firstChildElement(element, "ListType")) != null) {// TODO: write code for complex type
1269
} else if ((typeElement = UtilXml.firstChildElement(element, "BasicType")) != null) {
1270            value.set("dataTypeEnumId", "WDT_" + typeElement.getAttribute("Type"));
1271        } else if ((typeElement = UtilXml.firstChildElement(element, "PlainType")) != null) {
1272            value.set("dataTypeEnumId", "WDT_" + typeElement.getAttribute("Type"));
1273        } else if ((typeElement = UtilXml.firstChildElement(element, "DeclaredType")) != null) {
1274            // For DeclaredTypes complexTypeInfoId will actually be the type id
1275
value.set("dataTypeEnumId", "WDT_DECLARED");
1276            value.set("complexTypeInfoId", typeElement.getAttribute("Id"));
1277        }
1278
1279        /*
1280         <entity entity-name="WorkflowComplexTypeInfo"
1281         <field name="complexTypeInfoId" type="id-ne"></field>
1282         <field name="memberParentInfoId" type="id"></field>
1283         <field name="dataTypeEnumId" type="id"></field>
1284         <field name="subTypeEnumId" type="id"></field>
1285         <field name="arrayLowerIndex" type="numeric"></field>
1286         <field name="arrayUpperIndex" type="numeric"></field>
1287         */

1288    }
1289
1290    protected String JavaDoc getExtendedAttributeValue(Element JavaDoc element, String JavaDoc name, String JavaDoc defaultValue) {
1291        if (element == null || name == null)
1292            return defaultValue;
1293
1294        Element JavaDoc extendedAttributesElement = UtilXml.firstChildElement(element, "ExtendedAttributes");
1295
1296        if (extendedAttributesElement == null)
1297            return defaultValue;
1298        List JavaDoc extendedAttributes = UtilXml.childElementList(extendedAttributesElement, "ExtendedAttribute");
1299
1300        if (extendedAttributes == null || extendedAttributes.size() == 0)
1301            return defaultValue;
1302
1303        Iterator JavaDoc iter = extendedAttributes.iterator();
1304
1305        while (iter.hasNext()) {
1306            Element JavaDoc extendedAttribute = (Element JavaDoc) iter.next();
1307            String JavaDoc elementName = extendedAttribute.getAttribute("Name");
1308
1309            if (name.equals(elementName)) {
1310                return extendedAttribute.getAttribute("Value");
1311            }
1312        }
1313        return defaultValue;
1314    }
1315    
1316    // ---------------------------------------------------------
1317
// RUNTIME, TEST, AND SAMPLE METHODS
1318
// ---------------------------------------------------------
1319

1320    public static void main(String JavaDoc[] args) throws Exception JavaDoc {
1321        String JavaDoc sampleFileName = "../../docs/examples/sample.xpdl";
1322
1323        if (args.length > 0)
1324            sampleFileName = args[0];
1325        List JavaDoc values = readXpdl(UtilURL.fromFilename(sampleFileName), GenericDelegator.getGenericDelegator("default"));
1326        Iterator JavaDoc viter = values.iterator();
1327
1328        while (viter.hasNext())
1329            System.out.println(viter.next().toString());
1330    }
1331}
1332
1333
Popular Tags