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.kernel.modeshape.utils.iterators;
017
018import static org.slf4j.LoggerFactory.getLogger;
019import static org.fcrepo.kernel.modeshape.utils.NamespaceTools.getNamespaces;
020
021import javax.jcr.RepositoryException;
022import javax.jcr.Session;
023
024import org.fcrepo.kernel.api.models.FedoraResource;
025import org.fcrepo.kernel.api.identifiers.IdentifierConverter;
026import org.fcrepo.kernel.api.RdfStream;
027import org.slf4j.Logger;
028
029import com.hp.hpl.jena.rdf.model.Resource;
030import com.hp.hpl.jena.rdf.model.Statement;
031
032/**
033 * Consumes an {@link RdfStream} by adding its contents to the
034 * JCR.
035 *
036 * @see RdfRemover
037 * @author ajs6f
038 * @since Oct 24, 2013
039 */
040public class RdfAdder extends PersistingRdfStreamConsumer {
041
042    private static final Logger LOGGER = getLogger(RdfAdder.class);
043
044    /**
045     * Ordinary constructor.
046     *
047     * @param idTranslator the id translator
048     * @param session the session
049     * @param stream the rdf stream
050     */
051    public RdfAdder(final IdentifierConverter<Resource, FedoraResource> idTranslator, final Session session,
052        final RdfStream stream) {
053        super(idTranslator, session, stream);
054    }
055
056    @Override
057    protected void operateOnMixin(final Resource mixinResource, final FedoraResource resource)
058            throws RepositoryException {
059        jcrRdfTools().addMixin(resource, mixinResource, getNamespaces(resource.getNode().getSession()));
060    }
061
062
063    @Override
064    protected void operateOnProperty(final Statement t, final FedoraResource resource) throws RepositoryException {
065        LOGGER.debug("Adding property from triple: {} to resource: {}.", t, resource
066                .getPath());
067
068        jcrRdfTools().addProperty(resource, t.getPredicate(), t.getObject(),
069                getNamespaces(resource.getNode().getSession()));
070    }
071}