Search in sources :

Example 1 with Event

use of org.cristalise.kernel.events.Event in project kernel by cristal-ise.

the class ItemImplementation method initialise.

@Override
public void initialise(SystemKey agentId, String propString, String initWfString, String initCollsString) throws AccessRightsException, InvalidDataException, PersistencyException {
    Logger.msg(5, "Item::initialise(" + mItemPath + ") - agent:" + agentId);
    Object locker = new Object();
    AgentPath agentPath;
    try {
        agentPath = new AgentPath(agentId);
    } catch (InvalidItemPathException e) {
        throw new AccessRightsException("Invalid Agent Id:" + agentId);
    }
    // must supply properties
    if (propString == null || propString.length() == 0 || propString.equals("<NULL/>")) {
        throw new InvalidDataException("No properties supplied");
    }
    // store properties
    try {
        PropertyArrayList props = (PropertyArrayList) Gateway.getMarshaller().unmarshall(propString);
        for (Property thisProp : props.list) mStorage.put(mItemPath, thisProp, locker);
    } catch (Throwable ex) {
        Logger.msg(8, "ItemImplementation::initialise(" + mItemPath + ") - Properties were invalid: " + propString);
        Logger.error(ex);
        mStorage.abort(locker);
        throw new InvalidDataException("Properties were invalid");
    }
    // Store an event and the initial properties
    try {
        Schema initSchema = LocalObjectLoader.getSchema("ItemInitialization", 0);
        Outcome initOutcome = new Outcome(0, propString, initSchema);
        History hist = new History(mItemPath, locker);
        Event newEvent = hist.addEvent(new AgentPath(agentId), null, "", "Initialize", "", "", initSchema, Bootstrap.getPredefSM(), PredefinedStep.DONE, "last");
        initOutcome.setID(newEvent.getID());
        Viewpoint newLastView = new Viewpoint(mItemPath, initSchema, "last", newEvent.getID());
        mStorage.put(mItemPath, initOutcome, locker);
        mStorage.put(mItemPath, newLastView, locker);
    } catch (Throwable ex) {
        Logger.msg(8, "ItemImplementation::initialise(" + mItemPath + ") - Could not store event and outcome.");
        Logger.error(ex);
        mStorage.abort(locker);
        throw new PersistencyException("Error storing event and outcome");
    }
    // init collections
    if (initCollsString != null && initCollsString.length() > 0 && !initCollsString.equals("<NULL/>")) {
        try {
            CollectionArrayList colls = (CollectionArrayList) Gateway.getMarshaller().unmarshall(initCollsString);
            for (Collection<?> thisColl : colls.list) {
                mStorage.put(mItemPath, thisColl, locker);
            }
        } catch (Throwable ex) {
            Logger.msg(8, "ItemImplementation::initialise(" + mItemPath + ") - Collections were invalid: " + initCollsString);
            Logger.error(ex);
            mStorage.abort(locker);
            throw new InvalidDataException("Collections were invalid");
        }
    }
    // create wf
    Workflow lc = null;
    try {
        if (initWfString == null || initWfString.length() == 0 || initWfString.equals("<NULL/>")) {
            lc = new Workflow(new CompositeActivity(), getNewPredefStepContainer());
        } else {
            lc = new Workflow((CompositeActivity) Gateway.getMarshaller().unmarshall(initWfString), getNewPredefStepContainer());
        }
        mStorage.put(mItemPath, lc, locker);
    } catch (Throwable ex) {
        Logger.msg(8, "ItemImplementation::initialise(" + mItemPath + ") - Workflow was invalid: " + initWfString);
        Logger.error(ex);
        mStorage.abort(locker);
        throw new InvalidDataException("Workflow was invalid");
    }
    // All objects are in place, initialize the workflow to get the Item running
    lc.initialise(mItemPath, agentPath, locker);
    mStorage.put(mItemPath, lc, locker);
    mStorage.commit(locker);
    Logger.msg(3, "Initialisation of item " + mItemPath + " was successful");
}
Also used : InvalidItemPathException(org.cristalise.kernel.lookup.InvalidItemPathException) AccessRightsException(org.cristalise.kernel.common.AccessRightsException) AgentPath(org.cristalise.kernel.lookup.AgentPath) Schema(org.cristalise.kernel.persistency.outcome.Schema) Workflow(org.cristalise.kernel.lifecycle.instance.Workflow) History(org.cristalise.kernel.events.History) CollectionArrayList(org.cristalise.kernel.collection.CollectionArrayList) Outcome(org.cristalise.kernel.persistency.outcome.Outcome) Viewpoint(org.cristalise.kernel.persistency.outcome.Viewpoint) CompositeActivity(org.cristalise.kernel.lifecycle.instance.CompositeActivity) InvalidDataException(org.cristalise.kernel.common.InvalidDataException) PropertyArrayList(org.cristalise.kernel.property.PropertyArrayList) Event(org.cristalise.kernel.events.Event) PersistencyException(org.cristalise.kernel.common.PersistencyException) Property(org.cristalise.kernel.property.Property)

Example 2 with Event

use of org.cristalise.kernel.events.Event in project kernel by cristal-ise.

