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

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

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.enhydra.shark.api.RootException;
import org.enhydra.shark.api.client.timebase.UtcT;
import org.enhydra.shark.api.client.wfmc.wapi.WMSessionHandle;
import org.enhydra.shark.api.client.wfmodel.AlreadySuspended;
import org.enhydra.shark.api.client.wfmodel.CannotResume;
import org.enhydra.shark.api.client.wfmodel.CannotStop;
import org.enhydra.shark.api.client.wfmodel.CannotSuspend;
import org.enhydra.shark.api.client.wfmodel.InvalidData;
import org.enhydra.shark.api.client.wfmodel.NotRunning;
import org.enhydra.shark.api.client.wfmodel.NotSuspended;
import org.enhydra.shark.api.client.wfmodel.UpdateNotAllowed;
import org.enhydra.shark.api.common.SharkConstants;
import org.enhydra.shark.api.internal.working.WfExecutionObjectInternal;
import org.enhydra.shark.api.internal.working.WfStateEventAuditInternal;
import org.enhydra.shark.utilities.MiscUtilities;
import org.enhydra.shark.xpdl.XMLCollectionElement;
import org.enhydra.shark.xpdl.XMLUtil;
import org.enhydra.shark.xpdl.elements.Activity;
import org.enhydra.shark.xpdl.elements.ProcessHeader;
import org.enhydra.shark.xpdl.elements.WorkflowProcess;

/**
 * WfExecutionObjectImpl - Workflow Execution Object implementation The WfProcessImpl and
 * WfActivityImpl classes are extended from this class.
 * 
 * @author Sasa Bojanic
 * @author Vladimir Puskas
 */
public abstract class WfExecutionObjectImpl implements WfExecutionObjectInternal {

   protected String state = SharkConstants.STATE_OPEN_NOT_RUNNING_NOT_STARTED;

   protected String name;

   protected String key;

   protected String description;

   protected short priority;

   protected long lastStateTime;

   protected long limitTime = SharkConstants.UNDEFINED_TIME;

   protected WfStateEventAuditInternal lastStateEventAudit;

   public String state(WMSessionHandle shandle) throws Exception {
      return state;
   }

   public String name(WMSessionHandle shandle) throws Exception {
      return name;
   }

   public void set_name(WMSessionHandle shandle, String new_value) throws Exception {
      checkReadOnly();
      name = new_value;
      if (name!=null && name.length() > 254) {
         name = name.substring(0, 254);
      }
      persist(shandle);
   }

   public String key(WMSessionHandle shandle) throws Exception {
      return key;
   }

   public String description(WMSessionHandle shandle) throws Exception {
      return description;
   }

   public void set_description(WMSessionHandle shandle, String new_value)
      throws Exception {
      checkReadOnly();
      description = new_value;
      if (description!=null && description.length() > 254) {
         description = description.substring(0, 254);
      }
      persist(shandle);
   }

   public Map process_context(WMSessionHandle shandle) throws Exception {
      Map m = getContext(shandle);
      return duplicateContext(m);
   }

   protected Map duplicateContext(Map m) throws Exception {
      Map ret = new HashMap();
      Iterator it = m.entrySet().iterator();
      while (it.hasNext()) {
         Map.Entry me = (Map.Entry) it.next();
         Object id = me.getKey();
         Object val = me.getValue();
         try {
            ret.put(id, MiscUtilities.cloneWRD(val));
         } catch (Throwable thr) {
            throw new RootException(thr);
         }
      }
      return ret;
   }

   public abstract void set_process_context(WMSessionHandle shandle, Map new_value)
      throws Exception,
         InvalidData,
         UpdateNotAllowed;

   public short priority(WMSessionHandle shandle) throws Exception {
      return priority;
   }

   public void set_priority(WMSessionHandle shandle, short new_value) throws Exception {
      checkReadOnly();
      boolean allowOutOfRangePriority=Boolean.valueOf(SharkEngineManager.
                                                      getInstance().
                                                      getCallbackUtilities().
                                                      getProperty("SharkKernel.allowOutOfRangePriority","false")).booleanValue();

      if (!allowOutOfRangePriority && (new_value < 1 || new_value > 5))
         throw new Exception("Priority is out of range!");
      if (state(shandle).startsWith(SharkConstants.STATEPREFIX_CLOSED))
         throw new Exception("Priority cannot be updated.!");
      priority = new_value;

      persist(shandle);
   }

