view src/main/java/de/mpiwg/itgroup/annotationManager/RDFHandling/Convert.java @ 33:e5f5848892a2

new annotation model basically works.
author casties
date Thu, 31 May 2012 19:08:48 +0200
parents b37487b756ac
children
line wrap: on
line source

package de.mpiwg.itgroup.annotationManager.RDFHandling;

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

import org.apache.log4j.Logger;
import org.openrdf.repository.RepositoryException;

import de.mpiwg.itgroup.annotationManager.Constants.NS;
import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreStoreError;
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 = NS.MPIWG_ANNOT_CTX;
    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));
        /*
         * 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));

        if (annot.page == null || annot.page == "") {
            /*
             *  target without page number (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));
            if (annot.xpointer.contains("#")) {
                // isPartOf base resource (without xpointer/fragment)
                String baseUri = annot.xpointer.substring(0, annot.xpointer.indexOf("#"));
                retQuad.add(new Quadruple(annot.xpointer, NS.DCTERMS_NS + "isPartOf", baseUri, context));
            }                
        } else {
            /* 
             * ConstrainedTarget with page number
             */
            String ctUrl = createRessourceURL("annotCT:");
            // is ConstrainedTarget
            retQuad.add(new Quadruple(ctUrl, NS.RDF_NS + "type", NS.OAC_NS + "ConstrainedTarget", context));
            // is target of annotation
            retQuad.add(new Quadruple(annotationUrl, NS.OAC_NS + "hasTarget", ctUrl, context));
            // constrains Target (full URL)
            retQuad.add(new Quadruple(ctUrl, NS.OAC_NS + "constrains", annot.xpointer, context));
            // TextPageConstraint
            String tpcUrl = createRessourceURL("annotC:");
            // is TextPageConstraint
            retQuad.add(new Quadruple(tpcUrl, NS.RDF_NS + "type", NS.MPIWG_ANNOT_NS + "TextPageConstraint", context));
            // page number
            retQuad.add(new LiteralQuadruple(tpcUrl, NS.MPIWG_ANNOT_NS + "textPage", annot.page, context));
            // textPageDisplay
            if (annot.displayUrl != null && annot.displayUrl != "") {
                retQuad.add(new LiteralQuadruple(tpcUrl, NS.MPIWG_ANNOT_NS + "textPageDisplay", annot.displayUrl, context));
            }
            // ConstrainedTarget has this Constraint
            retQuad.add(new Quadruple(ctUrl, NS.OAC_NS + "hasConstraint", tpcUrl, context));  
            //TODO: displayUrl
        }

        // annotation body
        if (annot.url != null && annot.url.startsWith("http://")) {
            /*
             *  body is resource
             */
            retQuad.add(new Quadruple(annot.url, NS.RDF_NS + "type", NS.OAC_NS + "Body", context));
            // is body of annotation
            retQuad.add(new Quadruple(annotationUrl, NS.OAC_NS + "hasBody", annot.url, context));
        } else {
            /*
             *  body is literal text
             */
            String abUrl = createRessourceURL("annotB:");
            // is Body and ContentAsText
            retQuad.add(new Quadruple(abUrl, NS.RDF_NS + "type", NS.OAC_NS + "Body", context));
            retQuad.add(new Quadruple(abUrl, NS.RDF_NS + "type", NS.CNT_NS + "ContentAsText", context));
            // has text
            retQuad.add(new LiteralQuadruple(abUrl, NS.CNT_NS + "chars", annot.text, context));
            // is body of annotation
            retQuad.add(new Quadruple(annotationUrl, NS.OAC_NS + "hasBody", abUrl, context));
        }

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

        annot.setAnnotationUri(annotationUrl);
        return retQuad;
    }

    /**
     * Erzeuge eine urn aus der aktuellen 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);

    }

    /**
     * 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";
        }
        annotationRdf = annot2quadruple(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;
    }

}