the class ImportItem method create.

/**
 */
@Override
public Path create(AgentPath agentPath, boolean reset) throws InvalidDataException, ObjectCannotBeUpdated, ObjectNotFoundException, CannotManageException, ObjectAlreadyExistsException, InvalidCollectionModification, PersistencyException {
    domainPath = new DomainPath(new DomainPath(initialPath), name);
    if (domainPath.exists()) {
        ItemPath domItem = domainPath.getItemPath();
        if (!getItemPath().equals(domItem)) {
            throw new CannotManageException("Item " + domainPath + " was found with the wrong itemPath (" + domainPath.getItemPath() + " vs " + getItemPath() + ")");
        }
    } else
        isDOMPathExists = false;
    TraceableEntity newItem = getTraceableEntitiy();
    // (re)initialise the new item with properties, workflow and collections
    try {
        newItem.initialise(agentPath.getSystemKey(), Gateway.getMarshaller().marshall(createItemProperties()), Gateway.getMarshaller().marshall(createCompositeActivity()), Gateway.getMarshaller().marshall(createCollections()));
    } catch (Exception ex) {
        Logger.error("Error initialising new item " + ns + "/" + name);
        Logger.error(ex);
        if (isNewItem)
            Gateway.getLookupManager().delete(itemPath);
        throw new CannotManageException("Problem initialising new item. See server log:" + ex.getMessage());
    }
    History hist = new History(getItemPath(), null);
    // import outcomes
    for (ImportOutcome thisOutcome : outcomes) {
        String outcomeData = thisOutcome.getData(ns);
        // load schema and state machine
        Schema schema = LocalObjectLoader.getSchema(thisOutcome.schema, thisOutcome.version);
        // parse new outcome and validate
        Outcome newOutcome = new Outcome(-1, outcomeData, schema);
        newOutcome.validateAndCheck();
        Viewpoint impView;
        try {
            impView = (Viewpoint) Gateway.getStorage().get(getItemPath(), ClusterType.VIEWPOINT + "/" + thisOutcome.schema + "/" + thisOutcome.viewname, null);
            if (newOutcome.isIdentical(impView.getOutcome())) {
                Logger.msg(5, "ImportItem.create() - View " + thisOutcome.schema + "/" + thisOutcome.viewname + " in " + ns + "/" + name + " identical, no update required");
                continue;
            } else {
                Logger.msg("ImportItem.create() - Difference found in view " + thisOutcome.schema + "/" + thisOutcome.viewname + " in " + ns + "/" + name);
                if (!reset && !impView.getEvent().getStepPath().equals("Import")) {
                    Logger.msg("ImportItem.create() - Last edit was not done by import, and reset not requested. Not overwriting.");
                    continue;
                }
            }
        } catch (ObjectNotFoundException ex) {
            Logger.msg("ImportItem.create() - View " + thisOutcome.schema + "/" + thisOutcome.viewname + " not found in " + ns + "/" + name + ". Creating.");
            impView = new Viewpoint(getItemPath(), schema, thisOutcome.viewname, -1);
        }
        // write new view/outcome/event
        Event newEvent = hist.addEvent(agentPath, null, "Admin", "Import", "Import", "Import", schema, Bootstrap.getPredefSM(), PredefinedStep.DONE, thisOutcome.viewname);
        newOutcome.setID(newEvent.getID());
        impView.setEventId(newEvent.getID());
        Gateway.getStorage().put(getItemPath(), newOutcome, null);
        Gateway.getStorage().put(getItemPath(), impView, null);
    }
    // register domain path (before collections in case of recursive collections)
    if (!isDOMPathExists) {
        domainPath.setItemPath(getItemPath());
        Gateway.getLookupManager().add(domainPath);
    }
    return domainPath;
}
Also used : DomainPath(org.cristalise.kernel.lookup.DomainPath) CannotManageException(org.cristalise.kernel.common.CannotManageException) Schema(org.cristalise.kernel.persistency.outcome.Schema) History(org.cristalise.kernel.events.History) 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) TraceableEntity(org.cristalise.kernel.entity.TraceableEntity) Outcome(org.cristalise.kernel.persistency.outcome.Outcome) Viewpoint(org.cristalise.kernel.persistency.outcome.Viewpoint) ObjectNotFoundException(org.cristalise.kernel.common.ObjectNotFoundException) Event(org.cristalise.kernel.events.Event) ItemPath(org.cristalise.kernel.lookup.ItemPath)

Example 3 with Event

use of org.cristalise.kernel.events.Event in project kernel by cristal-ise.

the class Import method runActivityLogic.

/**
 * Params: Schemaname_version:Viewpoint (optional), Outcome, Timestamp (optional)
 */
