Search in sources :

Example 36 with PersistencyException

use of org.cristalise.kernel.common.PersistencyException in project kernel by cristal-ise.

the class AddNewSlot method runActivityLogic.

/**
 * Creates a new slot in the given aggregation, that holds instances of the given item description
 *
 * Params:
 * <ol>
 * <li>Collection name</li>
 * <li>Item Description key (optional)</li>
 * <li>Item Description version (optional)</li>
 * </ol>
 *
 * @throws InvalidDataException
 *             Then the parameters were incorrect
 * @throws PersistencyException
 *             There was a problem loading or saving the collection from persistency
 * @throws ObjectNotFoundException
 *             A required object, such as the collection or a PropertyDescription outcome, wasn't found
 */
@Override
protected String runActivityLogic(AgentPath agent, ItemPath item, int transitionID, String requestData, Object locker) throws InvalidDataException, PersistencyException, ObjectNotFoundException {
    String collName;
    ItemPath descKey = null;
    String descVer = "last";
    // extract parameters
    String[] params = getDataList(requestData);
    if (Logger.doLog(3))
        Logger.msg(3, "AddNewSlot: called by " + agent + " on " + item + " with parameters " + Arrays.toString(params));
    // resolve desc item path and version
    try {
        collName = params[0];
        if (params.length > 1 && params[1].length() > 0) {
            try {
                descKey = new ItemPath(params[1]);
            } catch (InvalidItemPathException e) {
                descKey = new DomainPath(params[1]).getItemPath();
            }
        }
        if (params.length > 2 && params[2].length() > 0)
            descVer = params[2];
    } catch (Exception e) {
        throw new InvalidDataException("AddNewSlot: Invalid parameters " + Arrays.toString(params));
    }
    // load collection
    C2KLocalObject collObj = Gateway.getStorage().get(item, ClusterType.COLLECTION + "/" + collName + "/last", locker);
    if (!(collObj instanceof Aggregation))
        throw new InvalidDataException("AddNewSlot operates on Aggregation only.");
    Aggregation agg = (Aggregation) collObj;
    // get props
    CastorHashMap props = new CastorHashMap();
    StringBuffer classProps = new StringBuffer();
    if (descKey != null) {
        PropertyDescriptionList propList;
        propList = PropertyUtility.getPropertyDescriptionOutcome(descKey, descVer, locker);
        for (PropertyDescription pd : propList.list) {
            props.put(pd.getName(), pd.getDefaultValue());
            if (pd.getIsClassIdentifier())
                classProps.append((classProps.length() > 0 ? "," : "")).append(pd.getName());
        }
    }
    agg.addSlot(props, classProps.toString());
    Gateway.getStorage().put(item, agg, locker);
    return requestData;
}
Also used : InvalidItemPathException(org.cristalise.kernel.lookup.InvalidItemPathException) DomainPath(org.cristalise.kernel.lookup.DomainPath) InvalidDataException(org.cristalise.kernel.common.InvalidDataException) PersistencyException(org.cristalise.kernel.common.PersistencyException) InvalidItemPathException(org.cristalise.kernel.lookup.InvalidItemPathException) ObjectNotFoundException(org.cristalise.kernel.common.ObjectNotFoundException) Aggregation(org.cristalise.kernel.collection.Aggregation) PropertyDescription(org.cristalise.kernel.property.PropertyDescription) C2KLocalObject(org.cristalise.kernel.entity.C2KLocalObject) CastorHashMap(org.cristalise.kernel.utils.CastorHashMap) InvalidDataException(org.cristalise.kernel.common.InvalidDataException) PropertyDescriptionList(org.cristalise.kernel.property.PropertyDescriptionList) ItemPath(org.cristalise.kernel.lookup.ItemPath)

Example 37 with PersistencyException

use of org.cristalise.kernel.common.PersistencyException 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)

Example 38 with PersistencyException

use of org.cristalise.kernel.common.PersistencyException in project kernel by cristal-ise.

the class RemoveSlotFromCollection method runActivityLogic.

/**
 * Params: 0 - collection name 1 - slot number OR if -1: 2 - target entity key
 *
 * @throws ObjectNotFoundException
 * @throws PersistencyException
 */
