001: package org.enhydra.jawe.base.idfactory;
002:
003: import java.util.ArrayList;
004: import java.util.HashSet;
005: import java.util.Iterator;
006: import java.util.List;
007: import java.util.Set;
008:
009: import org.enhydra.jawe.JaWEComponent;
010: import org.enhydra.shark.xpdl.XMLCollection;
011: import org.enhydra.shark.xpdl.XMLCollectionElement;
012: import org.enhydra.shark.xpdl.XMLElement;
013: import org.enhydra.shark.xpdl.XMLUtil;
014: import org.enhydra.shark.xpdl.elements.Activities;
015: import org.enhydra.shark.xpdl.elements.Activity;
016: import org.enhydra.shark.xpdl.elements.ActivitySet;
017: import org.enhydra.shark.xpdl.elements.ActivitySets;
018: import org.enhydra.shark.xpdl.elements.Applications;
019: import org.enhydra.shark.xpdl.elements.DataFields;
020: import org.enhydra.shark.xpdl.elements.FormalParameters;
021: import org.enhydra.shark.xpdl.elements.Participants;
022: import org.enhydra.shark.xpdl.elements.Tool;
023: import org.enhydra.shark.xpdl.elements.Transition;
024: import org.enhydra.shark.xpdl.elements.Transitions;
025: import org.enhydra.shark.xpdl.elements.TypeDeclarations;
026: import org.enhydra.shark.xpdl.elements.WorkflowProcess;
027: import org.enhydra.shark.xpdl.elements.WorkflowProcesses;
028:
029: /**
030: * Factory for generating XPDL objects.
031: * @author Sasa Bojanic
032: */
033: public class IdFactory {
034:
035: protected IdFactorySettings settings;
036:
037: public IdFactory() {
038: settings = new IdFactorySettings();
039: settings.init((JaWEComponent) null);
040: }
041:
042: public IdFactory(IdFactorySettings settings) {
043: this .settings = settings;
044: this .settings.init((JaWEComponent) null);
045: }
046:
047: public String generateUniqueId(XMLCollection cel) {
048: return generateUniqueId(cel, new HashSet());
049: }
050:
051: public String generateUniqueId(XMLCollection cel, Set skipIds) {
052: String id;
053: long nextId = 0;
054: String prefix = "";
055: if (!(cel instanceof WorkflowProcesses)) {
056: prefix = XMLUtil.getPackage(cel).getId() + "_";
057: if (XMLUtil.getWorkflowProcess(cel) != null) {
058: prefix = XMLUtil.getWorkflowProcess(cel).getId() + "_";
059: }
060: if (cel instanceof Activities) {
061: prefix += "act";
062: } else if (cel instanceof ActivitySets) {
063: prefix += "ase";
064: } else if (cel instanceof Applications) {
065: prefix += "app";
066: } else if (cel instanceof DataFields) {
067: prefix += "df";
068: } else if (cel instanceof FormalParameters) {
069: prefix += "fp";
070: } else if (cel instanceof Participants) {
071: prefix += "par";
072: } else if (cel instanceof Transitions) {
073: prefix += "tra";
074: } else if (cel instanceof TypeDeclarations) {
075: prefix += "td";
076: }
077: } else if (cel instanceof WorkflowProcesses) {
078: prefix = XMLUtil.getPackage(cel).getId() + "_wp";
079: }
080:
081: XMLCollectionElement cl = (XMLCollectionElement) cel
082: .generateNewElement();
083: do {
084: id = prefix + new Long(++nextId).toString();
085: } while (skipIds.contains(id) || !isIdUnique(cl, id));
086: return id;
087: }
088:
089: public String generateSimilarOrIdenticalUniqueId(XMLCollection cel,
090: Set skipIds, String origId) {
091: String id = origId;
092: long nextId = 0;
093:
094: XMLCollectionElement cl = (XMLCollectionElement) cel
095: .generateNewElement();
096: while (id.equals("") || skipIds.contains(id)
097: || !isIdUnique(cl, id)) {
098: id = origId + new Long(++nextId).toString();
099: }
100: return id;
101: }
102:
103: public boolean isIdUnique(XMLCollectionElement el, String newId) {
104:
105: XMLElement parent = el.getParent();
106: if (el instanceof Tool)
107: return true;
108: else if (el instanceof Activity)
109: return checkActivityId((Activity) el, newId);
110: else if (el instanceof Transition)
111: return checkTransitionId((Transition) el, newId);
112: else if (parent instanceof XMLCollection) {
113: List elsWithId = getElementsForId((XMLCollection) parent,
114: newId);
115: if (elsWithId.size() == 0
116: || (elsWithId.size() == 1 && elsWithId.contains(el))) {
117: return true;
118: }
119: return false;
120: } else {
121: return true;
122: }
123: }
124:
125: public boolean checkActivityId(Activity newEl, String newId) {
126: WorkflowProcess proc = XMLUtil.getWorkflowProcess(newEl);
127: Activities acts = proc.getActivities();
128: List elsWithId = getElementsForId(acts, newId);
129: ActivitySets actSets = proc.getActivitySets();
130: for (int y = 0; y < actSets.size(); y++) {
131: ActivitySet actSet = (ActivitySet) actSets.get(y);
132: acts = actSet.getActivities();
133: elsWithId.addAll(getElementsForId(acts, newId));
134: }
135: if (elsWithId.size() == 0
136: || (elsWithId.size() == 1 && elsWithId.contains(newEl))) {
137: return true;
138: }
139: return false;
140: }
141:
142: public boolean checkTransitionId(Transition newEl, String newId) {
143: WorkflowProcess proc = XMLUtil.getWorkflowProcess(newEl);
144:
145: Transitions trans = proc.getTransitions();
146: List elsWithId = getElementsForId(trans, newId);
147: ActivitySets actSets = proc.getActivitySets();
148: for (int y = 0; y < actSets.size(); y++) {
149: ActivitySet actSet = (ActivitySet) actSets.get(y);
150: trans = actSet.getTransitions();
151: elsWithId.addAll(getElementsForId(trans, newId));
152: }
153: if (elsWithId.size() == 0
154: || (elsWithId.size() == 1 && elsWithId.contains(newEl))) {
155: return true;
156: }
157: return false;
158: }
159:
160: public List getElementsForId(XMLCollection col, String id) {
161: List elsWithId = new ArrayList();
162: Iterator it = col.toElements().iterator();
163: if (col.generateNewElement() instanceof XMLCollectionElement) {
164: while (it.hasNext()) {
165: XMLCollectionElement ce = (XMLCollectionElement) it
166: .next();
167: if (ce.getId().equals(id)) {
168: elsWithId.add(ce);
169: }
170: }
171: }
172:
173: return elsWithId;
174: }
175:
176: }
|