package com.weblyzard.api.client;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.weblyzard.api.model.document.Document;

/** @author philipp.kuntschik@htwchur.ch */
public class RecognyzeClient extends BasicClient {

    private static final String TEMPLATE_PROFILE_NAME = "profileName";

    private static final String ADD_PROFILE_SERVICE_URL =
            "/recognize/rest/load_profile/{" + TEMPLATE_PROFILE_NAME + "}";
    private static final String SEARCH_DOCUMENT_SERVICE_URL = "/recognize/rest/search_document";
    private static final String SEARCH_DOCUMENTS_SERVICE_URL = "/recognize/rest/search_documents";
    private static final String STATUS_SERVICE_URL = "/recognize/rest/status";

    private static final String PARAM_PROFILE_NAME = "profileName";
    private static final String PARAM_LIMIT = "limit";

    public RecognyzeClient() {
        super();
    }

    public RecognyzeClient(String weblyzardUrl) {
        super(weblyzardUrl);
    }

    public RecognyzeClient(String weblyzardUrl, String username, String password) {
        super(weblyzardUrl, username, password);
    }

    public boolean loadProfile(String profileName) throws WebApplicationException {

        Response response = super.getTarget(ADD_PROFILE_SERVICE_URL)
                .resolveTemplate(TEMPLATE_PROFILE_NAME, profileName)
                .request(MediaType.APPLICATION_JSON_TYPE).get();

        super.checkResponseStatus(response);
        boolean result = response.readEntity(Boolean.class);
        response.close();

        return result;
    }

    public Document searchDocument(String profileName, Document data)
            throws WebApplicationException {

        return this.searchDocument(profileName, data, 0);
    }

    public Document searchDocument(String profileName, Document data, int limit)
            throws WebApplicationException {

        Response response = super.getTarget(SEARCH_DOCUMENT_SERVICE_URL)
                .queryParam(PARAM_PROFILE_NAME, profileName).queryParam(PARAM_LIMIT, limit)
                .request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(data));

        super.checkResponseStatus(response);
        Document result = response.readEntity(Document.class);
        response.close();

        return result;
    }

    public List<Document> searchDocuments(String profileName, Set<Document> data)
            throws WebApplicationException {

        return this.searchDocuments(profileName, data, 0);
    }

    public List<Document> searchDocuments(String profileName, Set<Document> data, int limit)
            throws WebApplicationException {

        Response response = super.getTarget(SEARCH_DOCUMENTS_SERVICE_URL)
                .queryParam(PARAM_PROFILE_NAME, profileName).queryParam(PARAM_LIMIT, limit)
                .request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(data));

        super.checkResponseStatus(response);
        List<Document> result = response.readEntity(new GenericType<List<Document>>() {});
        response.close();

        return result == null ? Collections.emptyList() : result;
    }

    public Map<String, Object> status() throws WebApplicationException {

        Response response =
                super.getTarget(STATUS_SERVICE_URL).request(MediaType.APPLICATION_JSON_TYPE).get();

        super.checkResponseStatus(response);
        Map<String, Object> result = response.readEntity(new GenericType<Map<String, Object>>() {});
        response.close();

        return result == null ? Collections.emptyMap() : result;
    }
}

@@ -344,17 +344,124 @@
 del.
-document.
+annotation.Annotation;%0Aimport com.weblyzard.api.model.document.Document;%0Aimport com.weblyzard.api.model.document.Xml
 Docu
@@ -965,16 +965,131 @@
 ments%22;%0A
+    private static final String SEARCH_XMLDOCUMENT_SERVICE_URL =%0A            %22/recognize/rest/search_xmldocument%22;%0A
     priv
@@ -1891,32 +1891,32 @@
 atus(response);%0A
-
         boolean 
@@ -2009,32 +2009,836 @@
  result;%0A    %7D%0A%0A
+    public List%3CAnnotation%3E searchXmlDocument(String profileName, XmlDocument document)%0A            throws WebApplicationException %7B%0A        return this.searchXmlDocument(profileName, document, 0);%0A    %7D%0A%0A%0A    public List%3CAnnotation%3E searchXmlDocument(String profileName, XmlDocument document, int limit)%0A            throws WebApplicationException %7B%0A%0A        Response response = super.getTarget(SEARCH_XMLDOCUMENT_SERVICE_URL)%0A                .queryParam(PARAM_PROFILE_NAME, profileName).queryParam(PARAM_LIMIT, limit)%0A                .request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(document));%0A%0A        super.checkResponseStatus(response);%0A        List%3CAnnotation%3E result = response.readEntity(new GenericType%3CList%3CAnnotation%3E%3E() %7B%7D);%0A        response.close();%0A%0A        return result;%0A    %7D%0A%0A
     public Docum

add new Webservice methods