@Override
protected String runActivityLogic(AgentPath agent, ItemPath item, int transitionID, String requestData, Object locker) throws InvalidDataException, ObjectNotFoundException, PersistencyException {
    String collName;
    int slotNo = -1;
    ItemPath currentChild = null;
    Collection<? extends CollectionMember> coll;
    // extract parameters
    String[] params = getDataList(requestData);
    if (Logger.doLog(3))
        Logger.msg(3, "RemoveSlotFromCollection: called by " + agent + " on " + item + " with parameters " + Arrays.toString(params));
    try {
        collName = params[0];
        if (params.length > 1 && params[1].length() > 0)
            slotNo = Integer.parseInt(params[1]);
        if (params.length > 2 && params[2].length() > 0) {
            try {
                currentChild = new ItemPath(params[2]);
            } catch (InvalidItemPathException e) {
                currentChild = new DomainPath(params[2]).getItemPath();
            }
        }
    } catch (Exception e) {
        throw new InvalidDataException("RemoveSlotFromCollection: Invalid parameters " + Arrays.toString(params));
    }
    if (slotNo == -1 && currentChild == null)
        throw new InvalidDataException("RemoveSlotFromCollection: Must give either slot number or item UUID");
    // load collection
    try {
        coll = (Collection<? extends CollectionMember>) Gateway.getStorage().get(item, ClusterType.COLLECTION + "/" + collName + "/last", locker);
    } catch (PersistencyException ex) {
        Logger.error(ex);
        throw new PersistencyException("RemoveSlotFromCollection: Error loading collection '\"+collName+\"': " + ex.getMessage());
    }
    // check the slot is there if it's given by id
    CollectionMember slot = null;
    if (slotNo > -1) {
        slot = coll.getMember(slotNo);
    }
    // if both parameters are supplied, check the given item is actually in that slot
    if (slot != null && currentChild != null && !slot.getItemPath().equals(currentChild)) {
        throw new ObjectNotFoundException("RemoveSlotFromCollection: Item " + currentChild + " was not in slot " + slotNo);
    }
    if (slotNo == -1) {
        // find slot from entity key
        for (CollectionMember member : coll.getMembers().list) {
            if (member.getItemPath().equals(currentChild)) {
                slotNo = member.getID();
                break;
            }
        }
    }
    if (slotNo == -1) {
        throw new ObjectNotFoundException("Could not find " + currentChild + " in collection " + coll.getName());
    }
    // Remove the slot
    coll.removeMember(slotNo);
    // Store the collection
    try {
        Gateway.getStorage().put(item, coll, locker);
    } catch (PersistencyException e) {
        Logger.error(e);
        throw new PersistencyException("Error storing collection");
    }
    return requestData;
}
Also used : InvalidItemPathException(org.cristalise.kernel.lookup.InvalidItemPathException) DomainPath(org.cristalise.kernel.lookup.DomainPath) CollectionMember(org.cristalise.kernel.collection.CollectionMember) ObjectNotFoundException(org.cristalise.kernel.common.ObjectNotFoundException) InvalidDataException(org.cristalise.kernel.common.InvalidDataException) PersistencyException(org.cristalise.kernel.common.PersistencyException) InvalidDataException(org.cristalise.kernel.common.InvalidDataException) PersistencyException(org.cristalise.kernel.common.PersistencyException) InvalidItemPathException(org.cristalise.kernel.lookup.InvalidItemPathException) ObjectNotFoundException(org.cristalise.kernel.common.ObjectNotFoundException) ItemPath(org.cristalise.kernel.lookup.ItemPath)

Example 39 with PersistencyException

use of org.cristalise.kernel.common.PersistencyException in project kernel by cristal-ise.

the class ModuleActivity method create.

