summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/lifecycle/instance/predefined/agent/CreateAgentFromDescription.java
blob: f311dc1227a39875eb87beeb160d3264f631c39b (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
/**************************************************************************
 * 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.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.DomainPath;
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();
	}

	//requestdata is xmlstring
	@Override
	protected String runActivityLogic(AgentPath agent, int itemSysKey,
			int transitionID, String requestData) throws InvalidDataException {
		
		String[] input = getDataList(requestData);
		String newName = input[0];
		String domPath = input[1];
        String wfDefName = null;
        int wfDefVer = -1;
        if (input.length > 2)  // override wf
            wfDefName = input[2];

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

        try {
            // check if the path is already taken
			DomainPath context = new DomainPath(new DomainPath(domPath), newName);
			Logger.debug(8,"context "+context.getSysKey()+" "+context.getPath()+" "+context.getString());
			if (context.getSysKey()!=-1)
                throw new ObjectAlreadyExistsException("The agent name " +newName+ " exists already.", "");

            // generate new entity key
            Logger.msg(6, "CreateItemFromDescription - Requesting new sysKey");
            AgentPath newAgentPath = Gateway.getNextKeyManager().generateNextAgentKey();

            // 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 = (ActiveEntity)factory.createEntity(newAgentPath);
            Gateway.getLookupManager().add(newAgentPath);


            // initialise it with its properties and workflow

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

            newAgent.initialise(
                agent.getSysKey(),
            	Gateway.getMarshaller().marshall(getNewProperties(itemSysKey, newName, agent)),
            	Gateway.getMarshaller().marshall(getNewWorkflow(itemSysKey, wfDefName, wfDefVer)),
            	Gateway.getMarshaller().marshall(getNewCollections(itemSysKey))
            	);

            // add its domain path
            Logger.msg(3, "CreateItemFromDescription - Creating "+context);
            context.setEntity(newAgentPath);
            Gateway.getLookupManager().add(context);
            return requestData;
        } catch (Exception e) {
            Logger.error(e);
            throw new InvalidDataException(e.getMessage(), "");
        }
	}
}