package alien4cloud.topology;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import alien4cloud.common.AbtractVersionService;
import alien4cloud.csar.services.CsarService;
import alien4cloud.dao.model.GetMultipleDataResult;
import alien4cloud.exception.DeleteReferencedObjectException;
import alien4cloud.model.components.Csar;
import alien4cloud.model.templates.TopologyTemplate;
import alien4cloud.model.templates.TopologyTemplateVersion;
import alien4cloud.model.topology.Topology;

@Service
public class TopologyTemplateVersionService extends AbtractVersionService<TopologyTemplateVersion> {

    @Resource
    private TopologyServiceCore topologyServiceCore;
    @Resource
    private CsarService csarService;

    @PostConstruct
    public void init() {
        // this code to ensure backward compatibility when introducing versions for topology templates (1.0.0-SM27)
        // search all topology templates
        GetMultipleDataResult<TopologyTemplate> result = alienDAO.find(TopologyTemplate.class, null, Integer.MAX_VALUE);
        for (TopologyTemplate tt : result.getData()) {
            String ttid = tt.getId();
            if (getByDelegateId(ttid).length == 0) {
                // no versions found for this topology template, we create one per default
                this.createVersion(ttid, tt.getTopologyId(), null);
            }
        }
    }

    @Override
    public void delete(String versionId) {
        TopologyTemplateVersion ttv = getOrFail(versionId);
        Topology topology = topologyServiceCore.getTopology(ttv.getTopologyId());
        Csar csar = null;
        if (topology.getSubstitutionMapping() != null && topology.getSubstitutionMapping().getSubstitutionType() != null) {
            // this topology expose substitution, we have to delete the related CSAR and type
            csar = csarService.getTopologySubstitutionCsar(topology.getId());
            // will fail if the stuff is used in a topology
            if (csar != null && csarService.isDependency(csar.getName(), csar.getVersion())) {
                throw new DeleteReferencedObjectException("This topology template version can not be deleted since it's already used.");
            }
        }
        super.delete(versionId);
        if (csar != null) {
            csarService.deleteCsar(csar.getId());
        }
    }

    @Override
    protected TopologyTemplateVersion buildVersionImplem() {
        return new TopologyTemplateVersion();
    }

    @Override
    protected TopologyTemplateVersion[] buildVersionImplemArray(int length) {
        return new TopologyTemplateVersion[length];
    }

    @Override
    protected Class<TopologyTemplateVersion> getVersionImplemClass() {
        return TopologyTemplateVersion.class;
    }

    @Override
    protected Class<?> getDelegateClass() {
        return TopologyTemplate.class;
    }

    @Override
    protected String getDelegatePropertyName() {
        return "topologyTemplateId";
    }

}

@@ -28,47 +28,8 @@
 y;%0A%0A
-import javax.annotation.PostConstruct;%0A
 impo
@@ -54,24 +54,24 @@
 n.Resource;%0A
+
 %0Aimport org.
@@ -206,60 +206,8 @@
 ce;%0A
-import alien4cloud.dao.model.GetMultipleDataResult;%0A
 impo
@@ -648,32 +648,32 @@
 ;%0A    @Resource%0A
+
     private Csar
@@ -698,666 +698,8 @@
 e;%0A%0A
-    @PostConstruct%0A    public void init() %7B%0A        // this code to ensure backward compatibility when introducing versions for topology templates (1.0.0-SM27)%0A        // search all topology templates%0A        GetMultipleDataResult%3CTopologyTemplate%3E result = alienDAO.find(TopologyTemplate.class, null, Integer.MAX_VALUE);%0A        for (TopologyTemplate tt : result.getData()) %7B%0A            String ttid = tt.getId();%0A            if (getByDelegateId(ttid).length == 0) %7B%0A                // no versions found for this topology template, we create one per default%0A                this.createVersion(ttid, tt.getTopologyId(), null);%0A            %7D%0A        %7D%0A    %7D%0A%0A
     

Remove deprecated code that ensures backward compatibility with 1.0.0-SM27.