package applications;

import java.util.Iterator;
import java.util.Scanner;

import com.ontotext.kim.client.GetService;
import com.ontotext.kim.client.KIMService;
import com.ontotext.kim.client.corpora.CorporaAPI;
import com.ontotext.kim.client.corpora.KIMAnnotationSet;
import com.ontotext.kim.client.corpora.KIMDocument;
import com.ontotext.kim.client.index.IndexAndPersistAPI;
import com.ontotext.kim.client.query.DocumentQueryResult;
import com.ontotext.kim.client.query.QueryAPI;
import com.ontotext.kim.client.query.SemanticQuery;
import com.ontotext.kim.client.query.SemanticQueryImpl;
import com.ontotext.kim.client.semanticannotation.SemanticAnnotationAPI;
import com.ontotext.kim.client.semanticrepository.SemanticRepositoryAPI;

public class BugTest
{
    public static final String INDEX_URL = "c:/kim-platform/context/default/populated/index-and-store";

    public static final String PERSIST_URL = "lucene:/c:/kim-platform/context/default/populated/index-and-store";

    public final static String INDEX_AND_PERSIST_URL = "lucene:/c:/kim-platform/context/default/populated/index-and-store";

    private static CorporaAPI apiCorpora = null;

    private static IndexAndPersistAPI apiIndexAndPersist = null;

    private static SemanticAnnotationAPI apiSemAnn = null;

    private static SemanticRepositoryAPI apiSesame = null;

    private static QueryAPI apiQuery = null;

    public static void main(String[] args)
    {
        try
        {
            KIMService serviceKim = GetService.from();
            System.out.println("KIM Server connected.");

            apiIndexAndPersist = serviceKim.getIndexAndPersistAPI(INDEX_AND_PERSIST_URL);
            apiQuery = serviceKim.getQueryAPI();
            apiCorpora = serviceKim.getCorporaAPI();
            apiSemAnn = serviceKim.getSemanticAnnotationAPI();
            apiSesame = serviceKim.getSemanticRepositoryAPI();
            // ---------------------------------------------------------------------

            System.out.println("Number of documents in store: " + apiIndexAndPersist.getDocumentCount(null));
            System.out.println("LastPersistenceID=" + apiIndexAndPersist.getLastPersistenceID());

            // create a document
            String content = "Blair and Bush ? are they doing the right thing for Iraq, America," + " Europe, the Earth... for civilization... "
                    + "or just guided by their blinded eyes are in favor of the big coporations:" + "enter here new unrecognized corporations with a clue suffix:" + "MicroZoftRR Inc.";

            KIMDocument kdoc = apiCorpora.createDocument(content, "UTF-8");
            System.out.println("New document created: \"" + content + "\"");

            // annotate document
            apiSemAnn.execute(kdoc);

            // index and store document
            apiIndexAndPersist.storeDoc(kdoc);

            System.out.println("Number of documents in store: " + apiIndexAndPersist.getDocumentCount(null));
            System.out.println();

            Scanner scanner = new Scanner(System.in);
            System.out.print("Type a class name: ");
            String className = scanner.nextLine();

            // Create a very simple semantic query
            SemanticQuery seq = new SemanticQueryImpl();
            seq.addRequestedVar("X");
            seq.setClass("X", apiSesame.getURIFromLocalName(className).getURI());
            System.out.println("Semantic query for documents containing: " + seq.getClassFor("X"));

            // Search for the document
            DocumentQueryResult resDocuments = apiQuery.getDocuments(seq, INDEX_URL, PERSIST_URL);
            System.out.println("Number of resulting documents: " + resDocuments.size());
            while (resDocuments.hasNext())
            {
                KIMDocument doc = resDocuments.nextDocument();
                System.out.println();
                System.out.println("[ DOCUMENT CONTENT BEGIN ]\n" + doc.getContent() + "\n[ DOCUMENT CONTENT END ]\n");
                KIMAnnotationSet kimASet = kdoc.getAnnotations();
                Iterator annIterator = kimASet.iterator();
                System.out.println("[ Document's Annotations (begin) ]");
                while (annIterator.hasNext())
                {
                    System.out.println(annIterator.next());
                }
                System.out.println("[ Document's Annotations (end) ]");
            }

            // delete the document
            apiIndexAndPersist.deleteDoc(kdoc.getLRPersistenceId());
            System.out.println("Document deleted.");
            System.out.println("Number of documents in store: " + apiIndexAndPersist.getDocumentCount(null));
            System.out.println("LastPersistenceID=" + apiIndexAndPersist.getLastPersistenceID());

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

}

