001: /*
002: * JFolder, Copyright 2001-2006 Gary Steinmetz
003: *
004: * Distributable under LGPL license.
005: * See terms of license at gnu.org.
006: */
007:
008: package org.jfolder.workflow.model.attributes;
009:
010: //base classes
011: import java.util.ArrayList;
012: import java.util.StringTokenizer;
013:
014: //project specific classes
015: import org.jfolder.workflow.query.AndQC;
016: import org.jfolder.workflow.query.OrQC;
017: import org.jfolder.workflow.query.QueryContainer;
018: import org.jfolder.workflow.query.SysStringIsEqualQC;
019: import org.jfolder.workflow.query.SysStringIsLikeQC;
020: import org.jfolder.workflow.query.SysStringIsNotEqualQC;
021:
022: //other classes
023:
024: public class WaitHelper {
025:
026: // state[trace1-,trace2,trace3+]
027:
028: private final static String RELEASE_TOKEN = "+";
029: private final static String TRACE_SEPERATOR = ",";
030: private final static String CONTAINER_START = "[";
031: private final static String CONTAINER_END = "]";
032:
033: public final static String NO_WAIT = "";
034:
035: private final static boolean ADD = true;
036: private final static boolean REMOVE = false;
037:
038: private WaitHelper() {
039: }
040:
041: private final static boolean isTraceAtState(String inHandle,
042: String inState, String inTrace) {
043:
044: boolean outValue = false;
045:
046: String traces[] = findAllTracesWaitingAtState(inHandle, inState);
047:
048: for (int i = 0; i < traces.length; i++) {
049: outValue |= inTrace.equals(traces[i]);
050: }
051:
052: return outValue;
053: }
054:
055: public final static String[] findAllTracesWaitingAtState(
056: String inHandle, String inState) {
057:
058: String outValue[] = null;
059:
060: //MiscHelper.println("WaitHelper - findAll - inHandle = " + inHandle);
061: //MiscHelper.println("WaitHelper - findAll - inState = " + inState);
062:
063: int startIndex = -1;
064: String traceStart = inState + CONTAINER_START;
065: if (inHandle.startsWith(traceStart)) {
066: startIndex = traceStart.length();
067: } else {
068: traceStart = CONTAINER_END + traceStart;
069: startIndex = inHandle.indexOf(traceStart);
070: if (startIndex != -1) {
071: startIndex = startIndex + traceStart.length();
072: }
073: }
074: //MiscHelper.println("WaitHelper - findAll - startIndex = "+startIndex);
075:
076: if (startIndex != -1) {
077: int endIndex = inHandle.indexOf(CONTAINER_END,
078: startIndex + 1);
079: //MiscHelper.println("WaitHelper - findAll - endIndex = "+endIndex);
080: String traceHandle = inHandle.substring(startIndex,
081: endIndex);
082: //MiscHelper.println("WaitHelper - findAll - tHand = "+traceHandle);
083:
084: ArrayList traceList = new ArrayList();
085: StringTokenizer st = new StringTokenizer(traceHandle,
086: TRACE_SEPERATOR);
087: while (st.hasMoreTokens()) {
088: String nextToken = st.nextToken();
089: if (nextToken.length() > 0) {
090: traceList.add(nextToken);
091: }
092: }
093:
094: outValue = new String[traceList.size()];
095: for (int i = 0; i < outValue.length; i++) {
096: outValue[i] = (String) traceList.get(i);
097: }
098: } else {
099: outValue = new String[0];
100: }
101:
102: //MiscHelper.println("WaitHelper - findAll - out = " + outValue);
103: //MiscHelper.println("WaitHelper - findAll - len = " + outValue.length);
104:
105: return outValue;
106: }
107:
108: private final static String removeStateFromHandle(String inHandle,
109: String inState) {
110:
111: String outValue = null;
112:
113: int startIndex = -1;
114: int endIndex = -1;
115: String traceStart = inState + CONTAINER_START;
116: if (inHandle.startsWith(traceStart)) {
117: startIndex = traceStart.length();
118: } else {
119: traceStart = CONTAINER_END + traceStart;
120: startIndex = inHandle.indexOf(traceStart);
121: if (startIndex != -1) {
122: startIndex = startIndex + traceStart.length();
123: }
124: }
125:
126: if (startIndex != -1) {
127: endIndex = inHandle.indexOf(CONTAINER_END, startIndex + 1);
128: //move start index back to beginning of the state name
129: startIndex = startIndex
130: - (inState + CONTAINER_START).length();
131:
132: outValue = inHandle.substring(0, startIndex)
133: + inHandle.substring(endIndex + 1);
134:
135: } else {
136: outValue = inHandle;
137: }
138:
139: return outValue;
140: }
141:
142: private final static String addStateAndTracesToHandle(
143: String inHandle, String inState, String inTraces[]) {
144:
145: StringBuffer outValue = new StringBuffer();
146:
147: outValue.append(inHandle);
148: if (inTraces.length > 0) {
149: outValue.append(inState);
150: outValue.append(CONTAINER_START);
151: for (int i = 0; i < inTraces.length; i++) {
152: outValue.append(inTraces[i]);
153: outValue.append(TRACE_SEPERATOR);
154: }
155: outValue.append(CONTAINER_END);
156: }
157:
158: return outValue.toString();
159: }
160:
161: private final static String[] adjustTrace(String inTraces[],
162: String inTrace, boolean inAdd) {
163:
164: String outValue[] = null;
165:
166: ArrayList traceList = new ArrayList();
167: for (int i = 0; i < inTraces.length; i++) {
168: if (!inTrace.equals(inTraces[i])) {
169: traceList.add(inTraces[i]);
170: }
171: }
172: if (inAdd) {
173: traceList.add(inTrace);
174: }
175:
176: outValue = new String[traceList.size()];
177: for (int i = 0; i < outValue.length; i++) {
178: outValue[i] = (String) traceList.get(i);
179: }
180:
181: return outValue;
182: }
183:
184: private final static String adjustHandle(String inHandle,
185: String inState, String inTrace, boolean inAdd) {
186:
187: String outValue = null;
188:
189: String traces[] = findAllTracesWaitingAtState(inHandle, inState);
190: traces = adjustTrace(traces, inTrace, inAdd);
191: String base = removeStateFromHandle(inHandle, inState);
192: outValue = addStateAndTracesToHandle(base, inState, traces);
193:
194: return outValue;
195: }
196:
197: public final static boolean isTraceWaitingAtState(String inHandle,
198: String inState, String inTrace) {
199:
200: //MiscHelper.println("isTraceWaitingAtState - handle - "
201: // + inHandle);
202: //MiscHelper.println("isTraceWaitingAtState - state - "
203: // + inState);
204: //MiscHelper.println("isTraceWaitingAtState - trace - "
205: // + inTrace);
206: //MiscHelper.println("isTraceWaitingAtState - result - "
207: // + isTraceAtState(inHandle, inState, inTrace));
208:
209: return isTraceAtState(inHandle, inState, inTrace);
210: }
211:
212: public final static boolean isTraceWaitingAtStateButReleased(
213: String inHandle, String inState, String inTrace) {
214:
215: //MiscHelper.println("isTraceWaitingAtStateButReleased - handle - "
216: // + inHandle);
217: //MiscHelper.println("isTraceWaitingAtStateButReleased - state - "
218: // + inState);
219: //MiscHelper.println("isTraceWaitingAtStateButReleased - trace - "
220: // + inTrace);
221: //MiscHelper.println("isTraceWaitingAtStateButReleased - result - "
222: // + isTraceAtState(inHandle, inState + RELEASE_TOKEN, inTrace));
223:
224: return isTraceAtState(inHandle, inState + RELEASE_TOKEN,
225: inTrace);
226: }
227:
228: public final static boolean isTraceNotWaitingAtState(
229: String inHandle, String inState, String inTrace) {
230:
231: boolean waitingOrReleased = isTraceWaitingAtState(inHandle,
232: inState, inTrace)
233: || isTraceWaitingAtStateButReleased(inHandle, inState,
234: inTrace);
235:
236: //MiscHelper.println("isTraceNotWaitingAtState - handle - "
237: // + inHandle);
238: //MiscHelper.println("isTraceNotWaitingAtState - state - "
239: // + inState);
240: //MiscHelper.println("isTraceNotWaitingAtState - trace - "
241: // + inTrace);
242: //MiscHelper.println("isTraceNotWaitingAtState - result - "
243: // + !waitingOrReleased);
244:
245: return !waitingOrReleased;
246: }
247:
248: public final static String releaseWaitAtState(String inHandle,
249: String inState, String inTrace) {
250:
251: String outValue = null;
252:
253: outValue = inHandle;
254: //MiscHelper.println("WaitHelper - release - before - " + outValue);
255: outValue = adjustHandle(outValue, inState, inTrace, REMOVE);
256: outValue = adjustHandle(outValue, inState + RELEASE_TOKEN,
257: inTrace, ADD);
258: //MiscHelper.println("WaitHelper - release - after - " + outValue);
259:
260: return outValue;
261: }
262:
263: public final static String establishWaitAtState(String inHandle,
264: String inState, String inTrace) {
265:
266: String outValue = null;
267:
268: outValue = inHandle;
269: //MiscHelper.println("WaitHelper - establish - before - " + outValue);
270: outValue = adjustHandle(outValue, inState, inTrace, ADD);
271: //MiscHelper.println("WaitHelper - establish - after - " + outValue);
272:
273: return outValue;
274: }
275:
276: public final static String enableWaitAtState(String inHandle,
277: String inState, String inTrace) {
278:
279: String outValue = null;
280:
281: outValue = inHandle;
282: //MiscHelper.println("WaitHelper - enable - before - " + outValue);
283: outValue = adjustHandle(outValue, inState + RELEASE_TOKEN,
284: inTrace, REMOVE);
285: //MiscHelper.println("WaitHelper - enable - after - " + outValue);
286:
287: return outValue;
288: }
289:
290: //QUERIES
291: public final static QueryContainer getWaitQC(String inApp,
292: String inState) {
293:
294: QueryContainer outValue = null;
295:
296: QueryContainer waitQc = OrQC.createQuery(SysStringIsLikeQC
297: .createQuery(AttributeSet.WAIT, inState + "[%"),
298: SysStringIsLikeQC.createQuery(AttributeSet.WAIT, "%]"
299: + inState + "[%"));
300:
301: if (inApp != null) {
302: outValue = AndQC.createQuery(getAppQC(inApp), waitQc);
303: } else {
304: outValue = waitQc;
305: }
306:
307: return outValue;
308: }
309:
310: public final static QueryContainer getWaitQC(String inApp) {
311:
312: QueryContainer outValue = null;
313:
314: QueryContainer waitQc = SysStringIsNotEqualQC.createQuery(
315: AttributeSet.WAIT, "");
316:
317: outValue = AndQC.createQuery(getAppQC(inApp), waitQc);
318:
319: return outValue;
320: }
321:
322: private final static QueryContainer getAppQC(String inApp) {
323:
324: QueryContainer outValue = null;
325:
326: outValue = SysStringIsEqualQC.createQuery(AttributeSet.APP,
327: inApp);
328:
329: return outValue;
330: }
331:
332: }
|