Search in sources :

Example 1 with State

use of org.cristalise.kernel.lifecycle.instance.stateMachine.State in project kernel by cristal-ise.

the class Activity method request.

public String request(AgentPath agent, AgentPath delegate, ItemPath itemPath, int transitionID, String requestData, Object locker) throws AccessRightsException, InvalidTransitionException, InvalidDataException, ObjectNotFoundException, PersistencyException, ObjectAlreadyExistsException, ObjectCannotBeUpdated, CannotManageException, InvalidCollectionModification {
    // Find requested transition
    Transition transition = getStateMachine().getTransition(transitionID);
    // Check if the transition is possible
    String usedRole = transition.getPerformingRole(this, agent);
    // Verify outcome
    boolean storeOutcome = false;
    if (transition.hasOutcome(getProperties())) {
        if (StringUtils.isNotBlank(requestData))
            storeOutcome = true;
        else if (transition.getOutcome().isRequired())
            throw new InvalidDataException("Transition requires outcome data, but none was given");
    }
    // Get new state
    State oldState = getStateMachine().getState(this.state);
    State newState = getStateMachine().traverse(this, transition, agent);
    // Run extra logic in predefined steps here
    String outcome = runActivityLogic(agent, itemPath, transitionID, requestData, locker);
    // set new state and reservation
    setState(newState.getId());
    setBuiltInProperty(AGENT_NAME, transition.getReservation(this, agent));
    try {
        History hist = getWf().getHistory(locker);
        if (storeOutcome) {
            Schema schema = transition.getSchema(getProperties());
            Outcome newOutcome = new Outcome(-1, outcome, schema);
            // TODO: if we were ever going to validate outcomes on storage, it would be here.
            // newOutcome.validateAndCheck();
            String viewpoint = resolveViewpointName(newOutcome);
            int eventID = hist.addEvent(agent, delegate, usedRole, getName(), getPath(), getType(), schema, getStateMachine(), transitionID, viewpoint).getID();
            newOutcome.setID(eventID);
            Gateway.getStorage().put(itemPath, newOutcome, locker);
            // update specific view if defined
            if (!viewpoint.equals("last")) {
                Gateway.getStorage().put(itemPath, new Viewpoint(itemPath, schema, viewpoint, eventID), locker);
            }
            // update the default "last" view
            Gateway.getStorage().put(itemPath, new Viewpoint(itemPath, schema, "last", eventID), locker);
            updateItemProperties(itemPath, newOutcome, locker);
        } else {
            hist.addEvent(agent, delegate, usedRole, getName(), getPath(), getType(), getStateMachine(), transitionID);
        }
    } catch (PersistencyException ex) {
        Logger.error(ex);
        throw ex;
    }
    if (newState.isFinished() && !(getBuiltInProperty(BREAKPOINT).equals(Boolean.TRUE) && !oldState.isFinished())) {
        runNext(agent, itemPath, locker);
    }
    DateUtility.setToNow(mStateDate);
    pushJobsToAgents(itemPath);
    return outcome;
}
Also used : State(org.cristalise.kernel.lifecycle.instance.stateMachine.State) Outcome(org.cristalise.kernel.persistency.outcome.Outcome) Viewpoint(org.cristalise.kernel.persistency.outcome.Viewpoint) Schema(org.cristalise.kernel.persistency.outcome.Schema) Transition(org.cristalise.kernel.lifecycle.instance.stateMachine.Transition) InvalidDataException(org.cristalise.kernel.common.InvalidDataException) PersistencyException(org.cristalise.kernel.common.PersistencyException) History(org.cristalise.kernel.events.History) Viewpoint(org.cristalise.kernel.persistency.outcome.Viewpoint)

Aggregations

InvalidDataException (org.cristalise.kernel.common.InvalidDataException)1 PersistencyException (org.cristalise.kernel.common.PersistencyException)1 History (org.cristalise.kernel.events.History)1 State (org.cristalise.kernel.lifecycle.instance.stateMachine.State)1 Transition (org.cristalise.kernel.lifecycle.instance.stateMachine.Transition)1 Outcome (org.cristalise.kernel.persistency.outcome.Outcome)1 Schema (org.cristalise.kernel.persistency.outcome.Schema)1 Viewpoint (org.cristalise.kernel.persistency.outcome.Viewpoint)1