package org.alien4cloud.tosca.editor.processors.workflow;

import javax.inject.Inject;

import org.alien4cloud.tosca.editor.operations.workflow.AbstractWorkflowOperation;
import org.alien4cloud.tosca.editor.operations.workflow.ReinitializeWorkflowOperation;
import org.alien4cloud.tosca.editor.processors.IEditorOperationProcessor;
import org.alien4cloud.tosca.model.Csar;
import org.alien4cloud.tosca.model.templates.Topology;
import org.alien4cloud.tosca.model.workflow.Workflow;

import alien4cloud.paas.wf.WorkflowsBuilderService;
import alien4cloud.paas.wf.exception.BadWorkflowOperationException;

/**
 * Abstract processor to get a workflow.
 */
public abstract class AbstractWorkflowProcessor<T extends AbstractWorkflowOperation> implements IEditorOperationProcessor<T> {

    @Inject
    protected WorkflowsBuilderService workflowBuilderService;

    @Override
    public void process(Csar csar, Topology topology, T operation) {
        Workflow workflow = workflowBuilderService.getWorkflow(operation.getWorkflowName(), topology);
        processWorkflowOperation(csar, topology, operation, workflow);
        if (!operation.getClass().getSimpleName().toString().equals(ReinitializeWorkflowOperation.class.getSimpleName().toString())) {
            workflow.setHasCustomModifications(true);
        }
    }

    /**
     * Ensure we are <em><b>NOT</b></em> working with a Standard workflow.<br/>
     * This might be useful, for example, for operations like renaming or removing that are allowed on non standard workflow
     * 
     * @param workflow
     */
    protected void ensureNotStandard(Workflow workflow, String message) {
        if (workflow.isStandard()) {
            throw new BadWorkflowOperationException(message);
        }
    }

    /**
     * Ensure we are working with a Standard workflow.<br/>
     * This might be usefull, for example, for operations like reinitializing that are allowed only on standard
     * workflow
     * 
     * @param workflow
     * @param message Message for the exception to throw
     */
    protected void ensureStandard(Workflow workflow, String message) {
        if (!workflow.isStandard()) {
            throw new BadWorkflowOperationException(message);
        }
    }

    protected abstract void processWorkflowOperation(Csar csar, Topology topology, T operation, Workflow workflow);
}

@@ -81,16 +81,99 @@
 nject;%0A%0A
+import alien4cloud.paas.wf.TopologyContext;%0Aimport com.google.common.collect.Sets;%0A
 import o
@@ -1340,47 +1340,255 @@
     
-workflow.setHasCustomModifications(true
+TopologyContext tc = workflowBuilderService.buildTopologyContext(topology,csar);%0A            workflow.setHasCustomModifications(true);%0A            workflowBuilderService.postProcessTopologyWorkflows(tc, Sets.newHashSet(operation.getWorkflowName())
 );%0A 

Update of unprocessedWorkflows after a wf edition