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.apache.jena.vocabulary.RDF.type;
021import static org.fcrepo.kernel.modeshape.utils.FedoraTypesUtils.getJcrNode;
022import static org.slf4j.LoggerFactory.getLogger;
023
024import java.util.Map;
025
026import javax.jcr.RepositoryException;
027import javax.jcr.Session;
028
029import org.apache.jena.rdf.model.Resource;
030import org.apache.jena.rdf.model.Statement;
031import org.fcrepo.kernel.api.RdfStream;
032import org.fcrepo.kernel.api.exception.MalformedRdfException;
033import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
034import org.fcrepo.kernel.api.identifiers.IdentifierConverter;
035import org.fcrepo.kernel.api.models.FedoraResource;
036import org.slf4j.Logger;
037
038/**
039 * Consumes an {@link RdfStream} by adding its contents to the JCR with relaxed restrictions on server managed
040 * properties.
041 *
042 * @author bbpennel
043 */
044public class RelaxedRdfAdder extends RdfAdder {
045
046    private static final Logger LOGGER = getLogger(RelaxedRdfAdder.class);
047
048    /**
049     * Default constructor
050     *
051     * @param idTranslator translator
052     * @param session session
053     * @param stream rdf stream
054     * @param userNamespaces namespaces
055     */
056    public RelaxedRdfAdder(final IdentifierConverter<Resource, FedoraResource> idTranslator, final Session session,
057            final RdfStream stream, final Map<String, String> userNamespaces) {
058        super(idTranslator, session, stream, userNamespaces);
059    }
060
061    @Override
062    protected void operateOnTriple(final Statement input) throws MalformedRdfException {
063        try {
064            final Statement t = jcrRdfTools().skolemize(translator(), input, stream().topic().toString());
065
066            final Resource subject = t.getSubject();
067            final FedoraResource subjectNode = translator().convert(subject);
068
069            // if this is a user-managed RDF type assertion, update the node's
070            // mixins. If it isn't, treat it as a "data" property.
071            if (t.getPredicate().equals(type) && t.getObject().isResource()) {
072                final Resource mixinResource = t.getObject().asResource();
073
074                LOGGER.debug("Operating on node: {} with mixin: {}.",
075                        subjectNode.getPath(), mixinResource);
076                operateOnMixin(mixinResource, subjectNode);
077            } else {
078                LOGGER.debug("Operating on node: {} from triple: {}.", subjectNode.getPath(), t);
079                operateOnProperty(t, subjectNode);
080            }
081        } catch (final RepositoryException | RepositoryRuntimeException e) {
082            throw new MalformedRdfException(e.getMessage(), e);
083        }
084    }
085
086    @Override
087    protected void operateOnProperty(final Statement t, final FedoraResource resource) throws RepositoryException {
088        LOGGER.debug("Adding property from triple: {} to resource: {}.", t, resource
089                .getPath());
090
091        jcrRdfTools().addProperty(resource, t.getPredicate(), t.getObject(),
092                getNamespaces(getJcrNode(resource).getSession()), true);
093    }
094}