package com.c2kernel.lifecycle.instance.predefined.entitycreation; import java.util.ArrayList; 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.ModuleImport; import com.c2kernel.property.Property; import com.c2kernel.property.PropertyArrayList; import com.c2kernel.utils.LocalObjectLoader; import com.c2kernel.utils.Logger; /** * 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 properties = new ArrayList(); public ArrayList aggregationList = new ArrayList(); public ArrayList dependencyList = new ArrayList(); public ArrayList outcomes = new ArrayList(); private String ns; public NewItem() { } public NewItem(String name, String initialPath, String wf) { this(); this.name = name; this.initialPath = initialPath; this.workflow = wf; } public void setNamespace(String ns) { this.ns = ns; if (initialPath == null) initialPath = "/desc/"+ns; } public String getNamespace() { return 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) { 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(), thisOutcome.getData(ns), 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); } @Override public String getPath(String ns) { setNamespace(ns); return initialPath; } }