KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* Participants.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 java.awt.*;
18 import javax.swing.tree.*;
19 import javax.swing.event.*;
20 import org.w3c.dom.*;
21
22 /**
23  * Represents a WfMC DTD element that has the similar name.
24  * Participants are collection of class Participant instances.
25  *
26  * @see Participant
27  */

28 public class Participants extends XMLCollection {
29    private ArrayList externalParticipants=new ArrayList();
30
31    /**
32     * Creates a new instance of the class.
33     * Constructor must know who the senior element is, because behaviour
34     * depends entirely on this information. Package contained Participants
35     * are visible throughout package, whilst those created whitin workflow
36     * process can be used only locally.
37     *
38     * @param senior DTDComplexElement
39     */

40    public Participants(XMLComplexElement senior) {
41       super(senior);
42       coloringTable=true;
43    }
44
45    /**
46    * Checks if specified participant can be removed. Participant can be
47    * removed only if it is not used anywhere.
48    */

49    public boolean canRemoveElement (XMLElement el) {
50       Participant toRemove=(Participant)el;
51       // if this participant doesn't belong to collection, return false
52
if (!isMine(toRemove)) return false;
53
54       return canRemoveParticipant(toRemove);
55    }
56
57    public boolean canRemoveParticipant (Participant toRemove) {
58       // check if it is somewhere in the graph of processes
59
if (toRemove.isGraphContained()) {
60 //System.out.println("Participant "+toRemove+" has "+toRemove.getNoOfGraphReferences()+" graph references");
61
return false;
62       }
63
64       // List of workflow processes to check. If this is
65
// participants instance at workflow process level, the list
66
// will contain only the owner of this instance, and
67
// if this is participants instance at package level,
68
// it will contain all processes within package
69
ArrayList wpsToCheck=new ArrayList();
70
71       if (myOwner instanceof Package JavaDoc) {
72          // check if participant is responsible for the package
73
Responsibles rs=(Responsibles)((RedefinableHeader)myOwner.
74             get("RedefinableHeader")).get("Responsibles");
75          if (!rs.canRemoveParticipant(toRemove)) {
76             return false;
77          }
78          // add all process to check
79
WorkflowProcesses wps=(WorkflowProcesses)myOwner.get("WorkflowProcesses");
80          wpsToCheck.addAll(wps.toCollection());
81       } else {
82          wpsToCheck.add(myOwner);
83       }
84
85       // check all processes for removal
86
Iterator it=wpsToCheck.iterator();
87       while (it.hasNext()) {
88          WorkflowProcess wp=(WorkflowProcess)it.next();
89          // check if it is a performer of some activity
90
Activities acts=(Activities)wp.get("Activities");
91          if (!acts.canRemoveParticipant(toRemove)) {
92             return false;
93          }
94          ActivitySets actsts=(ActivitySets)wp.get("ActivitySets");
95          if (!actsts.canRemoveParticipant(toRemove)) {
96             return false;
97          }
98
99          // check if it has referenced as responsible for a process
100
Responsibles rs=(Responsibles)((RedefinableHeader)wp.
101             get("RedefinableHeader")).get("Responsibles");
102          if (!rs.canRemoveParticipant(toRemove)) {
103             return false;
104          }
105       }
106       return true;
107    }
108
109    /**
110     * Oddly named method tells if this instance of Participants is
111     * workflow process contained, thus is able to show or hide participants.
112     * This feature is meaningless for package Participants.
113     *
114     * @return true - for workflow process contained Participants
115     */

116    public boolean canHideOrShow() {
117       return (myOwner instanceof WorkflowProcess);
118    }
119
120    /**
121    * Generates a new element of the class which instances
122    * are members of collection of this class.
123    *
124    * return The generated instance of class that makes collection.
125    */

126    public XMLElement generateNewElement() {
127       Participant p=new Participant(this);
128       p.setRequired(true);
129       p.get("ExternalReference").setRequired(false);
130       return p;
131    }
132
133    /**
134    * It is overriden to give the caller all Participants:
135    * <ul>
136    * <li> If Participants instance is defined at the the workflow process level,
137    * it gives all of it's participants plus the ones defined at the package
138    * level (including all participants from externaly referenced packages).
139    * <li> If Participants instance is defined at the the package level,
140    * it gives all of it's participants plus the ones from externaly
141    * referenced packages).
142    * </ul>
143    */

144    public Collection getTableElements () {
145       ArrayList mine = new ArrayList();
146       if (myOwner instanceof Package JavaDoc) {
147          mine.addAll(refCollectionElements);
148          mine.addAll(externalParticipants);
149          mine.add(Participant.getFreeTextExpressionParticipant());
150       } else {
151          Participants ps=(Participants)(((WorkflowProcess)myOwner)
152             .getPackage()).get("Participants");
153          mine.addAll(refCollectionElements);
154          mine.addAll(ps.refCollectionElements);
155          mine.addAll(ps.externalParticipants);
156          mine.add(Participant.getFreeTextExpressionParticipant());
157       }
158
159       // if this is collection within the package that is not external,
160
// remove read-only attribute for all participants that belong
161
// to the collection, and set to others
162
if (!getPackage().isReadOnly()) {
163          Iterator i=mine.iterator();
164          while (i.hasNext()) {
165             Participant p = (Participant)i.next();
166             p.setReadOnly(!isMine(p));
167             // set Id attribute of participant to be read-only
168
//p.get("Id").setReadOnly(true);
169
}
170       }
171       return mine;
172    }
173
174    /**
175     * Returns collection of all participants from the level where this
176     * collection belongs (package or process level) and all levels above,
177     * but leaving out the participants from higher level that were redefined
178     * at lower level.
179     *
180     * @see #getTableElements
181     */

182    public Collection getChoosable() {
183       ArrayList chos = new ArrayList();
184       if (myOwner instanceof Package JavaDoc) {
185          chos.addAll(refCollectionElements);
186          Iterator i=externalParticipants.iterator();
187          while (i.hasNext()) {
188             Participant p=(Participant)i.next();
189             if (getCollectionElement(p.getID())==null) {
190                chos.add(p);
191             }
192          }
193       } else {
194          Participants ps = (Participants)(((WorkflowProcess)myOwner)
195             .getPackage()).get("Participants");
196          chos.addAll(refCollectionElements);
197          Collection eps=ps.getChoosable();
198          Iterator i=eps.iterator();
199          while (i.hasNext()) {
200             Participant p=(Participant)i.next();
201             if (getCollectionElement(p.getID())==null) {
202                chos.add(p);
203             }
204          }
205       }
206
207       // if this is collection within the package that is not external,
208
// remove read-only attribute for all participants that belong
209
// to the collection, and set to others
210
if (!getPackage().isReadOnly()) {
211          Iterator i=chos.iterator();
212          while (i.hasNext()) {
213             Participant p = (Participant) i.next();
214             p.setReadOnly(!isMine(p));
215          }
216       }
217       return chos;
218    }
219
220    public boolean isMine(Participant p) {
221       return refCollectionElements.contains(p);
222    }
223
224    public boolean isExternal (Participant p) {
225       return (p.getPackage()!=getPackage());
226    }
227
228    public Package JavaDoc getPackage () {
229       Package JavaDoc pack = (myOwner instanceof WorkflowProcess)?
230             ((WorkflowProcess) myOwner).getPackage():
231             ((Package JavaDoc) myOwner);
232       return pack;
233    }
234
235    /**
236     *
237     */

238    protected void removeFromExternal(Package JavaDoc ep) {
239       Participants externalPs=(Participants)ep.get("Participants");
240       Iterator it=externalPs.toCollection().iterator();
241       while (it.hasNext()) {
242          Participant p=(Participant)it.next();
243          // removes only participants that directly belong to specified package
244
externalParticipants.remove(p);
245       }
246    }
247
248    /**
249     *
250     */

251    protected void insertFromExternal(Package JavaDoc ep) {
252       Participants externalPs=(Participants)ep.get("Participants");
253       // insert only the ps that owns package 'ep'
254
// and are not referenced from some ext. pack. of 'ep'
255
Iterator it=externalPs.refCollectionElements.iterator();
256       while (it.hasNext()) {
257          Participant p=(Participant)it.next();
258          if (!externalParticipants.contains(p)) {
259             externalParticipants.add(p);
260          }
261       }
262    }
263
264    public Participant getParticipant (String JavaDoc ID) {
265       Participant toReturn=(Participant)super.getCollectionElement(ID);
266
267       // if the participant haven't been found, and this is an instance of
268
// participants at workflow process level, search the package level
269
if ((toReturn==null) && (myOwner instanceof WorkflowProcess)) {
270          toReturn=((Participants)getPackage().get("Participants")).getParticipant(ID);
271       }
272
273       // if the participant haven't been found, search external participants
274
if (toReturn==null) {
275          String JavaDoc extID;
276          Iterator it=externalParticipants.iterator();
277          while (it.hasNext()) {
278             Participant ptmp=(Participant)it.next();
279             extID=ptmp.getID();
280             if (extID.equals(ID)) {
281                toReturn=ptmp;
282                break;
283             }
284          }
285       }
286
287       return toReturn;
288    }
289
290    public String JavaDoc getReadOnlyMessageName (XMLComplexElement el) {
291       if (!refCollectionElements.contains(el)) {
292          if (el==Participant.getFreeTextExpressionParticipant()) {
293             return "WarningCannotDeleteFreeTextExpressionParticipant";
294          } else if (myOwner instanceof Package JavaDoc) {
295             return "WarningCannotDeleteExternalParticipant";
296          } else {
297             return "WarningParticipantDefinedAtPackageLevelCannotBeDeletedFromProcessLevel";
298          }
299       }
300       return "";
301    }
302
303    public String JavaDoc getInUseMessageName (XMLComplexElement el) {
304       return "WarningCannotDeleteParticipantThatIsInUse";
305    }
306
307    public int[] getInvisibleTableFieldOrdinals () {
308       int[] itfo=new int[3];
309       itfo[0]=3;
310       itfo[1]=4;
311       itfo[2]=5;
312       return itfo;
313    }
314
315 }
316
Popular Tags