@Override
public Path create(AgentPath agentPath, boolean reset) throws ObjectNotFoundException, ObjectCannotBeUpdated, CannotManageException, ObjectAlreadyExistsException, InvalidDataException {
    try {
        domainPath = Bootstrap.verifyResource(ns, name, version, type.getTypeCode(), itemPath, getResourceLocation(), reset);
        itemPath = domainPath.getItemPath();
    } catch (Exception e) {
        Logger.error(e);
        throw new CannotManageException("Exception verifying module resource " + ns + "/" + name);
    }
    actDef = LocalObjectLoader.getActDef(name, version);
    populateActivityDef();
    CollectionArrayList colls;
    try {
        colls = actDef.makeDescCollections();
    } catch (InvalidDataException e) {
        Logger.error(e);
        throw new CannotManageException("Could not create description collections for " + getName() + ".");
    }
    for (Collection<?> coll : colls.list) {
        try {
            Gateway.getStorage().put(itemPath, coll, null);
            // create last collection
            coll.setVersion(null);
            Gateway.getStorage().put(itemPath, coll, null);
        } catch (PersistencyException e) {
            Logger.error(e);
            throw new CannotManageException("Persistency exception storing description collections for " + getName() + ".");
        }
    }
    return domainPath;
}
Also used : CannotManageException(org.cristalise.kernel.common.CannotManageException) InvalidDataException(org.cristalise.kernel.common.InvalidDataException) PersistencyException(org.cristalise.kernel.common.PersistencyException) CollectionArrayList(org.cristalise.kernel.collection.CollectionArrayList) InvalidDataException(org.cristalise.kernel.common.InvalidDataException) ObjectNotFoundException(org.cristalise.kernel.common.ObjectNotFoundException) ObjectAlreadyExistsException(org.cristalise.kernel.common.ObjectAlreadyExistsException) PersistencyException(org.cristalise.kernel.common.PersistencyException) CannotManageException(org.cristalise.kernel.common.CannotManageException)

Example 40 with PersistencyException

use of org.cristalise.kernel.common.PersistencyException in project kernel by cristal-ise.

the class ActDefCache method loadObject.

@Override
public ActivityDef loadObject(String name, int version, ItemProxy proxy) throws ObjectNotFoundException, InvalidDataException {
    String viewName;
    if (isComposite == null) {
        String prop = proxy.getProperty(COMPLEXITY);
        if ("Composite".equals(prop))
            viewName = COMP_ACT_DESC_RESOURCE.getSchemaName();
        else if ("Elementary".equals(prop))
            viewName = ELEM_ACT_DESC_RESOURCE.getSchemaName();
        else
            throw new InvalidDataException("Missing Item property:" + COMPLEXITY);
    } else {
        viewName = isComposite ? COMP_ACT_DESC_RESOURCE.getSchemaName() : ELEM_ACT_DESC_RESOURCE.getSchemaName();
    }
    try {
        Viewpoint actView = (Viewpoint) proxy.getObject(ClusterType.VIEWPOINT + "/" + viewName + "/" + version);
        String marshalledAct = actView.getOutcome().getData();
        return buildObject(name, version, proxy.getPath(), marshalledAct);
    } catch (PersistencyException ex) {
        Logger.error(ex);
        throw new ObjectNotFoundException("Problem loading Activity " + name + " v" + version + ": " + ex.getMessage());
    }
}
Also used : Viewpoint(org.cristalise.kernel.persistency.outcome.Viewpoint) ObjectNotFoundException(org.cristalise.kernel.common.ObjectNotFoundException) InvalidDataException(org.cristalise.kernel.common.InvalidDataException) PersistencyException(org.cristalise.kernel.common.PersistencyException)

Aggregations

PersistencyException (org.cristalise.kernel.common.PersistencyException)43 InvalidDataException (org.cristalise.kernel.common.InvalidDataException)26 ObjectNotFoundException (org.cristalise.kernel.common.ObjectNotFoundException)26 ObjectAlreadyExistsException (org.cristalise.kernel.common.ObjectAlreadyExistsException)10 IOException (java.io.IOException)9 Viewpoint (org.cristalise.kernel.persistency.outcome.Viewpoint)9 C2KLocalObject (org.cristalise.kernel.entity.C2KLocalObject)8 InvalidItemPathException (org.cristalise.kernel.lookup.InvalidItemPathException)8 AccessRightsException (org.cristalise.kernel.common.AccessRightsException)6 CannotManageException (org.cristalise.kernel.common.CannotManageException)6 ItemPath (org.cristalise.kernel.lookup.ItemPath)6 Outcome (org.cristalise.kernel.persistency.outcome.Outcome)6 MappingException (org.exolab.castor.mapping.MappingException)6 MarshalException (org.exolab.castor.xml.MarshalException)6 ValidationException (org.exolab.castor.xml.ValidationException)6 ObjectCannotBeUpdated (org.cristalise.kernel.common.ObjectCannotBeUpdated)5 History (org.cristalise.kernel.events.History)5 AgentPath (org.cristalise.kernel.lookup.AgentPath)5 DomainPath (org.cristalise.kernel.lookup.DomainPath)5 ArrayList (java.util.ArrayList)4