   public void calculateLimit(WMSessionHandle shandle) throws Exception {
      checkReadOnly();
      // check the current state
      if (this.state(shandle).startsWith(SharkConstants.STATEPREFIX_CLOSED)) {
         // we only set this if we are running
         return;
      }

      String durationStr;
      String limitStr;
      long startTime = -1;

      // get the duration unit string
      XMLCollectionElement xpdlObj = getXPDLObject(shandle);
      WorkflowProcess wp = XMLUtil.getWorkflowProcess(xpdlObj);
      ProcessHeader ph = wp.getProcessHeader();
      durationStr = ph.getDurationUnit();

      if (xpdlObj instanceof Activity) {
         limitStr = ((Activity) xpdlObj).getLimit();
         startTime = getCreationTime(shandle);
      } else {
         limitStr = ph.getLimit();
         startTime = getStartTime(shandle);
      }

      // duration unit
      int durationUnit = 0;

      // convert to calendar value
      if (durationStr == null || durationStr.trim().length() == 0) {
         SharkEngineManager.getInstance()
            .getCallbackUtilities()
            .debug(shandle,"DurationUnit is not set, not setting limit");
         return;
      }

      // get the Calendar value
      if ("Y".equals(durationStr)) {
         durationUnit = Calendar.YEAR;
      } else if ("M".equals(durationStr)) {
         durationUnit = Calendar.MONTH;
      } else if ("D".equals(durationStr)) {
         durationUnit = Calendar.DAY_OF_MONTH;
      } else if ("h".equals(durationStr)) {
         durationUnit = Calendar.HOUR;
      } else if ("m".equals(durationStr)) {
         durationUnit = Calendar.MINUTE;
      } else if ("s".equals(durationStr)) {
         durationUnit = Calendar.SECOND;
      }

      // limit value - convert to integer
      int limit = 0;
      if (limitStr == null || limitStr.trim().length() == 0) {
         SharkEngineManager.getInstance()
            .getCallbackUtilities()
            .debug(shandle,"Limit value is not set, not setting limit");
         return;
      }

      try {
         limit = Integer.parseInt(limitStr);
      } catch (NumberFormatException e) {
         SharkEngineManager.getInstance()
            .getCallbackUtilities()
            .error(shandle,"Defined Limit is not a valid integer, not setting limit : "
                   + limitStr);
         return;
      }

      // determine the limit runtime
      long runtime = 0;
      if (durationUnit > 0 && limit > 0) {
         Calendar cal = Calendar.getInstance();
         // MUST support JDK 1.3.x, so we cannot use
         // cal.setTimeInMillis & cal.getTimeInMillis
         cal.setTime(new Date(startTime));
         cal.add(durationUnit, limit);
         runtime = cal.getTime().getTime();
      }

      // set limitTime variable
      if (runtime > 0) {
         limitTime = runtime;
      }
   }

   public abstract long getCreationTime(WMSessionHandle shandle) throws Exception;

   public abstract long getStartTime(WMSessionHandle shandle) throws Exception;

   public UtcT last_state_time(WMSessionHandle shandle) throws Exception {
      return new UtcT(lastStateTime, 0, (short) 0, (short) 0);
   }

   public long getLimitTime(WMSessionHandle shandle) throws Exception {
      return limitTime;
   }

   public abstract void resume(WMSessionHandle shandle)
      throws Exception,
         CannotResume,
         NotSuspended;

   public abstract void suspend(WMSessionHandle shandle)
      throws Exception,
         CannotSuspend,
         NotRunning,
         AlreadySuspended;

   public abstract void terminate(WMSessionHandle shandle)
      throws Exception,
         CannotStop,
         NotRunning;

   public abstract void abort(WMSessionHandle shandle)
      throws Exception,
         CannotStop,
         NotRunning;

   protected abstract XMLCollectionElement getXPDLObject(WMSessionHandle shandle)
      throws Exception;

   protected abstract void checkReadOnly() throws Exception;
   
}
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.