/************************************************************************** * 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; import java.util.ArrayList; import java.util.Iterator; import com.c2kernel.collection.Collection; import com.c2kernel.collection.CollectionDescription; import com.c2kernel.collection.CollectionMember; import com.c2kernel.common.AccessRightsException; import com.c2kernel.common.InvalidDataException; import com.c2kernel.common.InvalidTransitionException; 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.stateMachine.Transitions; import com.c2kernel.lookup.AgentPath; import com.c2kernel.lookup.DomainPath; import com.c2kernel.lookup.EntityPath; import com.c2kernel.lookup.LDAPLookup; import com.c2kernel.persistency.ClusterStorage; import com.c2kernel.persistency.TransactionManager; import com.c2kernel.process.Gateway; import com.c2kernel.property.Property; import com.c2kernel.property.PropertyArrayList; import com.c2kernel.property.PropertyDescription; import com.c2kernel.property.PropertyDescriptionList; import com.c2kernel.property.PropertyUtility; import com.c2kernel.utils.CastorXMLUtility; 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 public void request(AgentPath agent, int transitionID, String requestData) throws AccessRightsException, InvalidTransitionException, InvalidDataException,ObjectAlreadyExistsException { String[] input = getDataList(requestData); String newName = input[0]; String domPath = input[1]; CompositeActivityDef wfDef; String wfDefName = null; if (input.length > 2) // override wf wfDefName = input[2]; PropertyArrayList props = new PropertyArrayList(); Logger.msg(1, "AddNewItem::request() - Starting."); TransactionManager storage = Gateway.getStorage(); LDAPLookup lookup = Gateway.getLDAPLookup(); EntityPath myPath = getItemEntityPath(); checkAccessRights(agent); 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 String[] collNames = storage.getClusterContents(myPath.getSysKey(), ClusterStorage.COLLECTION); ArrayList collections = new ArrayList(); // loop through collections to instantiate for (String collName : collNames) { Collection thisCol = (Collection)storage.get(myPath.getSysKey(), ClusterStorage.COLLECTION+"/"+collName, null); if (thisCol instanceof CollectionDescription) { CollectionDescription thisDesc = (CollectionDescription)thisCol; collections.add(CastorXMLUtility.marshall(thisDesc.newInstance())); } else if (thisCol.getName().equalsIgnoreCase("workflow") && wfDefName == null) { ArrayList members = thisCol.getMembers().list; // get the first member from the wf collection CollectionMember wfMember = members.get(0); wfDefName = wfMember.resolveEntity().getName(); } } // load workflow def if (wfDefName == null) throw new InvalidDataException("No workflow given or defined", ""); try { wfDef = (CompositeActivityDef)LocalObjectLoader.getActDef(wfDefName, "last"); } catch (ObjectNotFoundException ex) { throw new InvalidDataException("Workflow def '"+wfDefName+"' item not found", ""); } catch (ClassCastException ex) { throw new InvalidDataException("Activity def '"+wfDefName+"' was not Composite", ""); } // copy properties -- intend to create from propdesc PropertyDescriptionList pdList = PropertyUtility.getPropertyDescriptionOutcome(myPath.getSysKey()); for (int i = 0; i < pdList.list.size(); i++) { PropertyDescription pd = pdList.list.get(i); String propName = pd.getName(); String propVal = pd.getDefaultValue(); if (propName.equals("Name")) propVal = newName; props.list.add( new Property(propName, propVal)); } props.list.add( new Property("Creator", agent.getAgentName())); /* ITEM CREATION */ // generate new entity key Logger.msg(6, "CreateItemFromDescription - Requesting new sysKey"); EntityPath entityPath = lookup.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.getLDAPLookup().add(entityPath); // initialise it with its properties and workflow Logger.msg(3, "CreateItemFromDescription - Initializing Item"); newItem.initialise( agent.getSysKey(), CastorXMLUtility.marshall(props), CastorXMLUtility.marshall(wfDef.instantiate())); // add collections if (collections.size() > 0) { Logger.msg(6, "CreateItemFromDescription - Adding Collections"); String[] colls = new String[1]; for (Iterator iter = collections.iterator(); iter.hasNext();) { colls[0] = iter.next(); newItem.requestAction(agent.getSysKey(), "workflow/predefined/AddC2KObject", Transitions.COMPLETE, PredefinedStep.bundleData(colls)); } } // add its domain path Logger.msg(3, "CreateItemFromDescription - Creating "+context); context.setEntity(entityPath); Gateway.getLDAPLookup().add(context); } catch (ObjectAlreadyExistsException e) { Logger.error(e); throw e; } catch (AccessRightsException e) { Logger.error(e); throw e; } catch (Exception e) { Logger.error(e); throw new InvalidDataException(e.getMessage(), ""); } sendEventStoreOutcome(transitionID, requestData, agent); } }