KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* Transitions.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
18 /**
19 * Represents a WfMC DTD element that has the similar name.
20 * This class is a collection of class <b>Transition</b> instances.
21 *
22 * @see XML
23 */

24 public class Transitions extends XMLCollection {
25    /**
26    * Creates a new instance of the class.
27    *
28    * @param wpOrAs The workflow process or activiy set that holds transitions,
29    * and which member is this class instance.
30    */

31    public Transitions (XMLComplexElement wpOrAs) {
32       super(wpOrAs);
33    }
34
35    /**
36    * Generates a new element of the class which instances
37    * are members of collection of this class.
38    *
39    * return The generated instance of class that makes collection.
40    */

41    public XMLElement generateNewElement() {
42       Transition t=new Transition(this);
43       t.setRequired(true);
44       return t;
45    }
46
47
48    /**
49    * Returns a set of transitions which source or target is the
50    * graph object with specified 'Id' attribute.
51    *
52    * @param ID ID of source or target graph object for
53    * transition, depending of second attribute.
54    * @param st These are possible values:<p>
55    * <ul>
56    * <li> -1 - specified ID is source for transitions
57    * <li> 0 - specified ID is source and target for transitions
58    * <li> 1 - specified ID is target for transitions
59    * </ul>
60    * @return The set of transitions for which the graph object with
61    * specified ID is source or target, depending on second
62    * parameter of this method.
63    */

64    public Set getTransitions (String JavaDoc ID,int st) {
65       Set trans=new HashSet();
66       Transition t;
67       Iterator it=refCollectionElements.iterator();
68       while (it.hasNext()) {
69          t=(Transition)it.next();
70          if (st<=0) {
71             //if (t.get("From").toValue().toString().equals(ID)) {
72
try {
73                if (t.getFrom().get("Id").toString().equals(ID)) {
74                   trans.add(t);
75                }
76             } catch (Exception JavaDoc ex) {}
77          }
78          if (st>=0) {
79             //if (t.get("To").toValue().toString().equals(ID)) {
80
try {
81                if (t.getTo().get("Id").toString().equals(ID)) {
82                   trans.add(t);
83                }
84             } catch (Exception JavaDoc ex) {}
85          }
86       }
87       return trans;
88    }
89
90    /**
91    * Returns the transition object with specified ID.
92    *
93    * @param ID ID of wanted transition
94    * @return The transitions that have specified ID if exists,
95    * <tt>null</tt> otherwise.
96    */

97    public Transition getTransition (String JavaDoc ID) {
98       return (Transition)super.getCollectionElement(ID);
99    }
100
101    /**
102    * Returns <tt>true</tt> if specified data field or formal parameter is
103    * used in transition condition expression of any transition.
104    */

105    protected boolean canRemoveDataFieldOrFormalParameter (XMLCollectionElement toRemove) {
106       String JavaDoc IDToCheck=toRemove.getID();
107       Transition t;
108       Condition c;
109       Xpression x;
110       String JavaDoc tcx;
111       int foundAt;
112       char prev, next;
113       boolean prevOK, nextOK;
114
115       Iterator it=refCollectionElements.iterator();
116       while (it.hasNext()) {
117          t=(Transition)it.next();
118          c=(Condition)t.get("Condition");
119          x=(Xpression)c.get("Xpression");
120          tcx=x.toString();
121          foundAt=tcx.indexOf(IDToCheck);
122          // if given ID is found within expression string
123
// check if ID string is part of some other ID name
124
if (foundAt!=-1) {
125             // check if char previous to the position of found ID is OK
126
if (foundAt==0) {
127                prevOK=true;
128             } else {
129                prev=tcx.charAt(foundAt-1);
130                prevOK=!XMLCollection.isIdValid(String.valueOf(prev));
131             }
132
133             // check if char after found ID string is OK
134
if (foundAt+IDToCheck.length()==tcx.length()) {
135                nextOK=true;
136             } else {
137                next=tcx.charAt(foundAt+IDToCheck.length());
138                nextOK=!XMLCollection.isIdValid(String.valueOf(next));
139             }
140
141             // if this is really the ID, return that it can't be removed
142
if (prevOK && nextOK) {
143                return false;
144             }
145          }
146       }
147       return true;
148    }
149
150    public int[] getInvisibleTableFieldOrdinals () {
151       int[] itfo=new int[4];
152       itfo[0]=2;
153       itfo[1]=3;
154       itfo[2]=5;
155       itfo[3]=6;
156       return itfo;
157    }
158
159    /**
160    * Called by owner object after importing of XML file to do neccessary
161    * things on transitions after importing of XML.
162    */

163    protected void afterImporting () {
164       Iterator it=refCollectionElements.iterator();
165       while (it.hasNext()) {
166          Transition t=(Transition)it.next();
167          t.afterImporting();
168       }
169    }
170
171    // First, the controlled panel must be created, and then the control panel
172
public XMLPanel getPanel () {
173       isReadOnly=true;
174       controlledPanel=new XMLTablePanel(this,"",false,false);
175       controlPanel=new XMLTableControlPanel(this,"",true,false);
176       return new XMLGroupPanel(this,new XMLPanel[]{
177          controlledPanel,controlPanel},toLabel(),XMLPanel.BOX_LAYOUT,
178          false,true);
179    }
180
181    public Object JavaDoc toValue () {
182       return String.valueOf(refCollectionElements.size());
183    }
184
185     public WorkflowProcess getWorkflowProcess () {
186       Object JavaDoc own=getOwner();
187       if (own instanceof ActivitySet) {
188          return ((ActivitySet)own).getOwnerProcess();
189       } else {
190          return (WorkflowProcess)own;
191       }
192     }
193
194    /**
195     * Overrides parent method to handle the case when the activities
196     * from WorkflowProcess collection are copied to ActivitySet collection
197     * and vice-versa.
198     */

199    public void refreshCollection (Set elementsToAddOrRemove,boolean append) {
200       if (append) {
201          for (Iterator it=elementsToAddOrRemove.iterator(); it.hasNext();) {
202             Transition t=(Transition)it.next();
203             // generate new ID because of possible different id-prefixes
204
if (t.getCollection() != this){
205                decrementID();
206                t.get("Id").setValue(this.generateID());
207             }
208             t.setCollection(this);
209             refCollectionElements.add(t);
210          }
211       } else {
212          refCollectionElements.removeAll(elementsToAddOrRemove);
213       }
214    }
215
216
217    /**
218     * Overrides parent method to be accessible within WorkflowManager.
219     */

220    public void decrementID () {
221       getWorkflowProcess().decrementTransitionId();
222    }
223
224    public long getCurrentID () {
225       return getWorkflowProcess().getCurrentTransitionId();
226    }
227
228    public String JavaDoc generateID () {
229       if (IDPrefix==null) IDPrefix="";
230       String JavaDoc ID;
231       do {
232          ID=IDPrefix+new Long JavaDoc(getWorkflowProcess().getNextTransitionId()).toString();
233       } while (getWorkflowProcess().getTransition(ID)!=null);
234       return ID;
235    }
236
237    protected void resetID () {
238       getWorkflowProcess().resetTransitionId();
239    }
240
241    protected void updateID (String JavaDoc someID) {
242       // try to get ID as if it is ours
243
try {
244          long val;
245          if (someID.startsWith(IDPrefix)) {
246             String JavaDoc ID=someID.substring(IDPrefix.length(),someID.length());
247             val=Long.parseLong(ID);
248             if (val>getWorkflowProcess().getCurrentTransitionId()) {
249                getWorkflowProcess().setCurrentTransitionId(val);
250             }
251          }
252       // then try to update someID as if it is only number
253
} catch (Exception JavaDoc ex) {
254          return;
255       }
256    }
257
258 }
259
Popular Tags