package com.galactichorse;

import com.google.api.server.spi.config.*;
import com.google.appengine.api.datastore.*;

import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

@Api(name = "search")
public class Search {
    private static final String ENTITY_KIND_URL_TAGS = "url_tags";

    @ApiMethod(name = "put", httpMethod = ApiMethod.HttpMethod.POST)
    public void put(UrlTagsBean urlTags) throws Exception {
        URL url = new URL(urlTags.getUrl());
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        Entity entity = new Entity(ENTITY_KIND_URL_TAGS, urlTags.getUrl());
        for (Map.Entry<String, Object> tag: urlTags.getTags().entrySet()) {
            entity.setProperty(tag.getKey(), tag.getValue());
        }
        datastore.put(entity);
    }

    @ApiMethod(name = "search", httpMethod = ApiMethod.HttpMethod.POST)
    public Collection<UrlTagsBean> searchUrls(UrlsBean urls) {
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        Collection<Key> keys = new ArrayList<Key>();
        Collection<UrlTagsBean> urlTagsCollection = new ArrayList<UrlTagsBean>();
        for (String url : urls.getUrls()) {
            keys.add(KeyFactory.createKey(ENTITY_KIND_URL_TAGS, url));
        }
        for (Map.Entry<Key, Entity> entry: datastore.get(keys).entrySet()) {
            UrlTagsBean urlTags = new UrlTagsBean();
            urlTags.setUrl(entry.getKey().getName());
            urlTags.setTags(entry.getValue().getProperties());
            urlTagsCollection.add(urlTags);
        }
        return urlTagsCollection;
    }
}

class UrlTagsBean {
    private String url;
    private Map<String, Object> tags;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Map<String, Object> getTags() {
        return tags;
    }

    public void setTags(Map<String, Object> tags) {
        this.tags = tags;
    }
}

class UrlsBean {
    private Collection<String> urls;

    public Collection<String> getUrls() {
        return urls;
    }

    public void setUrls(Collection<String> urls) {
        this.urls = urls;
    }
}

@@ -109,16 +109,395 @@
 tore.*;%0A
+import com.hp.hpl.jena.rdf.model.InfModel;%0Aimport com.hp.hpl.jena.rdf.model.Model;%0Aimport com.hp.hpl.jena.rdf.model.ModelFactory;%0Aimport com.hp.hpl.jena.reasoner.Reasoner;%0Aimport com.hp.hpl.jena.reasoner.ReasonerRegistry;%0Aimport com.hp.hpl.jena.reasoner.ValidityReport;%0Aimport org.apache.jena.riot.Lang;%0A%0Aimport java.io.ByteArrayInputStream;%0Aimport java.io.ByteArrayOutputStream;
 %0Aimport 
@@ -700,16 +700,87 @@
 l_tags%22;
+%0A    private static final String ENTITY_KIND_URL_JSONLD = %22url_jsonld%22;
 %0A%0A    @A
@@ -1149,16 +1149,17 @@
 ect%3E tag
+ 
 : urlTag
@@ -1290,24 +1290,1149 @@
 ty);%0A    %7D%0A%0A
+    @ApiMethod(name = %22put_jsonld%22, httpMethod = ApiMethod.HttpMethod.POST)%0A    public void putJsonld(UrlJsonldBean urlJsonld) throws Exception %7B%0A        URL url = new URL(urlJsonld.getUrl());%0A        Model ontology = Ontology.getOntologyModel();%0A        Model model = ModelFactory.createDefaultModel();%0A        model.read(new ByteArrayInputStream(urlJsonld.getJsonld().getBytes()), null, Lang.JSONLD.getLabel());%0A        Reasoner reasoner = ReasonerRegistry.getOWLReasoner();%0A        reasoner = reasoner.bindSchema(ontology);%0A        InfModel infmodel = ModelFactory.createInfModel(reasoner, model);%0A        ValidityReport validityReport = infmodel.validate();%0A        if (!validityReport.isValid()) %7B%0A            throw new Exception();%0A        %7D%0A        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();%0A        Entity entity = new Entity(ENTITY_KIND_URL_JSONLD, urlJsonld.getUrl());%0A        ByteArrayOutputStream baos = new ByteArrayOutputStream();%0A        infmodel.write(baos, Lang.JSONLD.getLabel());%0A        entity.setProperty(%22jsonld%22, baos.toString());%0A        datastore.put(entity);%0A    %7D%0A%0A
     @ApiMeth
@@ -2939,16 +2939,17 @@
 y%3E entry
+ 
 : datast
@@ -3810,16 +3810,358 @@
 = urls;%0A    %7D%0A%7D%0A
+%0Aclass UrlJsonldBean %7B%0A    private String url;%0A    private String jsonld;%0A%0A    public String getUrl() %7B%0A        return url;%0A    %7D%0A%0A    public void setUrl(String url) %7B%0A        this.url = url;%0A    %7D%0A%0A    public String getJsonld() %7B%0A        return jsonld;%0A    %7D%0A%0A    public void setJsonld(String jsonld) %7B%0A        this.jsonld = jsonld;%0A    %7D%0A%7D%0A

add method to check/put jsonld