Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from futures/model-services
object and datastream creation services
- Loading branch information
Showing
4 changed files
with
123 additions
and
50 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
fcrepo-kernel/src/main/java/org/fcrepo/services/DatastreamService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package org.fcrepo.services; | ||
|
||
import org.modeshape.jcr.api.JcrTools; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.inject.Inject; | ||
import javax.jcr.Node; | ||
import javax.jcr.Property; | ||
import javax.jcr.Repository; | ||
import javax.jcr.RepositoryException; | ||
import javax.jcr.Session; | ||
import javax.ws.rs.core.MediaType; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URI; | ||
import java.util.Calendar; | ||
|
||
import static org.modeshape.jcr.api.JcrConstants.*; | ||
|
||
|
||
public class DatastreamService { | ||
|
||
private static final Logger logger = LoggerFactory | ||
.getLogger(DatastreamService.class); | ||
|
||
@Inject | ||
private Repository repo; | ||
|
||
private JcrTools jcrTools = new JcrTools(); | ||
|
||
public Node createDatastreamNode(final Session session, final String dsPath, final MediaType contentType, final InputStream requestBodyStream) throws RepositoryException, IOException { | ||
|
||
final Node ds = jcrTools.findOrCreateNode(session, dsPath, NT_FILE); | ||
ds.addMixin("fedora:datastream"); | ||
|
||
final Node contentNode = jcrTools.findOrCreateChild(ds, JCR_CONTENT, | ||
NT_RESOURCE); | ||
logger.debug("Created content node at path: " + contentNode.getPath()); | ||
/* | ||
* This next line of code deserves explanation. If we chose for the | ||
* simpler line: | ||
* | ||
* Property dataProperty = contentNode.setProperty(JCR_DATA, | ||
* requestBodyStream); | ||
* | ||
* then the JCR would not block on the stream's completion, and we would | ||
* return to the requestor before the mutation to the repo had actually | ||
* completed. So instead we use createBinary(requestBodyStream), because | ||
* its contract specifies: | ||
* | ||
* "The passed InputStream is closed before this method returns either | ||
* normally or because of an exception." | ||
* | ||
* which lets us block and not return until the job is done! The simpler | ||
* code may still be useful to us for an asychronous method that we | ||
* develop later. | ||
*/ | ||
Property dataProperty = contentNode.setProperty(JCR_DATA, session | ||
.getValueFactory().createBinary(requestBodyStream)); | ||
logger.debug("Created data property at path: " + dataProperty.getPath()); | ||
|
||
ds.setProperty("fedora:contentType", contentType.toString()); | ||
|
||
ds.addMixin("fedora:owned"); | ||
ds.setProperty("fedora:ownerId", session.getUserID()); | ||
|
||
if(!ds.hasProperty("fedora:created")) { | ||
ds.setProperty("fedora:created", Calendar.getInstance()); | ||
} | ||
ds.setProperty("jcr:lastModified", Calendar.getInstance()); | ||
|
||
// TODO: I guess we should also have the PID + DSID.. | ||
ds.setProperty("dc:identifier", new String[] { ds.getIdentifier(), ds.getParent().getName() + "/" + ds.getName() }); | ||
|
||
return ds; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
fcrepo-kernel/src/main/java/org/fcrepo/services/ObjectService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.fcrepo.services; | ||
|
||
|
||
import org.modeshape.jcr.api.JcrTools; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.inject.Inject; | ||
import javax.jcr.Node; | ||
import javax.jcr.Repository; | ||
import javax.jcr.RepositoryException; | ||
import javax.jcr.Session; | ||
import java.util.Calendar; | ||
|
||
import static javax.ws.rs.core.Response.created; | ||
|
||
public class ObjectService { | ||
|
||
private static final Logger logger = LoggerFactory | ||
.getLogger(ObjectService.class); | ||
|
||
@Inject | ||
private Repository repo; | ||
|
||
private JcrTools jcrTools = new JcrTools(); | ||
|
||
public Node createObjectNode(Session session, String path) throws RepositoryException { | ||
final Node obj = jcrTools.findOrCreateNode(session, path, "nt:folder"); | ||
obj.addMixin("fedora:object"); | ||
obj.addMixin("fedora:owned"); | ||
obj.setProperty("fedora:ownerId", session.getUserID()); | ||
obj.setProperty("jcr:lastModified", Calendar.getInstance()); | ||
obj.setProperty("dc:identifier", new String[]{obj.getIdentifier(), obj.getName()}); | ||
|
||
return obj; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters