Search in sources :

Example 1 with State

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

the class NewNodeOpWizard method performPostDiagramNodeAdded.

protected void performPostDiagramNodeAdded() {
    NewNodeOp newNodeOp = element().nearest(NewNodeOp.class);
    CanTransition newNode = newNodeOp.adapt(CanTransition.class);
    CanTransition node = _diagramNodePart.getLocalModelElement().nearest(CanTransition.class);
    WorkflowDefinition workflowDefinition = node.nearest(WorkflowDefinition.class);
    if ((newNode != null) && (node != null)) {
        Value<String> newNodeName = newNode.getName();
        node.setName(newNodeName.content());
        if (newNodeOp.getConnectedNodes().size() > 0) {
            for (Node diagramNode : newNodeOp.getConnectedNodes()) {
                Value<String> nodeName = diagramNode.getName();
                String diagramNodeName = nodeName.content(true);
                if (diagramNode instanceof ConditionForOp) {
                    ElementList<Condition> conditions = workflowDefinition.getConditions();
                    Condition insertCondition = conditions.insert();
                    insertCondition.setName(diagramNodeName);
                } else if (diagramNode instanceof ForkForOp) {
                    ElementList<Fork> forks = workflowDefinition.getForks();
                    Fork insertFork = forks.insert();
                    insertFork.setName(diagramNodeName);
                } else if (diagramNode instanceof JoinForOp) {
                    ElementList<Join> joins = workflowDefinition.getJoins();
                    Join insertJoin = joins.insert();
                    insertJoin.setName(diagramNodeName);
                } else if (diagramNode instanceof JoinXorForOp) {
                    ElementList<JoinXor> joinXors = workflowDefinition.getJoinXors();
                    JoinXor insertJoinXor = joinXors.insert();
                    insertJoinXor.setName(diagramNodeName);
                } else if (diagramNode instanceof StateForOp) {
                    ElementList<State> states = workflowDefinition.getStates();
                    State insertState = states.insert();
                    insertState.setName(diagramNodeName);
                } else if (diagramNode instanceof TaskForOp) {
                    ElementList<Task> tasks = workflowDefinition.getTasks();
                    Task insertTask = tasks.insert();
                    insertTask.setName(diagramNodeName);
                }
                if (diagramNode instanceof ChooseDiagramNode || node instanceof Condition || node instanceof Fork || node instanceof State || node instanceof Task) {
                    Transition newTransition = node.getTransitions().insert();
                    newTransition.setName(diagramNodeName);
                    newTransition.setTarget(diagramNodeName);
                } else {
                    CanTransition canTransition = diagramNode.nearest(CanTransition.class);
                    ElementList<Transition> transition = canTransition.getTransitions();
                    Transition joinTransition = transition.insert();
                    joinTransition.setName(node.getName().content());
                    joinTransition.setTarget(node.getName().content());
                }
            }
        }
    }
    _actionHandler.postDiagramNodePartAdded(element(), newNode, node);
}
Also used : Task(com.liferay.ide.kaleo.core.model.Task) Fork(com.liferay.ide.kaleo.core.model.Fork) JoinXor(com.liferay.ide.kaleo.core.model.JoinXor) JoinXorForOp(com.liferay.ide.kaleo.core.op.NewNodeOp.JoinXorForOp) ChooseDiagramNode(com.liferay.ide.kaleo.core.op.ChooseDiagramNode) Node(com.liferay.ide.kaleo.core.model.Node) StateForOp(com.liferay.ide.kaleo.core.op.NewNodeOp.StateForOp) CanTransition(com.liferay.ide.kaleo.core.model.CanTransition) ElementList(org.eclipse.sapphire.ElementList) ForkForOp(com.liferay.ide.kaleo.core.op.NewNodeOp.ForkForOp) ConditionForOp(com.liferay.ide.kaleo.core.op.NewNodeOp.ConditionForOp) Condition(com.liferay.ide.kaleo.core.model.Condition) WorkflowDefinition(com.liferay.ide.kaleo.core.model.WorkflowDefinition) Join(com.liferay.ide.kaleo.core.model.Join) ChooseDiagramNode(com.liferay.ide.kaleo.core.op.ChooseDiagramNode) State(com.liferay.ide.kaleo.core.model.State) NewNodeOp(com.liferay.ide.kaleo.core.op.NewNodeOp) CanTransition(com.liferay.ide.kaleo.core.model.CanTransition) Transition(com.liferay.ide.kaleo.core.model.Transition) JoinForOp(com.liferay.ide.kaleo.core.op.NewNodeOp.JoinForOp) TaskForOp(com.liferay.ide.kaleo.core.op.NewNodeOp.TaskForOp)

Example 2 with State

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

the class StateNodeAddActionHandler method postDiagramNodePartAdded.

