package com.c2kernel.lifecycle.instance.predefined.entitycreation; import java.util.ArrayList; import java.util.Iterator; 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.lifecycle.CompositeActivityDef; import com.c2kernel.lookup.DomainPath; import com.c2kernel.lookup.EntityPath; import com.c2kernel.persistency.ClusterStorageException; import com.c2kernel.process.Gateway; import com.c2kernel.property.PropertyArrayList; import com.c2kernel.utils.CastorXMLUtility; 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 { public String name; /** * The initial Domain Path to be created for this Item. */ public String initialPath; /** * The name of the Composite Activity Definition to be * instantiated for the workflow of this Item */ public String workflow; /** * New Properties for the item */ public ArrayList propertyList; /** * Field _aggregationList */ public ArrayList aggregationList; /** * Field _dependencyList */ public ArrayList dependencyList; public NewItem() { super(); propertyList = new ArrayList(); aggregationList = new ArrayList(); dependencyList = new ArrayList(); } public NewItem(String name, String initialPath, String wf) { this(); this.name = name; this.initialPath = initialPath; this.workflow = wf; } public void setProperty(String name, String value) { for (Iterator iter = propertyList.iterator(); iter.hasNext();) { Property prop = (Property) iter.next(); if (prop.name.equals(name)) { prop.value = value; return; } } propertyList.add(new Property(name, value)); } protected 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); // assemble properties PropertyArrayList propList = new PropertyArrayList(); propList.list.add(new com.c2kernel.property.Property("Name", name)); for (Iterator iter = propertyList.iterator(); iter.hasNext();) { Property element = (Property) iter.next(); propList.list.add(new com.c2kernel.property.Property(element.name, element.value)); } // init the new item try { // find workflow def CompositeActivityDef compact = (CompositeActivityDef)LocalObjectLoader.getActDef(workflow, "last"); newItem.initialise( agentId, CastorXMLUtility.marshall(propList), CastorXMLUtility.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.", ""); } // create collections for (Iterator iter = dependencyList.iterator(); iter.hasNext();) { Dependency element = (Dependency) iter.next(); 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 (Iterator iter = aggregationList.iterator(); iter.hasNext();) { Aggregation element = (Aggregation) iter.next(); try { Gateway.getStorage().put(entPath.getSysKey(), element.create(), null); } catch (ClusterStorageException ex) { Logger.error(ex); throw new CannotManageException("Could not create Aggregation "+element.name, ""); } } // register domain path domPath.setEntity(entPath); Gateway.getLDAPLookup().add(domPath); } }