@Override
protected String runActivityLogic(AgentPath agent, ItemPath item, int transitionID, String requestData, Object locker) throws InvalidDataException, PersistencyException, ObjectNotFoundException {
    String[] params = getDataList(requestData);
    if (Logger.doLog(3))
        Logger.msg(3, "Import: called by " + agent + " on " + item + " with parameters " + Arrays.toString(params));
    int split1 = params[0].indexOf('_');
    int split2 = params[0].indexOf(':');
    if (split1 == -1)
        throw new InvalidDataException("Import: Invalid parameters " + Arrays.toString(params));
    requestData = params[1];
    Schema schema;
    String viewpoint = null;
    String schemaName = params[0].substring(0, split1);
    int schemaVersion;
    if (split2 > -1) {
        schemaVersion = Integer.parseInt(params[0].substring(split1 + 1, split2));
        viewpoint = params[0].substring(split2 + 1);
    } else
        schemaVersion = Integer.parseInt(params[0].substring(split1 + 1));
    schema = LocalObjectLoader.getSchema(schemaName, schemaVersion);
    String timestamp;
    if (params.length == 3)
        timestamp = params[2];
    else
        timestamp = DateUtility.timeToString(DateUtility.getNow());
    // write event, outcome and viewpoints to storage
    TransactionManager storage = Gateway.getStorage();
    History hist = getWf().getHistory();
    Event event = hist.addEvent(agent, null, getCurrentAgentRole(), getName(), getPath(), getType(), schema, getStateMachine(), transitionID, viewpoint, timestamp);
    try {
        storage.put(item, new Outcome(event.getID(), requestData, schema), locker);
        storage.put(item, new Viewpoint(item, schema, viewpoint, event.getID()), locker);
        if (!"last".equals(viewpoint))
            storage.put(item, new Viewpoint(item, schema, "last", event.getID()), locker);
    } catch (PersistencyException e) {
        storage.abort(locker);
        throw e;
    }
    storage.commit(locker);
    return requestData;
}
Also used : TransactionManager(org.cristalise.kernel.persistency.TransactionManager) Outcome(org.cristalise.kernel.persistency.outcome.Outcome) Viewpoint(org.cristalise.kernel.persistency.outcome.Viewpoint) Schema(org.cristalise.kernel.persistency.outcome.Schema) InvalidDataException(org.cristalise.kernel.common.InvalidDataException) Event(org.cristalise.kernel.events.Event) PersistencyException(org.cristalise.kernel.common.PersistencyException) History(org.cristalise.kernel.events.History) Viewpoint(org.cristalise.kernel.persistency.outcome.Viewpoint)

Example 4 with Event

use of org.cristalise.kernel.events.Event in project kernel by cristal-ise.

the class WriteViewpoint method write.

public static void write(ItemPath item, String schemaName, String viewName, int eventId, Object locker) throws PersistencyException, ObjectNotFoundException, InvalidDataException {
    Event event = (Event) Gateway.getStorage().get(item, ClusterType.HISTORY + "/" + eventId, locker);
    if (StringUtils.isBlank(event.getSchemaName())) {
        throw new InvalidDataException("Event " + eventId + " does not reference an Outcome, so cannot be assigned to a Viewpoint.");
    }
    // checks Schema name/version
    Schema thisSchema = LocalObjectLoader.getSchema(schemaName, event.getSchemaVersion());
    if (!event.getSchemaName().equals(thisSchema.getItemID())) {
        throw new InvalidDataException("Event outcome schema is " + event.getSchemaName() + ", and cannot be used for a " + schemaName + " Viewpoint");
    }
    Gateway.getStorage().put(item, new Viewpoint(item, thisSchema, viewName, eventId), locker);
}
Also used : Viewpoint(org.cristalise.kernel.persistency.outcome.Viewpoint) Schema(org.cristalise.kernel.persistency.outcome.Schema) InvalidDataException(org.cristalise.kernel.common.InvalidDataException) Event(org.cristalise.kernel.events.Event)

Aggregations

InvalidDataException (org.cristalise.kernel.common.InvalidDataException)4 Event (org.cristalise.kernel.events.Event)4 Schema (org.cristalise.kernel.persistency.outcome.Schema)4 Viewpoint (org.cristalise.kernel.persistency.outcome.Viewpoint)4 PersistencyException (org.cristalise.kernel.common.PersistencyException)3 History (org.cristalise.kernel.events.History)3 Outcome (org.cristalise.kernel.persistency.outcome.Outcome)3 CollectionArrayList (org.cristalise.kernel.collection.CollectionArrayList)1 AccessRightsException (org.cristalise.kernel.common.AccessRightsException)1 CannotManageException (org.cristalise.kernel.common.CannotManageException)1 ObjectAlreadyExistsException (org.cristalise.kernel.common.ObjectAlreadyExistsException)1 ObjectNotFoundException (org.cristalise.kernel.common.ObjectNotFoundException)1 TraceableEntity (org.cristalise.kernel.entity.TraceableEntity)1 CompositeActivity (org.cristalise.kernel.lifecycle.instance.CompositeActivity)1 Workflow (org.cristalise.kernel.lifecycle.instance.Workflow)1 AgentPath (org.cristalise.kernel.lookup.AgentPath)1 DomainPath (org.cristalise.kernel.lookup.DomainPath)1 InvalidItemPathException (org.cristalise.kernel.lookup.InvalidItemPathException)1 ItemPath (org.cristalise.kernel.lookup.ItemPath)1 TransactionManager (org.cristalise.kernel.persistency.TransactionManager)1