001/**
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.client.impl;
017
018import com.hp.hpl.jena.graph.Node;
019import com.hp.hpl.jena.graph.NodeFactory;
020import com.hp.hpl.jena.graph.Triple;
021import com.hp.hpl.jena.util.iterator.ExtendedIterator;
022import org.fcrepo.client.FedoraException;
023import org.fcrepo.client.FedoraObject;
024import org.fcrepo.client.FedoraRepository;
025import org.fcrepo.client.FedoraResource;
026import org.fcrepo.client.utils.HttpHelper;
027
028import java.util.Collection;
029import java.util.HashSet;
030import java.util.Set;
031
032import static org.fcrepo.kernel.api.RdfLexicon.CONTAINS;
033import static org.fcrepo.kernel.api.RdfLexicon.HAS_MIXIN_TYPE;
034
035/**
036 * A Fedora Object Impl.
037 *
038 * @author lsitu
039 * @author escowles
040 * @since 2014-08-11
041 */
042public class FedoraObjectImpl extends FedoraResourceImpl implements FedoraObject {
043    private final static Node binaryType = NodeFactory.createLiteral("fedora:Binary");
044
045    /**
046     * Constructor for FedoraObjectImpl
047     *
048     * @param repository FedoraRepository that created this object
049     * @param httpHelper HTTP helper for making repository requests
050     * @param path Repository path
051     */
052    public FedoraObjectImpl(final FedoraRepository repository, final HttpHelper httpHelper, final String path) {
053        super(repository, httpHelper, path);
054    }
055
056    /**
057     * Get the Object and Datastream nodes that are children of the current Object.
058     *
059     * @param mixin If not null, limit to results that have this mixin.
060     */
061    public Collection<FedoraResource> getChildren(final String mixin) throws FedoraException {
062        Node mixinLiteral = null;
063        if ( mixin != null ) {
064            mixinLiteral = NodeFactory.createLiteral(mixin);
065        }
066        final ExtendedIterator<Triple> it = graph.find(Node.ANY, CONTAINS.asNode(), Node.ANY);
067        final Set<FedoraResource> set = new HashSet<>();
068        while (it.hasNext()) {
069            final Node child = it.next().getObject();
070            if ( mixin == null || graph.contains(child, HAS_MIXIN_TYPE.asNode(), mixinLiteral) ) {
071                final String path = child.getURI().toString()
072                        .replaceAll(repository.getRepositoryUrl(),"");
073                if ( graph.contains(child, HAS_MIXIN_TYPE.asNode(), binaryType) ) {
074                    set.add( repository.getDatastream(path) );
075                } else {
076                    set.add( repository.getObject(path) );
077                }
078            }
079        }
080        return set;
081    }
082
083    @Override
084    public FedoraObject createObject() throws FedoraException {
085        return repository.createResource(getPath());
086    }
087}