summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/lifecycle/instance/predefined/agent/CreateAgentFromDescription.java
blob: 520f70f0dbfa4dc06eb460b6092d67d82efd8eb0 (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
/**************************************************************************
 * CreateItemFromDescription
 *
 * $Workfile$
 * $Revision: 1.47 $
 * $Date: 2005/10/13 08:13:58 $
 *
 * Copyright (C) 2001 CERN - European Organization for Nuclear Research
 * All rights reserved.
 **************************************************************************/

package com.c2kernel.lifecycle.instance.predefined.agent;

import com.c2kernel.common.AccessRightsException;
import com.c2kernel.common.InvalidDataException;
import com.c2kernel.common.ObjectAlreadyExistsException;
import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.entity.CorbaServer;
import com.c2kernel.entity.agent.ActiveEntity;
import com.c2kernel.lifecycle.instance.predefined.item.CreateItemFromDescription;
import com.c2kernel.lookup.AgentPath;
import com.c2kernel.lookup.ItemPath;
import com.c2kernel.lookup.RolePath;
import com.c2kernel.process.Gateway;
import com.c2kernel.utils.Logger;

/**************************************************************************
 *
 * @author $Author: abranson $ $Date: 2005/10/13 08:13:58 $
 * @version $Revision: 1.47 $
 **************************************************************************/
public class CreateAgentFromDescription extends CreateItemFromDescription
{
	public CreateAgentFromDescription()
	{
		super();
	}

	/**
	 * Params:
	 * <ol><li>1: new Agent name</li>
	 * <li>2...: Roles to assign to the agent. Must already exist.
	 * @see com.c2kernel.lifecycle.instance.predefined.item.CreateItemFromDescription#runActivityLogic(com.c2kernel.lookup.AgentPath, int, int, java.lang.String)
	 */
	@Override
	protected String runActivityLogic(AgentPath agent, ItemPath itemPath,
			int transitionID, String requestData) throws InvalidDataException {
		
		String[] input = getDataList(requestData);
		String newName = input[0];

		Logger.msg(1, "CreateAgentFromDescription::request() - Starting.");

        try {
        	
        	if (input.length < 2)
        		throw new InvalidDataException("Agent should have at least one Role defined on creation");
        	// check if given roles exist
        	for(int i=1; i<input.length; i++) {
            	RolePath thisRole = new RolePath(input[i]);
            	if (!thisRole.exists()) throw new InvalidDataException("Role "+input[i]+" does not exist");
            }
        	
            // check if the path is already taken
        	try {
        		Gateway.getLookup().getAgentPath(newName);
        		throw new ObjectAlreadyExistsException("The agent name " +newName+ " exists already.", "");
        	} catch (ObjectNotFoundException ex) { }

            // generate new entity key
            Logger.msg(6, "CreateItemFromDescription - Requesting new agent path");
            AgentPath newAgentPath = new AgentPath(newName);

            // resolve the item factory
            Logger.msg(6, "CreateItemFromDescription - Resolving item factory");

            // create the Item object
            Logger.msg(3, "CreateItemFromDescription - Creating Item");
            CorbaServer factory = Gateway.getCorbaServer();
            if (factory == null) throw new AccessRightsException("This process cannot create new Items", "");
            ActiveEntity newAgent = factory.createAgent(newAgentPath);
            Gateway.getLookupManager().add(newAgentPath);

            // initialise it with its properties and workflow

            Logger.msg(3, "CreateItemFromDescription - Initializing Item");

            newAgent.initialise(
                agent.getSystemKey(),
            	Gateway.getMarshaller().marshall(getNewProperties(itemPath, newName, agent)),
            	Gateway.getMarshaller().marshall(getNewWorkflow(itemPath)),
            	Gateway.getMarshaller().marshall(getNewCollections(itemPath))
            	);
            
            // add roles if given
            
            for(int i=1; i<input.length; i++) {
            	newAgent.addRole(input[i]);
            }

            return requestData;
        } catch (Exception e) {
            Logger.error(e);
            throw new InvalidDataException(e.getMessage(), "");
        }
	}
}