summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/entity/agent/Job.java
blob: fe2084de376f9c26939d5ee2b09e058dc02ab5de (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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package com.c2kernel.entity.agent;

import java.util.HashMap;
import java.util.UUID;

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.InvalidAgentPathException;
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 ItemPath itemPath;

    private String stepName;
    
    private String stepPath;
    
    private String stepType;
    
    private Transition transition;
    
    private String originStateName;
    
    private String targetStateName;

    private String agentRole;

    private CastorHashMap actProps = new CastorHashMap();
    
    // non-persistent
    
    private String name;
    
    private AgentPath agentPath;
    
    private String agentName;
    
    private String outcomeData;
    
    private ErrorInfo error;

    private ItemProxy item = null;

    private boolean outcomeSet;
    
    // outcome initiator cache
    
    static private HashMap<String, OutcomeInitiator> ocInitCache = new HashMap<String, OutcomeInitiator>();

    /***************************************************************************
     * Empty constructor for Castor
     **************************************************************************/
    public Job()
    {
    }

    public Job(Activity act, ItemPath itemPath, Transition transition, AgentPath agent, String role) throws InvalidDataException, ObjectNotFoundException, InvalidAgentPathException {
        
    	setItemPath(itemPath);
        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());
        if (agent != null) 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 ItemPath getItemPath() {
        return itemPath;
    }
	
    public void setItemPath(ItemPath path) {
    	itemPath = path;
        item = null;
    }	

    public void setItemUUID( String uuid )
    {
    	setItemPath(new ItemPath(UUID.fromString(uuid)));
    }
    
    public String getItemUUID() {
    	return getItemPath().getUUID().toString();
    }
    
    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 AgentPath getAgentPath() throws ObjectNotFoundException {
    	if (agentPath == null && getAgentName() != null) {
    		agentPath = Gateway.getLookup().getAgentPath(getAgentName());
    	}
        return agentPath;
    }

    public void setAgentPath(AgentPath agentPath) {
    	this.agentPath = agentPath;
    	agentName = agentPath.getAgentName();
    }
    
    public void setAgentUUID( String uuid )
    {
    	if (uuid != null) 
    		try {
				setAgentPath(new AgentPath(UUID.fromString(uuid)));
			} catch (InvalidAgentPathException e) {
				Logger.error("Invalid agent path in Job: "+uuid);
			}
    }
    
    public String getAgentUUID() {
    	try {
			if (getAgentPath() != null)
				return getAgentPath().getUUID().toString();
		} catch (ObjectNotFoundException e) { }
    	return null;
    }
    
    public String getAgentName()
    {
        if (agentName == null)
            agentName = (String) actProps.get("Agent Name");
        return agentName;
    }

    public void setAgentName(String agentName) throws ObjectNotFoundException
    {
        this.agentName = agentName;
        agentPath = Gateway.getLookup().getAgentPath(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(itemPath);
        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(itemPath, 
						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 item "+itemPath.getUUID());
			}
		}
		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 (itemPath.equals(job.getItemPath()) && 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);
    }
}