KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > jawe > xml > elements > WorkflowProcesses


1 /* WorkflowProcesses.java
2  *
3  * Authors:
4  * Stefanovic Nenad chupo@iis.ns.ac.yu
5  * Bojanic Sasa sasaboy@neobee.net
6  * Puskas Vladimir vpuskas@eunet.yu
7  * Pilipovic Goran zboniek@uns.ac.yu
8  *
9  */

10
11 package org.enhydra.jawe.xml.elements;
12
13 import org.enhydra.jawe.xml.*;
14 import org.enhydra.jawe.xml.panels.*;
15
16 import java.util.*;
17 import org.w3c.dom.*;
18 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
19 import javax.xml.parsers.DocumentBuilder JavaDoc;
20
21 /**
22  * Represents a WfMC DTD element that has the similar name.
23  * This class is a collection of class <b>WorkflowProcess</b> instances.
24  */

25 public class WorkflowProcesses extends XMLCollection {
26
27    private ArrayList externalProcesses=new ArrayList();
28
29    /**
30     * Creates a new instance of the class.
31     *
32     * @param p The container for all processes.
33     */

34    public WorkflowProcesses (Package JavaDoc p) {
35       super(p);
36    }
37
38    public boolean isMine (WorkflowProcess wp) {
39       return refCollectionElements.contains(wp);
40    }
41
42    // min=0, max=unbounded
43
/**
44     * Generates a new element of the class which instances
45     * are members of collection of this class.
46     *
47     * return The generated instance of class that makes collection.
48     */

49    public XMLElement generateNewElement() {
50       WorkflowProcess wp=new WorkflowProcess(this,(Package JavaDoc)myOwner);
51       wp.setRequired(true);
52       return wp;
53    }
54
55    /**
56     * Returns a workflow process with specified ID.
57     *
58     * @param ID The 'Id' attribute of wanted instance of
59     * WorkflowProcess element defined within a
60     * XML file.
61     * @return Returns the instance of WorkflowProcess element
62     * that have the specified ID as an 'Id' attribute
63     * if it exist among all defined processes within
64     * package, if it doesn't exist, <tt>null</tt> is
65     * returned.
66     */

67    public WorkflowProcess getWorkflowProcess(String JavaDoc ID) {
68       WorkflowProcess wp=(WorkflowProcess)super.getCollectionElement(ID);
69       // if the process haven't been found, search external processes
70
if (wp==null) {
71          String JavaDoc extID;
72          Iterator it=externalProcesses.iterator();
73          while (it.hasNext()) {
74             WorkflowProcess wptmp=(WorkflowProcess)it.next();
75             extID=wptmp.getID();
76             if (extID.equals(ID)) {
77                wp=wptmp;
78                break;
79             }
80          }
81       }
82       return wp;
83    }
84
85    /**
86     * Returns the collection of processes defined in the current package. It
87     * doesn't include processes from referenced external packages.
88     */

89    public Collection getTableElements () {
90       //ArrayList chos=new ArrayList(externalProcesses);
91
//chos.addAll(refCollectionElements);
92
ArrayList chos=new ArrayList(refCollectionElements);
93       return chos;
94    }
95
96    /**
97     * Returns collection of all workflow processes from current package and
98     * processes from referenced external packages, but leaving out the
99     * processes from external packages which Id is equal to any of current
100     * package processes Ids.
101     */

102    public Collection getChoosable () {
103       ArrayList chos=new ArrayList(refCollectionElements);
104       Iterator i=externalProcesses.iterator();
105       while (i.hasNext()) {
106          WorkflowProcess wp=(WorkflowProcess)i.next();
107          if (getCollectionElement(wp.getID())==null) {
108             chos.add(wp);
109          }
110       }
111       return chos;
112    }
113
114    /**
115     * Checks if specified workflow process can be removed. Workflow process
116     * can be removed only if it is not referenced within any subflow.
117     */

118    public boolean canRemoveElement (XMLElement el) {
119       boolean remove=true;
120       WorkflowProcess toRemove=(WorkflowProcess)el;
121       Iterator it=refCollectionElements.iterator();
122       while (it.hasNext()) {
123          WorkflowProcess wp=(WorkflowProcess)it.next();
124          // do not check itself and external workflows
125
if (wp!=toRemove) {
126             Activities acts=(Activities)wp.get("Activities");
127             remove=acts.canRemoveWorkflow(toRemove);
128             if (!remove) {
129                break;
130             }
131             ActivitySets actsts=(ActivitySets)wp.get("ActivitySets");
132             remove=actsts.canRemoveWorkflow(toRemove);
133             if (!remove) {
134                break;
135             }
136          }
137       }
138       return remove;
139    }
140
141    public Collection getExternalWorkflowProcesses () {
142       return externalProcesses;
143    }
144
145    public WorkflowProcess createCopyOfWorkflowProcess (WorkflowProcess wp,boolean localCopy) {
146       if (wp==null) return null;
147
148       Package JavaDoc mine=(Package JavaDoc)myOwner;
149       Package JavaDoc ext=wp.getPackage();
150
151       Document document=null;
152       try {
153          DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
154          DocumentBuilder JavaDoc dbuilder = dbf.newDocumentBuilder();
155          document = dbuilder.newDocument();
156       } catch (Exception JavaDoc ex) {}
157       ext.toXML(document);
158
159
160       WorkflowProcess copyOfWP=(WorkflowProcess)generateNewElement();
161       String JavaDoc ID=copyOfWP.getID();
162
163       NodeList nl=document.getElementsByTagName("WorkflowProcess");
164       Node wantedWorkflowNode=null;
165       if (nl.getLength()>0) {
166          String JavaDoc extID=wp.getID();
167          for (int i=0; i<nl.getLength(); i++) {
168             Node n=nl.item(i);
169             String JavaDoc IDOfNode=XMLUtil.getID(n);
170             if (IDOfNode.equals(extID)) {
171                wantedWorkflowNode=n;
172                break;
173             }
174          }
175       } else {
176          return null;
177       }
178
179       if (wantedWorkflowNode==null) return null;
180       //String XML=wp.toXML(null,0).trim(); // trimming is neccessary for our parser
181
copyOfWP.readFromXML(wantedWorkflowNode);
182
183       if(!localCopy) {
184          // Add all TypeDeclarations from package of wp that we want to import
185
// to the current package
186
TypeDeclarations mineTD=(TypeDeclarations)mine.get("TypeDeclarations");
187          TypeDeclarations extTD=(TypeDeclarations)ext.get("TypeDeclarations");
188          TypeDeclarations tmpTD=new TypeDeclarations((Package JavaDoc)myOwner);
189          // I'm doing this to avoid to implement complex cloning
190
// NOTE: can't just add all TDs from extTD directly - it would be the same objects
191
// that would be read only
192
nl=document.getElementsByTagName("TypeDeclarations");
193          if (nl.getLength()>0) {
194             Node n=nl.item(0);
195             tmpTD.fromXML(n);
196          }
197          //String TDXml=extTD.toXML(null,0).trim(); // trimming is neccessary for our parser
198
//tmpTD.fromXML(null,TDXml);
199
// Do not add type dec. if it already exists
200
Iterator it=tmpTD.toCollection().iterator();
201          while (it.hasNext()) {
202             TypeDeclaration td=(TypeDeclaration)it.next();
203             if (mineTD.getDeclaredType(td.getID())==null) {
204                mineTD.toCollection().add(td);
205             }
206          }
207
208          // put all DataFields defined at the
209
// package level of imported workflow process to the workflow process
210
// level of the newly created workflow process
211
DataFields extDF=(DataFields)ext.get("DataFields");
212          DataFields copyDF=(DataFields)copyOfWP.get("DataFields");
213          DataFields tmpDF=new DataFields((Package JavaDoc)myOwner);
214          // I'm doing this to avoid to implement complex cloning
215
// NOTE: can't just add all DFs from copyDF directly - it would be the same objects
216
// that would be read only
217
//String DFXml=extDF.toXML(null,0).trim(); // trimming is neccessary for our parser
218
nl=document.getElementsByTagName("DataFields");
219          if (nl.getLength()>0) {
220             Node n=nl.item(0);
221             tmpDF.fromXML(n);
222          }
223          //tmpDF.fromXML(null,DFXml);
224
copyDF.toCollection().addAll(tmpDF.toCollection());
225       }
226
227       // do not process external user properties
228
copyOfWP.afterImporting();
229
230       // Setting back newly generated ID (do not keep ID of external workflow process)
231
copyOfWP.set("Id",ID);
232       if (localCopy) {
233          copyOfWP.set("Name",
234                       XMLUtil.getLanguageDependentString("CopyOfKey")+
235                          " "+copyOfWP.get("Name").toString());
236       }
237       
238       // add it to collection
239
refCollectionElements.add(copyOfWP);
240       return copyOfWP;
241    }
242    /**
243     *
244     */

245    protected void insertFromExternal(Package JavaDoc ep) {
246       WorkflowProcesses externalWPs=(WorkflowProcesses)ep.
247          get("WorkflowProcesses");
248       // insert only the workflows that owns package 'ep'
249
// and are not referenced from some ext. pack. of 'ep'
250
Iterator it=externalWPs.refCollectionElements.iterator();
251       while (it.hasNext()) {
252          WorkflowProcess wp=(WorkflowProcess)it.next();
253          if (!externalProcesses.contains(wp)) {
254             externalProcesses.add(wp);
255          }
256       }
257       /*System.out.println("WPS before for package "+myOwner+"="+refCollectionElements);
258        refCollectionElements.addAll(externalWPs.refCollectionElements);
259        System.out.println("WPS after for package "+myOwner+"="+refCollectionElements);*/

260    }
261
262    /**
263     *
264     */

265    protected void removeFromExternal(Package JavaDoc ep) {
266       WorkflowProcesses externalWPs=(WorkflowProcesses)ep.
267          get("WorkflowProcesses");
268
269       Iterator it=externalWPs.toCollection().iterator();
270       while (it.hasNext()) {
271          WorkflowProcess wp=(WorkflowProcess)it.next();
272          externalProcesses.remove(wp);
273       }
274    }
275
276    public int[] getInvisibleTableFieldOrdinals () {
277       int[] itfo=new int[10];
278       itfo[0]=3;
279       itfo[1]=4;
280       itfo[2]=5;
281       itfo[3]=6;
282       itfo[4]=7;
283       itfo[5]=8;
284       itfo[6]=9;
285       itfo[7]=10;
286       itfo[8]=11;
287       itfo[9]=12;
288       return itfo;
289    }
290
291    /**
292     * @return XMLPanel to be shown.
293     */

294    public XMLPanel getPanel () {
295       isReadOnly=true;
296       return super.getPanel();
297    }
298
299    public void refreshCollection (Set elementsToAddOrRemove,boolean append) {
300       super.refreshCollection(elementsToAddOrRemove,append);
301       if (append) {
302          Iterator it=refCollectionElements.iterator();
303          while (it.hasNext()) {
304             WorkflowProcess wp=(WorkflowProcess)it.next();
305             if (isMine(wp)) {
306                wp.setIDPrefixForCollections();
307             }
308          }
309       }
310    }
311
312    /**
313     * Overrides parent method to be accessible within PEGraph.
314     */

315    public void decrementID () {
316       super.decrementID();
317    }
318
319    /**
320     * Overrides parent method to be accessible within workflow process.
321     */

322    protected void updateID (String JavaDoc someID) {
323       super.updateID(someID);
324    }
325
326 }
327
Popular Tags