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

import java.util.Map;

import javax.annotation.Resource;

import org.alien4cloud.tosca.editor.EditionContextManager;
import org.alien4cloud.tosca.editor.operations.nodetemplate.ResetNodeDeploymentArtifactOperation;
import org.alien4cloud.tosca.editor.processors.IEditorOperationProcessor;
import org.alien4cloud.tosca.model.definitions.DeploymentArtifact;
import org.alien4cloud.tosca.model.templates.NodeTemplate;
import org.alien4cloud.tosca.model.templates.Topology;
import org.alien4cloud.tosca.model.types.NodeType;
import org.springframework.stereotype.Component;

import alien4cloud.exception.NotFoundException;
import alien4cloud.topology.TopologyServiceCore;

/**
 * Process an {@link ResetNodeDeploymentArtifactOperation}.
 */
@Component
public class ResetNodeDeploymentArtifactProcessor implements IEditorOperationProcessor<ResetNodeDeploymentArtifactOperation> {
    @Resource
    private TopologyServiceCore topologyServiceCore;

    @Override
    public void process(ResetNodeDeploymentArtifactOperation operation) {
        Topology topology = EditionContextManager.getTopology();

        // Get the node template's artifacts to reset
        Map<String, NodeTemplate> nodeTemplates = TopologyServiceCore.getNodeTemplates(topology);
        NodeTemplate nodeTemplate = TopologyServiceCore.getNodeTemplate(topology.getId(), operation.getNodeName(), nodeTemplates);
        DeploymentArtifact currentArtifact = nodeTemplate.getArtifacts() == null ? null : nodeTemplate.getArtifacts().get(operation.getArtifactName());
        if (currentArtifact == null) {
            throw new NotFoundException("Artifact with key [" + operation.getArtifactName() + "] do not exist in node template");
        }

        // Get the node type's artifact
        Map<String, NodeType> nodeTypes = topologyServiceCore.getIndexedNodeTypesFromTopology(topology, false, false, true);
        NodeType nodeType = nodeTypes.get(nodeTemplate.getType());
        DeploymentArtifact artifactFromNodeType = nodeType.getArtifacts() == null ? null : nodeType.getArtifacts().get(operation.getArtifactName());
        if (artifactFromNodeType == null) {
            throw new NotFoundException("Artifact with key [" + operation.getArtifactName() + "] do not exist in node type");
        }

        currentArtifact.setArtifactRef(artifactFromNodeType.getArtifactRef());
        currentArtifact.setArtifactName(null);
    }
}
@@ -2449,20 +2449,493 @@
 actName(
-null
+artifactFromNodeType.getArtifactName());%0A        currentArtifact.setArtifactType(artifactFromNodeType.getArtifactType());%0A        currentArtifact.setArtifactRepository(artifactFromNodeType.getArtifactRepository());%0A        currentArtifact.setRepositoryName(artifactFromNodeType.getRepositoryName());%0A        currentArtifact.setRepositoryURL(artifactFromNodeType.getRepositoryURL());%0A        currentArtifact.setRepositoryCredential(artifactFromNodeType.getRepositoryCredential()
 );%0A    %7D

Reset node deployment artifact should reset repository informations