WaitHelper.java :  » Workflow-Engines » JFolder » org » jfolder » workflow » model » attributes » Java Open Source

Java Open Source » Workflow Engines » JFolder 
JFolder » org » jfolder » workflow » model » attributes » WaitHelper.java
/*
 * JFolder, Copyright 2001-2006 Gary Steinmetz
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package org.jfolder.workflow.model.attributes;

//base classes
import java.util.ArrayList;
import java.util.StringTokenizer;

//project specific classes
import org.jfolder.workflow.query.AndQC;
import org.jfolder.workflow.query.OrQC;
import org.jfolder.workflow.query.QueryContainer;
import org.jfolder.workflow.query.SysStringIsEqualQC;
import org.jfolder.workflow.query.SysStringIsLikeQC;
import org.jfolder.workflow.query.SysStringIsNotEqualQC;

//other classes

public class WaitHelper {
    
    //  state[trace1-,trace2,trace3+]
    
    private final static String RELEASE_TOKEN = "+";
    private final static String TRACE_SEPERATOR = ",";
    private final static String CONTAINER_START = "[";
    private final static String CONTAINER_END = "]";
    
    public final static String NO_WAIT = "";
    
    private final static boolean ADD = true;
    private final static boolean REMOVE = false;
    
    private WaitHelper() {
    }
    
    private final static boolean isTraceAtState(String inHandle,
        String inState, String inTrace) {
        
        boolean outValue = false;
        
        String traces[] = findAllTracesWaitingAtState(inHandle, inState);
        
        for (int i = 0; i < traces.length; i++) {
            outValue |= inTrace.equals(traces[i]);
        }
        
        return outValue;
    }
    
    public final static String[] findAllTracesWaitingAtState(String inHandle,
        String inState) {
        
        String outValue[] = null;
        
        //MiscHelper.println("WaitHelper - findAll - inHandle = " + inHandle);
        //MiscHelper.println("WaitHelper - findAll - inState = " + inState);
        
        int startIndex = -1;
        String traceStart = inState + CONTAINER_START;
        if (inHandle.startsWith(traceStart)) {
            startIndex = traceStart.length();
        }
        else {
            traceStart = CONTAINER_END + traceStart;
            startIndex = inHandle.indexOf(traceStart);
            if (startIndex != -1) {
                startIndex = startIndex + traceStart.length();
            }
        }
        //MiscHelper.println("WaitHelper - findAll - startIndex = "+startIndex);
        
        if (startIndex != -1) {
            int endIndex = inHandle.indexOf(CONTAINER_END, startIndex + 1);
            //MiscHelper.println("WaitHelper - findAll - endIndex = "+endIndex);
            String traceHandle = inHandle.substring(startIndex, endIndex);
            //MiscHelper.println("WaitHelper - findAll - tHand = "+traceHandle);
            
            ArrayList traceList = new ArrayList();
            StringTokenizer st = new StringTokenizer(traceHandle,
                TRACE_SEPERATOR);
            while (st.hasMoreTokens()) {
                String nextToken = st.nextToken();
                if (nextToken.length() > 0) {
                    traceList.add(nextToken);
                }
            }
            
            outValue = new String[traceList.size()];
            for (int i = 0; i < outValue.length; i++) {
                outValue[i] = (String)traceList.get(i);
            }
        }
        else {
            outValue = new String[0];
        }
        
        //MiscHelper.println("WaitHelper - findAll - out = " + outValue);
        //MiscHelper.println("WaitHelper - findAll - len = " + outValue.length);
        
        return outValue;
    }
    
    private final static String removeStateFromHandle(String inHandle,
        String inState) {
        
        String outValue = null;
        
        int startIndex = -1;
        int endIndex = -1;
        String traceStart = inState + CONTAINER_START;
        if (inHandle.startsWith(traceStart)) {
            startIndex = traceStart.length();
        }
        else {
            traceStart = CONTAINER_END + traceStart;
            startIndex = inHandle.indexOf(traceStart);
            if (startIndex != -1) {
                startIndex = startIndex + traceStart.length();
            }
        }
        
        if (startIndex != -1) {
            endIndex = inHandle.indexOf(CONTAINER_END, startIndex + 1);
            //move start index back to beginning of the state name
            startIndex = startIndex - (inState + CONTAINER_START).length();
            
            outValue = inHandle.substring(0, startIndex)
                + inHandle.substring(endIndex + 1);
            
        }
        else {
            outValue = inHandle;
        }
        
        
        return outValue;
    }
    
    private final static String addStateAndTracesToHandle(String inHandle,
        String inState, String inTraces[]) {
        
        StringBuffer outValue = new StringBuffer();
        
        outValue.append(inHandle);
        if (inTraces.length > 0) {
            outValue.append(inState);
            outValue.append(CONTAINER_START);
            for (int i = 0; i < inTraces.length; i++) {
                outValue.append(inTraces[i]);
                outValue.append(TRACE_SEPERATOR);
            }
            outValue.append(CONTAINER_END);
        }
        
        return outValue.toString();
    }
    
    private final static String[] adjustTrace(String inTraces[],
        String inTrace, boolean inAdd) {
        
        String outValue[] = null;
        
        ArrayList traceList = new ArrayList();
        for (int i = 0; i < inTraces.length; i++) {
            if (!inTrace.equals(inTraces[i])) {
                traceList.add(inTraces[i]);
            }
        }
        if (inAdd) {
            traceList.add(inTrace);
        }
        
        outValue = new String[traceList.size()];
        for (int i = 0; i < outValue.length; i++) {
            outValue[i] = (String)traceList.get(i);
        }
        
        return outValue;
    }
    
    private final static String adjustHandle(String inHandle,
        String inState, String inTrace, boolean inAdd) {
            
        String outValue = null;
        
        String traces[] = findAllTracesWaitingAtState(inHandle, inState);
        traces = adjustTrace(traces, inTrace, inAdd);
        String base = removeStateFromHandle(inHandle, inState);
        outValue = addStateAndTracesToHandle(base, inState, traces);
        
        return outValue;
    }
    
    public final static boolean isTraceWaitingAtState(String inHandle,
        String inState, String inTrace) {
        
        //MiscHelper.println("isTraceWaitingAtState - handle - "
        //    + inHandle);
        //MiscHelper.println("isTraceWaitingAtState - state - "
        //    + inState);
        //MiscHelper.println("isTraceWaitingAtState - trace - "
        //    + inTrace);
        //MiscHelper.println("isTraceWaitingAtState - result - "
        //    + isTraceAtState(inHandle, inState, inTrace));
        
        return isTraceAtState(inHandle, inState, inTrace);
    }
    
    public final static boolean isTraceWaitingAtStateButReleased(
        String inHandle, String inState, String inTrace) {
        
        //MiscHelper.println("isTraceWaitingAtStateButReleased - handle - "
        //    + inHandle);
        //MiscHelper.println("isTraceWaitingAtStateButReleased - state - "
        //    + inState);
        //MiscHelper.println("isTraceWaitingAtStateButReleased - trace - "
        //    + inTrace);
        //MiscHelper.println("isTraceWaitingAtStateButReleased - result - "
        //    + isTraceAtState(inHandle, inState + RELEASE_TOKEN, inTrace));
        
        return isTraceAtState(inHandle, inState + RELEASE_TOKEN, inTrace);
    }
    
    public final static boolean isTraceNotWaitingAtState(String inHandle,
        String inState, String inTrace) {
        
        boolean waitingOrReleased =
            isTraceWaitingAtState(inHandle, inState, inTrace)
                || isTraceWaitingAtStateButReleased(inHandle, inState, inTrace);
        
        //MiscHelper.println("isTraceNotWaitingAtState - handle - "
        //    + inHandle);
        //MiscHelper.println("isTraceNotWaitingAtState - state - "
        //    + inState);
        //MiscHelper.println("isTraceNotWaitingAtState - trace - "
        //    + inTrace);
        //MiscHelper.println("isTraceNotWaitingAtState - result - "
        //    + !waitingOrReleased);
        
        return !waitingOrReleased;
    }
    
    public final static String releaseWaitAtState(String inHandle,
        String inState, String inTrace) {
            
        String outValue = null;
        
        outValue = inHandle;
        //MiscHelper.println("WaitHelper - release - before - " + outValue);
        outValue = adjustHandle(outValue, inState, inTrace, REMOVE);
        outValue = adjustHandle(outValue, inState + RELEASE_TOKEN, inTrace,
            ADD);
        //MiscHelper.println("WaitHelper - release - after - " + outValue);
        
        return outValue;
    }
    
    public final static String establishWaitAtState(String inHandle,
        String inState, String inTrace) {
            
        String outValue = null;
        
        outValue = inHandle;
        //MiscHelper.println("WaitHelper - establish - before - " + outValue);
        outValue = adjustHandle(outValue, inState, inTrace, ADD);
        //MiscHelper.println("WaitHelper - establish - after - " + outValue);
        
        return outValue;
    }
    
    public final static String enableWaitAtState(String inHandle,
        String inState, String inTrace) {
        
        String outValue = null;
        
        outValue = inHandle;
        //MiscHelper.println("WaitHelper - enable - before - " + outValue);
        outValue = adjustHandle(outValue, inState + RELEASE_TOKEN, inTrace,
            REMOVE);
        //MiscHelper.println("WaitHelper - enable - after - " + outValue);
        
        return outValue;
    }
    
    //QUERIES
    public final static QueryContainer getWaitQC(String inApp, String inState) {
        
        QueryContainer outValue = null;
        
        QueryContainer waitQc = OrQC.createQuery(
            SysStringIsLikeQC.createQuery(
                AttributeSet.WAIT, inState + "[%"),
            SysStringIsLikeQC.createQuery(
                AttributeSet.WAIT, "%]" + inState + "[%"));
        
        if (inApp != null) {
            outValue = AndQC.createQuery(getAppQC(inApp), waitQc);
        }
        else {
            outValue = waitQc;
        }
        
        return outValue;
    }
    
    public final static QueryContainer getWaitQC(String inApp) {
        
        QueryContainer outValue = null;
        
        QueryContainer waitQc = SysStringIsNotEqualQC.createQuery(
            AttributeSet.WAIT, "");
        
        outValue = AndQC.createQuery(getAppQC(inApp), waitQc);
        
        return outValue;
    }
    
    private final static QueryContainer getAppQC(String inApp) {
        
        QueryContainer outValue = null;
        
        outValue = SysStringIsEqualQC.createQuery(AttributeSet.APP, inApp);
        
        return outValue;
    }
    
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.