package com.c2kernel.entity.agent; import java.util.HashMap; import com.c2kernel.common.InvalidDataException; import com.c2kernel.common.ObjectNotFoundException; import com.c2kernel.entity.C2KLocalObject; 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.InvalidItemPathException; import com.c2kernel.lookup.ItemPath; import com.c2kernel.persistency.ClusterStorage; import com.c2kernel.persistency.ClusterStorageException; import com.c2kernel.persistency.outcome.Outcome; import com.c2kernel.persistency.outcome.OutcomeInitiator; 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 Transition transition; private String originStateName; private String targetStateName; private int agentId = -1; private String agentRole; private CastorHashMap actProps = new CastorHashMap(); // non-persistent private String name; private String agentName; private String outcomeData; private ErrorInfo error; private ItemProxy item = null; private boolean outcomeSet; // outcome initiator cache static private HashMap ocInitCache = new HashMap(); /*************************************************************************** * 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()); setTransition(transition); setOriginStateName(act.getStateMachine().getState(transition.getOriginStateId()).getName()); setTargetStateName(act.getStateMachine().getState(transition.getTargetStateId()).getName()); setStepName(act.getName()); setActProps(act.getProperties()); setStepType(act.getType()); setAgentName(agent.getAgentName()); setAgentRole(role); } // Castor persistent fields public String getOriginStateName() { return originStateName; } public void setOriginStateName(String originStateName) { this.originStateName = originStateName; } public String getTargetStateName() { return targetStateName; } public void setTargetStateName(String targetStateName) { this.targetStateName = targetStateName; } 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 Transition getTransition() { return transition; } public void setTransition(Transition transition) { this.transition = transition; } public int getAgentId() throws ObjectNotFoundException { if (agentId == -1) agentId = Gateway.getLDAPLookup().getRoleManager().getAgentPath(getAgentName()).getSysKey(); return agentId; } public void setAgentId(int id) { agentId = id; } 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() throws InvalidDataException, ObjectNotFoundException { if (transition.hasOutcome(actProps)) { Schema schema = transition.getSchema(actProps); return schema.docType; } return null; } public int getSchemaVersion() throws InvalidDataException, ObjectNotFoundException { if (transition.hasOutcome(actProps)) { Schema schema = transition.getSchema(actProps); return schema.docVersion; } return -1; } public boolean isOutcomeRequired() { return transition.hasOutcome(actProps) && transition.getOutcome().isRequired(); } public String getScriptName() { if (transition.hasScript(actProps)) { return transition.getScript().getScriptName(); } return null; } public int getScriptVersion() throws InvalidDataException { if (transition.hasScript(actProps)) { return transition.getScriptVersion(actProps); } return -1; } 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, InvalidItemPathException { if (item == null) item = Gateway.getProxyManager().getProxy(new ItemPath(itemSysKey)); return item; } 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 getLastView() throws InvalidDataException { String viewName = (String) getActProp("Viewpoint"); if (viewName.length() > 0) { // find schema String schemaName; try { schemaName = getSchemaName(); } catch (ObjectNotFoundException e1) { throw new InvalidDataException("Schema "+getActProp("SchemaType")+" v"+getActProp("SchemaVersion")+" not found"); } try { Viewpoint view = (Viewpoint) Gateway.getStorage().get(getItemSysKey(), ClusterStorage.VIEWPOINT + "/" + schemaName + "/" + viewName, null); return view.getOutcome().getData(); } catch (ObjectNotFoundException ex) { // viewpoint doesn't exist yet return null; } catch (ClusterStorageException e) { Logger.error(e); throw new InvalidDataException("ViewpointOutcomeInitiator: ClusterStorageException loading viewpoint " + ClusterStorage.VIEWPOINT + "/" + schemaName + "/" + viewName+" in syskey "+getItemSysKey()); } } else return null; } public OutcomeInitiator getOutcomeInitiator() throws InvalidDataException { String ocInitName = (String) getActProp("OutcomeInit"); OutcomeInitiator ocInit; if (ocInitName.length() > 0) { String ocPropName = "OutcomeInit."+ocInitName; synchronized (ocInitCache) { ocInit = ocInitCache.get(ocPropName); if (ocInit == null) { Object ocInitObj = Gateway.getProperties().get(ocPropName); if (ocInitObj instanceof String) { try { ocInitObj = Class.forName((String)ocInitObj).newInstance(); } catch (Exception e) { Logger.error(e); throw new InvalidDataException("Could not instantiate OutcomeInstantiator "+ocInitObj+" for "+ocPropName); } } ocInit = (OutcomeInitiator)ocInitObj; // throw runtime class cast if it isn't one ocInitCache.put(ocPropName, ocInit); } } return ocInit; } else return null; } public String getOutcomeString() throws InvalidDataException { if (outcomeData == null && transition.hasOutcome(actProps)) { outcomeData = getLastView(); if (outcomeData == null) { OutcomeInitiator ocInit = getOutcomeInitiator(); if (ocInit != null) outcomeData = ocInit.initOutcome(this); } if (outcomeData != null) outcomeSet = true; } return outcomeData; } public Outcome getOutcome() throws InvalidDataException, ObjectNotFoundException { return new Outcome(-1, getOutcomeString(), getSchemaName(), getSchemaVersion()); } public boolean hasOutcome() { return transition.hasOutcome(actProps); } public boolean hasScript() { return transition.hasScript(actProps); } 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) && transition.getId() == job.getTransition().getId(); } private void setActProps(CastorHashMap actProps) { this.actProps = actProps; } public Object getActProp(String name) { return actProps.get(name); } public String getActPropString(String name) { Object obj = getActProp(name); return obj==null?null:String.valueOf(obj); } }