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.identifiers.NodeResourceConverter.nodeConverter;
021
022import java.util.Optional;
023
024import javax.jcr.Node;
025import javax.jcr.Property;
026import javax.jcr.RepositoryException;
027
028import org.fcrepo.kernel.api.exception.AccessDeniedException;
029import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
030import org.fcrepo.kernel.api.models.FedoraResource;
031import org.fcrepo.kernel.api.models.FedoraTimeMap;
032
033/**
034 * @author lsitu
035 * @since Oct. 04, 2017
036 */
037public class FedoraTimeMapImpl extends FedoraResourceImpl implements FedoraTimeMap {
038
039    /**
040     * Construct a {@link org.fcrepo.kernel.api.models.FedoraResource} from an existing JCR Node
041     * @param node an existing JCR node to be treated as a fcrepo object
042     */
043    public FedoraTimeMapImpl(final Node node) {
044        super(node);
045    }
046
047    @Override
048    public boolean isOriginalResource() {
049        return false;
050    }
051
052    @Override
053    public void delete() {
054        try {
055            // check to remove references to the TimeMap if it exists
056            removeReferences(node);
057
058            // delete the TimeMap node
059            node.remove();
060        } catch (final javax.jcr.AccessDeniedException e) {
061            throw new AccessDeniedException(e);
062        } catch (final RepositoryException e) {
063            throw new RepositoryRuntimeException(e);
064        }
065    }
066
067    /**
068     * Check if the JCR node has a fedora:TimeMap mixin
069     * @param node the JCR node
070     * @return true if the JCR node has the fedora:TimeMap mixin
071     */
072    public static boolean hasMixin(final Node node) {
073        try {
074            return node.isNodeType(FEDORA_TIME_MAP);
075        } catch (final RepositoryException e) {
076            throw new RepositoryRuntimeException(e);
077        }
078    }
079
080    @Override
081    public FedoraResource getOriginalResource() {
082        try {
083            final Property originalProperty = node.getProperty(MEMENTO_ORIGINAL);
084            final Node originalNode = originalProperty.getNode();
085
086            return Optional.of(originalNode).map(nodeConverter::convert).orElse(null);
087        } catch (final RepositoryException e) {
088            throw new RepositoryRuntimeException(e);
089        }
090    }
091}