package org.jboss.da.communication.scm.impl;

import java.io.File;
import java.util.Optional;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import org.apache.maven.scm.ScmException;
import org.jboss.da.communication.aprox.model.GAVDependencyTree;
import org.jboss.da.communication.model.GAV;
import org.jboss.da.communication.pom.PomAnalysisException;
import org.jboss.da.communication.pom.api.PomAnalyzer;
import org.jboss.da.communication.pom.model.MavenProject;
import org.jboss.da.communication.scm.api.SCMConnector;
import org.jboss.da.scm.api.SCM;
import org.jboss.da.scm.api.SCMType;

/**
 *
 * @author Honza Brázdil <jbrazdil@redhat.com>
 */
@ApplicationScoped
public class SCMConnectorImpl implements SCMConnector {

    @Inject
    private SCM scmManager;

    @Inject
    private PomAnalyzer pomAnalyzer;

    @Override
    public GAVDependencyTree getDependencyTreeOfRevision(String scmUrl, String revision, GAV gav)
            throws ScmException, PomAnalysisException {
        try {
            // git clone
            // TODO: hardcoded to git right now
            File tempDir = Files.createTempDirectory("cloned_repo").toFile();

            try {
                scmManager.cloneRepository(SCMType.GIT, scmUrl, revision, tempDir.toString());
                GAVDependencyTree gavDependencyTree = pomAnalyzer.readRelationships(tempDir, gav);
                return gavDependencyTree;
            } finally {
                // cleanup
                FileUtils.deleteDirectory(tempDir);
            }
        } catch (IOException e) {
            throw new ScmException("Could not create temp directory for cloning the repository", e);
        }
    }

    @Override
    public GAVDependencyTree getDependencyTreeOfRevision(String scmUrl, String revision,
            String pomPath) throws ScmException, PomAnalysisException {
        // git clone
        // TODO: hardcoded to git right now
        File tempDir = scmManager.cloneRepository(SCMType.GIT, scmUrl, revision);

        GAVDependencyTree gavDependencyTree = pomAnalyzer.readRelationships(tempDir, new File(
                tempDir, pomPath));

        return gavDependencyTree;
    }

    @Override
    public Optional<MavenProject> getPom(String scmUrl, String revision, String pomPath)
            throws ScmException {
        if (!pomPath.endsWith("pom.xml")) {
            pomPath += "/pom.xml";
        }

        // git clone
        // TODO: hardcoded to git right now
        File tempDir = scmManager.cloneRepository(SCMType.GIT, scmUrl, revision);

        return pomAnalyzer.readPom(new File(tempDir, pomPath));
    }

}

@@ -1011,34 +1011,16 @@
 ption %7B%0A
-        try %7B%0A    
         
@@ -1028,28 +1028,24 @@
 / git clone%0A
-    
         // T
@@ -1072,28 +1072,24 @@
 t right now%0A
-    
         File
@@ -1102,94 +1102,8 @@
 ir =
- Files.createTempDirectory(%22cloned_repo%22).toFile();%0A%0A            try %7B%0A               
  scm
@@ -1159,39 +1159,12 @@
 sion
-, tempDir.toString());%0A        
+);%0A%0A
     
@@ -1250,24 +1250,16 @@
 , gav);%0A
-        
         
@@ -1288,270 +1288,8 @@
 ee;%0A
-            %7D finally %7B%0A                // cleanup%0A                FileUtils.deleteDirectory(tempDir);%0A            %7D%0A        %7D catch (IOException e) %7B%0A            throw new ScmException(%22Could not create temp directory for cloning the repository%22, e);%0A        %7D%0A
     

fix compile problems with 2 PR clashing together