WfActivityIteratorWrapper.java :  » Workflow-Engines » shark » org » enhydra » shark » Java Open Source

Java Open Source » Workflow Engines » shark 
shark » org » enhydra » shark » WfActivityIteratorWrapper.java
package org.enhydra.shark;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.enhydra.shark.api.client.wfmc.wapi.WMSessionHandle;
import org.enhydra.shark.api.client.wfmodel.WfActivity;
import org.enhydra.shark.api.client.wfmodel.WfActivityIterator;
import org.enhydra.shark.api.common.SharkConstants;
import org.enhydra.shark.api.internal.instancepersistence.ActivityPersistenceObject;
import org.enhydra.shark.api.internal.instancepersistence.ActivityVariablePersistenceObject;
import org.enhydra.shark.api.internal.instancepersistence.PersistentManagerInterface;
import org.enhydra.shark.api.internal.instancepersistence.ProcessPersistenceObject;
import org.enhydra.shark.api.internal.instancepersistence.ProcessVariablePersistenceObject;
import org.enhydra.shark.api.internal.scripting.Evaluator;
import org.enhydra.shark.utilities.MiscUtilities;

/**
 * Iterator for activities in the process. The following names may be used in queries:
 * key, name, priority, description, state, activitySetDefinitionId, definitionId,
 * activatedTime_ms, lastStateTime_ms, limitTime_ms, resourceUsername, accepted,
 * acceptedTime_ms. Also the names of activity context variables can be used, but the
 * "context_" prefix should be placed before variable Id, i.e. "context_myvariable".
 * 
 * @author Sasa Bojanic
 * @version 1.0
 */
