summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/lifecycle/instance/predefined/item/CreateItemFromDescription.java
blob: 728631cfe711ee6b2b2d50d1551e87712e22a249 (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
/**************************************************************************
 * 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.item;

import java.util.ArrayList;

import com.c2kernel.collection.Collection;
import com.c2kernel.collection.CollectionArrayList;
import com.c2kernel.collection.CollectionDescription;
import com.c2kernel.collection.CollectionMember;
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.TraceableEntity;
import com.c2kernel.lifecycle.CompositeActivityDef;
import com.c2kernel.lifecycle.instance.CompositeActivity;
import com.c2kernel.lifecycle.instance.predefined.PredefinedStep;
import com.c2kernel.lookup.AgentPath;
import com.c2kernel.lookup.DomainPath;
import com.c2kernel.lookup.ItemPath;
import com.c2kernel.persistency.ClusterStorage;
import com.c2kernel.persistency.ClusterStorageException;
import com.c2kernel.process.Gateway;
import com.c2kernel.property.Property;
import com.c2kernel.property.PropertyArrayList;
import com.c2kernel.property.PropertyDescriptionList;
import com.c2kernel.property.PropertyUtility;
import com.c2kernel.utils.LocalObjectLoader;
import com.c2kernel.utils.Logger;

/**************************************************************************
 *
 * @author $Author: abranson $ $Date: 2005/10/13 08:13:58 $
 * @version $Revision: 1.47 $
 **************************************************************************/
public class CreateItemFromDescription extends PredefinedStep
{
	public CreateItemFromDescription()
	{
		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];

		Logger.msg(1, "CreateItemFromDescription - 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 item name " +newName+ " exists already.", "");

            // get init objects

			/* ITEM CREATION */

            // generate new entity key
            Logger.msg(6, "CreateItemFromDescription - Requesting new sysKey");
            ItemPath entityPath = Gateway.getNextKeyManager().generateNextEntityKey();

            // 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", "");
            TraceableEntity newItem = (TraceableEntity)factory.createEntity(entityPath);
            Gateway.getLookupManager().add(entityPath);


            // initialise it with its properties and workflow

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

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

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

	}
	
	protected PropertyArrayList getNewProperties(int itemSysKey, String newName, AgentPath agent) throws ObjectNotFoundException {
        // copy properties -- intend to create from propdesc
        PropertyDescriptionList pdList = PropertyUtility.getPropertyDescriptionOutcome(itemSysKey);
        PropertyArrayList props = pdList.instanciate();
        // set Name prop or create if not present
        boolean foundName = false;
        for (Property prop : props.list) {
			if (prop.getName().equals("Name")) {
				foundName = true;
				prop.setValue(newName);
			}
		}
        if (!foundName) props.list.add(new Property("Name", newName, true));
        props.list.add( new Property("Creator", agent.getAgentName(), false));
        return props;
	}
	
	protected CompositeActivity getNewWorkflow(int itemSysKey) throws ClusterStorageException, ObjectNotFoundException, InvalidDataException {
        // loop through collections, collecting instantiated descriptions and finding the default workflow def
        String[] collNames = Gateway.getStorage().getClusterContents(itemSysKey, ClusterStorage.COLLECTION);
        String wfDefName = null; Integer wfDefVer = null;
        for (String collName : collNames) {
        	if (collName.equalsIgnoreCase("workflow")) {
            	Collection<? extends CollectionMember> thisCol = (Collection<? extends CollectionMember>)Gateway.getStorage().get(itemSysKey, ClusterStorage.COLLECTION+"/"+collName, null);
        		ArrayList<? extends CollectionMember> members = thisCol.getMembers().list;
        		// get the first member from the wf collection
        		CollectionMember wfMember = members.get(0);
        		wfDefName = wfMember.resolveItem().getName();
    			Object wfVerObj = wfMember.getProperties().get("Version");
    			try {
        			wfDefVer = Integer.parseInt(wfVerObj.toString());
        		} catch (NumberFormatException ex) {
        			throw new InvalidDataException("Invalid workflow version number: "+wfVerObj.toString(), "");
        		}
        	}
        }

        // load workflow def
        if (wfDefName == null)
            throw new InvalidDataException("No workflow given or defined", "");
        if (wfDefVer == null)
        	throw new InvalidDataException("No workflow def version given","");

        try {
        	CompositeActivityDef wfDef = (CompositeActivityDef)LocalObjectLoader.getActDef(wfDefName, wfDefVer);
            return (CompositeActivity)wfDef.instantiate();
        } catch (ObjectNotFoundException ex) {
            throw new InvalidDataException("Workflow def '"+wfDefName+"'v"+wfDefVer+" not found", "");
        } catch (ClassCastException ex) {
         	throw new InvalidDataException("Activity def '"+wfDefName+"' was not Composite", "");
        }
	}
	
	protected CollectionArrayList getNewCollections(int itemSysKey) throws ClusterStorageException, ObjectNotFoundException {
        // loop through collections, collecting instantiated descriptions and finding the default workflow def
        CollectionArrayList colls = new CollectionArrayList();
        String[] collNames = Gateway.getStorage().getClusterContents(itemSysKey, ClusterStorage.COLLECTION);
        for (String collName : collNames) {
        	Collection<? extends CollectionMember> thisCol = (Collection<? extends CollectionMember>)Gateway.getStorage().get(itemSysKey, ClusterStorage.COLLECTION+"/"+collName, null);
        	if (thisCol instanceof CollectionDescription) {
        		CollectionDescription<? extends CollectionMember> thisDesc = (CollectionDescription<? extends CollectionMember>)thisCol;
        		colls.put(thisDesc.newInstance());
        	}
        }
        return colls;
	}
}