001: package org.enhydra.jawe.components.graph;
002:
003: import java.util.ArrayList;
004: import java.util.HashMap;
005: import java.util.HashSet;
006: import java.util.Iterator;
007: import java.util.List;
008: import java.util.Map;
009: import java.util.Set;
010:
011: import org.enhydra.jawe.ResourceManager;
012: import org.enhydra.shark.xpdl.XMLCollectionElement;
013: import org.enhydra.shark.xpdl.XMLElement;
014: import org.enhydra.shark.xpdl.elements.Participants;
015: import org.enhydra.shark.xpdl.elements.WorkflowProcess;
016:
017: /**
018: * Represents collection of imaginary participants for entering
019: * common expressions for activity performers.
020: *
021: * @author Sasa Bojanic
022: */
023: public class CommonExpressionParticipants extends Participants {
024:
025: private static CommonExpressionParticipants instance;
026:
027: private Map wpOrAsToCEPs = new HashMap();
028:
029: public static CommonExpressionParticipants getInstance() {
030: if (instance == null) {
031: instance = new CommonExpressionParticipants();
032: }
033: return instance;
034: }
035:
036: private CommonExpressionParticipants() {
037: super ((WorkflowProcess) null);
038: }
039:
040: public XMLElement generateNewElement() {
041: throw new RuntimeException(
042: "Please use method generateCommonExpressionParticipant (XMLCollectionElement wpOrAs)");
043: }
044:
045: public CommonExpressionParticipant generateCommonExpressionParticipant(
046: XMLCollectionElement wpOrAs) {
047: CommonExpressionParticipant cep = new CommonExpressionParticipant(
048: this , wpOrAs);
049: int i = 1;
050: String prefix = ResourceManager
051: .getLanguageDependentString("CommonExpressionParticipantPrefix")
052: + " ";
053: String id = prefix + String.valueOf(i);
054: while (getCommonExpressionParticipant(wpOrAs, id) != null) {
055: i++;
056: id = prefix + String.valueOf(i);
057: }
058: cep.setId(id);
059: Set ceps = (Set) wpOrAsToCEPs.get(wpOrAs);
060: if (ceps == null) {
061: ceps = new HashSet();
062: }
063: ceps.add(cep);
064: wpOrAsToCEPs.put(wpOrAs, ceps);
065: return cep;
066: }
067:
068: public Set getCommonExpressionParticipants(
069: XMLCollectionElement wpOrAs) {
070: Set ceps = (Set) wpOrAsToCEPs.get(wpOrAs);
071: if (ceps == null) {
072: ceps = new HashSet();
073: }
074: return ceps;
075: }
076:
077: public CommonExpressionParticipant getUpdatedCommonExpressionParticipant(
078: List vo, XMLCollectionElement wpOrAs) {
079: Set ceps = (Set) wpOrAsToCEPs.get(wpOrAs);
080: CommonExpressionParticipant cep = null;
081: if (ceps != null) {
082: Iterator it = ceps.iterator();
083: while (it.hasNext()) {
084: CommonExpressionParticipant p = (CommonExpressionParticipant) it
085: .next();
086: String idToSearch = GraphEAConstants.COMMON_EXPRESSION_PARTICIPANT_PREFIX
087: + p.getId()
088: + GraphEAConstants.COMMON_EXPRESSION_PARTICIPANT_SUFIX;
089: if (!vo.contains(idToSearch)) {
090: cep = p;
091: break;
092: }
093: }
094: }
095: if (cep == null) {
096: // System.err.println("CAN'T FIND UPDATEDCEP !!!");
097: } else {
098: // System.err.println("GETUPDATEDCEP -> "+cep.getId());
099: }
100: return cep;
101: }
102:
103: public void removeUnusedCommonExpressionParticipants(List vo,
104: XMLCollectionElement wpOrAs) {
105: List cepsToRemove = new ArrayList();
106: Set s = (Set) wpOrAsToCEPs.get(wpOrAs);
107: if (s != null) {
108: Iterator it = s.iterator();
109: while (it.hasNext()) {
110: CommonExpressionParticipant cep = (CommonExpressionParticipant) it
111: .next();
112: String idToSearch = GraphEAConstants.COMMON_EXPRESSION_PARTICIPANT_PREFIX
113: + cep.getId()
114: + GraphEAConstants.COMMON_EXPRESSION_PARTICIPANT_SUFIX;
115: if (!vo.contains(idToSearch)) {
116: cepsToRemove.add(cep);
117: }
118: }
119: it = cepsToRemove.iterator();
120: while (it.hasNext()) {
121: CommonExpressionParticipant cepToRemove = (CommonExpressionParticipant) it
122: .next();
123: s.remove(cepToRemove);
124: // System.err.println("REMUNUSEDCEPS -> removing cep "+cepToRemove.getId());
125: }
126: }
127: }
128:
129: public CommonExpressionParticipant getCommonExpressionParticipant(
130: XMLCollectionElement wpOrAs, String id) {
131: Set ceps = (Set) wpOrAsToCEPs.get(wpOrAs);
132: CommonExpressionParticipant cep = null;
133: if (ceps != null) {
134: Iterator it = ceps.iterator();
135: while (it.hasNext()) {
136: CommonExpressionParticipant p = (CommonExpressionParticipant) it
137: .next();
138: if (p.getId().equals(id)) {
139: cep = p;
140: break;
141: }
142: }
143: }
144: if (cep != null) {
145: // System.err.println("GETCEP -> Found cep "+id);
146: } else {
147: // System.err.println("GETCEP -> Can't find cep "+id);
148: }
149: return cep;
150: }
151:
152: public void printList(XMLCollectionElement wpOrAs) {
153: Set ceps = getCommonExpressionParticipants(wpOrAs);
154: Iterator it = ceps.iterator();
155: int i = 1;
156: while (it.hasNext()) {
157: System.err
158: .println("cep "
159: + (i++)
160: + " is "
161: + ((CommonExpressionParticipant) it.next())
162: .getId());
163: }
164: }
165:
166: public String getIdForVisualOrderEA(String pId) {
167: return GraphEAConstants.COMMON_EXPRESSION_PARTICIPANT_PREFIX
168: + pId
169: + GraphEAConstants.COMMON_EXPRESSION_PARTICIPANT_SUFIX;
170: }
171:
172: public String getIdFromVisualOrderEA(String pId) {
173: return pId
174: .substring(
175: GraphEAConstants.COMMON_EXPRESSION_PARTICIPANT_PREFIX
176: .length(),
177: pId.length()
178: - GraphEAConstants.COMMON_EXPRESSION_PARTICIPANT_SUFIX
179: .length());
180: }
181:
182: public boolean isCommonExpressionParticipantId(String pId) {
183: return pId
184: .startsWith(GraphEAConstants.COMMON_EXPRESSION_PARTICIPANT_PREFIX)
185: && pId
186: .endsWith(GraphEAConstants.COMMON_EXPRESSION_PARTICIPANT_SUFIX);
187: }
188:
189: }
|