package com.c2kernel.entity.agent; import com.c2kernel.common.InvalidDataException; import com.c2kernel.common.ObjectNotFoundException; import com.c2kernel.entity.C2KLocalObject; import com.c2kernel.entity.proxy.AgentProxy; import com.c2kernel.entity.proxy.ItemProxy; import com.c2kernel.lifecycle.instance.Activity; 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.persistency.ClusterStorage; 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.ErrorInfo; import com.c2kernel.utils.CastorHashMap; import com.c2kernel.utils.KeyValuePair; import com.c2kernel.utils.Logger; /******************************************************************************* * @author $Author: abranson $ $Date: 2005/05/20 13:07:49 $ * @version $Revision: 1.62 $ ******************************************************************************/ public class Job implements C2KLocalObject { // persistent private int id; private int itemSysKey; private String stepName; private String stepPath; private String stepType; private int transitionId; private int currentStateId; private int targetStateId; private int agentId = -1; private String agentRole; private String schemaName; private int schemaVersion; private boolean outcomeRequired; private String scriptName; private int scriptVersion; private CastorHashMap actProps = new CastorHashMap(); // non-persistent private String name; private String agentName; private String outcomeData; private ErrorInfo error; private ItemProxy item = null; private AgentProxy agent = null; private boolean outcomeSet; /*************************************************************************** * Empty constructor for Castor **************************************************************************/ public Job() { } public Job(Activity act, int itemSysKey, Transition transition, AgentPath agent, String role) throws InvalidDataException, ObjectNotFoundException { setItemSysKey(itemSysKey); setStepPath(act.getPath()); setTransitionId(transition.getId()); setCurrentStateId(transition.getOriginStateCode()); setTargetStateId(transition.getTerminalStateCode()); setStepName(act.getName()); setActProps(act.getProperties()); setStepType(act.getType()); setAgentName(agent.getAgentName()); setAgentRole(role); if (transition.hasOutcome()) { Schema schema = transition.getSchema(act.getProperties()); setSchemaName(schema.docType); setSchemaVersion(schema.docVersion); outcomeRequired = transition.getOutcome().isRequired(); } if (transition.hasScript()) { setScriptName(transition.getScript().getScriptName()); setScriptVersion(Integer.parseInt(transition.getScript().getScriptVersion())); } } // Castor persistent fields public int getId() { return id; } public void setId(int id) { this.id = id; name = String.valueOf(id); } public int getItemSysKey() { return itemSysKey; } public void setItemSysKey(int sysKey) { itemSysKey = sysKey; item = null; } public String getStepName() { return stepName; } public void setStepName(String string) { stepName = string; } public String getStepPath() { return stepPath; } public void setStepPath(String string) { stepPath = string; } public String getStepType() { return stepType; } public void setStepType(String actType) { stepType = actType; } public int getTransitionId() { return transitionId; } public void setTransitionId(int tid) { transitionId = tid; } public int getCurrentStateId() { return currentStateId; } public void setCurrentStateId(int stateId) { currentStateId = stateId; } public int getTargetStateId() { return targetStateId; } public void setTargetStateId(int targetStateId) { this.targetStateId = targetStateId; } public int getAgentId() throws ObjectNotFoundException { if (agentId == -1) agentId = Gateway.getLDAPLookup().getRoleManager().getAgentPath(getAgentName()).getSysKey(); return agentId; } public void setAgentId(int id) { agentId = id; agent = null; } public String getAgentName() { if (agentName == null) agentName = (String) actProps.get("Agent Name"); return agentName; } public void setAgentName(String agentName) { this.agentName = agentName; } public String getAgentRole() { return agentRole; } public void setAgentRole(String role) { agentRole = role; } public String getSchemaName() { return schemaName; } public void setSchemaName(String schemaName) { this.schemaName = schemaName; } public int getSchemaVersion() { return schemaVersion; } public void setSchemaVersion(int schemaVersion) { this.schemaVersion = schemaVersion; } public boolean isOutcomeRequired() { return outcomeRequired; } public void setOutcomeRequired(boolean outcomeRequired) { this.outcomeRequired = outcomeRequired; } public String getScriptName() { return scriptName; } public void setScriptName(String scriptName) { this.scriptName = scriptName; } public int getScriptVersion() { return scriptVersion; } public void setScriptVersion(int scriptVersion) { this.scriptVersion = scriptVersion; } public KeyValuePair[] getKeyValuePairs() { return actProps.getKeyValuePairs(); } public void setKeyValuePairs(KeyValuePair[] pairs) { actProps.setKeyValuePairs(pairs); } // Non-persistent fields @Override public String getName() { return name; } @Override public void setName(String name) { this.name = name; try { id = Integer.parseInt(name); } catch (NumberFormatException ex) { id = -1; } } public ItemProxy getItemProxy() throws ObjectNotFoundException, InvalidEntityPathException { if (item == null) item = (ItemProxy) Gateway.getProxyManager().getProxy(new EntityPath(itemSysKey)); return item; } public AgentProxy getAgentProxy() throws ObjectNotFoundException, InvalidEntityPathException { if (agent == null) agent = (AgentProxy) Gateway.getProxyManager().getProxy(new EntityPath(getAgentId())); return agent; } public String getDescription() { String desc = (String) actProps.get("Description"); if (desc == null) desc = "No Description"; return desc; } public void setOutcome(String outcome) { outcomeData = outcome; outcomeSet = !(outcomeData == null); } public void setError(ErrorInfo errors) { error = errors; try { outcomeData = Gateway.getMarshaller().marshall(error); } catch (Exception e) { Logger.error("Error marshalling ErrorInfo in job"); Logger.error(e); } } public String getOutcomeString() { if (outcomeData == null && isOutcomeRequired()) { String viewName = (String) getActProp("Viewpoint"); outcomeData = null; if (viewName.length() > 0) try { Viewpoint view = (Viewpoint) Gateway.getStorage().get(getItemSysKey(), ClusterStorage.VIEWPOINT + "/" + getSchemaName() + "/" + viewName, null); outcomeData = view.getOutcome().getData(); outcomeSet = true; } catch (Exception ex) { outcomeData = null; outcomeSet = false; } } return outcomeData; } public Outcome getOutcome() { return new Outcome(-1, getOutcomeString(), getSchemaName(), getSchemaVersion()); } public boolean hasOutcome() { return schemaName!=null; } public boolean hasScript() { return scriptName!=null; } public boolean isOutcomeSet() { return outcomeSet; } @Override public String getClusterType() { return ClusterStorage.JOB; } public boolean equals(Job job) { return (getItemSysKey() == job.getItemSysKey()) && this.stepPath.equals(job.stepPath) && this.transitionId == job.transitionId; } private void setActProps(CastorHashMap actProps) { this.actProps = actProps; } public Object getActProp(String name) { return actProps.get(name); } public String getActPropString(String name) { return String.valueOf(actProps.get(name)); } // Deprecated methods from old state machine @Deprecated public String getSchemaType() { return getSchemaName(); } @Deprecated public int getPossibleTransition() { return transitionId; } @Deprecated public void setPossibleTransition(int lint) { transitionId = lint; } @Deprecated public CastorHashMap getActProps() { return actProps; } @Deprecated public int getCurrentState() { return getCurrentStateId(); } @Deprecated public int getTargetState() { return getTargetStateId(); } }