package alien4cloud.tosca.parser.impl.advanced;

import java.nio.file.Path;
import java.nio.file.Paths;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.ScalarNode;

import alien4cloud.component.model.IndexedArtifactType;
import alien4cloud.dao.IGenericSearchDAO;
import alien4cloud.dao.model.GetMultipleDataResult;
import alien4cloud.tosca.model.ArchiveRoot;
import alien4cloud.tosca.model.ImplementationArtifact;
import alien4cloud.tosca.parser.INodeParser;
import alien4cloud.tosca.parser.ParserUtils;
import alien4cloud.tosca.parser.ParsingContextExecution;
import alien4cloud.utils.MapUtil;

import com.google.common.io.Files;

@Component
public class ImplementationArtifactParser implements INodeParser<ImplementationArtifact> {
    @Resource(name = "alien-es-dao")
    private IGenericSearchDAO alienDao;

    @Override
    public ImplementationArtifact parse(Node node, ParsingContextExecution context) {
        if (node instanceof ScalarNode) {
            String artifactReference = ((ScalarNode) node).getValue();

            Path artifactPath = Paths.get(artifactReference);
            String extension = Files.getFileExtension(artifactPath.getFileName().toString());

            String type = null;
            if (extension != null) {
                ArchiveRoot archiveRoot = (ArchiveRoot) context.getRoot().getWrappedInstance();
                IndexedArtifactType indexedType = getFromArchiveRoot(archiveRoot, extension);

                if (indexedType == null) {
                    GetMultipleDataResult<IndexedArtifactType> artifactType = alienDao.find(IndexedArtifactType.class,
                            MapUtil.newHashMap(new String[] { "fileExt" }, new String[][] { new String[] { extension } }), 1);

                    if (artifactType != null && artifactType.getData() != null && artifactType.getData().length > 0) {
                        type = artifactType.getData()[0].getElementId();
                    } else {
                        type = "unknown";
                    }
                } else {
                    type = indexedType.getElementId();
                }
            }

            ImplementationArtifact artifact = new ImplementationArtifact();
            artifact.setArtifactRef(artifactReference);
            artifact.setArtifactType(type);
            return artifact;
        } else {
            ParserUtils.addTypeError(node, context.getParsingErrors(), "Artifact definition");
        }
        return null;
    }

    private IndexedArtifactType getFromArchiveRoot(ArchiveRoot archiveRoot, String extension) {
        if (archiveRoot == null || archiveRoot.getArtifactTypes() == null || extension == null) {
            return null;
        }
        for (IndexedArtifactType artifactType : archiveRoot.getArtifactTypes().values()) {
            if (artifactType.getFileExt() != null && artifactType.getFileExt().contains(extension)) {
                return artifactType;
            }
        }
        return null;
    }

    @Override
    public boolean isDeferred() {
        return true;
    }
}
@@ -661,16 +661,110 @@
 cution;%0A
+import alien4cloud.tosca.parser.ParsingError;%0Aimport alien4cloud.tosca.parser.impl.ErrorCode;%0A
 import a
@@ -2140,32 +2140,366 @@
        %7D else %7B%0A
+                        context.getParsingErrors().add(%0A                                new ParsingError(ErrorCode.UNKNOWN_IMPLEMENTATION_ARTIFACT, %22Implementation artifact%22, node.getStartMark(),%0A                                        %22No artifact type in the repository uses this extension is not.%22, node.getEndMark(), extension));%0A
                 

Manage missing artifacts type as an error.