summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/entity/agent/Job.java
blob: 02dd541fe1af821b5b60f25956e3860aa6927a0d (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
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.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 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;

    /***************************************************************************
     * 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, InvalidEntityPathException {
        if (item == null)
            item = (ItemProxy) Gateway.getProxyManager().getProxy(new EntityPath(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 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() 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)
    {
        return String.valueOf(actProps.get(name));
    }
}