summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/lifecycle/instance/WfVertex.java
blob: 0fc9bf9a3c7cf6755bf1b58e57c8d7dbc4da4ab6 (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
package com.c2kernel.lifecycle.instance;



import java.util.HashMap;

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.graph.model.GraphableVertex;
import com.c2kernel.lifecycle.routingHelpers.ViewpointDataHelper;
import com.c2kernel.lookup.AgentPath;
import com.c2kernel.lookup.ItemPath;
import com.c2kernel.persistency.ClusterStorage;
import com.c2kernel.process.Gateway;
import com.c2kernel.scripting.Script;
import com.c2kernel.scripting.ScriptingEngineException;
import com.c2kernel.utils.KeyValuePair;
import com.c2kernel.utils.Logger;

/**
 * @version $Revision: 1.38 $ $Date: 2005/09/07 13:46:31 $
 * @author  $Author: abranson $
 */
public abstract class WfVertex extends GraphableVertex
{
    /**sets the activity available to be executed on start of Workflow or composite activity (when it is the first one of the
     * (sub)process
     * @throws InvalidDataException 
     * @throws ObjectAlreadyExistsException 
     * @throws ObjectNotFoundException 
     * @throws AccessRightsException 
     * @throws InvalidTransitionException 
     * @throws PersistencyException */
    public abstract void runFirst(AgentPath agent, int itemSysKey) throws ScriptingEngineException, InvalidDataException, InvalidTransitionException, AccessRightsException, ObjectNotFoundException, ObjectAlreadyExistsException, PersistencyException;

    /**
	 * @see java.lang.Object#Object()
	 */
	public WfVertex()
    {
    	super();
		setIsLayoutable(true);
		setIsComposite(false);
    }

    /**
	 * Method runNext.
     * @throws InvalidDataException 
     * @throws ObjectNotFoundException 
     * @throws AccessRightsException 
     * @throws InvalidTransitionException 
     * @throws PersistencyException 
     * @throws ObjectAlreadyExistsException 
	 */
	public void runNext(AgentPath agent, int itemSysKey) throws InvalidDataException, InvalidTransitionException, AccessRightsException, ObjectNotFoundException, PersistencyException
    {
		try
		{
			((CompositeActivity)getParent()).request(agent, itemSysKey, CompositeActivity.COMPLETE, null);
		}
		catch (Exception e)
		{
			//Logger.error(e);
		}

    }

    /**
	 * Method reinit.
	 * @param idLoop
     * @throws InvalidDataException 
     * @throws ObjectNotFoundException 
	 */
	public abstract void reinit( int idLoop ) throws InvalidDataException;

    /**
	 * Method verify.
	 * @return boolean
	 */
	public abstract boolean verify();

    /**
	 * Method getErrors.
	 * @return String
	 */
	public abstract String getErrors();

    /**
	 * Method run.
     * @throws InvalidDataException 
     * @throws ObjectAlreadyExistsException 
     * @throws ObjectNotFoundException 
     * @throws AccessRightsException 
     * @throws InvalidTransitionException 
     * @throws PersistencyException 
	 */
	public abstract void run(AgentPath agent, int itemSysKey) throws InvalidDataException, InvalidTransitionException, AccessRightsException, ObjectNotFoundException, PersistencyException;

    /**
	 * Method loop.
	 * @return boolean
	 */
	public abstract boolean loop();

    /**
	 * Method addNext.
	 * @param vertex
	 */
	public abstract Next addNext(WfVertex vertex);

    protected Object evaluateScript(String scriptName, Integer scriptVersion, int itemSysKey) throws ScriptingEngineException
    {

        try
        {
            Script script = getScript(scriptName, scriptVersion);

            KeyValuePair[] k = getProperties().getKeyValuePairs();
            HashMap<?, ?> requiredInput = script.getAllInputParams();
            for (KeyValuePair element : k) {
                if (requiredInput.containsKey(element.getKey()))
                {
                    String value = element.getStringValue();
                    Object inputParam = value;

                    if (value.startsWith("viewpoint//"))
                    {
                        value = value.substring(11);
                        if (value.startsWith("."))
                            value = itemSysKey + value.substring(1);
                        try {
                            inputParam = ViewpointDataHelper.get(value)[0];
                        } catch (ArrayIndexOutOfBoundsException ex) {
                            throw new InvalidDataException("Could not retrieve data from viewpoint: "+value, "");
                        }
                    }
                    if (value.startsWith("property//"))
                    {
                    	value = value.substring(10);
                    	try {
                    		inputParam = Gateway.getStorage().get(itemSysKey, ClusterStorage.PROPERTY+"/"+value, null);
                    	} catch (ObjectNotFoundException ex) {
                    		inputParam = null;
                    	}
                    }
                    Logger.msg(5, "Split.evaluateScript() - Setting param " + element.getKey() + " to " + inputParam.toString());
                    script.setInputParamValue(element.getKey(), inputParam);
                }
            }

            //TODO: is this right?
            if (requiredInput.containsKey("item")) {
                script.setInputParamValue("item", Gateway.getProxyManager().getProxy(new ItemPath(itemSysKey)));
            }
            if (requiredInput.containsKey("agent")) {
                AgentPath systemAgent = Gateway.getLDAPLookup().getRoleManager().getAgentPath("system");
                script.setInputParamValue("agent", Gateway.getProxyManager().getProxy(systemAgent));
            }
            Object retVal = script.execute();
            Logger.msg(2, "Split.evaluateScript() - Script returned "+retVal);
            if (retVal == null) retVal = "";
            return retVal;
        }
        catch (Exception e)
        {
            Logger.msg(1, "Split.evaluateScript() - Error: Script " + scriptName);
            Logger.error(e);
            throw new ScriptingEngineException();
        }
    }

    private static Script getScript(String name, Integer version) throws ScriptingEngineException
    {
    	if (name == null || name.length() == 0)  
    		throw new ScriptingEngineException("Script name is empty");
        Script script;
        if (version!=null) {
            script = new Script(name, version);
        }
        else { // empty version: try expression
            int split = name.indexOf(":");
            script = new Script(name.substring(0, split), name.substring(split + 1));
        }

        return script;
    }


	public Workflow getWf()
	{
		return ((CompositeActivity)getParent()).getWf();
	}
}