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.Iterator;
024import java.util.Map;
025import javax.jcr.RepositoryException;
026import javax.jcr.Session;
027
028import org.fcrepo.kernel.api.models.FedoraResource;
029import org.fcrepo.kernel.api.identifiers.IdentifierConverter;
030import org.fcrepo.kernel.api.RdfStream;
031import org.fcrepo.kernel.modeshape.utils.NamespaceTools;
032import org.slf4j.Logger;
033
034import org.apache.jena.rdf.model.Resource;
035import org.apache.jena.rdf.model.Statement;
036
037/**
038 * Consumes an {@link RdfStream} by adding its contents to the
039 * JCR.
040 *
041 * @see RdfRemover
042 * @author ajs6f
043 * @since Oct 24, 2013
044 */
045public class RdfAdder extends PersistingRdfStreamConsumer {
046
047    private static final Logger LOGGER = getLogger(RdfAdder.class);
048    private Map<String, String> userNamespaces;
049
050    /**
051     * Ordinary constructor.
052     *
053     * @param idTranslator the id translator
054     * @param session the session
055     * @param stream the rdf stream
056     * @param userNamespaces user-provided namespace mapping
057     */
058    public RdfAdder(final IdentifierConverter<Resource, FedoraResource> idTranslator, final Session session,
059        final RdfStream stream, final Map<String, String> userNamespaces) {
060        super(idTranslator, session, stream);
061        this.userNamespaces = userNamespaces;
062    }
063
064    private Map<String, String> getNamespaces(final Session session) {
065        final Map<String, String> namespaces = NamespaceTools.getNamespaces(session);
066        if (userNamespaces != null) {
067            for (final Iterator<String> it = userNamespaces.keySet().iterator(); it.hasNext(); ) {
068                final String prefix = it.next();
069                final String uri = userNamespaces.get(prefix);
070                if (!namespaces.containsKey(prefix) && !namespaces.containsValue(uri)) {
071                    LOGGER.debug("Adding user-supplied namespace mapping {}: {}", prefix, uri);
072                    namespaces.put(prefix, uri);
073                } else {
074                    LOGGER.debug("Not adding conflicting user-supplied namespace mapping {}: {}", prefix, uri);
075                }
076            }
077        }
078        return namespaces;
079    }
080
081    @Override
082    protected void operateOnMixin(final Resource mixinResource, final FedoraResource resource)
083            throws RepositoryException {
084        jcrRdfTools().addMixin(resource, mixinResource, getNamespaces(getJcrNode(resource).getSession()));
085    }
086
087
088    @Override
089    protected void operateOnProperty(final Statement t, final FedoraResource resource) throws RepositoryException {
090        LOGGER.debug("Adding property from triple: {} to resource: {}.", t, resource
091                .getPath());
092
093        jcrRdfTools().addProperty(resource, t.getPredicate(), t.getObject(),
094                getNamespaces(getJcrNode(resource).getSession()));
095    }
096}