Search in sources :

Example 1 with Workflow

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

the class ItemImplementation method delegatedAction.

@Override
public String delegatedAction(SystemKey agentId, SystemKey delegateId, String stepPath, int transitionID, String requestData) throws AccessRightsException, InvalidTransitionException, ObjectNotFoundException, InvalidDataException, PersistencyException, ObjectAlreadyExistsException, InvalidCollectionModification {
    Workflow lifeCycle = null;
    try {
        AgentPath agent = new AgentPath(agentId);
        AgentPath delegate = delegateId == null ? null : new AgentPath(delegateId);
        Logger.msg(1, "ItemImplementation::request(" + mItemPath + ") - Transition " + transitionID + " on " + stepPath + " by " + (delegate == null ? "" : delegate + " on behalf of ") + agent);
        // TODO: check if delegate is allowed valid for agent
        lifeCycle = (Workflow) mStorage.get(mItemPath, ClusterType.LIFECYCLE + "/workflow", null);
        String finalOutcome = lifeCycle.requestAction(agent, delegate, stepPath, mItemPath, transitionID, requestData);
        // store the workflow if we've changed the state of the domain wf
        if (!(stepPath.startsWith("workflow/predefined")))
            mStorage.put(mItemPath, lifeCycle, lifeCycle);
        // remove entity path if transaction was successful
        if (stepPath.equals("workflow/predefined/Erase")) {
            Logger.msg("Erasing item path " + mItemPath.toString());
            Gateway.getLookupManager().delete(mItemPath);
        }
        mStorage.commit(lifeCycle);
        return finalOutcome;
    } catch (AccessRightsException | InvalidTransitionException | ObjectNotFoundException | PersistencyException | InvalidDataException | ObjectAlreadyExistsException | InvalidCollectionModification ex) {
        if (Logger.doLog(8))
            Logger.error(ex);
        String errorOutcome = handleError(agentId, delegateId, stepPath, lifeCycle, ex);
        if (StringUtils.isBlank(errorOutcome)) {
            mStorage.abort(lifeCycle);
            throw ex;
        } else {
            mStorage.commit(lifeCycle);
            return errorOutcome;
        }
    } catch (InvalidAgentPathException | ObjectCannotBeUpdated | CannotManageException ex) {
        if (Logger.doLog(8))
            Logger.error(ex);
        String errorOutcome = handleError(agentId, delegateId, stepPath, lifeCycle, ex);
        if (StringUtils.isBlank(errorOutcome)) {
            mStorage.abort(lifeCycle);
            throw new InvalidDataException(ex.getClass().getName() + " - " + ex.getMessage());
        } else {
            mStorage.commit(lifeCycle);
            return errorOutcome;
        }
    } catch (Exception ex) {
        // non-CORBA exception hasn't been caught!
        Logger.error("Unknown Error: requestAction on " + mItemPath + " by " + agentId + " executing " + stepPath);
        Logger.error(ex);
        String errorOutcome = handleError(agentId, delegateId, stepPath, lifeCycle, ex);
        if (StringUtils.isBlank(errorOutcome)) {
            mStorage.abort(lifeCycle);
            throw new InvalidDataException("Extraordinary Exception during execution:" + ex.getClass().getName() + " - " + ex.getMessage());
        } else {
            mStorage.commit(lifeCycle);
            return errorOutcome;
        }
    }
}
Also used : InvalidTransitionException(org.cristalise.kernel.common.InvalidTransitionException) AccessRightsException(org.cristalise.kernel.common.AccessRightsException) CannotManageException(org.cristalise.kernel.common.CannotManageException) AgentPath(org.cristalise.kernel.lookup.AgentPath) Workflow(org.cristalise.kernel.lifecycle.instance.Workflow) InvalidCollectionModification(org.cristalise.kernel.common.InvalidCollectionModification) MappingException(org.exolab.castor.mapping.MappingException) InvalidDataException(org.cristalise.kernel.common.InvalidDataException) InvalidItemPathException(org.cristalise.kernel.lookup.InvalidItemPathException) ObjectNotFoundException(org.cristalise.kernel.common.ObjectNotFoundException) ValidationException(org.exolab.castor.xml.ValidationException) AccessRightsException(org.cristalise.kernel.common.AccessRightsException) MarshalException(org.exolab.castor.xml.MarshalException) InvalidTransitionException(org.cristalise.kernel.common.InvalidTransitionException) ObjectAlreadyExistsException(org.cristalise.kernel.common.ObjectAlreadyExistsException) IOException(java.io.IOException) PersistencyException(org.cristalise.kernel.common.PersistencyException) CannotManageException(org.cristalise.kernel.common.CannotManageException) InvalidAgentPathException(org.cristalise.kernel.lookup.InvalidAgentPathException) ObjectNotFoundException(org.cristalise.kernel.common.ObjectNotFoundException) InvalidDataException(org.cristalise.kernel.common.InvalidDataException) PersistencyException(org.cristalise.kernel.common.PersistencyException) ObjectCannotBeUpdated(org.cristalise.kernel.common.ObjectCannotBeUpdated) ObjectAlreadyExistsException(org.cristalise.kernel.common.ObjectAlreadyExistsException) InvalidAgentPathException(org.cristalise.kernel.lookup.InvalidAgentPathException)

