podd.model.entity.impl.AbstractPoddEntityImpl.java Source code

Java tutorial

Introduction

Here is the source code for podd.model.entity.impl.AbstractPoddEntityImpl.java

Source

/*
 * Copyright (c) 2009 - 2010. School of Information Technology and Electrical
 * Engineering, The University of Queensland.  This software is being developed
 * for the "Phenomics Ontoogy Driven Data Management Project (PODD)" project.
 * PODD is a National e-Research Architecture Taskforce (NeAT) project
 * co-funded by ANDS and ARCS.
 *
 * PODD is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * PODD is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PODD.  If not, see <http://www.gnu.org/licenses/>.
 */

package podd.model.entity.impl;

import org.apache.commons.lang3.StringEscapeUtils;
import org.openrdf.query.parser.sparql.StringEscapesProcessor;
import org.semanticweb.owl.model.OWLAnnotation;
import org.semanticweb.owl.model.OWLDataFactory;
import org.semanticweb.owl.model.OWLException;
import org.semanticweb.owl.model.OWLOntology;
import org.semanticweb.owl.model.OWLOntologyChangeException;
import org.semanticweb.owl.model.OWLUntypedConstant;
import org.semanticweb.owl.model.SetOntologyURI;
import podd.model.Annotation;
import podd.model.entity.PoddEntity;
import podd.model.user.User;
import podd.util.owl.PoddOWLOntologyManager;
import podd.util.owl.impl.PoddOWLOntologyManagerImpl;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.util.List;

import static org.semanticweb.owl.vocab.OWLRDFVocabulary.OWL_VERSION_INFO;
import static podd.util.common.Constants.ONTOLOGY_BASE_URI_POSTFIX;

/**
 * @author Yuan-Fang Li
 * @version $Id$
 */

public abstract class AbstractPoddEntityImpl extends AbstractEntityImpl implements PoddEntity {
    protected User lastModifier;
    protected String lastModified;
    protected List<Annotation> annotations;

    protected AbstractPoddEntityImpl() {
        super();
    }

    @Override
    public List<Annotation> getAnnotations() {
        return annotations;
    }

    @Override
    public User getLastModifier() {
        return lastModifier;
    }

    @Override
    public void setLastModifier(User lastModifier) {
        this.lastModifier = lastModifier;
    }

    @Override
    public String getLastModified() {
        return lastModified;
    }

    @Override
    public void setLastModified(String lastModified) {
        this.lastModified = lastModified;
    }

    @Override
    public void setAnnotations(List<Annotation> annotations) {
        this.annotations = annotations;
    }

    protected OWLOntology createOntology(String name, String namespace, String timestamp) throws OWLException {
        URI baseURI = makeOntologyURI(name, namespace);
        PoddOWLOntologyManager manager = new PoddOWLOntologyManagerImpl();
        final OWLOntology ontology = manager.createOntology(baseURI);
        try {
            final OWLDataFactory factory = manager.getOWLDataFactory();
            final OWLUntypedConstant constant = factory.getOWLUntypedConstant(timestamp);
            final OWLAnnotation annotation = factory.getOWLConstantAnnotation(OWL_VERSION_INFO.getURI(), constant);
            manager.addAxiom(ontology, factory.getOWLOntologyAnnotationAxiom(ontology, annotation));
            return ontology;
        } finally {
            manager.removeOntology(baseURI);
        }
    }

    protected boolean doChangeOntologyURI(String newLocalName, OWLOntology ontology, String namespace)
            throws OWLOntologyChangeException {
        if (!ontology.getURI().toString().endsWith(newLocalName + ONTOLOGY_BASE_URI_POSTFIX)) {
            URI newURI = makeOntologyURI(newLocalName, namespace);
            PoddOWLOntologyManager manager = new PoddOWLOntologyManagerImpl();
            SetOntologyURI setURI = new SetOntologyURI(ontology, newURI);
            manager.applyChange(setURI);
            return true;
        } else {
            return false;
        }
    }

    private URI makeOntologyURI(String pid, String namespace) {
        String ns = namespace;
        if (namespace.endsWith("#")) {
            ns = namespace.replace('#', '/');
        } else if (!namespace.endsWith("/")) {
            ns = namespace + "/";
        }
        return URI.create(ns + pid + ONTOLOGY_BASE_URI_POSTFIX);
    }

    @Override
    public String labelWithoutLineReturn() {
        if (label.indexOf("\n") > -1) {
            return label.substring(0, label.indexOf("\n"));
        }
        return label;
    }

    @Override
    public String escapeLabelForHTML() {
        return StringEscapeUtils.escapeHtml4(label).replace("\n", "<br />").replace("\r", "");
    }
}