Search in sources :

Example 1 with Collection

use of org.cristalise.kernel.collection.Collection in project kernel by cristal-ise.

the class TransferItem method importItem.

public void importItem(File dir) throws Exception {
    // check if already exists
    try {
        Property name = (Property) Gateway.getStorage().get(itemPath, PROPERTY + "/" + NAME, null);
        throw new Exception("Item " + itemPath + " already in use as " + name.getValue());
    } catch (Exception ex) {
    }
    // retrieve objects
    ArrayList<String> objectFiles = FileStringUtility.listDir(dir.getCanonicalPath(), false, true);
    ArrayList<C2KLocalObject> objects = new ArrayList<C2KLocalObject>();
    for (String element : objectFiles) {
        String xmlFile = FileStringUtility.file2String(element);
        C2KLocalObject newObj;
        String choppedPath = element.substring(dir.getCanonicalPath().length() + 1, element.length() - 4);
        Logger.msg(choppedPath);
        if (choppedPath.startsWith(OUTCOME.getName()))
            newObj = new Outcome(choppedPath, xmlFile);
        else
            newObj = (C2KLocalObject) Gateway.getMarshaller().unmarshall(xmlFile);
        objects.add(newObj);
    }
    // create item
    TraceableEntity newItem = Gateway.getCorbaServer().createItem(itemPath);
    Gateway.getLookupManager().add(itemPath);
    PropertyArrayList props = new PropertyArrayList();
    CollectionArrayList colls = new CollectionArrayList();
    Workflow wf = null;
    // put objects
    for (C2KLocalObject obj : objects) {
        if (obj instanceof Property)
            props.list.add((Property) obj);
        else if (obj instanceof Collection)
            colls.list.add((Collection<?>) obj);
        else if (obj instanceof Workflow)
            wf = (Workflow) obj;
    }
    if (wf == null)
        throw new Exception("No workflow found in import for " + itemPath);
    // init item
    newItem.initialise(importAgentId.getSystemKey(), Gateway.getMarshaller().marshall(props), Gateway.getMarshaller().marshall(wf.search("workflow/domain")), Gateway.getMarshaller().marshall(colls));
    // store objects
    importByType(ClusterType.HISTORY, objects);
    importByType(ClusterType.OUTCOME, objects);
    importByType(ClusterType.VIEWPOINT, objects);
    Gateway.getStorage().commit(this);
    // add domPaths
    for (String element : domainPaths) {
        DomainPath newPath = new DomainPath(element, itemPath);
        Gateway.getLookupManager().add(newPath);
    }
}
Also used : DomainPath(org.cristalise.kernel.lookup.DomainPath) CollectionArrayList(org.cristalise.kernel.collection.CollectionArrayList) ArrayList(java.util.ArrayList) PropertyArrayList(org.cristalise.kernel.property.PropertyArrayList) Workflow(org.cristalise.kernel.lifecycle.instance.Workflow) CollectionArrayList(org.cristalise.kernel.collection.CollectionArrayList) InvalidItemPathException(org.cristalise.kernel.lookup.InvalidItemPathException) ObjectNotFoundException(org.cristalise.kernel.common.ObjectNotFoundException) TraceableEntity(org.cristalise.kernel.entity.TraceableEntity) C2KLocalObject(org.cristalise.kernel.entity.C2KLocalObject) Outcome(org.cristalise.kernel.persistency.outcome.Outcome) PropertyArrayList(org.cristalise.kernel.property.PropertyArrayList) Collection(org.cristalise.kernel.collection.Collection) Property(org.cristalise.kernel.property.Property)

Example 2 with Collection

use of org.cristalise.kernel.collection.Collection in project kernel by cristal-ise.

the class CreateItemFromDescription method instantiateCollections.

/**
 * Copies the CollectionDescriptions of the Item requesting this predefined step.
 *
 * @param descItemPath
 * @param descVer
 * @param locker
 * @return the new collection
 * @throws ObjectNotFoundException
 * @throws PersistencyException
 * @throws InvalidDataException
 */
