1 package org.enhydra.shark; 2 3 4 import java.util.Calendar ; 5 import java.util.Date ; 6 import java.util.HashMap ; 7 import java.util.Iterator ; 8 import java.util.Map ; 9 10 import org.enhydra.shark.api.SharkTransaction; 11 import org.enhydra.shark.api.client.timebase.UtcT; 12 import org.enhydra.shark.api.client.wfbase.BaseException; 13 import org.enhydra.shark.api.client.wfmodel.AlreadySuspended; 14 import org.enhydra.shark.api.client.wfmodel.CannotResume; 15 import org.enhydra.shark.api.client.wfmodel.CannotStop; 16 import org.enhydra.shark.api.client.wfmodel.CannotSuspend; 17 import org.enhydra.shark.api.client.wfmodel.InvalidData; 18 import org.enhydra.shark.api.client.wfmodel.NotRunning; 19 import org.enhydra.shark.api.client.wfmodel.NotSuspended; 20 import org.enhydra.shark.api.client.wfmodel.UpdateNotAllowed; 21 import org.enhydra.shark.api.common.SharkConstants; 22 import org.enhydra.shark.api.internal.limitagent.LimitAgentManager; 23 import org.enhydra.shark.api.internal.working.WfExecutionObjectInternal; 24 import org.enhydra.shark.api.internal.working.WfStateEventAuditInternal; 25 import org.enhydra.shark.utilities.MiscUtilities; 26 import org.enhydra.shark.xpdl.XMLCollectionElement; 27 import org.enhydra.shark.xpdl.XMLUtil; 28 import org.enhydra.shark.xpdl.elements.Activity; 29 import org.enhydra.shark.xpdl.elements.ProcessHeader; 30 import org.enhydra.shark.xpdl.elements.WorkflowProcess; 31 32 38 public abstract class WfExecutionObjectImpl implements WfExecutionObjectInternal { 39 40 protected String state=SharkConstants.STATE_OPEN_NOT_RUNNING_NOT_STARTED; 41 protected String name; 42 protected String key; 43 protected String description; 44 protected short priority; 45 protected long lastStateTime; 46 protected long limitTime=Long.MAX_VALUE/2; 47 48 protected WfStateEventAuditInternal lastStateEventAudit; 49 50 public String state (SharkTransaction t) throws BaseException { 51 return state; 52 } 53 54 public String name (SharkTransaction t) throws BaseException { 55 return name; 56 } 57 58 public void set_name (SharkTransaction t,String new_value) throws BaseException { 59 try { 60 name=new_value; 61 if (name.length()>254) { 62 name=name.substring(0,254); 63 } 64 persist(t); 65 } catch (Exception tme) { 66 throw new BaseException(tme); 67 } 68 } 69 70 public String key (SharkTransaction t) throws BaseException { 71 return key; 72 } 73 74 public String description (SharkTransaction t) throws BaseException { 75 return description; 76 } 77 78 public void set_description (SharkTransaction t,String new_value) throws BaseException { 79 try { 80 description=new_value; 81 if (description.length()>254) { 82 description=description.substring(0,254); 83 } 84 persist(t); 85 } catch (Exception tme) { 86 throw new BaseException(tme); 87 } 88 } 89 90 public Map process_context (SharkTransaction t) throws BaseException { 91 Map m=getContext(t); 92 Map ret=new HashMap (); 93 Iterator it=m.entrySet().iterator(); 94 while (it.hasNext()) { 95 Map.Entry me=(Map.Entry )it.next(); 96 Object id=me.getKey(); 97 Object val=me.getValue(); 98 try { 99 ret.put(id,MiscUtilities.cloneWRD(val)); 100 } catch (Throwable thr) { 101 throw new BaseException(thr); 102 } 103 } 104 return ret; 105 } 106 107 public abstract void set_process_context (SharkTransaction t,Map new_value) throws BaseException, InvalidData, UpdateNotAllowed; 108 109 public short priority (SharkTransaction t) throws BaseException { 110 return priority; 111 } 112 113 public void set_priority (SharkTransaction t,short new_value) throws BaseException { 114 if (new_value<1 || new_value>5) throw new BaseException("Priority is out of range!"); 115 if (state(t).startsWith(SharkConstants.STATEPREFIX_CLOSED)) throw new BaseException("Priority cannot be updated.!"); 116 priority=new_value; 117 try { 118 persist(t); 119 } catch (Exception ex) { 120 throw new BaseException(ex); 121 } 122 } 123 124 public void activateLimitAgent(SharkTransaction trans) throws BaseException { 125 LimitAgentManager mgr = SharkEngineManager.getInstance().getLimitAgentManager(); 126 127 if (mgr==null || this.state(trans).startsWith(SharkConstants.STATEPREFIX_CLOSED)) { 129 return; 131 } 132 133 String durationStr; 134 String limitStr; 135 long startTime=-1; 136 137 XMLCollectionElement xpdlObj=getXPDLObject(trans); 139 WorkflowProcess wp=XMLUtil.getWorkflowProcess(xpdlObj); 140 ProcessHeader ph = wp.getProcessHeader(); 141 durationStr = ph.getDurationUnit(); 142 143 if (xpdlObj instanceof Activity) { 144 limitStr = ((Activity)xpdlObj).getLimit(); 145 startTime=getCreationTime(trans); 146 } else { 147 limitStr = ph.getLimit(); 148 startTime=getStartTime(trans); 149 } 150 151 int durationUnit = 0; 153 154 if (durationStr == null || durationStr.trim().length() == 0) { 156 SharkEngineManager.getInstance().getCallbackUtilities().debug("DurationUnit is not set, not setting limit agent"); 157 return; 158 } else { 159 if ("Y".equals(durationStr)) { 161 durationUnit = Calendar.YEAR; 162 } else if ("M".equals(durationStr)) { 163 durationUnit = Calendar.MONTH; 164 } else if ("D".equals(durationStr)) { 165 durationUnit = Calendar.DAY_OF_MONTH; 166 } else if ("h".equals(durationStr)) { 167 durationUnit = Calendar.HOUR; 168 } else if ("m".equals(durationStr)) { 169 durationUnit = Calendar.MINUTE; 170 } else if ("s".equals(durationStr)) { 171 durationUnit = Calendar.SECOND; 172 } 173 } 174 175 int limit = 0; 177 if (limitStr == null || limitStr.trim().length() == 0) { 178 SharkEngineManager.getInstance().getCallbackUtilities().debug("Limit value is not set, not setting limit agent"); 179 return; 180 } else { 181 try { 182 limit = Integer.parseInt(limitStr); 183 } catch (NumberFormatException e) { 184 SharkEngineManager.getInstance().getCallbackUtilities().error("Defined Limit is not a valid integer, not setting limit : " + limitStr); 185 return; 186 } 187 } 188 189 long runtime = 0; 191 if (durationUnit > 0 && limit > 0) { 192 Calendar cal = Calendar.getInstance(); 193 cal.setTime(new Date (startTime)); 196 cal.add(durationUnit, limit); 197 runtime = cal.getTime().getTime(); 198 } 199 200 Map context = this.process_context(trans); 202 203 if (runtime > 0) { 205 notifyStart(trans,context,runtime); 206 limitTime=runtime; 207 } 208 } 209 210 public abstract long getCreationTime (SharkTransaction trans) throws BaseException; 211 212 public abstract long getStartTime (SharkTransaction trans) throws BaseException; 213 214 public UtcT last_state_time(SharkTransaction t) throws BaseException { 215 return new UtcT(lastStateTime, 0, (short)0, (short)0); 216 } 217 218 public abstract void resume(SharkTransaction t) throws BaseException, CannotResume, NotSuspended; 219 220 public abstract void suspend(SharkTransaction t) throws BaseException, CannotSuspend, NotRunning, AlreadySuspended; 221 222 public abstract void terminate(SharkTransaction t) throws BaseException, CannotStop, NotRunning; 223 224 public abstract void abort(SharkTransaction t) throws BaseException, CannotStop, NotRunning; 225 226 protected abstract XMLCollectionElement getXPDLObject(SharkTransaction t) throws BaseException; 227 228 protected abstract void notifyStart (SharkTransaction t,Map context,long runtime) throws BaseException; 229 } 230 231 232 | Popular Tags |