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.kernel.api.models;
017
018import java.net.URI;
019import java.util.Date;
020
021import java.util.List;
022import java.util.Set;
023import java.util.stream.Stream;
024
025import javax.jcr.Node;
026import javax.jcr.version.Version;
027import javax.jcr.version.VersionHistory;
028
029import com.hp.hpl.jena.rdf.model.Resource;
030
031import org.fcrepo.kernel.api.exception.AccessDeniedException;
032import org.fcrepo.kernel.api.exception.MalformedRdfException;
033import org.fcrepo.kernel.api.identifiers.IdentifierConverter;
034import org.fcrepo.kernel.api.TripleCategory;
035import org.fcrepo.kernel.api.RdfStream;
036
037import com.hp.hpl.jena.rdf.model.Model;
038
039/**
040 * @author ajs6f
041 * @since Jan 10, 2014
042 */
043public interface FedoraResource {
044
045    /**
046     * @return The JCR node that backs this object.
047     */
048    Node getNode();
049
050    /**
051     * Get the path to the JCR node
052     * @return path
053     */
054    String getPath();
055
056    /**
057     * Get the children of this resource
058     * @return a stream of Fedora resources
059     */
060    default Stream<FedoraResource> getChildren() {
061        return getChildren(false);
062    }
063
064    /**
065     * Get the children of this resource, possibly recursively
066     * @param recursive whether to recursively fetch child resources
067     * @return a stream of Fedora resources
068     */
069    Stream<FedoraResource> getChildren(Boolean recursive);
070
071    /**
072     * Get the container of this resource
073     * @return the container of this resource
074     */
075    FedoraResource getContainer();
076
077    /**
078     * Get the child of this resource at the given path
079     * @param relPath the given path
080     * @return the child of this resource
081     */
082    FedoraResource getChild(String relPath);
083
084    /**
085     * Does this resource have a property
086     * @param relPath the given path
087     * @return the boolean value whether the resource has a property
088     */
089    boolean hasProperty(String relPath);
090
091    /**
092     * Delete this resource, and any inbound references to it
093     */
094    void delete();
095
096    /**
097     * Get the date this datastream was created
098     * @return created date
099     */
100    Date getCreatedDate();
101
102    /**
103     * Get the date this datastream was last modified
104     * @return last modified date
105     */
106    Date getLastModifiedDate();
107
108    /**
109     * Check if this object uses a given RDF type
110     *
111     * <p>Note: the type parameter should be in prefixed short form, so ldp:Container or ex:Image
112     * are both acceptable types. This method does not assume any jcr to fedora prefix mappings are
113     * managed by the implementation, so hasType("jcr:frozenNode") is a valid use of this method.</p>
114     *
115     * @param type the given type
116     * @return whether the object has the given type
117     */
118    boolean hasType(final String type);
119
120    /**
121     * Get the RDF:type values for this resource
122     * @return a list of types for this resource
123     */
124    List<URI> getTypes();
125
126    /**
127     * Update the provided properties with a SPARQL Update query. The updated
128     * properties may be serialized to the JCR store.
129     *
130     * After applying the statement, clients SHOULD check the result
131     * of #getDatasetProblems, which may include problems when attempting to
132     * serialize the data to JCR.
133     *
134     * @param idTranslator the property of idTranslator
135     * @param sparqlUpdateStatement sparql update statement
136     * @param originalTriples original triples
137     * @throws MalformedRdfException if malformed rdf exception occurred
138     * @throws AccessDeniedException if access denied in updating properties
139     */
140    void updateProperties(final IdentifierConverter<Resource, FedoraResource> idTranslator,
141                          final String sparqlUpdateStatement,
142                          final RdfStream originalTriples) throws MalformedRdfException, AccessDeniedException;
143
144    /**
145     * Return the RDF properties of this object using the provided context
146     * @param idTranslator the property of idTranslator
147     * @param context the context
148     * @return the rdf properties of this object using the provided context
149     */
150    RdfStream getTriples(final IdentifierConverter<Resource, FedoraResource> idTranslator,
151                         final TripleCategory context);
152
153    /**
154     * Return the RDF properties of this object using the provided contexts
155     * @param idTranslator the property of idTranslator
156     * @param contexts the provided contexts
157     * @return the rdf properties of this object
158     */
159    RdfStream getTriples(final IdentifierConverter<Resource, FedoraResource> idTranslator,
160                         final Set<? extends TripleCategory> contexts);
161
162    /**
163     * Get the JCR Base version for the node
164     *
165     * @return base version
166     */
167    public Version getBaseVersion();
168
169    /**
170     * Get JCR VersionHistory for the node.
171     *
172     * @return version history
173     */
174    public VersionHistory getVersionHistory();
175
176    /**
177     * Check if a resource was created in this session
178     * @return if resource created in this session
179     */
180    Boolean isNew();
181
182    /**
183     * Replace the properties of this object with the properties from the given
184     * model
185     *
186     * @param idTranslator the given property of idTranslator
187     * @param inputModel the input model
188     * @param originalTriples the original triples
189     * @throws MalformedRdfException if malformed rdf exception occurred
190     */
191    void replaceProperties(final IdentifierConverter<Resource, FedoraResource> idTranslator,
192                                final Model inputModel,
193                                final RdfStream originalTriples) throws MalformedRdfException;
194
195    /**
196         * Construct an ETag value from the last modified date and path. JCR has a
197     * mix:etag type, but it only takes into account binary properties. We
198     * actually want whole-object etag data. TODO : construct and store an ETag
199     * value on object modify
200     *
201     * @return constructed etag value
202     */
203    String getEtagValue();
204
205    /**
206     * Enable versioning
207     */
208    void enableVersioning();
209
210    /**
211     * Disable versioning
212     */
213    void disableVersioning();
214
215    /**
216     * Check if a resource is versioned
217     * @return whether the resource is versioned
218     */
219    boolean isVersioned();
220
221    /**
222     * Check if a resource is frozen (a historic version).
223     * @return whether the resource is frozen
224     */
225    boolean isFrozenResource();
226
227    /**
228     * When this is a frozen node, get the ancestor that was explicitly versioned
229     * @return the ancestor that was explicity versioned
230     */
231    FedoraResource getVersionedAncestor();
232
233    /**
234     * Get the unfrozen equivalent of a frozen versioned node
235     * @return the unfrozen equivalent of a frozen versioned node
236     */
237    FedoraResource getUnfrozenResource();
238
239    /**
240     * Get the node for this object at the version provided.
241     * @param label the label
242     * @return the node for this object at the version provided
243     */
244    Node getNodeVersion(String label);
245
246    /**
247     * This method returns the version label of this frozen resource.
248     * If this resource is not frozen, null is returned.
249     * @return version label
250     */
251    String getVersionLabelOfFrozenResource();
252}