protected CollectionArrayList instantiateCollections(ItemPath descItemPath, String descVer, PropertyArrayList newProps, Object locker) throws ObjectNotFoundException, PersistencyException, InvalidDataException {
    // loop through collections, collecting instantiated descriptions and finding the default workflow def
    CollectionArrayList colls = new CollectionArrayList();
    String[] collNames = Gateway.getStorage().getClusterContents(descItemPath, ClusterType.COLLECTION);
    for (String collName : collNames) {
        @SuppressWarnings("unchecked") Collection<? extends CollectionMember> thisCol = (Collection<? extends CollectionMember>) Gateway.getStorage().get(descItemPath, ClusterType.COLLECTION + "/" + collName + "/" + descVer, locker);
        if (thisCol instanceof CollectionDescription) {
            Logger.msg(5, "CreateItemFromDescription - Instantiating CollectionDescription:" + collName);
            CollectionDescription<?> thisDesc = (CollectionDescription<?>) thisCol;
            colls.put(thisDesc.newInstance());
        } else if (thisCol instanceof Dependency) {
            Logger.msg(5, "CreateItemFromDescription - Instantiating Dependency:" + collName);
            ((Dependency) thisCol).addToItemProperties(newProps);
        } else {
            Logger.warning("CreateItemFromDescription - CANNOT instantiate collection:" + collName + " class:" + thisCol.getClass().getName());
        }
    }
    return colls;
}
Also used : CollectionMember(org.cristalise.kernel.collection.CollectionMember) CollectionDescription(org.cristalise.kernel.collection.CollectionDescription) Collection(org.cristalise.kernel.collection.Collection) Dependency(org.cristalise.kernel.collection.Dependency) CollectionArrayList(org.cristalise.kernel.collection.CollectionArrayList)

Example 3 with Collection

use of org.cristalise.kernel.collection.Collection in project kernel by cristal-ise.

the class CreateItemFromDescription method instantiateWorkflow.

/**
 * Retrieve the Workflow dependency for the given description version, instantiate the loaded CompositeActivityDef
 *
 * @param descItemPath
 * @param descVer
 * @param locker
 * @return the Workflow instance
 * @throws ObjectNotFoundException
 * @throws InvalidDataException
 * @throws PersistencyException
 */
protected CompositeActivity instantiateWorkflow(ItemPath descItemPath, String descVer, Object locker) throws ObjectNotFoundException, InvalidDataException, PersistencyException {
    @SuppressWarnings("unchecked") Collection<? extends CollectionMember> thisCol = (Collection<? extends CollectionMember>) Gateway.getStorage().get(descItemPath, ClusterType.COLLECTION + "/" + WORKFLOW + "/" + descVer, locker);
    CollectionMember wfMember = thisCol.getMembers().list.get(0);
    String wfDefName = wfMember.resolveItem().getName();
    Object wfVerObj = wfMember.getProperties().getBuiltInProperty(VERSION);
    if (wfVerObj == null || String.valueOf(wfVerObj).length() == 0) {
        throw new InvalidDataException("Workflow version number not set");
    }
    try {
        Integer wfDefVer = Integer.parseInt(wfVerObj.toString());
        if (wfDefName == null)
            throw new InvalidDataException("No workflow given or defined");
        // load workflow def
        CompositeActivityDef wfDef = (CompositeActivityDef) LocalObjectLoader.getActDef(wfDefName, wfDefVer);
        return (CompositeActivity) wfDef.instantiate();
    } catch (NumberFormatException ex) {
        throw new InvalidDataException("Invalid workflow version number: " + wfVerObj.toString());
    } catch (ClassCastException ex) {
        Logger.error(ex);
        throw new InvalidDataException("Activity def '" + wfDefName + "' was not Composite");
    }
}
Also used : CompositeActivity(org.cristalise.kernel.lifecycle.instance.CompositeActivity) CollectionMember(org.cristalise.kernel.collection.CollectionMember) InvalidDataException(org.cristalise.kernel.common.InvalidDataException) Collection(org.cristalise.kernel.collection.Collection) CompositeActivityDef(org.cristalise.kernel.lifecycle.CompositeActivityDef)

