001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.kernel.modeshape.utils.iterators;
019
020import static org.slf4j.LoggerFactory.getLogger;
021import static org.fcrepo.kernel.modeshape.utils.FedoraTypesUtils.getJcrNode;
022
023import java.util.Map;
024import javax.jcr.RepositoryException;
025import javax.jcr.Session;
026
027import org.fcrepo.kernel.api.models.FedoraResource;
028import org.fcrepo.kernel.api.identifiers.IdentifierConverter;
029import org.fcrepo.kernel.api.RdfStream;
030import org.fcrepo.kernel.modeshape.utils.NamespaceTools;
031import org.slf4j.Logger;
032
033import org.apache.jena.rdf.model.Resource;
034import org.apache.jena.rdf.model.Statement;
035
036/**
037 * Consumes an {@link RdfStream} by adding its contents to the
038 * JCR.
039 *
040 * @see RdfRemover
041 * @author ajs6f
042 * @since Oct 24, 2013
043 */
044public class RdfAdder extends PersistingRdfStreamConsumer {
045
046    private static final Logger LOGGER = getLogger(RdfAdder.class);
047    private final Map<String, String> userNamespaces;
048
049    /**
050     * Ordinary constructor.
051     *
052     * @param idTranslator the id translator
053     * @param session the session
054     * @param stream the rdf stream
055     * @param userNamespaces user-provided namespace mapping
056     */
057    public RdfAdder(final IdentifierConverter<Resource, FedoraResource> idTranslator, final Session session,
058        final RdfStream stream, final Map<String, String> userNamespaces) {
059        super(idTranslator, session, stream);
060        this.userNamespaces = userNamespaces;
061    }
062
063    protected Map<String, String> getNamespaces(final Session session) {
064        final Map<String, String> namespaces = NamespaceTools.getNamespaces(session);
065        if (userNamespaces != null) {
066            for (final String prefix : userNamespaces.keySet()) {
067                final String uri = userNamespaces.get(prefix);
068                if (!namespaces.containsKey(prefix) && !namespaces.containsValue(uri)) {
069                    LOGGER.debug("Adding user-supplied namespace mapping {}: {}", prefix, uri);
070                    namespaces.put(prefix, uri);
071                } else {
072                    LOGGER.debug("Not adding conflicting user-supplied namespace mapping {}: {}", prefix, uri);
073                }
074            }
075        }
076        return namespaces;
077    }
078
079    @Override
080    protected void operateOnMixin(final Resource mixinResource, final FedoraResource resource)
081            throws RepositoryException {
082        final FedoraResource description = resource.getDescription();
083        jcrRdfTools().addMixin(description, mixinResource, getNamespaces(getJcrNode(description).getSession()));
084    }
085
086
087    @Override
088    protected void operateOnProperty(final Statement t, final FedoraResource resource) throws RepositoryException {
089        LOGGER.debug("Adding property from triple: {} to resource: {}.", t, resource
090                .getPath());
091
092        jcrRdfTools().addProperty(resource, t.getPredicate(), t.getObject(),
093                getNamespaces(getJcrNode(resource).getSession()));
094    }
095}