view src/main/java/de/mpiwg/itgroup/annotationManager/RDFHandling/Convert.java @ 26:235b91ba8dff

on the way to new annotations...
author casties
date Thu, 26 Apr 2012 11:44:57 +0200
parents 52dff477e45f
children b4a798d417fe
line wrap: on
line source

package de.mpiwg.itgroup.annotationManager.RDFHandling;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.openrdf.repository.RepositoryException;
import org.restlet.Context;
import org.restlet.engine.component.ChildContext;

import de.mpiwg.itgroup.annotationManager.Constants.NS;
import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreStoreError;
import de.mpiwg.itgroup.annotationManager.Errors.XPointerError;
import de.mpiwg.itgroup.triplestoremanager.exceptions.TripleStoreHandlerException;
import de.mpiwg.itgroup.triplestoremanager.owl.TripleStoreHandler;
import de.mpiwg.itgroup.triplestoremanager.owl.TripleStoreHandler.LiteralQuadruple;
import de.mpiwg.itgroup.triplestoremanager.owl.TripleStoreHandler.Quadruple;

/**
 * @author dwinter
 * 
 *         Klasse zum Konvertieren von Annotationen zum mpiwg RDF-Format: http://ontologies.mpiwg-berlin.mpg.de/annotations/
 */

public class Convert {
    private String context = "file:///annotations";
    private static Logger logger = Logger.getRootLogger();
    private String urlBase = "http://entities.mpiwg-berlin.mpg.de/annotations/"; // TODO should go into config

    public Convert(String context) {
        this.context = context;
    }

    /**
     * 
     * @param xpointer
     *            Beschreibt die Annotation
     * @param creator
     *            Username des Creators
     * @param time
     *            Erstellungszeit, wenn null wird das aktuelle Datum verwenden beim Konvertieren
     * @param text
     *            der Annotation
     * @param type
     *            Annotationstype (Entsprechend den in http://www.w3.org/2000/10/annotationType# definierten.)
     * @return
     */

    private List<Quadruple> annot2quadruple(String xpointer, String creator, String time, String annot, String type) {
        return annot2quadruple(new Annotation(xpointer, creator, time, annot, type));
    }

    /**
     * @param annot
     * @return
     */
    public List<Quadruple> annot2quadruple(Annotation annot) {
        List<Quadruple> retQuad = new ArrayList<Quadruple>();

        // create new URL if annot has no annotationUrl
        String annotationUrl = annot.getAnnotationUri();
        if (annotationUrl == null) {
            annotationUrl = createRessourceURL("annot:");
        }

        // annotation class
        retQuad.add(new Quadruple(annotationUrl, NS.RDF_NS + "type", NS.OAC_NS + "Annotation", context));
        // TODO: what types?
        //retQuad.add(new LiteralQuadruple(annotationUrl, NS.RDF + "type", annot.type, context));

        // author
        if (annot.creator.startsWith("http")) {
            retQuad.add(new Quadruple(annotationUrl, NS.DCTERMS_NS + "creator", annot.creator, context));
        } else {
            // TODO: this should not happen
            retQuad.add(new LiteralQuadruple(annotationUrl, NS.DCTERMS_NS + "creator", annot.creator, context));
        }
        // creation time
        if (annot.time == null) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
            annot.time = format.format(Calendar.getInstance().getTime());
        }
        retQuad.add(new LiteralQuadruple(annotationUrl, NS.DCTERMS_NS + "created", annot.time, context));

        // target (full URL)
        retQuad.add(new Quadruple(annot.xpointer, NS.RDF_NS + "type", NS.OAC_NS + "Target", context));
        // is target of annotation
        retQuad.add(new Quadruple(annotationUrl, NS.OAC_NS + "hasTarget", annot.xpointer, context));
        
        // now add the xpointers
        /* String[] xpointerSplitted = annot.xpointer.split("#");
        if (xpointerSplitted.length > 2) {
            throw new XPointerError();
        }
        retQuad.add(new Quadruple(annotationUrl, NS.MPIWG_ANNOT_URL + "annotatesDocuviewerText", xpointerSplitted[0], context));
        retQuad.add(new Quadruple(annotationUrl, NS.MPIWG_ANNOT_URL + "textSelection", annot.xpointer, context));
        */

        // annotation body
        String annotationtext = createRessourceURL("annotationBody:");
        