Example 4 with Collection

use of org.cristalise.kernel.collection.Collection in project kernel by cristal-ise.

the class CreateNewCollectionVersion method runActivityLogic.

/**
 * Generates a new snapshot of a collection from its current state.
 * The new version is given the next available number, starting at 0.
 * <pre>
 * Params:
 * 0 - Collection name
 * 1 - Version (optional)
 * </pre>
 */
@Override
protected String runActivityLogic(AgentPath agent, ItemPath item, int transitionID, String requestData, Object locker) throws InvalidDataException, PersistencyException, ObjectNotFoundException {
    // extract parameters
    String[] params = getDataList(requestData);
    if (Logger.doLog(3))
        Logger.msg(3, "CreateNewCollectionVersion: called by " + agent + " on " + item + " with parameters " + Arrays.toString(params));
    if (params.length == 0 || params.length > 2) {
        throw new InvalidDataException("CreateNewCollectionVersion: Invalid parameters " + Arrays.toString(params));
    }
    String collName = params[0];
    Collection<?> coll = (Collection<?>) Gateway.getStorage().get(item, ClusterType.COLLECTION + "/" + collName + "/last", locker);
    int newVersion;
    if (params.length > 1) {
        newVersion = Integer.valueOf(params[1]);
    } else {
        // find last numbered version
        String[] versions = Gateway.getStorage().getClusterContents(item, ClusterType.COLLECTION + "/" + collName);
        int lastVer = -1;
        for (String thisVerStr : versions) {
            try {
                int thisVer = Integer.parseInt(thisVerStr);
                if (thisVer > lastVer)
                    lastVer = thisVer;
            }// ignore non-integer versions
             catch (NumberFormatException ex) {
            }
        }
        newVersion = lastVer + 1;
    }
    // Remove it from the cache before we change it
    Gateway.getStorage().clearCache(item, ClusterType.COLLECTION + "/" + collName + "/last");
    // Set the version & store it
    try {
        coll.setVersion(newVersion);
        Gateway.getStorage().put(item, coll, locker);
    } catch (PersistencyException e) {
        throw new PersistencyException("CreateNewCollectionVersion: Error saving new collection '" + collName + "': " + e.getMessage());
    }
    return requestData;
}
Also used : InvalidDataException(org.cristalise.kernel.common.InvalidDataException) Collection(org.cristalise.kernel.collection.Collection) PersistencyException(org.cristalise.kernel.common.PersistencyException)

Aggregations

Collection (org.cristalise.kernel.collection.Collection)4 CollectionArrayList (org.cristalise.kernel.collection.CollectionArrayList)2 CollectionMember (org.cristalise.kernel.collection.CollectionMember)2 InvalidDataException (org.cristalise.kernel.common.InvalidDataException)2 ArrayList (java.util.ArrayList)1 CollectionDescription (org.cristalise.kernel.collection.CollectionDescription)1 Dependency (org.cristalise.kernel.collection.Dependency)1 ObjectNotFoundException (org.cristalise.kernel.common.ObjectNotFoundException)1 PersistencyException (org.cristalise.kernel.common.PersistencyException)1 C2KLocalObject (org.cristalise.kernel.entity.C2KLocalObject)1 TraceableEntity (org.cristalise.kernel.entity.TraceableEntity)1 CompositeActivityDef (org.cristalise.kernel.lifecycle.CompositeActivityDef)1 CompositeActivity (org.cristalise.kernel.lifecycle.instance.CompositeActivity)1 Workflow (org.cristalise.kernel.lifecycle.instance.Workflow)1 DomainPath (org.cristalise.kernel.lookup.DomainPath)1 InvalidItemPathException (org.cristalise.kernel.lookup.InvalidItemPathException)1 Outcome (org.cristalise.kernel.persistency.outcome.Outcome)1 Property (org.cristalise.kernel.property.Property)1 PropertyArrayList (org.cristalise.kernel.property.PropertyArrayList)1