Example 2 with Workflow

use of org.cristalise.kernel.lifecycle.instance.Workflow 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 3 with Workflow

use of org.cristalise.kernel.lifecycle.instance.Workflow 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 4 with Workflow

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

the class LifecycleRendererTest method generateInstance_SVG.

@Test
public void generateInstance_SVG() throws Exception {
    String wfXML = FileStringUtility.url2String(LifecycleRendererTest.class.getResource("/LifeCycle.workflow"));
    Workflow wf = (Workflow) Gateway.getMarshaller().unmarshall(wfXML);
    LifecycleRenderer generator = new LifecycleRenderer(wf.search("workflow/domain").getChildrenGraphModel(), false);
    int zoomFactor = generator.getZoomFactor(500, 500);
    SVGGraphics2D svgG2D = new SVGGraphics2D(500, 500);
    svgG2D.scale((double) zoomFactor / 100, (double) zoomFactor / 100);
    generator.draw(svgG2D);
    SVGUtils.writeToSVG(new File("target/workflow.svg"), svgG2D.getSVGElement());
}
Also used : LifecycleRenderer(org.cristalise.kernel.lifecycle.renderer.LifecycleRenderer) Workflow(org.cristalise.kernel.lifecycle.instance.Workflow) SVGGraphics2D(org.jfree.graphics2d.svg.SVGGraphics2D) File(java.io.File) Test(org.junit.Test)

Example 5 with Workflow

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

the class ItemImplementation method queryLifeCycle.

/**
 */
