summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/entity/proxy/ItemProxy.java
blob: 077ddf877891df1db1e97614b6deb1613a45fa3b (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
/**************************************************************************
 * ItemProxy.java
 *
 * $Revision: 1.25 $
 * $Date: 2005/05/10 11:40:09 $
 *
 * Copyright (C) 2001 CERN - European Organization for Nuclear Research
 * All rights reserved.
 **************************************************************************/

package com.c2kernel.entity.proxy;

import java.util.ArrayList;

import com.c2kernel.collection.Collection;
import com.c2kernel.collection.CollectionMember;
import com.c2kernel.common.AccessRightsException;
import com.c2kernel.common.InvalidDataException;
import com.c2kernel.common.InvalidTransitionException;
import com.c2kernel.common.ObjectAlreadyExistsException;
import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.common.PersistencyException;
import com.c2kernel.entity.Item;
import com.c2kernel.entity.ItemHelper;
import com.c2kernel.entity.ManageableEntity;
import com.c2kernel.entity.agent.Job;
import com.c2kernel.entity.agent.JobArrayList;
import com.c2kernel.lifecycle.instance.Workflow;
import com.c2kernel.lifecycle.instance.stateMachine.Transitions;
import com.c2kernel.persistency.ClusterStorage;
import com.c2kernel.persistency.outcome.Viewpoint;
import com.c2kernel.process.Gateway;
import com.c2kernel.utils.Logger;

/******************************************************************************
 * It is a wrapper for the connection and communication with Item
 * It caches data loaded from the Item to reduce communication
 *
 * @version $Revision: 1.25 $ $Date: 2005/05/10 11:40:09 $
 * @author  $Author: abranson $
 ******************************************************************************/
public class ItemProxy extends EntityProxy
{

   /**************************************************************************
    *
    **************************************************************************/
    protected ItemProxy( org.omg.CORBA.Object  ior,
                         int               systemKey)
        throws ObjectNotFoundException
    {
        super(ior, systemKey);

    }

     @Override
	public ManageableEntity narrow() throws ObjectNotFoundException
     {
         try {
             return ItemHelper.narrow(mIOR);
         } catch (org.omg.CORBA.BAD_PARAM ex) { }
         throw new ObjectNotFoundException("CORBA Object was not an Item, or the server is down.");
     }
   /**************************************************************************
    *
    *
    **************************************************************************/
    public void initialise( int agentId,
                            String  itemProps,
                            String  workflow )
        throws AccessRightsException,
               InvalidDataException,
               PersistencyException,
               ObjectNotFoundException
    {
        Logger.msg(7, "ItemProxy::initialise - started");

        ((Item)getEntity()).initialise( agentId, itemProps, workflow );
    }

    public void setProperty(AgentProxy agent, String name, String value)
        throws AccessRightsException,
        PersistencyException
    {
        String[] params = new String[2];
        params[0] = name;
        params[1] = value;
        try {
            agent.execute(this, "WriteProperty", params);
        } catch (AccessRightsException e) {
            throw (e);
        } catch (PersistencyException e) {
            throw (e);
        } catch (Exception e) {
            Logger.error(e);
            throw new PersistencyException("Could not store property");
        }
    }
   /**************************************************************************
    *
    **************************************************************************/
    protected void requestAction( Job thisJob )
        throws AccessRightsException,
            InvalidTransitionException,
            ObjectNotFoundException,
            InvalidDataException,
            PersistencyException,
            ObjectAlreadyExistsException
    {
		String outcome = thisJob.getOutcomeString();
        // check fields that should have been filled in
        if (outcome==null)
        	if (thisJob.requiresOutcome())
	            throw new InvalidDataException("Outcome is required.", "");
			else
				outcome="";

        if (thisJob.getAgentId() == -1)
            throw new InvalidDataException("No Agent specified.", "");

        Logger.msg(7, "ItemProxy - executing "+thisJob.getStepPath()+" for "+thisJob.getAgentName());
        requestAction (thisJob.getAgentId(), thisJob.getStepPath(),
            thisJob.getPossibleTransition(), outcome);
    }

	//requestData is xmlString
    public void requestAction( int agentId,
                               String              stepPath,
                               int              transitionID,
                               String   requestData
                              )
        throws AccessRightsException,
               InvalidTransitionException,
               ObjectNotFoundException,
               InvalidDataException,
               PersistencyException,
               ObjectAlreadyExistsException
    {
        ((Item)getEntity()).requestAction( agentId,
                                    stepPath,
                                    transitionID,
                                    requestData );
    }

   /**************************************************************************
    *
    **************************************************************************/
    public String queryLifeCycle( int agentId,
                                  boolean     filter
                                )
        throws AccessRightsException,
            ObjectNotFoundException,
            PersistencyException
    {
        return ((Item)getEntity()).queryLifeCycle( agentId,
                                            filter );
    }


   /**************************************************************************
    *
    **************************************************************************/
    private ArrayList<Job> getJobList(int agentId, boolean filter)
        throws AccessRightsException,
               ObjectNotFoundException,
               PersistencyException
    {
        JobArrayList thisJobList;
        try {
            String jobs = queryLifeCycle(agentId, filter);
            thisJobList = (JobArrayList)Gateway.getMarshaller().unmarshall(jobs);
            }
        catch (Exception e) {
            Logger.error(e);
            throw new PersistencyException("Exception::ItemProxy::getJobList() - Cannot unmarshall the jobs", null);
        }
        return thisJobList.list;
    }

    /**
     * Returns all Jobs (possible state transitions in all active Activities) that the given Agent may perform in this Item.
     * 
     * @param agent - the Agent who will perform the execution
     * @return ArrayList of Jobs
     * @throws AccessRightsException 
     * @throws ObjectNotFoundException 
     * @throws PersistencyException
     */
    public ArrayList<Job> getJobList(AgentProxy agent)
    throws AccessRightsException,
           ObjectNotFoundException,
           PersistencyException
    {
        return getJobList(agent.getSystemKey());
    }

    private ArrayList<Job> getJobList(int agentId)
        throws AccessRightsException,
               ObjectNotFoundException,
               PersistencyException
    {
        return getJobList(agentId, true);
    }

    private Job getJobByName(String actName, int agentId)
    throws AccessRightsException,
           ObjectNotFoundException,
           PersistencyException {

    	ArrayList<Job> jobList = getJobList(agentId);
    	for (Job job : jobList) {
			int transition = job.getPossibleTransition();
			if (job.getStepName().equals(actName))
				if (transition == Transitions.COMPLETE || transition == Transitions.DONE)
					return job;
    	}
    	return null;

    }
    
    public Collection<? extends CollectionMember> getCollection(String collName) throws ObjectNotFoundException {
    	return (Collection<? extends CollectionMember>)getObject(ClusterStorage.COLLECTION+"/"+collName);
    }
    
    public Workflow getWorkflow() throws ObjectNotFoundException {
    	return (Workflow)getObject(ClusterStorage.LIFECYCLE+"/workflow");
    }
    
    public Viewpoint getViewpoint(String schemaName, String viewName) throws ObjectNotFoundException {
    	return (Viewpoint)getObject(ClusterStorage.VIEWPOINT+"/"+schemaName+"/"+viewName);
    }
    
    /**
     * Queries for DONE or COMPLETE Jobs in the named Activity
     * 
     * @param actName The activity name to search for
     * @param agent The agent who will be performing the transition
     * @return A Job object to complete the named Activity
     * @throws AccessRightsException
     * @throws ObjectNotFoundException
     * @throws PersistencyException
     */
    public Job getJobByName(String actName, AgentProxy agent)
    throws AccessRightsException,
           ObjectNotFoundException,
           PersistencyException {
    	return getJobByName(actName, agent.getSystemKey());
    }
}