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;
019
020import static org.fcrepo.kernel.modeshape.utils.FedoraTypesUtils.isNonRdfSourceDescription;
021import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
022import static org.slf4j.LoggerFactory.getLogger;
023
024import javax.jcr.Node;
025import javax.jcr.RepositoryException;
026
027import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
028import org.fcrepo.kernel.api.models.FedoraResource;
029import org.fcrepo.kernel.api.models.NonRdfSourceDescription;
030import org.slf4j.Logger;
031
032import java.util.Calendar;
033
034/**
035 * Abstraction for a Fedora datastream backed by a JCR node.
036 *
037 * @author ajs6f
038 * @since Feb 21, 2013
039 */
040public class NonRdfSourceDescriptionImpl extends FedoraResourceImpl implements NonRdfSourceDescription {
041
042    private static final Logger LOGGER = getLogger(NonRdfSourceDescriptionImpl.class);
043
044    /**
045     * The JCR node for this datastream
046     *
047     * @param n an existing {@link Node}
048     */
049    public NonRdfSourceDescriptionImpl(final Node n) {
050        super(n);
051    }
052
053    @Override
054    public FedoraResource getDescribedResource() {
055        return new FedoraBinaryImpl(getContentNode());
056    }
057
058    @Override
059    public FedoraResource getBaseVersion() {
060        try {
061            return new NonRdfSourceDescriptionImpl(getVersionManager().getBaseVersion(getPath()).getFrozenNode());
062        } catch (final RepositoryException e) {
063            throw new RepositoryRuntimeException(e);
064        }
065    }
066
067    private Node getContentNode() {
068        LOGGER.trace("Retrieved datastream content node.");
069        try {
070            return node.getNode(JCR_CONTENT);
071        } catch (final RepositoryException e) {
072            throw new RepositoryRuntimeException(e);
073        }
074    }
075
076    /**
077     * Check if the node has a fedora:datastream mixin
078     *
079     * @param node node to check
080     * @return whether the node has a fedora:datastream mixin
081     */
082    public static boolean hasMixin(final Node node) {
083        return isNonRdfSourceDescription.test(node);
084    }
085
086    /**
087     * Overrides the superclass to propagate updates to certain properties to the binary if explicitly set.
088     */
089    public void touch(final boolean includeMembershipResource, final Calendar createdDate, final String createdUser,
090                      final Calendar modifiedDate, final String modifyingUser) throws RepositoryException {
091        super.touch(includeMembershipResource, createdDate, createdUser, modifiedDate, modifyingUser);
092        if (createdDate != null || createdUser != null || modifiedDate != null || modifyingUser != null) {
093            ((FedoraBinaryImpl) getDescribedResource()).touch(false, createdDate, createdUser,
094                    modifiedDate, modifyingUser);
095        }
096    }
097
098}