@Override
public void postDiagramNodePartAdded(NewNodeOp op, CanTransition newNodeFromWizard, CanTransition newNode) {
    NewStateNodeOp newStateNodeOp = op.nearest(NewStateNodeOp.class);
    NewStateNode newStateNode = newNodeFromWizard.nearest(NewStateNode.class);
    State state = newNode.nearest(State.class);
    if ((newStateNode != null) && (state != null)) {
        state.setName(newStateNode.getName().content());
        NewStateType newStateType = newStateNodeOp.getType().content();
        if (newStateType.equals(NewStateType.START)) {
            state.setInitial(true);
        } else if (newStateType.equals(NewStateType.END)) {
            state.setEnd(true);
        }
        String workflowStatus = newStateNodeOp.getWorkflowStatus().content(false);
        if (!empty(workflowStatus)) {
            Action newAction = state.getActions().insert();
            newAction.setName(state.getName().content());
            newAction.setScriptLanguage(KaleoModelUtil.getDefaultValue(state, KaleoCore.DEFAULT_SCRIPT_LANGUAGE_KEY, ScriptLanguageType.GROOVY));
            newAction.setExecutionType(Executable.DEFAULT_EXECUTION_TYPE);
            IKaleoEditorHelper editorHelper = KaleoUI.getKaleoEditorHelper(newAction.getScriptLanguage().text());
            if (editorHelper != null) {
                try {
                    File statusUpdatesFolder = new File(FileLocator.toFileURL(Platform.getBundle(editorHelper.getContributorName()).getEntry("palette/Status Updates")).getFile());
                    File statusSnippet = new File(statusUpdatesFolder, workflowStatus + "." + editorHelper.getFileExtension());
                    if (FileUtil.exists(statusSnippet)) {
                        newAction.setScript(FileUtil.readContents(statusSnippet, true));
                    }
                } catch (Exception e) {
                }
            }
        }
        if (!newStateType.equals(NewStateType.END) && (newStateNodeOp.getExitTransitionName().content() != null)) {
            Transition newTransition = state.getTransitions().insert();
            newTransition.setTarget(newStateNodeOp.getExitTransitionName().content());
            newTransition.setName(newStateNodeOp.getExitTransitionName().content());
        }
    }
}
Also used : Action(com.liferay.ide.kaleo.core.model.Action) NewStateNodeOp(com.liferay.ide.kaleo.core.op.NewStateNodeOp) State(com.liferay.ide.kaleo.core.model.State) IKaleoEditorHelper(com.liferay.ide.kaleo.ui.IKaleoEditorHelper) NewStateType(com.liferay.ide.kaleo.core.op.NewStateType) CanTransition(com.liferay.ide.kaleo.core.model.CanTransition) Transition(com.liferay.ide.kaleo.core.model.Transition) NewStateNode(com.liferay.ide.kaleo.core.op.NewStateNode) File(java.io.File)

Example 3 with State

use of com.liferay.ide.kaleo.core.model.State 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 4 with State

use of com.liferay.ide.kaleo.core.model.State 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)

Aggregations

State (com.liferay.ide.kaleo.core.model.State)4 Transition (com.liferay.ide.kaleo.core.model.Transition)4 Task (com.liferay.ide.kaleo.core.model.Task)3 WorkflowDefinition (com.liferay.ide.kaleo.core.model.WorkflowDefinition)3 Action (com.liferay.ide.kaleo.core.model.Action)2 CanTransition (com.liferay.ide.kaleo.core.model.CanTransition)2 Condition (com.liferay.ide.kaleo.core.model.Condition)2 Fork (com.liferay.ide.kaleo.core.model.Fork)2 Join (com.liferay.ide.kaleo.core.model.Join)2 JoinXor (com.liferay.ide.kaleo.core.model.JoinXor)1 Node (com.liferay.ide.kaleo.core.model.Node)1 Position (com.liferay.ide.kaleo.core.model.Position)1 ScriptLanguageType (com.liferay.ide.kaleo.core.model.ScriptLanguageType)1 TemplateLanguageType (com.liferay.ide.kaleo.core.model.TemplateLanguageType)1 TransitionMetadata (com.liferay.ide.kaleo.core.model.TransitionMetadata)1 WorkflowNodeMetadata (com.liferay.ide.kaleo.core.model.WorkflowNodeMetadata)1 ChooseDiagramNode (com.liferay.ide.kaleo.core.op.ChooseDiagramNode)1 NewNodeOp (com.liferay.ide.kaleo.core.op.NewNodeOp)1 ConditionForOp (com.liferay.ide.kaleo.core.op.NewNodeOp.ConditionForOp)1 ForkForOp (com.liferay.ide.kaleo.core.op.NewNodeOp.ForkForOp)1