KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > jawe > xml > PackageValidator


1 package org.enhydra.jawe.xml;
2
3 import java.util.*;
4 import java.io.*;
5
6 import org.enhydra.jawe.xml.*;
7 import org.enhydra.jawe.xml.elements.*;
8 import org.enhydra.jawe.xml.elements.Package;
9
10 import javax.xml.parsers.*;
11 import javax.xml.transform.*;
12 import javax.xml.transform.dom.*;
13 import javax.xml.transform.stream.*;
14 import org.w3c.dom.*;
15
16 /**
17  * Validates an XPDL package.
18  */

19 public class PackageValidator {
20
21    protected static final String JavaDoc CURRENT_XPDL_VERSION="1.0";
22
23    protected Package JavaDoc pkg;
24    protected boolean checkingByJaWE;
25    protected boolean checkingForJaWE;
26    protected boolean getExistingSchemaValidationErrors;
27    protected boolean checkExternalPackages;
28    protected boolean allowUndefinedStart;
29    protected boolean allowUndefinedEnd;
30
31    protected Map xpdlSchemaValidationErrors=new HashMap();
32    protected Map graphsConnectionErrors=new HashMap();
33    protected Map basicGraphConnectionErrors=new HashMap();
34    protected Map graphsConformanceErrors=new HashMap();
35    protected Map basicGraphsConformanceErrors=new HashMap();
36    protected Map logicErrors=new HashMap();
37    protected Map basicLogicErrors=new HashMap();
38
39    protected String JavaDoc encoding;
40
41    public Map getXPDLSchemaValidationErrors () {
42       return xpdlSchemaValidationErrors;
43    }
44
45    public Map getGraphsConnectionErrors (XMLComplexElement pkgOrWpOrAs) {
46       return (Map)graphsConnectionErrors.get(pkgOrWpOrAs);
47    }
48
49    public String JavaDoc getBasicGraphConnectionError (XMLComplexElement pkgOrWpOrAs) {
50       return (String JavaDoc)basicGraphConnectionErrors.get(pkgOrWpOrAs);
51    }
52
53    public Map getGraphConformanceErrors (XMLComplexElement pkgOrWpOrAs) {
54       return (Map)graphsConformanceErrors.get(pkgOrWpOrAs);
55    }
56
57    public List getBasicGraphConformanceErrors (XMLComplexElement pkgOrWpOrAs) {
58       return (List)basicGraphsConformanceErrors.get(pkgOrWpOrAs);
59    }
60
61    public Map getLogicErrors (XMLComplexElement pkgOrWpOrAs) {
62       return (Map)logicErrors.get(pkgOrWpOrAs);
63    }
64
65    public String JavaDoc getBasicLogicError (XMLComplexElement pkgOrWpOrAs) {
66       return (String JavaDoc)basicLogicErrors.get(pkgOrWpOrAs);
67    }
68
69    public PackageValidator(Package JavaDoc pkg,boolean checkingByJaWE,
70                            boolean checkingForJaWE, boolean getExistingSchemaValidationErrors,
71                            boolean checkExternalPackages,boolean allowUndefinedStart,
72                            boolean allowUndefinedEnd,String JavaDoc enc) {
73       this.pkg=pkg;
74       this.checkingByJaWE=checkingByJaWE;
75       this.checkingForJaWE=checkingForJaWE;
76       this.getExistingSchemaValidationErrors=getExistingSchemaValidationErrors;
77       this.checkExternalPackages=checkExternalPackages;
78       this.allowUndefinedStart=allowUndefinedStart;
79       this.allowUndefinedEnd=allowUndefinedEnd;
80       this.encoding=enc;
81    }
82
83    public boolean validateAll (boolean fullCheck) {
84       try {
85          boolean isValid=validateAgainstXPDLSchema();
86          if (fullCheck || isValid) {
87             isValid=checkPackage(fullCheck) && isValid;
88          }
89          if (fullCheck || isValid) {
90             isValid=checkGraphConnections(fullCheck) && isValid;
91          }
92          if (fullCheck || isValid) {
93             isValid=checkGraphConformance(fullCheck) && isValid;
94          }
95
96          return isValid;
97       } catch (Exception JavaDoc ex) {
98          //ex.printStackTrace();
99
return false;
100       }
101    }
102
103    //********************* validation against XPDL schema *********************
104
public boolean validateAgainstXPDLSchema () {
105       if (getExistingSchemaValidationErrors) {
106          xpdlSchemaValidationErrors=pkg.getXMLInterface().getParsingErrorMessages();
107          if (xpdlSchemaValidationErrors.size()>0) {
108             return false;
109          } else {
110             return true;
111          }
112       }
113       try {
114          Document document = null;
115
116          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
117          DocumentBuilder dbuilder = dbf.newDocumentBuilder();
118          document = dbuilder.newDocument();
119          ByteArrayOutputStream baos=new ByteArrayOutputStream();
120
121          // The extended attributes for all package elements must
122
// be updated if current package is not externally
123
// referenced package.
124
//Save.updateExtendedAttributesForWorkflowProcesses();
125

126          // Here we get all document elements
127
pkg.toXML(document);
128
129          // Use a Transformer for output
130
TransformerFactory tFactory =
131             TransformerFactory.newInstance();
132          Transformer transformer = tFactory.newTransformer();
133          transformer.setOutputProperty("indent","yes");
134          transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","4");
135          transformer.setOutputProperty("encoding",encoding);
136          DOMSource source = new DOMSource(document);
137          StreamResult result = new StreamResult(baos);
138          transformer.transform(source,result);
139
140          pkg.getXMLInterface().clearParserErrorMessages();
141          pkg.getXMLInterface().parseDocument(baos.toString(encoding),false);
142          baos.close();
143          xpdlSchemaValidationErrors=pkg.getXMLInterface().getParsingErrorMessages();
144          if (xpdlSchemaValidationErrors.size()>0) {
145             return false;
146          }
147       } catch (Exception JavaDoc ex) {
148          return false;
149       }
150       return true;
151
152    }
153    //********************* Logic checking **************************************
154
public boolean checkPackage (boolean fullCheck) {
155       Map les=new HashMap();
156       //Map les=new OrderedHashMap();
157
//Map les=new LinkedHashMap();
158
logicErrors.put(pkg,les);
159       basicLogicErrors.remove(pkg);
160
161       boolean isPackageValid=true;
162       boolean invalidId=false;
163       if (!isIdValid(pkg.get("Id").toString())) {
164          isPackageValid=false;
165          invalidId=true;
166          les.put(pkg,XMLUtil.getLanguageDependentString("ErrorPackageIdIsNotValid"));
167       }
168       if (fullCheck || isPackageValid) {
169          checkPackageHeader(fullCheck);
170       }
171       if (fullCheck || isPackageValid) {
172          isPackageValid=checkRedefinableHeader(pkg,fullCheck) && isPackageValid;;
173       }
174       if (fullCheck || isPackageValid) {
175          isPackageValid=checkConformanceClass(fullCheck) && isPackageValid;;
176       }
177       if (fullCheck || isPackageValid) {
178          isPackageValid=checkScript(fullCheck) && isPackageValid;;
179       }
180       if ((fullCheck || isPackageValid) && checkExternalPackages) {
181          isPackageValid=checkExternalPackages(fullCheck) && isPackageValid;;
182       }
183       if (fullCheck || isPackageValid) {
184          isPackageValid=checkCollection("TypeDeclarations",pkg,fullCheck) && isPackageValid;;
185       }
186       if (fullCheck || isPackageValid) {
187          isPackageValid=checkCollection("Participants",pkg,fullCheck) && isPackageValid;;
188       }
189       if (fullCheck || isPackageValid) {
190          isPackageValid=checkCollection("Applications",pkg,fullCheck) && isPackageValid;;
191       }
192       if (fullCheck || isPackageValid) {
193          isPackageValid=checkCollection("DataFields",pkg,fullCheck) && isPackageValid;;
194       }
195       boolean areProcessesValid=true;
196       if (fullCheck || isPackageValid) {
197          areProcessesValid=checkCollection("WorkflowProcesses",pkg,fullCheck);
198          isPackageValid=areProcessesValid && isPackageValid;;
199       }
200       if (!isPackageValid) {
201          if (invalidId) {
202             basicLogicErrors.put(pkg,XMLUtil.getLanguageDependentString("ErrorPackageIdIsNotValid"));
203          } else if (!areProcessesValid){
204             basicLogicErrors.put(pkg,XMLUtil.getLanguageDependentString("ErrorOneOrMoreProcessesHaveLogicErrors"));
205          } else {
206             basicLogicErrors.put(pkg,les.values().toArray()[0]);
207          }
208       }
209       return isPackageValid;
210    }
211
212    public boolean checkPackageHeader (boolean fullCheck) {
213       PackageHeader phdr=(PackageHeader)pkg.get("PackageHeader");
214       String JavaDoc xpdlv="XPDLVersion";
215       if (!phdr.get(xpdlv).toString().trim().equals(CURRENT_XPDL_VERSION)) {
216          Map les=getLogicErrors(pkg);
217          les.put(phdr,XMLUtil.getLanguageDependentString("ErrorInvalidXPDLVersion"));
218          return false;
219       } else {
220          return true;
221       }
222    }
223
224    public boolean checkRedefinableHeader (XMLComplexElement pkgOrWp,boolean fullCheck) {
225       boolean isValid=true;
226       //RedefinableHeader rh=(RedefinableHeader)pkg.get("RedefinableHeader");
227
RedefinableHeader rh=(RedefinableHeader)pkgOrWp.get("RedefinableHeader"); //Harald Meister: use argument instead of package
228

229       Iterator rspns=((Responsibles)rh.get("Responsibles")).toCollection().iterator();
230       while (rspns.hasNext()) {
231          Responsible r=(Responsible)rspns.next();
232          if (!(r.toValue() instanceof Participant)) {
233             isValid=false;
234             Map les=getLogicErrors(pkgOrWp);
235             les.put(rh,XMLUtil.getLanguageDependentString("ErrorOneOrMoreResponsibleParticipantsDoNotExist"));
236             break;
237          }
238       }
239       return isValid;
240    }
241
242    public boolean checkConformanceClass (boolean fullCheck) {
243       //ConformanceClass cc=(ConformanceClass)pkg.get("ConformanceClass");
244
return true;
245    }
246
247    public boolean checkScript(boolean fullCheck) {
248       //
249
return true;
250    }
251
252    public boolean checkExternalPackages(boolean fullCheck) {
253       boolean isValid=true;
254       Map les=getLogicErrors(pkg);
255       Iterator it=pkg.getAllExternalPackages().iterator();
256       while (it.hasNext() && (fullCheck || isValid)) {
257          Package JavaDoc p=(Package JavaDoc)it.next();
258          PackageValidator pv=new PackageValidator(p,false,true,
259                                                   getExistingSchemaValidationErrors,false,allowUndefinedStart,
260                                                   allowUndefinedEnd,this.encoding);
261          if (!pv.validateAll(false)) {
262             isValid=false;
263             if (les!=null) {
264                les.put(p,XMLUtil.getLanguageDependentString("ErrorInvalidExternalPackage"));
265             }
266          }
267       }
268       return isValid;
269    }
270
271    public boolean checkCollection (String JavaDoc colName,XMLComplexElement cOwner,boolean fullCheck) {
272       boolean isValid=true;
273       Iterator it=((XMLCollection)cOwner.get(colName)).
274          toCollection().iterator();
275       while (it.hasNext() && (fullCheck || isValid)) {
276          isValid=checkCollectionElement((XMLCollectionElement)it.next(),fullCheck) && isValid;
277       }
278       return isValid;
279    }
280
281    public boolean checkCollectionElement (XMLCollectionElement ce,boolean fullCheck) {
282       boolean isValid=true;
283       if (!isIdValid(ce.getID())) {
284          isValid=false;
285          XMLComplexElement firstOwner=ce.getCollection().getOwner();
286          Map les;
287          if (firstOwner instanceof Application) {
288             les=getLogicErrors(((Application)firstOwner).getCollection().getOwner());
289          } else {
290             les=getLogicErrors(firstOwner);
291          }
292          les.put(ce,XMLUtil.getLanguageDependentString("ErrorIdIsNotValid"));
293       }
294       if (fullCheck || isValid) {
295          if (!isUniqueId(ce.getCollection(),ce.getID())) {
296             isValid=false;
297             XMLComplexElement firstOwner=ce.getCollection().getOwner();
298             Map les;
299             if (firstOwner instanceof Application) {
300                les=getLogicErrors(((Application)firstOwner).getCollection().getOwner());
301             } else {
302                les=getLogicErrors(firstOwner);
303             }
304             String JavaDoc msg=(String JavaDoc)les.get(ce);
305             msg=prepareMessageString(msg);
306             msg=msg+XMLUtil.getLanguageDependentString("ErrorIdIsNotUnique");
307             les.put(ce,msg);
308          }
309       }
310       if (fullCheck || isValid) {
311          if (ce instanceof TypeDeclaration) {
312             isValid=checkTypeDeclaration((TypeDeclaration)ce,fullCheck) && isValid;
313          } else if (ce instanceof Participant) {
314             isValid=checkParticipant((Participant)ce,fullCheck) && isValid;
315          } else if (ce instanceof Application) {
316             isValid=checkApplication((Application)ce,fullCheck) && isValid;
317          } else if (ce instanceof DataField) {
318             isValid=checkDataField((DataField)ce,fullCheck) && isValid;
319          } else if (ce instanceof FormalParameter) {
320             isValid=checkFormalParameter((FormalParameter)ce,fullCheck) && isValid;
321          } else if (ce instanceof WorkflowProcess) {
322             isValid=checkWorkflowProcess((WorkflowProcess)ce,!checkingForJaWE) && isValid;
323          } else if (ce instanceof ActivitySet) {
324             isValid=checkActivitySet((ActivitySet)ce,fullCheck) && isValid;
325          } else if (ce instanceof Activity) {
326             isValid=checkActivity((Activity)ce,fullCheck) && isValid;
327          } else if (ce instanceof Transition) {
328             isValid=checkTransition((Transition)ce,fullCheck) && isValid;
329          }
330       }
331       return isValid;
332    }
333
334    public boolean checkTypeDeclaration(TypeDeclaration td,boolean fullCheck) {
335       //
336
return true;
337    }
338
339    public boolean checkParticipant (Participant p,boolean fullCheck) {
340       //
341
return true;
342    }
343
344    public boolean checkApplication (Application app,boolean fullCheck) {
345       boolean isValid=true;
346       if (((XMLComplexChoice)app.get("Choice")).getChoosen() instanceof FormalParameters) {
347          FormalParameters fps=(FormalParameters)((XMLComplexChoice)app.
348                                                     get("Choice")).getChoices()[0];
349          Iterator it=fps.toCollection().iterator();
350          while (it.hasNext() && (fullCheck || isValid)) {
351             isValid=checkCollectionElement((XMLCollectionElement)it.next(),fullCheck) && isValid;
352          }
353       }
354       return isValid;
355    }
356
357    public boolean checkDataField (DataField df,boolean fullCheck) {
358       //
359
return checkDataType(df,fullCheck);
360    }
361
362    public boolean checkFormalParameter (FormalParameter fp,boolean fullCheck) {
363       //
364
return checkDataType(fp,fullCheck);
365    }
366
367    public boolean checkDataType (XMLCollectionElement dfOrFp,boolean fullCheck) {
368       boolean isValid=true;
369
370       DataType xpdlType=(DataType)dfOrFp.get("DataType");
371       Object JavaDoc type=xpdlType.get("Type").toValue();
372       if (type instanceof DeclaredType) {
373          TypeDeclarations tds=(TypeDeclarations)pkg.get("TypeDeclarations");
374          TypeDeclaration td=(TypeDeclaration)tds.getDeclaredType(
375                                                                    ((DeclaredType)type).get("Id").toString());
376          if (td==null) {
377             isValid=false;
378             XMLComplexElement firstOwner=dfOrFp.getCollection().getOwner();
379             Map les;
380             if (dfOrFp instanceof DataField) {
381                les=getLogicErrors(firstOwner);
382             } else {
383                if (firstOwner instanceof Application) {
384                   les=getLogicErrors(((Application)firstOwner).getCollection().getOwner());
385                } else {
386                   les=getLogicErrors(firstOwner);
387                }
388             }
389             String JavaDoc msg=(String JavaDoc)les.get(dfOrFp);
390             msg=prepareMessageString(msg);
391             msg=msg+XMLUtil.getLanguageDependentString("ErrorNonExistingDeclaredTypeReference");
392             les.put(dfOrFp,msg);
393          }
394       }
395       return isValid;
396    }
397
398    public boolean checkWorkflowProcess (WorkflowProcess wp,boolean fullCheck) {
399       Map les=new HashMap();
400       //Map les=new OrderedHashMap();
401
//Map les=new LinkedHashMap();
402
logicErrors.put(wp,les);
403       basicLogicErrors.remove(wp);
404
405       boolean notDefined=false;
406       boolean isValid=checkProcessHeader(wp,fullCheck);
407       if (fullCheck || isValid) {
408          isValid=checkRedefinableHeader(wp,fullCheck) && isValid;
409       }
410       if (fullCheck || isValid) {
411          isValid=checkCollection("FormalParameters",wp,fullCheck) && isValid;
412       }
413       if (fullCheck || isValid) {
414          isValid=checkCollection("DataFields",wp,fullCheck) && isValid;
415       }
416       if (fullCheck || isValid) {
417          isValid=checkCollection("Participants",wp,fullCheck) && isValid;
418       }
419       if (fullCheck || isValid) {
420          isValid=checkCollection("Applications",wp,fullCheck) && isValid;
421       }
422       if (fullCheck || isValid) {
423          isValid=checkCollection("ActivitySets",wp,fullCheck) && isValid;
424       }
425       if (fullCheck || isValid) {
426          if (((Activities)wp.get("Activities")).toCollection().size()==0) {
427             isValid=false;
428             notDefined=true;
429             les.put(wp,XMLUtil.getLanguageDependentString("ErrorProcessIsNotDefined"));
430          } else {
431             isValid=checkCollection("Activities",wp,fullCheck) && isValid;
432          }
433       }
434       if (fullCheck || isValid) {
435          isValid=checkCollection("Transitions",wp,fullCheck) && isValid;
436       }
437       if (!isValid) {
438          basicLogicErrors.put(wp,les.values().toArray()[0]);
439          Map pkgles=getLogicErrors(pkg);
440          if (pkgles!=null) {
441             if (notDefined) {
442                pkgles.put(wp,XMLUtil.getLanguageDependentString("ErrorProcessIsNotDefined"));
443             } else {
444                pkgles.put(wp,XMLUtil.getLanguageDependentString("ErrorProcessContainsOneOrMoreLogicErrors"));
445             }
446          }
447       }
448       return isValid;
449    }
450
451    public boolean checkProcessHeader(WorkflowProcess wp,boolean fullCheck) {
452       //
453
return true;
454    }
455
456    public boolean checkActivitySet (ActivitySet as,boolean fullCheck) {
457       Map les=new HashMap();
458       //Map les=new OrderedHashMap();
459
//Map les=new LinkedHashMap();
460
logicErrors.put(as,les);
461       basicLogicErrors.remove(as);
462       boolean isValid=true;
463       boolean notDefined=false;
464       if (((Activities)as.get("Activities")).toCollection().size()==0) {
465          isValid=false;
466          notDefined=true;
467          les.put(as,XMLUtil.getLanguageDependentString("ErrorBlockActivityIsNotDefined"));
468       } else {
469          isValid=checkCollection("Activities",as,fullCheck);
470       }
471       if (fullCheck || isValid) {
472          isValid=checkCollection("Transitions",as,fullCheck) && isValid;
473       }
474       if (!isValid) {
475          basicLogicErrors.put(as,getLogicErrors(as).values().toArray()[0]);
476          Map wples=getLogicErrors(as.getOwnerProcess());
477          Activity blockActivity=findBlockActivity(as);
478          if (!(wples==null || blockActivity==null)) {
479             if (notDefined) {
480                wples.put(blockActivity,XMLUtil.getLanguageDependentString("ErrorBlockActivityIsNotDefined"));
481             } else {
482                wples.put(blockActivity,XMLUtil.getLanguageDependentString("ErrorInnerLogicError"));
483             }
484          } else if (wples!=null) {
485             wples.put(as,XMLUtil.getLanguageDependentString("ErrorBlockActivityIsNotDefined"));
486          }
487       }
488       return isValid;
489    }
490
491    public boolean checkActivity (Activity act,boolean fullCheck) {
492       // check performer
493
boolean isValid=checkActivityPerformer(act,fullCheck);
494
495       if (!(fullCheck || isValid)) {
496          return false;
497       }
498
499       // if this is a block activity
500
int type=act.getType();
501       switch (type) {
502          case 0: // Route type
503
break;
504          case 1: // No type
505
break;
506          case 2: // Tool type
507
isValid=checkActivityTools(act,fullCheck) && isValid;
508             break;
509          case 3: // SubFlow type
510
isValid=checkActivitySubFlow(act,fullCheck) && isValid;
511             break;
512          case 4: // Block type
513
isValid=checkActivityBlock(act,fullCheck) && isValid;
514             break;
515       }
516
517       if (!(fullCheck || isValid)) {
518          return false;
519       }
520
521       Transitions trans=(Transitions)act.getCollection().getOwner().get("Transitions");
522       Set outTrans=trans.getTransitions(act.getID(),-1);
523       Set inTrans=trans.getTransitions(act.getID(),1);
524
525       // check deadlines
526
isValid=checkActivityDeadlines(act,fullCheck) && isValid;
527       if (!(fullCheck || isValid)) {
528          return false;
529       }
530
531       Map les=getLogicErrors(act.getCollection().getOwner());
532       String JavaDoc msg=(String JavaDoc)les.get(act);
533       // Split type and no. of outgoing transitions
534
Split split=act.getSplit();
535       if (split.get("Type").toValue().toString().trim().length()==0 && outTrans.size()>1) {
536          isValid=false;
537          msg=prepareMessageString(msg);
538          msg=msg+XMLUtil.getLanguageDependentString("ErrorMultipleOutgoingTransitionsWithoutSplitTypeDefined");
539          les.put(act,msg);
540       }
541
542       if (!(fullCheck || isValid)) {
543          return false;
544       }
545
546       if (!checkingByJaWE) {
547          // TransitionRefs size must be the same as the one of outgoing transitions
548
TransitionRefs tRfs=(TransitionRefs)split.get("TransitionRefs");
549          if ((tRfs.size()!=outTrans.size()) && outTrans.size()>1 && !split.get("Type").toValue().toString().equals("AND")) {
550             isValid=false;
551             msg=prepareMessageString(msg);
552             msg=msg+XMLUtil.getLanguageDependentString("ErrorNumberOfActivitiesOutgoingTransitionsAndTransitionRefsIsNotSame");
553             les.put(act,msg);
554          }
555          if (!(fullCheck || isValid)) {
556             return false;
557          }
558          // TransitionRefs must refer to valid transitions.
559
Iterator tRefs=tRfs.toCollection().iterator();
560          boolean invalidTref=false;
561          while (tRefs.hasNext()) {
562             String JavaDoc transitionId=tRefs.next().toString();
563             Transition t=trans.getTransition(transitionId);
564             if (t==null || !outTrans.contains(t)) {
565                isValid=false;
566                invalidTref=true;
567             }
568          }
569
570          if (invalidTref) {
571             msg=prepareMessageString(msg);
572             msg=msg+XMLUtil.getLanguageDependentString("ErrorTransitionRefIsNotValid");
573             les.put(act,msg);
574          }
575
576          if (!(fullCheck || isValid)) {
577             return false;
578          }
579       }
580
581       // Join type and no. of incoming transitions
582
Join join=act.getJoin();
583       if (join.get("Type").toValue().toString().trim().length()==0 && inTrans.size()>1) {
584          isValid=false;
585          msg=prepareMessageString(msg);
586          msg=msg+XMLUtil.getLanguageDependentString(
587             "ErrorMultipleIncomingTransitionsWithoutJoinTypeDefined");
588          les.put(act,msg);
589       }
590
591       if (!(fullCheck || isValid)) {
592          return false;
593       }
594
595       isValid=checkMultipleOtherwiseOrDefaultExceptionTransitions(act,fullCheck) && isValid;
596
597       return isValid;
598    }
599
600    public boolean checkActivityPerformer (Activity act,boolean fullCheck) {
601       boolean isValid=true;
602
603       // check performer
604
Object JavaDoc performer=act.get("Performer").toValue();
605       // if this is a block activity
606
int type=act.getType();
607       if (type!=1 && type !=2 && ((performer instanceof Participant) ||
608                                      performer.toString().trim().length()>0)) {
609          isValid=false;
610          Map les=getLogicErrors(act.getCollection().getOwner());
611          String JavaDoc msg=(String JavaDoc)les.get(act);
612          msg=prepareMessageString(msg);
613          msg=msg+XMLUtil.getLanguageDependentString("ErrorActivityCannotHavePerformer");
614          les.put(act,msg);
615       }
616       return isValid;
617    }
618
619    public boolean checkActivityTools (Activity act,boolean fullCheck) {
620       boolean isValid=true;
621       boolean nonExistingToolReference=false;
622
623       Tools tools=act.getTools();
624       if (tools!=null) {
625          Iterator it=tools.toCollection().iterator();
626          while (it.hasNext() && (fullCheck || isValid)) {
627             Tool tool=(Tool)it.next();
628             XMLComplexChoice apps=(XMLComplexChoice)tool.get("Application");
629             Object JavaDoc choosenApp=apps.getChoosen();
630             String JavaDoc toolID=null;
631             if (choosenApp!=null && choosenApp instanceof Application) {
632                toolID=((Application)choosenApp).getID();
633             }
634             if (toolID==null) {
635                isValid=false;
636                nonExistingToolReference=true;
637             }
638             if (!(isValid || fullCheck)) break;
639             try {
640                isValid=checkParameterMappings(tool,(Application)choosenApp,fullCheck) && isValid;
641             } catch (Exception JavaDoc ex) {}
642          }
643       }
644
645       if (!isValid) {
646          Map les=getLogicErrors(act.getCollection().getOwner());
647          String JavaDoc msg=(String JavaDoc)les.get(act);
648          msg=prepareMessageString(msg);
649          if (nonExistingToolReference) {
650             msg+=XMLUtil.getLanguageDependentString("ErrorNonExistingToolReference");
651          } else {
652             msg+=XMLUtil.getLanguageDependentString("ErrorToolsFormalAndActualParametersDoNotMatch");
653          }
654          les.put(act,msg);
655       }
656
657       return isValid;
658    }
659
660    public boolean checkActivitySubFlow (Activity act,boolean fullCheck) {
661       boolean isValid=true;
662       boolean nonExistingProcessReference=false;
663       boolean notAllowedProcessReference=false;
664       SubFlow s=act.getSubflow();
665       if (s!=null) {
666          XMLComplexChoice wp=(XMLComplexChoice)s.get("WorkflowProcess");
667          Object JavaDoc choosenWorkflow=wp.getChoosen();
668          String JavaDoc subflowID=null;
669          if (choosenWorkflow!=null) {
670             if (choosenWorkflow instanceof WorkflowProcess) {
671                subflowID=((WorkflowProcess)choosenWorkflow).getID();
672             } else {
673                subflowID=choosenWorkflow.toString();
674             }
675          }
676          if (subflowID==null || subflowID.trim().equals("")) {
677             isValid=false;
678             nonExistingProcessReference=true;
679          } /*else if (subflowID==act.getOwnerProcess().getID()) {
680           isValid=false;
681           notAllowedProcessReference=true;
682           }*/

683          if ((fullCheck || isValid) && (choosenWorkflow instanceof WorkflowProcess)) {
684             try {
685                isValid=checkParameterMappings(s,(WorkflowProcess)choosenWorkflow,fullCheck) && isValid;
686             } catch (Exception JavaDoc ex) {}
687          }
688       }
689       if (!isValid) {
690          Map les=getLogicErrors(act.getCollection().getOwner());
691          String JavaDoc msg=(String JavaDoc)les.get(act);
692          msg=prepareMessageString(msg);
693          if (nonExistingProcessReference) {
694             msg=msg+XMLUtil.getLanguageDependentString("ErrorNonExistingProcessReference");
695          } else if (nonExistingProcessReference) {
696             msg=msg+XMLUtil.getLanguageDependentString("ErrorNotAllowedProcessReference");
697          } else {
698             msg=msg+XMLUtil.getLanguageDependentString("ErrorSubFlowFormalAndActualParametersDoNotMatch");
699          }
700          les.put(act,msg);
701       }
702       return isValid;
703    }
704
705    public boolean checkActivityBlock (Activity act,boolean fullCheck) {
706       boolean isValid=true;
707       BlockActivity blk=act.getBlockActivity();
708       String JavaDoc blockId=blk.get("BlockId").toString();
709       // check if the activity set exists
710
ActivitySets ass=(ActivitySets)act.getOwnerProcess().get("ActivitySets");
711       ActivitySet as=ass.getActivitySet(blockId);
712       // check if there is activity set with the referenced id and if
713
// block activity is inside other block activity, and references it's owner
714
if (as==null || act.getCollection().getOwner().equals(as)) {
715          isValid=false;
716          Map les=getLogicErrors(act.getCollection().getOwner());
717          String JavaDoc msg=(String JavaDoc)les.get(act);
718          msg=prepareMessageString(msg);
719          if (as==null) {
720             msg=msg+XMLUtil.getLanguageDependentString("ErrorNonExistingActivitySetReference");
721          } else {
722             msg=msg+XMLUtil.getLanguageDependentString("ErrorNotAllowedActivitySetReference");
723          }
724          les.put(act,msg);
725       }
726       return isValid;
727    }
728
729    public boolean checkActivityDeadlines (Activity act,boolean fullCheck) {
730       boolean isValid=true;
731
732       Collection deadlines=((Deadlines)act.get("Deadlines")).toCollection();
733       if (deadlines.size()==0) return isValid;
734
735       Iterator dls=deadlines.iterator();
736       //Set deadlineExceptions = new HashSet();
737
int syncCount = 0;
738       while (dls.hasNext()) {
739          Deadline dl=(Deadline)dls.next();
740          // TODO: validate condition.
741
//XMLElement dc=dl.get("DeadlineCondition");
742
//XMLElement en=dl.get("ExceptionName");
743
//deadlineExceptions.add(en.toString().trim());
744
if (dl.get("Execution").toValue().toString().equals("SYNCHR")) {
745             syncCount++;
746          }
747       }
748
749       Map les=getLogicErrors(act.getCollection().getOwner());
750       String JavaDoc msg=(String JavaDoc)les.get(act);
751       if (syncCount>1) {
752          isValid=false;
753          msg=prepareMessageString(msg);
754          msg+=XMLUtil.getLanguageDependentString("ErrorActivityCanHaveOnlyOneSynchronousDeadline");
755          les.put(act,msg);
756       }
757       if (!(fullCheck || isValid)) {
758          return false;
759       }
760
761       Transitions trans=(Transitions)act.getCollection().getOwner().get("Transitions");
762       Set outTrans=trans.getTransitions(act.getID(),-1);
763
764       boolean hasExceptionTransition=false;
765       Iterator it=outTrans.iterator();
766       while (it.hasNext()) {
767          Transition t=(Transition)it.next();
768          Condition c=(Condition)t.get("Condition");
769          String JavaDoc ct=((Condition)t.get("Condition")).get("Type").
770             toValue().toString();
771          if (ct.equals("DEFAULTEXCEPTION") || ct.equals("EXCEPTION")) {
772             hasExceptionTransition=true;
773             break;
774          }
775       }
776       if (!hasExceptionTransition) {
777          isValid=false;
778          msg=prepareMessageString(msg);
779          msg+=XMLUtil.getLanguageDependentString(
780             "ErrorDeadlineExceptionIsNotHandledByAnyOutgoingTransitionWithExceptionOrDefaultExceptionConditionType");
781          les.put(act,msg);
782       }
783       return isValid;
784    }
785
786    public boolean checkMultipleOtherwiseOrDefaultExceptionTransitions (Activity act,boolean fullCheck) {
787       Transitions trans=(Transitions)act.getCollection().getOwner().get("Transitions");
788       Set outTrans=trans.getTransitions(act.getID(),-1);
789       // Check outgoing transitions
790
// do not allow more then 1 transitions of type otherwise or default_exception
791
boolean foundOtherwise=false;
792       boolean foundMultipleOtherwise=false;
793       boolean foundDefaultException=false;
794       boolean foundMultipleDefaultException=false;
795       Iterator ts=outTrans.iterator();
796       while (ts.hasNext()) {
797          Transition t=(Transition)ts.next();
798          String JavaDoc ct=((Condition)t.get("Condition")).get("Type").
799             toValue().toString();
800          if (ct.equals(Condition.CONDITION_TYPE_OTHERWISE)) {
801             if (foundOtherwise) {
802                foundMultipleOtherwise=true;
803                if (foundMultipleDefaultException || !fullCheck) break;
804             } else {
805                foundOtherwise=true;
806             }
807          } else if (ct.equals(Condition.CONDITION_TYPE_DEFAULTEXCEPTION)) {
808             if (foundDefaultException) {
809                foundMultipleDefaultException=true;
810                if (foundMultipleOtherwise || !fullCheck) break;
811             } else {
812                foundDefaultException=true;
813             }
814          }
815       }
816
817       if (foundMultipleOtherwise || foundMultipleDefaultException) {
818          Map les=getLogicErrors(act.getCollection().getOwner());
819          String JavaDoc msg=(String JavaDoc)les.get(act);
820          msg=prepareMessageString(msg);
821          if (foundMultipleDefaultException && foundMultipleOtherwise) {
822             msg=msg+XMLUtil.getLanguageDependentString(
823                "ErrorMoreThenOneOTHERWISEAndDEFAULTEXCEPTIONTypeOutgoingTransition");
824          } else if (foundMultipleOtherwise) {
825             msg=msg+XMLUtil.getLanguageDependentString(
826                "ErrorMoreThenOneOTHERWISETypeOutgoingTransition");
827          } else if (foundMultipleDefaultException) {
828             msg=msg+XMLUtil.getLanguageDependentString(
829                "ErrorMoreThenOneDEFAULTEXCEPTIONTypeOutgoingTransition");
830          }
831          les.put(act,msg);
832          ret