summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/entity/agent/Job.java
blob: 216a88d96a8e44c9b5483c9b21ac9b998b350cfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
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;
	}

    /**
     * Returns the actType.
     *
     * @return String
     */
    public String getStepType()
    {
        return mStepType;
    }

    /**
     * Sets the actType.
     *
     * @param actType
     *            The actType to set
     */
    public void setStepType(String actType)
    {
        mStepType = actType;
    }
}