From 0bb38e90ac6e88cb406facbc075983384a2e164f Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Sat, 20 Jul 2013 11:56:53 +0200 Subject: ActivityDef is a DescriptionObject --- .../com/c2kernel/lifecycle/instance/Activity.java | 390 ++++++--------------- 1 file changed, 107 insertions(+), 283 deletions(-) (limited to 'src/main/java/com/c2kernel/lifecycle/instance/Activity.java') diff --git a/src/main/java/com/c2kernel/lifecycle/instance/Activity.java b/src/main/java/com/c2kernel/lifecycle/instance/Activity.java index 2384dc3..1fc0c14 100644 --- a/src/main/java/com/c2kernel/lifecycle/instance/Activity.java +++ b/src/main/java/com/c2kernel/lifecycle/instance/Activity.java @@ -8,26 +8,27 @@ import com.c2kernel.common.InvalidDataException; import com.c2kernel.common.InvalidTransitionException; import com.c2kernel.common.ObjectAlreadyExistsException; import com.c2kernel.common.ObjectNotFoundException; +import com.c2kernel.common.PersistencyException; import com.c2kernel.entity.agent.Job; import com.c2kernel.events.Event; import com.c2kernel.events.History; import com.c2kernel.graph.model.Vertex; import com.c2kernel.lifecycle.WfCastorHashMap; +import com.c2kernel.lifecycle.instance.stateMachine.State; import com.c2kernel.lifecycle.instance.stateMachine.StateMachine; -import com.c2kernel.lifecycle.instance.stateMachine.States; -import com.c2kernel.lifecycle.instance.stateMachine.Transitions; +import com.c2kernel.lifecycle.instance.stateMachine.Transition; import com.c2kernel.lookup.AgentPath; -import com.c2kernel.lookup.EntityPath; -import com.c2kernel.lookup.InvalidEntityPathException; import com.c2kernel.lookup.LDAPRoleManager; import com.c2kernel.lookup.RolePath; import com.c2kernel.persistency.ClusterStorage; -import com.c2kernel.persistency.TransactionManager; +import com.c2kernel.persistency.ClusterStorageException; import com.c2kernel.persistency.outcome.Outcome; +import com.c2kernel.persistency.outcome.Schema; import com.c2kernel.persistency.outcome.Viewpoint; import com.c2kernel.process.Gateway; import com.c2kernel.scripting.ScriptingEngineException; import com.c2kernel.utils.DateUtility; +import com.c2kernel.utils.LocalObjectLoader; import com.c2kernel.utils.Logger; /** * @version $Revision: 1.222 $ $Date: 2005/10/05 07:39:37 $ @@ -41,54 +42,26 @@ public class Activity extends WfVertex protected Vector mErrors; /** @associates a State machine engine */ private StateMachine machine; - /** true is avalaibe to be executed */ + private int state = -1; + /** true is available to be executed */ public boolean active = false; /** used in verify() */ private boolean loopTested; private GTimeStamp mStartDate; private GTimeStamp mActiveDate; private String mType; - private EntityPath mEntityPath; + public Activity() { super(); setProperties(new WfCastorHashMap()); mErrors = new Vector(0, 1); - machine = new StateMachine(this); mStartDate = new GTimeStamp(); mActiveDate = new GTimeStamp(); DateUtility.setToNow(mActiveDate); DateUtility.setToNow(mStartDate); } - /** @return the SystemKey of the item that contains the workflow */ - public EntityPath getItemEntityPath() - { - if (mEntityPath == null) - try - { - Integer i = (Integer) (getWf().getProperties().get("ItemSystemKey")); - if (i == null) - return null; // no item yet - EntityPath entityPath = new EntityPath(i.intValue()); - mEntityPath = entityPath; - } - catch (InvalidEntityPathException ex) - { - Logger.error("InvalidEntityPathException::Activity::getItemSystemKey() " + ex.toString()); - return null; - } - return mEntityPath; - } - /** @return the StateMachine */ - public StateMachine getMachine() - { - return machine; - } - /** sets the StateMachine (Only for Serialisation) */ - public void setMachine(StateMachine sm) - { - machine = sm; - } + /** add the activity which id is idNext as next of the current one */ void addNext(String idNext) { @@ -102,153 +75,116 @@ public class Activity extends WfVertex { return new Next(this, vertex); } - /** return the current State of the State machine */ - public int getCurrentState() - { - return machine.getCurrentState(); + + private StateMachine getStateMachine() throws ObjectNotFoundException, InvalidDataException { + if (machine == null) { + String name = (String)getProperties().get("StateMachineName"); + String version = (String)getProperties().get("StateMachineVersion"); + machine = LocalObjectLoader.getStateMachine(name, version); + } + return machine; } + /** return the current State of the State machine (Used in Serialisation) */ public int getState() { - if (machine == null) - machine = new StateMachine(this); - return getCurrentState(); + return state; } - /** Sets a new State in a State machine */ - public void setState(int stat) + /** Sets a new State */ + public void setState(int state) { - if (machine == null) - machine = new StateMachine(this); - machine.state = stat; + this.state = state; } - /** check the abiltity of the agent passed in param to act on the activity */ - //return's the agentName - public String checkAccessRights(AgentPath agent) throws AccessRightsException + + + /** cf Item request + * @throws ObjectNotFoundException + * @throws PersistencyException */ + public final void request(AgentPath agent, int itemSysKey, int transitionID, String requestData) throws AccessRightsException, InvalidTransitionException, InvalidDataException, ObjectAlreadyExistsException, ObjectNotFoundException, PersistencyException { - String agentID = getCurrentAgentName(); - boolean authorised = agentID.equals(agent.getAgentName()); - String actRole = getCurrentAgentRole(); - if (!authorised) - { - authorised = actRole == null || actRole.equals("") || actRole.equals("all"); + + // Find requested transition + Transition transition = getStateMachine().getTransition(transitionID); + + // Check if the transition is possible + String usedRole = transition.getPerformingRole(this, agent); + + // Verify outcome + Schema schema = null; + String viewName = null; + boolean storeOutcome = false; + if (transition.hasOutcome()) { + schema = transition.getSchema(getProperties()); + viewName = (String)getProperties().get("Viewpoint"); + if (requestData != null && requestData.length()>0) + storeOutcome = true; + else if (transition.getOutcome().isRequired()) + throw new InvalidDataException("Transition requires outcome data, but none was given", null); } - if (!authorised) - { - RolePath[] roles = agent.getRoles(); - for (int i = 0; !authorised && i < roles.length; i++) - { - if (roles[i].getName().equalsIgnoreCase("Admin")) - authorised = true; - if (roles[i].getName().equalsIgnoreCase(actRole)) - authorised = true; - if (roles[i].getName().equalsIgnoreCase("Guest")) - throw new AccessRightsException("Guest execution forbidden"); + + // Get new state + State newState = getStateMachine().traverse(this, transition, agent); + + // Run extra logic in predefined steps here + runActivityLogic(agent, itemSysKey, transitionID, requestData); + + // set new state and reservation + setState(newState.getId()); + getProperties().put("Agent Name", transition.getReservation(this, agent)); + + // store new event + Event newEvent = null; + try { + History hist = (History) Gateway.getStorage().get(itemSysKey, ClusterStorage.HISTORY, this); + if (storeOutcome) + newEvent = hist.addEvent(agent.getAgentName(), usedRole, transitionID, getName(), getPath(), getType(), schema.docType, schema.docVersion, viewName, state); + else + newEvent = hist.addEvent(agent.getAgentName(), usedRole, transitionID, getName(), getPath(), getType(), state); + + Logger.msg(7, "Activity::auditEvent() - Event:" + newEvent.getName() + " was added to the AuditTrail"); + + if (storeOutcome) { + Outcome newOutcome = new Outcome(newEvent.getID(), requestData, schema.docType, schema.docVersion); + Gateway.getStorage().put(itemSysKey, newOutcome, this); + + // update specific view if defined + if (viewName != null && !viewName.equals("")) { + Viewpoint currentView = new Viewpoint(itemSysKey, schema.docType, viewName, schema.docVersion, newEvent.getID()); + Gateway.getStorage().put(itemSysKey, currentView, this); + } + // update last view + Viewpoint currentView = new Viewpoint(itemSysKey, schema.docType, "last", schema.docVersion, newEvent.getID()); + Gateway.getStorage().put(itemSysKey, currentView, this); } + Gateway.getStorage().commit(this); + } catch (ClusterStorageException ex) { + Logger.error(ex); + throw new PersistencyException("Exception storing event data"); } - if (!authorised) - throw new AccessRightsException("Activity::checkAccessRights() - Agent does not hold the correct role."); - return agent.getAgentName(); - } - /** cf Item request */ - public void request(AgentPath agent, int transitionID, String requestData) throws AccessRightsException, InvalidTransitionException, InvalidDataException, ObjectAlreadyExistsException - { - int state = getState(); - String agentName = checkAccessRights(agent); - if (machine.traverse(transitionID)) - { - setReservation(transitionID, agentName); - sendEventStoreOutcome(transitionID, requestData, agent); - if (transitionID == Transitions.REPEAT) - { - setActive(true); - if (getIsComposite()) - { - WfVertex v = (WfVertex) ((CompositeActivity) this).search(getPath() + "/" + ((CompositeActivity) this).getChildGraphModel().getStartVertexId()); - v.reinit(getID()); - try - { - runfirst(agent); - } - catch (ScriptingEngineException e) - { - Logger.error(e); - } - } - } - if ((transitionID == Transitions.COMPLETE || transitionID == Transitions.DONE) && - (state == States.RSTARTED || getProperties().get("Breakpoint").equals(Boolean.TRUE))) - setActive(false); - else if (transitionID == Transitions.START) - start(); - else if ((transitionID == Transitions.SKIP && getActive()) - || transitionID == Transitions.DONE - || transitionID == Transitions.COMPLETE - || transitionID == Transitions.PROCEED) - try - { + + if (newState.isProceeds()) { + setActive(false); + if (!getProperties().get("Breakpoint").equals(Boolean.TRUE)) + try { runNext(agent); + } catch (ScriptingEngineException ex) { + Logger.error("Error calculating next actvity in lifecycle of "+itemSysKey); + Logger.error(ex); } - catch (ScriptingEngineException e) - { - Logger.error(e); - } - // run post execution script now - try - { - String postSubmitScr = (String) getProperties().get("PostExecScriptName"); - String postSubmitVer = (String) getProperties().get("PostExecScriptVersion"); - if (postSubmitScr != null && (transitionID == Transitions.COMPLETE || transitionID == Transitions.DONE)) - evaluateScript(postSubmitScr, postSubmitVer); - } - catch (ScriptingEngineException ex) - { - Logger.error(ex); - } - //refresh all the job lists - pushJobsToAgents(); } else - throw new InvalidTransitionException("Activity is in the wrong state."); - } - public void setReservation(int transitionID, String agentName) - { - String actAgentName = (String) getProperties().get("Agent Name"); - switch (transitionID) - { - // these transition reserve the activity - case Transitions.REASSIGN : - case Transitions.RESERVE : - case Transitions.START : - actAgentName = agentName; - break; - // these clear any current reservation - case Transitions.COMPLETE : - case Transitions.DONE : - case Transitions.IGNORE : - case Transitions.SKIP : - actAgentName = ""; - // other transitions have no effect on the reservations - default : - } - getProperties().put("Agent Name", actAgentName); + DateUtility.setToNow(mStartDate); + + //refresh all the job lists + pushJobsToAgents(); } - public String getTransitions() - { - String result = ""; - int i; - for (i = 0; i < machine.possibleTransition().length; i++) - { - result += machine.possibleTransition()[i] + ","; - } - //cuts out the last comma(',') if required - if (i > 0) - { - result = result.substring(0, result.length() - 1); - } - result += ""; - return result; + + protected String runActivityLogic(AgentPath agent, int itemSysKey, + int transitionID, String requestData) throws InvalidDataException { + // Overriden in predefined steps + return requestData; } - /** launch the verification of the activity */ + @Override public boolean verify() { @@ -369,9 +305,8 @@ public class Activity extends WfVertex @Override public void reinit(int idLoop) { - Logger.debug(7, "reinit " + getItemEntityPath().getSysKey() + " " + getPath()); Vertex[] outVertices = getOutGraphables(); - machine.state = States.WAITING; + state = getStateMachine().getInitialState(); if (outVertices.length > 0) { WfVertex nextAct = (WfVertex) outVertices[0]; @@ -392,7 +327,7 @@ public class Activity extends WfVertex @Override public void run(AgentPath agent) throws ScriptingEngineException { - Logger.debug(8, getPath() + " run " + getCurrentState()); + Logger.debug(8, getPath() + " run " + getState()); if (!getActive()) setActive(true); @@ -509,118 +444,7 @@ public class Activity extends WfVertex Logger.msg(6, "Agent "+ agent.getAgentName() +" is not allowed to interact with "+getItemEntityPath().getSysKey()+":"+getPath()); } // empty joblist then return jobs; - } - - - /** Adds an event to the AuditTrail of the Item if any */ - private Event auditEvent(int transitionID, AgentPath agent, boolean hasOutcome, boolean isError) - { - EntityPath entityPath = getItemEntityPath(); - if (entityPath != null) - { - Event event = null; - History hist = null; - String viewName = hasOutcome?(String)getProperties().get("Viewpoint"):null; - try - { - hist = (History) Gateway.getStorage().get(entityPath.getSysKey(), ClusterStorage.HISTORY, this); - if (hasOutcome) { - String schemaName = isError?"Errors":(String)getProperties().get("SchemaType"); - String schemaVersion = isError?"0":(String)getProperties().get("SchemaVersion"); - event = hist.addEvent(agent.getAgentName(), getCurrentAgentRole(), transitionID, getName(), getPath(), getType(), schemaName, Integer.valueOf(schemaVersion), viewName, getCurrentState()); - } - else - event = hist.addEvent(agent.getAgentName(), getCurrentAgentRole(), transitionID, getName(), getPath(), getType(), getCurrentState()); - Logger.msg(7, "Activity::auditEvent() - Event:" + event.getName() + " was added to the AuditTrail"); - } - catch (Exception ex) - { - Logger.error("Activity::auditEvent() - Item '" + entityPath.toString() + "'!"); - Logger.error(ex); - } - return event; - } - else - return null; - } - - /** - * Stores the request data as an outcome of the Item It does a great deal of storing outcomes in different configuration - */ //requestdata is xmlstring - - private String storeOutcome(int eventID, String requestData, boolean isError) throws InvalidDataException - { - EntityPath entityPath = getItemEntityPath(); - if (requestData == null || requestData.length() == 0) - throw new InvalidDataException("Empty outcome", ""); - if (entityPath != null) - { - String schemaType; - int schemaVersion; - - if (isError) { - schemaType="Errors"; - schemaVersion=0; - } - else { - schemaType = (String) getProperties().get("SchemaType"); - if (schemaType == null || schemaType.length() == 0) return null; - String versionString = (String) getProperties().get("SchemaVersion"); - try - { - schemaVersion = Integer.parseInt(versionString); - } catch (Exception e) { - throw new InvalidDataException("Activity.storeOutcome() - invalid schemaVersion " + versionString, ""); - } - } - - Logger.msg(5, "Activity::storeOutcome() - type:" + schemaType + " version:" + schemaVersion); - try - { - Outcome newOutcome = new Outcome(eventID, requestData, schemaType, schemaVersion); - Gateway.getStorage().put(entityPath.getSysKey(), newOutcome, this); - // update specific view if defined - String specificView = (String) getProperties().get("Viewpoint"); - if (specificView != null && !specificView.equals("")) - { - Viewpoint currentView = new Viewpoint(entityPath.getSysKey(), schemaType, specificView, schemaVersion, eventID); - Gateway.getStorage().put(entityPath.getSysKey(), currentView, this); - } // update last view - Viewpoint currentView = new Viewpoint(entityPath.getSysKey(), schemaType, "last", schemaVersion, eventID); - Gateway.getStorage().put(entityPath.getSysKey(), currentView, this); - return schemaType + "/" + schemaVersion + "/" + eventID; - } - catch (Exception ex) - { - Logger.error("ActivityBase::storeOutcome() - Item '" + entityPath.toString() + "'!"); - Logger.error(ex); - } - return null; - } - else - return null; - } - - /** the method to be called by the requestAction() method - * @throws InvalidDataException */ - public void sendEventStoreOutcome(int transitionID, String requestData, AgentPath agent) throws InvalidDataException - { - int eventID = -1; - String schemaType = (String) getProperties().get("SchemaType"); - boolean hasOutcome = (transitionID == Transitions.DONE || transitionID == Transitions.COMPLETE) && (schemaType != null && schemaType.length() > 0); - boolean hasErrorOutcome = transitionID == Transitions.SUSPEND && requestData != null && requestData.length()>0; - Event event = auditEvent(transitionID, agent, hasOutcome, hasErrorOutcome); - if (event != null) - eventID = event.getID(); - if (hasOutcome || hasErrorOutcome) - storeOutcome(eventID, requestData, hasErrorOutcome); - EntityPath entityPath = getItemEntityPath(); - TransactionManager storage = Gateway.getStorage(); - if (entityPath != null) - { - storage.commit(this); - } - } + } public void pushJobsToAgents() { @@ -696,7 +520,7 @@ public class Activity extends WfVertex private void start() { Logger.debug(8, getPath() + " start"); - DateUtility.setToNow(mStartDate); + } } -- cgit v1.2.3