package com.divae.ageto.hybris.utils.dependencies;

import java.util.List;

import org.apache.maven.model.Dependency;
import org.springframework.web.client.RestTemplate;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
 * @author Klaus Hauschild
 */
public class MavenCentralDependencyResolver extends AbstractDependencyResolver {

    @Override
    protected Dependency resolve(final String artifactId, final String version) {
        final RestTemplate restTemplate = new RestTemplate();
        final Result result = restTemplate.getForObject(
                "http://search.maven.org/solrsearch/select?q=a:\"{0}\" AND v:\"{1}\" AND p:\"jar\"&rows=1&wt=json", Result.class,
                artifactId, version);
        final List<Result.Response.Doc> docs = result.getResponse().getDocs();
        if (docs.isEmpty()) {
            throw new UnresolvableDependencyException(artifactId, version);
        }
        final Result.Response.Doc doc = docs.get(0);
        final Dependency dependency = new Dependency();
        dependency.setGroupId(doc.getG());
        dependency.setArtifactId(doc.getA());
        dependency.setVersion(doc.getV());
        return dependency;
    }

    @JsonIgnoreProperties(ignoreUnknown = true)
    private static class Result {

        private Response response;

        Response getResponse() {
            return response;
        }

        @JsonIgnoreProperties(ignoreUnknown = true)
        static class Response {

            private List<Doc> docs;

            List<Doc> getDocs() {
                return docs;
            }

            @JsonIgnoreProperties(ignoreUnknown = true)
            static class Doc {

                private String g;
                private String a;
                private String v;

                String getG() {
                    return g;
                }

                String getA() {
                    return a;
                }

                String getV() {
                    return v;
                }

            }

        }

    }

}

@@ -164,16 +164,68 @@
 plate;%0A%0A
+import com.fasterxml.jackson.annotation.JsonGetter;%0A
 import c
@@ -1340,24 +1340,16 @@
         
-private 
 Response
@@ -1360,16 +1360,36 @@
 ponse;%0A%0A
+        @JsonGetter%0A
         
@@ -1550,24 +1550,16 @@
         
-private 
 List%3CDoc
@@ -1567,16 +1567,40 @@
  docs;%0A%0A
+            @JsonGetter%0A
         
@@ -1860,16 +1860,44 @@
 ing v;%0A%0A
+                @JsonGetter%0A
         
@@ -1961,32 +1961,60 @@
              %7D%0A%0A
+                @JsonGetter%0A
                 
@@ -2070,32 +2070,60 @@
              %7D%0A%0A
+                @JsonGetter%0A
                 

Resolve external dependencies #26