001/**
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.oai.dublincore;
017
018import com.hp.hpl.jena.graph.Triple;
019import org.apache.commons.lang.StringEscapeUtils;
020import org.fcrepo.http.api.FedoraNodes;
021import org.fcrepo.http.commons.api.rdf.HttpResourceConverter;
022import org.fcrepo.kernel.modeshape.rdf.impl.PropertiesRdfContext;
023import org.fcrepo.kernel.api.models.Container;
024import org.fcrepo.kernel.api.utils.iterators.RdfStream;
025import org.openarchives.oai._2_0.oai_dc.OaiDcType;
026import org.purl.dc.elements._1.ElementType;
027import org.purl.dc.elements._1.ObjectFactory;
028
029import javax.jcr.RepositoryException;
030import javax.jcr.Session;
031import javax.ws.rs.core.UriInfo;
032import javax.xml.bind.JAXBElement;
033
034/**
035 * The type Jcr properties generator.
036 *
037 * @author Frank Asseg
038 */
039public class JcrPropertiesGenerator {
040    private static final ObjectFactory dcFactory = new ObjectFactory();
041    private static final org.openarchives.oai._2_0.oai_dc.ObjectFactory oaiDcFactory =
042            new org.openarchives.oai._2_0.oai_dc.ObjectFactory();
043
044
045    /**
046     * Generate dC.
047     *
048     * @param session the session
049     * @param obj the obj
050     * @param uriInfo the uri info
051     * @return the jAXB element
052     * @throws RepositoryException if repository exception occurred
053     */
054    public JAXBElement<OaiDcType> generateDC(final Session session, final Container obj, final UriInfo uriInfo)
055            throws RepositoryException {
056
057        final HttpResourceConverter converter = new HttpResourceConverter(session, uriInfo.getBaseUriBuilder()
058                .clone().path(FedoraNodes.class));
059        final OaiDcType oaidc = this.oaiDcFactory.createOaiDcType();
060
061        final ElementType valId = this.dcFactory.createElementType();
062        valId.setValue(escape(converter.toDomain(obj.getPath()).getURI()));
063        oaidc.getTitleOrCreatorOrSubject().add(this.dcFactory.createIdentifier(valId));
064
065        final ElementType valCreated = this.dcFactory.createElementType();
066        valCreated.setValue(escape(obj.getCreatedDate().toString()));
067        oaidc.getTitleOrCreatorOrSubject().add(this.dcFactory.createDate(valCreated));
068
069        final ElementType valCreator = this.dcFactory.createElementType();
070        valCreator.setValue(escape(obj.getProperty("jcr:createdBy").getValue().getString()));
071        oaidc.getTitleOrCreatorOrSubject().add(this.dcFactory.createCreator(valCreator));
072
073        final RdfStream triples = obj.getTriples(converter, PropertiesRdfContext.class);
074        while (triples.hasNext()) {
075            final ElementType valRel = this.dcFactory.createElementType();
076            final Triple triple = triples.next();
077            valRel.setValue(escape(triple.getPredicate().toString() + " " + triple.getObject().toString()));
078            oaidc.getTitleOrCreatorOrSubject().add(this.dcFactory.createRelation(valRel));
079        }
080
081        oaidc.getTitleOrCreatorOrSubject().add(this.dcFactory.createSubject(valId));
082
083        return this.oaiDcFactory.createDc(oaidc);
084    }
085
086    private String escape(final String orig) {
087        return StringEscapeUtils.escapeXml(orig);
088    }
089}