public class WfActivityIteratorWrapper extends BaseIteratorWrapper implements
                                                                  WfActivityIterator {

   protected String procId;

   protected WfActivityIteratorWrapper(WMSessionHandle shandle, String procId) throws Exception {
      super(shandle);
      this.procId = procId;
   }

   public WfActivity get_next_object() throws Exception {
      long tStamp = SharkUtilities.methodStart(shandle,"WfActivityIteratorWrapper.get_next_object");
      try {
         checkSecurity("get_next_object", null);

         return (WfActivity) super.getNextObject();
      } finally {
         SharkUtilities.methodEnd(shandle,tStamp,
                                  "WfActivityIteratorWrapper.get_next_object",
                                  this);
      }
   }

   public WfActivity get_previous_object() throws Exception {
      long tStamp = SharkUtilities.methodStart(shandle,"WfActivityIteratorWrapper.get_previous_object");
      try {
         checkSecurity("get_previous_object", null);

         return (WfActivity) super.getPreviousObject();
      } finally {
         SharkUtilities.methodEnd(shandle,tStamp,
                                  "WfActivityIteratorWrapper.get_previous_object",
                                  this);
      }
   }

   public WfActivity[] get_next_n_sequence(int max_number) throws Exception {
      long tStamp = SharkUtilities.methodStart(shandle,"WfActivityIteratorWrapper.get_next_n_sequence");
      try {
         checkSecurity("get_next_n_sequence", null);

         WfActivity[] ret = null;
         List l = super.getNextNSequence(max_number);
         ret = new WfActivity[l.size()];
         l.toArray(ret);
         return ret;
      } finally {
         SharkUtilities.methodEnd(shandle,tStamp,
                                  "WfActivityIteratorWrapper.get_next_n_sequence",
                                  this);
      }
   }

   public WfActivity[] get_previous_n_sequence(int max_number) throws Exception {
      long tStamp = SharkUtilities.methodStart(shandle,"WfActivityIteratorWrapper.get_previous_n_sequence");
      try {
         checkSecurity("get_previous_n_sequence", null);

         WfActivity[] ret = null;
         List l = super.getPreviousNSequence(max_number);
         ret = new WfActivity[l.size()];
         l.toArray(ret);
         return ret;
      } finally {
         SharkUtilities.methodEnd(shandle,tStamp,
                                  "WfActivityIteratorWrapper.get_previous_n_sequence",
                                  this);
      }
   }

   protected void fillObjectList() throws Exception {
      if (objectList != null)
         return;

      List activities = new ArrayList();

      PersistentManagerInterface ipm = SharkEngineManager.getInstance()
         .getInstancePersistenceManager();

      String qemod = queryExpression;
      if (queryExpression.startsWith(SharkConstants.QUERY_STATE_PREFIX)) {
         qemod = queryExpression.substring(SharkConstants.QUERY_STATE_PREFIX.length());
      }
      List l = new ArrayList();
      if (null != this.sqlWhere) {
         l.addAll(ipm.getActivitiesWhere(shandle, sqlWhere, startAt, limit));
      } else if (qemod.equals(queryExpression)
                 || queryGrammar.equals(SharkConstants.GRAMMAR_PYTHON_SCRIPT)) {
         l.addAll(ipm.getAllActivitiesForProcess(shandle, procId));
      } else {
         String actState = null;
         if (qemod.indexOf(SharkConstants.STATE_OPEN_RUNNING) != -1) {
            actState = SharkConstants.STATE_OPEN_RUNNING;
         } else if (qemod.indexOf(SharkConstants.STATE_OPEN_NOT_RUNNING_NOT_STARTED) != -1) {
            actState = SharkConstants.STATE_OPEN_NOT_RUNNING_NOT_STARTED;
         } else if (qemod.indexOf(SharkConstants.STATE_OPEN_NOT_RUNNING_SUSPENDED) != -1) {
            actState = SharkConstants.STATE_OPEN_NOT_RUNNING_SUSPENDED;
         } else if (qemod.indexOf(SharkConstants.STATE_CLOSED_COMPLETED) != -1) {
            actState = SharkConstants.STATE_CLOSED_COMPLETED;
         } else if (qemod.indexOf(SharkConstants.STATE_CLOSED_ABORTED) != -1) {
            actState = SharkConstants.STATE_CLOSED_ABORTED;
         } else if (qemod.indexOf(SharkConstants.STATE_CLOSED_TERMINATED) != -1) {
            actState = SharkConstants.STATE_CLOSED_TERMINATED;
         }
         l.addAll(ipm.getActivitiesForProcess(shandle, procId, actState));
         eval = false;
      }
      Evaluator evaluator = SharkEngineManager.getInstance()
         .getScriptingManager()
         .getEvaluator(shandle,queryGrammar);
      for (int i = 0; i < l.size(); i++) {
         ActivityPersistenceObject po = (ActivityPersistenceObject) l.get(i);
         boolean toAdd = true;
         if (eval) {
            Map context = new HashMap();
            ProcessPersistenceObject ppo = null;
            if (ignoreUsedContext
                || (!ignoreUsedContext && 0 <= usedContext.indexOf(SharkConstants.ACT_MGR_PACKAGE_ID))) {
               ppo = ipm.restoreProcess(shandle, po.getProcessId());
               context.put(SharkConstants.ACT_MGR_PACKAGE_ID,
                           MiscUtilities.getProcessMgrVersion(ppo.getProcessMgrName()));
            }
            if (ignoreUsedContext
                || (!ignoreUsedContext && 0 <= usedContext.indexOf(SharkConstants.ACT_MGR_PROCESS_DEFINITION_ID))) {
               if (null == ppo)
                  ppo = ipm.restoreProcess(shandle, po.getProcessId());
               context.put(SharkConstants.ACT_MGR_PROCESS_DEFINITION_ID,
                           MiscUtilities.getProcessMgrProcDefId(ppo.getProcessMgrName()));
            }
            if (ignoreUsedContext
                || (!ignoreUsedContext && 0 <= usedContext.indexOf(SharkConstants.ACT_MGR_VERSION))) {
               if (null == ppo)
                  ppo = ipm.restoreProcess(shandle, po.getProcessId());
               context.put(SharkConstants.ACT_MGR_PACKAGE_ID,
                           MiscUtilities.getProcessMgrPkgId(ppo.getProcessMgrName()));
            }
            if (ignoreUsedContext
                || (!ignoreUsedContext && 0 <= usedContext.indexOf(SharkConstants.ACT_MGR_NAME))) {
               if (null == ppo)
                  ppo = ipm.restoreProcess(shandle, po.getProcessId());
               context.put(SharkConstants.ACT_MGR_NAME, ppo.getProcessMgrName());
            }

            if (ignoreUsedContext
                || (!ignoreUsedContext && 0 <= usedContext.indexOf(SharkConstants.ACT_PROC_KEY))) {
               context.put(SharkConstants.ACT_PROC_KEY, po.getProcessId());
            }
            if (ignoreUsedContext
                || (!ignoreUsedContext && 0 <= usedContext.indexOf(SharkConstants.ACT_PROC_STATE))) {
               if (null == ppo)
                  ppo = ipm.restoreProcess(shandle, po.getProcessId());
               context.put(SharkConstants.ACT_PROC_STATE, ppo.getState());
            }
            if (ignoreUsedContext
                || (!ignoreUsedContext && 0 <= usedContext.indexOf(SharkConstants.ACT_PROC_REQUESTER_ID))) {
               if (null == ppo)
                  ppo = ipm.restoreProcess(shandle, po.getProcessId());
               String reqId;
               if (ppo.getActivityRequesterId() != null) {
                  reqId = ppo.getActivityRequesterId();
               } else {
                  reqId = ppo.getResourceRequesterId();
               }
               context.put(SharkConstants.ACT_PROC_REQUESTER_ID, reqId);
            }
            if (queryExpression.indexOf(SharkConstants.ACT_PROC_CONTEXT_) != -1) {
               if (null == ppo)
                  ppo = ipm.restoreProcess(shandle, po.getProcessId());
               List pc = ipm.getAllVariablesForProcess(shandle, ppo.getId());
               if (pc != null) {
                  Iterator iter = pc.iterator();
                  while (iter.hasNext()) {
                     ProcessVariablePersistenceObject pvpo = (ProcessVariablePersistenceObject) iter.next();

                     String name = SharkConstants.ACT_PROC_CONTEXT_
                                   + pvpo.getDefinitionId();
                     Object value = pvpo.getValue();
                     context.put(name, value);
                  }
               }
            }

            if (ignoreUsedContext
                || (!ignoreUsedContext && 0 <= usedContext.indexOf(SharkConstants.ACT_STATE))) {
               context.put(SharkConstants.ACT_STATE, po.getState());
            }
            if (ignoreUsedContext
                || (!ignoreUsedContext && 0 <= usedContext.indexOf(SharkConstants.ACT_KEY))) {
               context.put(SharkConstants.ACT_KEY, po.getId());
            }
            if (ignoreUsedContext
                || (!ignoreUsedContext && 0 <= usedContext.indexOf(SharkConstants.ACT_NAME))) {
               context.put(SharkConstants.ACT_NAME, po.getName());
            }
            if (ignoreUsedContext
                || (!ignoreUsedContext && 0 <= usedContext.indexOf(SharkConstants.ACT_PRIORITY))) {
               context.put(SharkConstants.ACT_PRIORITY, new Integer(po.getPriority()));
            }
            if (ignoreUsedContext
                || (!ignoreUsedContext && 0 <= usedContext.indexOf(SharkConstants.ACT_DESCRIPTION))) {
               context.put(SharkConstants.ACT_DESCRIPTION, po.getDescription());
            }
            if (ignoreUsedContext
                || (!ignoreUsedContext && 0 <= usedContext.indexOf(SharkConstants.ACT_ACTIVITY_SET_DEFINITION_ID))) {
               context.put(SharkConstants.ACT_ACTIVITY_SET_DEFINITION_ID,
                           po.getActivitySetDefinitionId());
            }
            if (ignoreUsedContext
                || (!ignoreUsedContext && 0 <= usedContext.indexOf(SharkConstants.ACT_DEFINITION_ID))) {
               context.put(SharkConstants.ACT_DEFINITION_ID, po.getActivityDefinitionId());
            }
            if (ignoreUsedContext
                || (!ignoreUsedContext && 0 <= usedContext.indexOf(SharkConstants.ACT_ACTIVATED_TIME_MS))) {
               context.put(SharkConstants.ACT_ACTIVATED_TIME_MS,
                           new Long(po.getActivatedTime()));
            }
            if (ignoreUsedContext
                || (!ignoreUsedContext && 0 <= usedContext.indexOf(SharkConstants.ACT_LAST_STATE_TIME_MS))) {
               context.put(SharkConstants.ACT_LAST_STATE_TIME_MS,
                           new Long(po.getLastStateTime()));
            }
            if (ignoreUsedContext
                || (!ignoreUsedContext && 0 <= usedContext.indexOf(SharkConstants.ACT_LIMIT_TIME_MS))) {
               context.put(SharkConstants.ACT_LIMIT_TIME_MS, new Long(po.getLimitTime()));
            }
            if (ignoreUsedContext
                || (!ignoreUsedContext && 0 <= usedContext.indexOf(SharkConstants.ACT_RESOURCE_USERNAME))) {
               context.put(SharkConstants.ACT_RESOURCE_USERNAME, po.getResourceUsername());
            }
            if (ignoreUsedContext
                || (!ignoreUsedContext && 0 <= usedContext.indexOf(SharkConstants.ACT_ACCEPTED))) {
               context.put(SharkConstants.ACT_ACCEPTED,
                           new Boolean(po.getResourceUsername() != null));
            }
            if (ignoreUsedContext
                || (!ignoreUsedContext && 0 <= usedContext.indexOf(SharkConstants.ACT_ACCEPTED_TIME_MS))) {
               context.put(SharkConstants.ACT_ACCEPTED_TIME_MS,
                           new Long(po.getAcceptedTime()));
            }

            if (queryExpression.indexOf(SharkConstants.ACT_CONTEXT_) != -1) {
               List ac = ipm.getAllVariablesForActivity(shandle, procId, po.getId());
               if (ac != null) {
                  Iterator iter = ac.iterator();
                  while (iter.hasNext()) {
                     ActivityVariablePersistenceObject avpo = (ActivityVariablePersistenceObject) iter.next();

                     String name = SharkConstants.ACT_CONTEXT_ + avpo.getDefinitionId();
                     Object value = avpo.getValue();
                     context.put(name, value);
                  }
               }
            }
            toAdd = evaluator.evaluateCondition(shandle,
                                                po.getProcessId(),
                                                po.getId(),
                                                queryExpression,
                                                context);
         }
         if (toAdd) {
            activities.add(SharkEngineManager.getInstance()
               .getObjectFactory()
               .createActivityWrapper(shandle,
                                      po.getProcessMgrName(),
                                      po.getProcessId(),
                                      po.getId()));
         }

      }
      setObjectList(activities);
   }
}
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.