Search in sources :

Example 11 with WorkflowDefinition

use of com.liferay.ide.kaleo.core.model.WorkflowDefinition in project liferay-ide by liferay.

the class NewWorkflowDefinitionOpMethods method execute.

public static Status execute(NewWorkflowDefinitionOp op, ProgressMonitor monitor) {
    try {
        ReferenceValue<String, IProject> referProject = op.getProject();
        IProject project = referProject.target();
        Value<Path> opFolder = op.getFolder();
        Path projectPath = opFolder.content();
        IContainer folder = null;
        if (projectPath != null) {
            folder = project.getFolder(projectPath.toPortableString());
        } else {
            folder = project;
        }
        if (!folder.exists()) {
            folder = folder.getParent();
        }
        Value<String> opName = op.getName();
        String name = opName.content();
        String lowerCaseName = name.toLowerCase();
        String[] segments = lowerCaseName.split("\\s+");
        StringBuilder newName = new StringBuilder();
        for (String segment : segments) {
            StringBuilder segNewName = newName.append(segment);
            segNewName.append('-');
        }
        String fileNameBase = newName.toString() + "definition";
        String extension = ".xml";
        String fileName = fileNameBase + extension;
        IPath proRelativePath = folder.getProjectRelativePath();
        IFile newDefinitionFile = project.getFile(proRelativePath.append(fileName));
        int count = 1;
        while (newDefinitionFile.exists()) {
            fileName = fileNameBase + " (" + count + ")" + extension;
            newDefinitionFile = project.getFile(proRelativePath.append(fileName));
            count++;
        }
        newDefinitionFile.create(new ByteArrayInputStream("".getBytes()), true, null);
        Value<ScriptLanguageType> scLanguageType = op.getDefaultScriptLanguage();
        newDefinitionFile.setPersistentProperty(KaleoCore.DEFAULT_SCRIPT_LANGUAGE_KEY, scLanguageType.text(true));
        Value<TemplateLanguageType> deTemplateLanguage = op.getDefaultTemplateLanguage();
        newDefinitionFile.setPersistentProperty(KaleoCore.DEFAULT_TEMPLATE_LANGUAGE_KEY, deTemplateLanguage.text(true));
        RootXmlResource rootXmlResource = new RootXmlResource(new XmlResourceStore(new WorkspaceFileResourceStore(newDefinitionFile)));
        WorkflowDefinition workflowDefinition = WorkflowDefinition.TYPE.instantiate(rootXmlResource);
        workflowDefinition.setName(name);
        workflowDefinition.setVersion("1");
        Value<String> initStateName = op.getInitialStateName();
        String initialStateName = initStateName.content(true);
        ElementList<State> workflowState = workflowDefinition.getStates();
        State state = workflowState.insert();
        state.setName(initialStateName);
        state.setInitial(true);
        Value<String> initTaskName = op.getInitialTaskName();
        String initialTaskName = initTaskName.content(true);
        ElementList<Task> workflowTask = workflowDefinition.getTasks();
        Task task = workflowTask.insert();
        task.setName(initialTaskName);
        KaleoModelUtil.changeTaskAssignments(task, op);
        ElementList<Transition> transitions = state.getTransitions();
        Transition transition = transitions.insert();
        transition.setName(initialTaskName);
        transition.setTarget(initialTaskName);
        Value<String> opFinalName = op.getFinalStateName();
        String finalStateName = opFinalName.content(true);
        ElementList<State> states = workflowDefinition.getStates();
        State finalState = states.insert();
        finalState.setName(finalStateName);
        finalState.setEnd(true);
        ElementList<Action> actions = finalState.getActions();
        Action finalAction = actions.insert();
        finalAction.setName("approve");
        finalAction.setExecutionType(ExecutionType.ON_ENTRY);
        finalAction.setScriptLanguage(ScriptLanguageType.JAVASCRIPT);
        String updateStatus = "Packages.com.liferay.portal.kernel.workflow.WorkflowStatusManagerUtil.updateStatus";
        String workflowConstants = "(Packages.com.liferay.portal.kernel.workflow.WorkflowConstants.";
        String toStatus = "toStatus(\"approved\"), workflowContext);";
        finalAction.setScript(updateStatus + workflowConstants + toStatus);
        ElementList<Transition> taskTransitions = task.getTransitions();
        Transition taskTransition = taskTransitions.insert();
        taskTransition.setName(finalStateName);
        taskTransition.setTarget(finalStateName);
        ElementHandle<WorkflowNodeMetadata> wfMetadata = state.getMetadata();
        WorkflowNodeMetadata stateMetadata = wfMetadata.content();
        Position statePosition = stateMetadata.getPosition();
        statePosition.setX(100);
        statePosition.setY(50);
        ElementList<TransitionMetadata> transitionMetadata = stateMetadata.getTransitionsMetadata();
        TransitionMetadata insertTransitionMetadata = transitionMetadata.insert();
        insertTransitionMetadata.setName(initialTaskName);
        ElementHandle<WorkflowNodeMetadata> taskWorkflow = task.getMetadata();
        WorkflowNodeMetadata taskMetadata = taskWorkflow.content();
        Position taskPosition = taskMetadata.getPosition();
        taskPosition.setX(300);
        taskPosition.setY(35);
        ElementList<TransitionMetadata> transitionTask = taskMetadata.getTransitionsMetadata();
        TransitionMetadata transitionTaskMetadata = transitionTask.insert();
        transitionTaskMetadata.setName(finalStateName);
        ElementHandle<WorkflowNodeMetadata> finalMetadata = finalState.getMetadata();
        WorkflowNodeMetadata finalStateMetadata = finalMetadata.content();
        Position finalStatePosition = finalStateMetadata.getPosition();
        finalStatePosition.setX(520);
        finalStatePosition.setY(50);
        /*
			 * Document document = rootXmlResource.getDomDocument(); Element
			 * docElement = document.getDocumentElement(); Attr schemaLocation =
			 * docElement.getAttributeNode( "xsi:schemaLocation" ); =
			 * schemaLocation.getNodeValue(); String nodeValue =
			 * schemaLocation.getNodeValue(); schemaLocation.setNodeValue(
			 * nodeValue.replaceAll( " http://www.w3.org/XML/1998/namespace ",
			 * "" ) );
			 */
        rootXmlResource.save();
        IPath fullPath = newDefinitionFile.getFullPath();
        op.setNewFilePath(fullPath.toPortableString());
        return Status.createOkStatus();
    } catch (Exception e) {
        KaleoCore.logError("Error creating new kaleo workflow file.", e);
        return Status.createErrorStatus("Error creating new kaleo workflow file.", e);
    }
}
Also used : Task(com.liferay.ide.kaleo.core.model.Task) Action(com.liferay.ide.kaleo.core.model.Action) IFile(org.eclipse.core.resources.IFile) TransitionMetadata(com.liferay.ide.kaleo.core.model.TransitionMetadata) XmlResourceStore(org.eclipse.sapphire.modeling.xml.XmlResourceStore) ScriptLanguageType(com.liferay.ide.kaleo.core.model.ScriptLanguageType) WorkflowNodeMetadata(com.liferay.ide.kaleo.core.model.WorkflowNodeMetadata) WorkspaceFileResourceStore(org.eclipse.sapphire.workspace.WorkspaceFileResourceStore) TemplateLanguageType(com.liferay.ide.kaleo.core.model.TemplateLanguageType) IContainer(org.eclipse.core.resources.IContainer) IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.sapphire.modeling.Path) IPath(org.eclipse.core.runtime.IPath) Position(com.liferay.ide.kaleo.core.model.Position) WorkflowDefinition(com.liferay.ide.kaleo.core.model.WorkflowDefinition) RootXmlResource(org.eclipse.sapphire.modeling.xml.RootXmlResource) IProject(org.eclipse.core.resources.IProject) ByteArrayInputStream(java.io.ByteArrayInputStream) State(com.liferay.ide.kaleo.core.model.State) Transition(com.liferay.ide.kaleo.core.model.Transition)

Example 12 with WorkflowDefinition

use of com.liferay.ide.kaleo.core.model.WorkflowDefinition in project liferay-ide by liferay.

the class EmailAddressValidationService method _getSchemaVersion.

private Version _getSchemaVersion() {
    Version schemaVersion = new Version(KaleoCore.DEFAULT_KALEO_VERSION);
    if (context(WorkflowDefinition.class) != null) {
        WorkflowDefinition workflowDefinition = context(WorkflowDefinition.class);
        Value<Version> version = workflowDefinition.getSchemaVersion();
        schemaVersion = version.content();
    } else if (context(NewNodeOp.class) != null) {
        NewNodeOp newNodeOp = context(NewNodeOp.class);
        ElementHandle<WorkflowDefinition> workflowDef = newNodeOp.getWorkflowDefinition();
        WorkflowDefinition workflowDefinition = workflowDef.content();
        Value<Version> version = workflowDefinition.getSchemaVersion();
        schemaVersion = version.content();
    } else if (context(NewWorkflowDefinitionOp.class) != null) {
        NewWorkflowDefinitionOp newWorkflowDenitionOp = context(NewWorkflowDefinitionOp.class);
        ReferenceValue<String, IProject> opProject = newWorkflowDenitionOp.getProject();
        IProject project = opProject.target();
        ILiferayProject liferayProj = LiferayCore.create(project);
        ILiferayPortal portal = liferayProj.adapt(ILiferayPortal.class);
        if (portal != null) {
            schemaVersion = new Version(portal.getVersion());
        }
    }
    return schemaVersion;
}
Also used : Version(org.eclipse.sapphire.Version) ILiferayProject(com.liferay.ide.core.ILiferayProject) NewNodeOp(com.liferay.ide.kaleo.core.op.NewNodeOp) ReferenceValue(org.eclipse.sapphire.ReferenceValue) Value(org.eclipse.sapphire.Value) WorkflowDefinition(com.liferay.ide.kaleo.core.model.WorkflowDefinition) ElementHandle(org.eclipse.sapphire.ElementHandle) NewWorkflowDefinitionOp(com.liferay.ide.kaleo.core.op.NewWorkflowDefinitionOp) ILiferayPortal(com.liferay.ide.core.ILiferayPortal) IProject(org.eclipse.core.resources.IProject)

Example 13 with WorkflowDefinition

use of com.liferay.ide.kaleo.core.model.WorkflowDefinition in project liferay-ide by liferay.

the class NodeNameListener method handleTypedEvent.

@Override
protected void handleTypedEvent(PropertyContentEvent event) {
    Property property = event.property();
    Element element = property.element();
    WorkflowDefinition workflow = element.adapt(WorkflowDefinition.class);
    Map<String, String> eventInfo = new HashMap<>();
    event.fillTracingInfo(eventInfo);
    String oldName = eventInfo.get("before");
    if (oldName == null) {
        oldName = _lastNodeNames.get(property);
    }
    String newName = eventInfo.get("after");
    List<Transition> allTransitions = new ArrayList<>();
    if (workflow != null) {
        ElementList<Condition> allConditions = workflow.getConditions();
        ElementList<Fork> allForks = workflow.getForks();
        ElementList<Join> allJoins = workflow.getJoins();
        ElementList<State> allStates = workflow.getStates();
        ElementList<Task> allTasks = workflow.getTasks();
        for (Condition condition : allConditions) {
            ElementList<Transition> conditionTransitions = condition.getTransitions();
            for (Transition transition : conditionTransitions) {
                allTransitions.add(transition);
            }
        }
        for (Fork fork : allForks) {
            ElementList<Transition> forkTransitions = fork.getTransitions();
            for (Transition transition : forkTransitions) {
                allTransitions.add(transition);
            }
        }
        for (Join join : allJoins) {
            ElementList<Transition> joinTransitions = join.getTransitions();
            for (Transition transition : joinTransitions) {
                allTransitions.add(transition);
            }
        }
        for (State state : allStates) {
            ElementList<Transition> stateTransitions = state.getTransitions();
            for (Transition transition : stateTransitions) {
                allTransitions.add(transition);
            }
        }
        for (Task task : allTasks) {
            ElementList<Transition> taskTransitions = task.getTransitions();
            for (Transition transition : taskTransitions) {
                allTransitions.add(transition);
            }
        }
    }
    for (Transition transition : allTransitions) {
        String targetName = transition.getTarget().toString();
        if (targetName.equals(oldName)) {
            if (newName != null) {
                transition.setTarget(newName);
                if (_lastNodeNames.containsKey(property)) {
                    _lastNodeNames.remove(property);
                }
            } else {
                _lastNodeNames.put(property, oldName);
            }
        }
    }
}
Also used : Condition(com.liferay.ide.kaleo.core.model.Condition) Task(com.liferay.ide.kaleo.core.model.Task) Fork(com.liferay.ide.kaleo.core.model.Fork) HashMap(java.util.HashMap) Element(org.eclipse.sapphire.Element) ArrayList(java.util.ArrayList) WorkflowDefinition(com.liferay.ide.kaleo.core.model.WorkflowDefinition) Join(com.liferay.ide.kaleo.core.model.Join) State(com.liferay.ide.kaleo.core.model.State) Transition(com.liferay.ide.kaleo.core.model.Transition) Property(org.eclipse.sapphire.Property)

Example 14 with WorkflowDefinition

use of com.liferay.ide.kaleo.core.model.WorkflowDefinition in project liferay-ide by liferay.

the class TransitionReferenceService method compute.

@Override
public Node compute() {
    String reference = context(Value.class).text();
    if (reference != null) {
        WorkflowDefinition workflow = context(WorkflowDefinition.class);
        List<Node> nodes = new ArrayList<>();
        if (workflow != null) {
            nodes.addAll(workflow.getTasks());
            nodes.addAll(workflow.getStates());
            nodes.addAll(workflow.getConditions());
            nodes.addAll(workflow.getForks());
            nodes.addAll(workflow.getJoins());
            Value<Version> schemaVersion = workflow.getSchemaVersion();
            Version version = schemaVersion.content();
            if (version.compareTo(new Version("6.2")) >= 0) {
                nodes.addAll(workflow.getJoinXors());
            }
            for (Node node : nodes) {
                if (reference.equals(node.getName().content())) {
                    return node;
                }
            }
        }
    }
    return null;
}
Also used : Version(org.eclipse.sapphire.Version) Node(com.liferay.ide.kaleo.core.model.Node) Value(org.eclipse.sapphire.Value) ArrayList(java.util.ArrayList) WorkflowDefinition(com.liferay.ide.kaleo.core.model.WorkflowDefinition)

Example 15 with WorkflowDefinition

use of com.liferay.ide.kaleo.core.model.WorkflowDefinition in project liferay-ide by liferay.

the class TransitionPossibleValuesService method compute.

@Override
protected void compute(Set<String> values) {
    /*
		 * if we are a task return states and tasks, if we are a state, find
		 * tasks.
		 */
    Element modelElement = context(Element.class);
    WorkflowDefinition workflow = modelElement.nearest(WorkflowDefinition.class);
    if (workflow == null) {
        workflow = modelElement.adapt(WorkflowDefinition.class);
    }
    if (workflow != null) {
        addNodeNames(values, workflow.getTasks());
        addNodeNames(values, workflow.getStates());
        addNodeNames(values, workflow.getConditions());
        addNodeNames(values, workflow.getForks());
        addNodeNames(values, workflow.getJoins());
        Value<Version> schemaVersion = workflow.getSchemaVersion();
        Version version = schemaVersion.content();
        if (version.compareTo(new Version("6.2")) >= 0) {
            addNodeNames(values, workflow.getJoinXors());
        }
    }
}
Also used : Version(org.eclipse.sapphire.Version) Element(org.eclipse.sapphire.Element) WorkflowDefinition(com.liferay.ide.kaleo.core.model.WorkflowDefinition)

Aggregations

WorkflowDefinition (com.liferay.ide.kaleo.core.model.WorkflowDefinition)16 Transition (com.liferay.ide.kaleo.core.model.Transition)6 Node (com.liferay.ide.kaleo.core.model.Node)5 WorkflowNode (com.liferay.ide.kaleo.core.model.WorkflowNode)5 CanTransition (com.liferay.ide.kaleo.core.model.CanTransition)4 Join (com.liferay.ide.kaleo.core.model.Join)4 Fork (com.liferay.ide.kaleo.core.model.Fork)3 State (com.liferay.ide.kaleo.core.model.State)3 Task (com.liferay.ide.kaleo.core.model.Task)3 NewNodeOp (com.liferay.ide.kaleo.core.op.NewNodeOp)3 Value (org.eclipse.sapphire.Value)3 Version (org.eclipse.sapphire.Version)3 Condition (com.liferay.ide.kaleo.core.model.Condition)2 JoinXor (com.liferay.ide.kaleo.core.model.JoinXor)2 AssignableOp (com.liferay.ide.kaleo.core.op.AssignableOp)2 ArrayList (java.util.ArrayList)2 IFile (org.eclipse.core.resources.IFile)2 IProject (org.eclipse.core.resources.IProject)2 Element (org.eclipse.sapphire.Element)2 RootXmlResource (org.eclipse.sapphire.modeling.xml.RootXmlResource)2