        retQuad.add(new Quadruple(annotationUrl, NS.OAC_NS + "Body", annotationtext, context));

        retQuad.add(new Quadruple(annotationtext, NS.RDF_NS + "type", NS.MPIWG_ANNOT_URL + "StandardTextNote", context));

        retQuad.add(new LiteralQuadruple(annotationtext, NS.MPIWG_ANNOT_URL + "containsText", annot.text, context));

        for (Quadruple ret : retQuad) {
            logger.debug(ret.toString());
        }

        annot.setAnnotationUri(annotationUrl);
        return retQuad;
    }

    /**
     * Erzeuge eine urn aus der aktullen Zeit in millis
     * 
     * @return
     */
    private String createRessourceURL(String prefix) {

        Calendar cal = Calendar.getInstance();

        long time = cal.getTimeInMillis();

        return String.format("%s%s%s", urlBase, prefix, time);

    }

    /**
     * Hier ist die uri der Annotation angegeben.
     * 
     * @param annot
     * @return
     */

    private List<Quadruple> rel2quadruple(Annotation annot) {

        List<Quadruple> retQuad = new ArrayList<Quadruple>();

        String annotation = createRessourceURL("annot:");

        if (annot.time == null) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
            annot.time = format.format(Calendar.getInstance().getTime());

        }

        // TODO: check type
        retQuad.add(new Quadruple(annotation, NS.RDF_NS + "type", NS.ANNOTATION_TYPE + annot.type, context));

        // add author
        if (annot.creator.startsWith("http")) {
            retQuad.add(new Quadruple(annotation, NS.OAC_NS + "author", annot.creator, context));
        } else {
            retQuad.add(new LiteralQuadruple(annotation, NS.OAC_NS + "author", annot.creator, context));
        }

        // creation time
        retQuad.add(new LiteralQuadruple(annotation, NS.OAC_NS + "created", annot.time, context));

        String[] xpointerSplitted = annot.xpointer.split("#");

        if (xpointerSplitted.length > 2) {
            throw new XPointerError();
        }

        // now add the xpointers
        retQuad.add(new Quadruple(annotation, NS.MPIWG_ANNOT_URL + "annotatesDocuviewerText", xpointerSplitted[0], context));
        retQuad.add(new Quadruple(annotation, NS.MPIWG_ANNOT_URL + "textSelection", annot.xpointer, context));

        // String annotationtext =createRessourceURL("annotText:");

        retQuad.add(new Quadruple(annotation, NS.OAC_NS + "body", annot.url, context));

        retQuad.add(new Quadruple(annot.url, NS.RDF_NS + "type", NS.MPIWG_ANNOT_URL + "ExtendedAnnotation", context));

        for (Quadruple ret : retQuad) {
            logger.debug(ret.toString());
        }

        // save new annotation url in annotation object
        annot.setAnnotationUri(annotation);
        return retQuad;

    }

    /** 
     * Stores the Annotation in the TripleStore.
     * 
     * @param annot
     * @return
     * @throws TripleStoreStoreError
     */
    public Annotation storeAnnotation(Annotation annot) throws TripleStoreStoreError {
        List<Quadruple> annotationRdf = new ArrayList<Quadruple>();
        if ((annot.type == null) || annot.type.equals("")) {
            annot.type = "Comment";
        }
        if (annot.text != null && !annot.text.equals("")) {
            annotationRdf = annot2quadruple(annot);
        }
        if (annot.url != null && !annot.url.equals("")) {
            annotationRdf.addAll(rel2quadruple(annot));
        }
        try {
            TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler();
            th.write(annotationRdf);
        } catch (RepositoryException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            throw new TripleStoreStoreError();
        } catch (TripleStoreHandlerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            throw new TripleStoreStoreError();
        }
        return annot;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        Convert myConvert = new Convert("http://annotations.rdf");
        List<Quadruple> rets = myConvert
                .annot2quadruple(
                        "http://mpdl-dev.mpiwg-berlin.mpg.de/ECHOdocuViewfullTest?url=/mpiwg/online/permanent/library/163127KK&amp;viewMode=text&amp;pn=7#xpointer(string-range(id(&quot;s1&quot;), &quot;&quot;, 66, 12))",
                        "mbuchman", null, "myannot", "Example");
        for (Quadruple ret : rets) {
            System.out.println(ret.toString());
        }
    }

}