package net.qldarch.web.service;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;
import java.util.Collection;
import java.util.Set;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import static com.google.common.base.Functions.toStringFunction;
import static com.google.common.base.Optional.fromNullable;
import static com.google.common.collect.Collections2.transform;
import static com.google.common.collect.Sets.newHashSet;

@Path("/fileSummary")
public class FileSummaryResource {
    public static Logger logger = LoggerFactory.getLogger(FileSummaryResource.class);
    public static final String XSD_BOOLEAN = "http://www.w3.org/2001/XMLSchema#boolean";

    public static String formatQuery(Collection<String> ids) {
        StringBuilder builder = new StringBuilder(
                "PREFIX :<http://qldarch.net/ns/rdf/2012-06/terms#> " +
                "select ?s ?p ?o from <http://qldarch.net/ns/omeka-export/2013-02-06> where {" +
                "  ?s a :DigitalFile ." +
                "  ?s ?p ?o ." +
                "  } BINDINGS ?s { (<");

        String query = Joiner.on(">) (<").appendTo(builder, transform(ids, toStringFunction())).append(">) }").toString();
        logger.debug("FileSummaryResource performing SPARQL query: {}", query);
        
        return query;
    }

    @GET
    @Produces("application/json")
    public String performGet(
            @QueryParam("PREFIX") String prefix,
            @QueryParam("ID") Set<String> idParam,
            @DefaultValue("") @QueryParam("IDLIST") String idlist) {
        logger.debug("Querying PREFIX: {}, ID: {}, IDLIST: {}", prefix, idParam, idlist);
        Set<String> ids = newHashSet(idParam);
        Iterables.addAll(ids, Splitter.on(',').trimResults().omitEmptyStrings().split(idlist));
        logger.debug("Raw ids: {}", ids);
        return new SparqlToJsonString().performQuery(formatQuery(resolvePrefix(prefix, ids)));
    }

    private static Collection<String> resolvePrefix(String prefixString, Collection<String> ids) {
        Optional<String> prefix = fromNullable(prefixString);

        return prefix.isPresent() ? transform(ids, resolver(prefix.get())) : ids;
    }

    private static Function<String,String> resolver(final String prefix) {
        return new Function<String,String>() {
            public String apply(String s) {
                if (s.indexOf(':') == -1) {
                    return prefix + s;
                } else {
                    return s;
                }
            }
        };
    }
}

@@ -765,18 +765,11 @@
 file
-Summary
 %22)%0A
+
 publ
@@ -1189,75 +1189,434 @@
 ect 
-?s ?p ?o from %3Chttp://qldarch.net/ns/omeka-export/2013-02-06%3E where
+distinct ?s ?p ?o where %7B%22 + %0A                %22  %7B%22 + %0A                %22    graph %3Chttp://qldarch.net/rdf/2013-09/catalog%3E %7B%22 + %0A                %22      ?u %3Chttp://qldarch.net/ns/rdf/2013-09/catalog#hasFileGraph%3E ?g.%22 + %0A                %22    %7D .%22 + %0A                %22  %7D UNION %7B%22 + %0A                %22    BIND ( %3Chttp://qldarch.net/ns/omeka-export/2013-02-06%3E AS ?g ) .%22 + %0A                %22  %7D .%22 + %0A                %22  graph ?g
  %7B%22 
@@ -1630,24 +1630,26 @@
            %22
+  
   ?s a :Digi
@@ -1678,16 +1678,18 @@
        %22
+  
   ?s ?p 
@@ -1716,16 +1716,41 @@
     %22  %7D
+ . %22 +%0A                %22%7D
  BINDING
@@ -2000,24 +2000,24 @@
 ery;%0A    %7D%0A%0A
-
     @GET%0A   
@@ -2013,16 +2013,37 @@
    @GET%0A
+    @Path(%22summary%22)%0A
     @Pro

Add user file graphs to file-summary query