@Override
public String queryLifeCycle(SystemKey agentId, boolean filter) throws AccessRightsException, ObjectNotFoundException, PersistencyException {
    Logger.msg(1, "ItemImplementation::queryLifeCycle(" + mItemPath + ") - agent: " + agentId);
    try {
        AgentPath agent;
        try {
            agent = new AgentPath(agentId);
        } catch (InvalidItemPathException e) {
            throw new AccessRightsException("Agent " + agentId + " doesn't exist");
        }
        Workflow wf = (Workflow) mStorage.get(mItemPath, ClusterType.LIFECYCLE + "/workflow", null);
        JobArrayList jobBag = new JobArrayList();
        CompositeActivity domainWf = (CompositeActivity) wf.search("workflow/domain");
        jobBag.list = filter ? domainWf.calculateJobs(agent, mItemPath, true) : domainWf.calculateAllJobs(agent, mItemPath, true);
        Logger.msg(1, "ItemImplementation::queryLifeCycle(" + mItemPath + ") - Returning " + jobBag.list.size() + " jobs.");
        try {
            return Gateway.getMarshaller().marshall(jobBag);
        } catch (Exception e) {
            Logger.error(e);
            throw new PersistencyException("Error marshalling job bag");
        }
    } catch (AccessRightsException | ObjectNotFoundException | PersistencyException e) {
        Logger.error(e);
        throw e;
    } catch (Throwable ex) {
        Logger.error("ItemImplementation::queryLifeCycle(" + mItemPath + ") - Unknown error");
        Logger.error(ex);
        throw new PersistencyException("Unknown error querying jobs. Please see server log.");
    }
}
Also used : InvalidItemPathException(org.cristalise.kernel.lookup.InvalidItemPathException) AccessRightsException(org.cristalise.kernel.common.AccessRightsException) AgentPath(org.cristalise.kernel.lookup.AgentPath) CompositeActivity(org.cristalise.kernel.lifecycle.instance.CompositeActivity) ObjectNotFoundException(org.cristalise.kernel.common.ObjectNotFoundException) Workflow(org.cristalise.kernel.lifecycle.instance.Workflow) PersistencyException(org.cristalise.kernel.common.PersistencyException) JobArrayList(org.cristalise.kernel.entity.agent.JobArrayList) MappingException(org.exolab.castor.mapping.MappingException) InvalidDataException(org.cristalise.kernel.common.InvalidDataException) InvalidItemPathException(org.cristalise.kernel.lookup.InvalidItemPathException) ObjectNotFoundException(org.cristalise.kernel.common.ObjectNotFoundException) ValidationException(org.exolab.castor.xml.ValidationException) AccessRightsException(org.cristalise.kernel.common.AccessRightsException) MarshalException(org.exolab.castor.xml.MarshalException) InvalidTransitionException(org.cristalise.kernel.common.InvalidTransitionException) ObjectAlreadyExistsException(org.cristalise.kernel.common.ObjectAlreadyExistsException) IOException(java.io.IOException) PersistencyException(org.cristalise.kernel.common.PersistencyException) CannotManageException(org.cristalise.kernel.common.CannotManageException) InvalidAgentPathException(org.cristalise.kernel.lookup.InvalidAgentPathException)

Aggregations

Workflow (org.cristalise.kernel.lifecycle.instance.Workflow)8 InvalidDataException (org.cristalise.kernel.common.InvalidDataException)5 PersistencyException (org.cristalise.kernel.common.PersistencyException)4 InvalidItemPathException (org.cristalise.kernel.lookup.InvalidItemPathException)4 AccessRightsException (org.cristalise.kernel.common.AccessRightsException)3 ObjectNotFoundException (org.cristalise.kernel.common.ObjectNotFoundException)3 CompositeActivity (org.cristalise.kernel.lifecycle.instance.CompositeActivity)3 AgentPath (org.cristalise.kernel.lookup.AgentPath)3 Outcome (org.cristalise.kernel.persistency.outcome.Outcome)3 IOException (java.io.IOException)2 CollectionArrayList (org.cristalise.kernel.collection.CollectionArrayList)2 CannotManageException (org.cristalise.kernel.common.CannotManageException)2 InvalidTransitionException (org.cristalise.kernel.common.InvalidTransitionException)2 ObjectAlreadyExistsException (org.cristalise.kernel.common.ObjectAlreadyExistsException)2 InvalidAgentPathException (org.cristalise.kernel.lookup.InvalidAgentPathException)2 Schema (org.cristalise.kernel.persistency.outcome.Schema)2 Viewpoint (org.cristalise.kernel.persistency.outcome.Viewpoint)2 Property (org.cristalise.kernel.property.Property)2 PropertyArrayList (org.cristalise.kernel.property.PropertyArrayList)2 MappingException (org.exolab.castor.mapping.MappingException)2