summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/lifecycle/instance/predefined/entitycreation/NewItem.java
blob: 050615ff33640641fa0f4ce18d55f1b79aa97103 (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
package com.c2kernel.lifecycle.instance.predefined.entitycreation;


import java.util.ArrayList;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

import com.c2kernel.collection.MembershipException;
import com.c2kernel.common.CannotManageException;
import com.c2kernel.common.ObjectAlreadyExistsException;
import com.c2kernel.common.ObjectCannotBeUpdated;
import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.entity.TraceableEntity;
import com.c2kernel.events.Event;
import com.c2kernel.events.History;
import com.c2kernel.lifecycle.CompositeActivityDef;
import com.c2kernel.lifecycle.instance.stateMachine.States;
import com.c2kernel.lifecycle.instance.stateMachine.Transitions;
import com.c2kernel.lookup.DomainPath;
import com.c2kernel.lookup.EntityPath;
import com.c2kernel.persistency.ClusterStorageException;
import com.c2kernel.persistency.outcome.Viewpoint;
import com.c2kernel.process.Gateway;
import com.c2kernel.process.module.Module;
import com.c2kernel.process.module.ModuleImport;
import com.c2kernel.property.Property;
import com.c2kernel.property.PropertyArrayList;
import com.c2kernel.utils.LocalObjectLoader;
import com.c2kernel.utils.Logger;
import com.c2kernel.utils.Resource;

/**
 * Complete Structure for new item
 *
 * @version $Revision: 1.8 $ $Date: 2006/03/03 13:52:21 $
 */

public class NewItem extends ModuleImport {

    public String initialPath;
    public String workflow;
    public ArrayList<Property> properties  = new ArrayList<Property>();
    public ArrayList<Aggregation> aggregationList = new ArrayList<Aggregation>();
    public ArrayList<Dependency> dependencyList = new ArrayList<Dependency>();
    public ArrayList<Outcome> outcomes = new ArrayList<Outcome>();
    public String ns;

    public NewItem() {
        super(null);
    }

    public NewItem(String name, String initialPath, String wf) {
        this();
        this.name = name;
        this.initialPath = initialPath;
        this.workflow = wf;
    }

	public NewItem(String ns, Element elem) {
		super(elem);
		this.ns = ns;
		workflow = elem.getAttribute("workflow");
		if (elem.hasAttribute("initialPath"))
			initialPath = elem.getAttribute("initialPath");
		else
			initialPath = "/desc/"+ns;
		NodeList pnl = elem.getElementsByTagName("Property");
		for (int j=0; j<pnl.getLength(); j++) {
			Element p = (Element)pnl.item(j);
			properties.add(Module.newProperty(p));
		}
		NodeList ocnl = elem.getElementsByTagName("Outcome");
		for (int j=0; j<ocnl.getLength(); j++) {
			Element oc = (Element)ocnl.item(j);
			outcomes.add(new Outcome(oc.getAttribute("schema"), oc.getAttribute("version"), oc.getAttribute("viewname"), ((Text)oc.getFirstChild()).getData()));
		}
		
		NodeList depnl = elem.getElementsByTagName("Dependency");
		for (int j=0; j<depnl.getLength(); j++) {
			Element dep = (Element)depnl.item(j);
			Dependency newDep = new Dependency(dep);
			dependencyList.add(newDep);
		}
		NodeList aggnl = elem.getElementsByTagName("Aggregation");
		for (int j=0; j<aggnl.getLength(); j++) {
			Element agg = (Element)aggnl.item(j);
			Aggregation newAgg = new Aggregation(agg);
			aggregationList.add(newAgg);
		}
	}

	public void setNs(String ns) {
		this.ns = ns;
	}

	public void create(int agentId) throws ObjectCannotBeUpdated, ObjectNotFoundException, CannotManageException, ObjectAlreadyExistsException {
        DomainPath domPath = new DomainPath(new DomainPath(initialPath), name);
        if (domPath.exists())
            throw new ObjectAlreadyExistsException(domPath+" already exists!", "");

        // create item
        EntityPath entPath = Gateway.getLDAPLookup().getNextKeyManager().generateNextEntityKey();
        TraceableEntity newItem = (TraceableEntity)Gateway.getCorbaServer().createEntity(entPath);
        Gateway.getLDAPLookup().add(entPath);

        // set the name property
        properties.add(new Property("Name", name));

        // init the new item
        try {

        	// find workflow def
        	CompositeActivityDef compact = (CompositeActivityDef)LocalObjectLoader.getActDef(workflow, "0");
            newItem.initialise(
                    agentId,
                    Gateway.getMarshaller().marshall(new PropertyArrayList(properties)),
                    Gateway.getMarshaller().marshall(compact.instantiate()));
        } catch (Exception ex) {
            Logger.error("Error initialising new item");
            Logger.error(ex);
            throw new CannotManageException("Problem initialising new item. See server log.", "");
        }

        // import outcomes
        
        History hist = new History(entPath.getSysKey(), null);
        for (Outcome thisOutcome : outcomes) {
			String data = Resource.getTextResource(ns, thisOutcome.path);
            Event newEvent = hist.addEvent("system", "Admin", Transitions.DONE, "Import", "Import", "Import", States.FINISHED);
            com.c2kernel.persistency.outcome.Outcome newOutcome = new com.c2kernel.persistency.outcome.Outcome(newEvent.getID(), data, thisOutcome.schema, thisOutcome.version);
            Viewpoint newLastView = new Viewpoint(entPath.getSysKey(), thisOutcome.schema, thisOutcome.viewname, thisOutcome.version, newEvent.getID());
            try {
				Gateway.getStorage().put(entPath.getSysKey(), newOutcome, null);
				Gateway.getStorage().put(entPath.getSysKey(), newLastView, null);
			} catch (ClusterStorageException e) {
				throw new ObjectCannotBeUpdated("Could not store data for view "+thisOutcome.schema+"/"+thisOutcome.viewname+" in "+name);
			}
		}
        
        // create collections

        for (Dependency element: dependencyList) {
            try {
                Gateway.getStorage().put(entPath.getSysKey(), element.create(), null);
            } catch (ClusterStorageException ex) {
                Logger.error(ex);
                throw new CannotManageException("Could not create Dependency "+element.name, "");
            } catch (MembershipException ex) {
                Logger.error(ex);
                throw new CannotManageException("A specified member is not of the correct type in "+element.name, "");
            }
        }

        for (Aggregation element : aggregationList) {
            try {
                Gateway.getStorage().put(entPath.getSysKey(), element.create(), null);
            } catch (ClusterStorageException ex) {
                Logger.error(ex);
                throw new CannotManageException("Could not create Aggregation "+element.name, "");
            } catch (MembershipException ex) {
	            Logger.error(ex);
	            throw new CannotManageException("A specified member is not of the correct type in "+element.name, "");
	        }
        }
        // register domain path
        domPath.setEntity(entPath);
        Gateway.getLDAPLookup().add(domPath);
    }
}