package com.c2kernel.entity.agent; 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.stateMachine.Transitions; 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.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 { private int mID; private String mName; private int mItemSysKey; private String mStepPath; private int mPossibleTransition; private int mCurrentState; private int mTargetState; private String mStepName; private int mAgentId = -1; private String mAgentName; private String mAgentRole; private CastorHashMap mActProps = new CastorHashMap(); private String mOutcome; private ErrorInfo mError; private String mStepType; private ItemProxy item = null; private AgentProxy agent = null; private boolean outcomeSet; /*************************************************************************** * Empty constructor for Castor **************************************************************************/ public Job() { } /*************************************************************************** * **************************************************************************/ public Job(int sysKey, String path, int transition, int currState, int targState, String stepName, CastorHashMap actProps, String stepType, String agentName) { setItemSysKey(sysKey); setStepPath(path); setPossibleTransition(transition); setCurrentState(currState); setTargetState(targState); setStepName(stepName); setActProps(actProps); setStepType(stepType); setAgentName(agentName); } public int getItemSysKey() { return mItemSysKey; } public ItemProxy getItemProxy() throws ObjectNotFoundException, InvalidEntityPathException { if (item == null) item = (ItemProxy) Gateway.getProxyManager().getProxy(new EntityPath(mItemSysKey)); 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) mActProps.get("Description"); if (desc == null) desc = "No Description"; return desc; } public String getSchemaType() { if (isError()) return "Errors"; if (requiresOutcome()) return (String)mActProps.get("SchemaType"); else return null; } public int getSchemaVersion() { if (isError()) return 0; if (requiresOutcome()) try { return Integer.parseInt((String) mActProps.get("SchemaVersion")); } catch (NumberFormatException ex) { return -1; } return -1; } public void setOutcome(String outcome) { mOutcome = outcome; outcomeSet = !(mOutcome == null); } public void setError(ErrorInfo errors) { mError = errors; try { mOutcome = Gateway.getMarshaller().marshall(errors); } catch (Exception e) { Logger.error("Error marshalling ErrorInfo in job"); Logger.error(e); } } public String getOutcomeString() { if (mOutcome == null && requiresOutcome()) { String viewName = (String) getActProp("Viewpoint"); mOutcome = null; if (viewName.length() > 0) try { Viewpoint view = (Viewpoint) Gateway.getStorage().get(getItemSysKey(), ClusterStorage.VIEWPOINT + "/" + getSchemaType() + "/" + viewName, null); mOutcome = view.getOutcome().getData(); outcomeSet = true; } catch (Exception ex) { mOutcome = null; outcomeSet = false; } } return mOutcome; } public Outcome getOutcome() { return new Outcome(-1, getOutcomeString(), getSchemaType(), getSchemaVersion()); } public int getAgentId() throws ObjectNotFoundException { if (mAgentId == -1) mAgentId = Gateway.getLDAPLookup().getRoleManager().getAgentPath(getAgentName()).getSysKey(); return mAgentId; } public void setAgentId(int id) { mAgentId = id; agent = null; } public String getAgentName() { if (mAgentName == null) mAgentName = (String) mActProps.get("AgentName"); return mAgentName; } public void setAgentName(String agentName) { mAgentName = agentName; } public String getAgentRole() { if (mAgentRole == null) mAgentRole = (String) mActProps.get("Agent Role"); return mAgentRole; } public void setAgentRole(String role) { mAgentRole = role; } public boolean requiresOutcome() { String schemaType = (String) mActProps.get("SchemaVersion"); return (mPossibleTransition == Transitions.DONE || mPossibleTransition == Transitions.COMPLETE) && !(schemaType == null || schemaType.equals("")); } public boolean isError() { return (mPossibleTransition == Transitions.SUSPEND); } public boolean hasOutcome() { return requiresOutcome() || isError(); } public boolean isOutcomeSet() { return outcomeSet; } public int getID() { return mID; } @Override public String getName() { return mName; } public void setID(int id) { mID = id; mName = String.valueOf(id); } @Override public void setName(String name) { mName = name; try { mID = Integer.parseInt(name); } catch (NumberFormatException ex) { mID = -1; } } public void setItemSysKey(int sysKey) { mItemSysKey = sysKey; item = null; } @Override public String getClusterType() { return ClusterStorage.JOB; } public boolean equals(Job job) { return (getItemSysKey() == job.getItemSysKey()) && this.mStepPath.equals(job.mStepPath) && this.mPossibleTransition == job.mPossibleTransition; } public Object getActProp(String name) { return mActProps.get(name); } public String getActPropString(String name) { try { return mActProps.get(name).toString(); } catch (NullPointerException ex) { return null; } } public String getStepName() { return mStepName; } public void setStepName(String string) { mStepName = string; } public String getStepPath() { return mStepPath; } public void setStepPath(String string) { mStepPath = string; } public int getPossibleTransition() { return mPossibleTransition; } public void setPossibleTransition(int lint) { mPossibleTransition = lint; } public CastorHashMap getActProps() { return mActProps; } public void setActProps(CastorHashMap map) { mActProps = map; } public KeyValuePair[] getKeyValuePairs() { return mActProps.getKeyValuePairs(); } public void setKeyValuePairs(KeyValuePair[] pairs) { mActProps.setKeyValuePairs(pairs); } public int getCurrentState() { return mCurrentState; } public void setCurrentState(int string) { mCurrentState = string; } public int getTargetState() { return mTargetState; } public void setTargetState(int mTargetState) { this.mTargetState = mTargetState; } public String getStepType() { return mStepType; } public void setStepType(String actType) { mStepType = actType; } }