Skip to content

Commit

Permalink
Added getLabel() and setLabel() to Datastream
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Feb 21, 2013
1 parent 2e7fe72 commit efe1db1
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 14 deletions.
61 changes: 59 additions & 2 deletions fcrepo-kernel/src/main/java/org/fcrepo/Datastream.java
@@ -1,15 +1,29 @@

package org.fcrepo;

import static com.google.common.base.Joiner.on;
import static com.google.common.collect.Collections2.transform;
import static com.google.common.collect.ImmutableSet.copyOf;
import static org.fcrepo.utils.FedoraJcrTypes.DC_TITLE;
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
import static org.modeshape.jcr.api.JcrConstants.JCR_DATA;

import java.io.InputStream;
import java.util.Collection;

import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.jcr.ValueFormatException;
import javax.jcr.lock.LockException;
import javax.jcr.nodetype.ConstraintViolationException;
import javax.jcr.version.VersionException;

import org.modeshape.common.SystemFailureException;

import com.google.common.base.Function;

public class Datastream {

Expand All @@ -22,8 +36,9 @@ public Datastream(Node n) {
public Node getNode() {
return node;
}

public InputStream getContent() throws ValueFormatException, PathNotFoundException, RepositoryException {

public InputStream getContent() throws ValueFormatException,
PathNotFoundException, RepositoryException {
return node.getNode(JCR_CONTENT).getProperty(JCR_DATA).getBinary()
.getStream();
}
Expand All @@ -34,4 +49,46 @@ public String getMimeType() throws ValueFormatException,
"fedora:contentType").getString() : "application/octet-stream";
}

public String getLabel() throws PathNotFoundException, RepositoryException {
if (node.hasProperty(DC_TITLE)) {

Property labels = node.getProperty(DC_TITLE);
String label;
if (!labels.isMultiple())
label = node.getProperty(DC_TITLE).getString();
else {
label = on('/').join(map(labels.getValues(), value2string));
}
return label;
} else
return "";

}

public void setLabel(String label) throws ValueFormatException,
VersionException, LockException, ConstraintViolationException,
RepositoryException {
node.setProperty(DC_TITLE, label);
node.getSession().save();
}

private Function<Value, String> value2string =
new Function<Value, String>() {

@Override
public String apply(Value v) {
try {
return v.getString();
} catch (RepositoryException e) {
throw new SystemFailureException(e);
} catch (IllegalStateException e) {
throw new SystemFailureException(e);
}
}
};

private static <From, To> Collection<To> map(From[] input,
Function<From, To> f) {
return transform(copyOf(input), f);
}
}
Expand Up @@ -91,7 +91,13 @@ public static Node createDatastreamNode(final Session session,

public static Node getDatastreamNode(final String pid, final String dsId)
throws PathNotFoundException, RepositoryException {
return getObjectNode(pid).getNode(dsId);
logger.trace("Executing getDatastreamNode() with pid: " + pid +
" and dsId: " + dsId);
final Node objNode = getObjectNode(pid);
logger.trace("Retrieved object node: " + objNode.getName());
final Node dsNode = objNode.getNode(dsId);
logger.trace("Retrieved datastream node: " + dsNode.getName());
return dsNode;
}

public static Datastream getDatastream(final String pid, final String dsId)
Expand Down
Expand Up @@ -35,9 +35,10 @@ public static Node createObjectNodeByName(final Session session,
return new FedoraObject(session, "/objects/" + name).getNode();
}

public static Node getObjectNode(final String name)
public static Node getObjectNode(final String pid)
throws PathNotFoundException, RepositoryException {
return readOnlySession.getNode("/objects/" + name);
logger.trace("Executing getObjectNode() with pid: " + pid);
return readOnlySession.getNode("/objects/" + pid);
}

@PostConstruct
Expand Down
44 changes: 44 additions & 0 deletions fcrepo-kernel/src/test/java/org/fcrepo/DatastreamTest.java
@@ -0,0 +1,44 @@

package org.fcrepo;

import static org.fcrepo.services.DatastreamService.createDatastreamNode;
import static org.fcrepo.services.DatastreamService.getDatastream;
import static org.fcrepo.services.ObjectService.createObjectNodeByName;
import static org.junit.Assert.assertEquals;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.inject.Inject;
import javax.jcr.Node;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;

import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;

@ContextConfiguration({"/spring-test/repo.xml"})
public class DatastreamTest extends AbstractTest {

@Inject
Repository repo;

@Test
public void testLabel() throws RepositoryException, IOException {
Session session = repo.login();
createObjectNodeByName(session, "testObject");
Node dsNode =
createDatastreamNode(session,
"/objects/testObject/testDatastreamNode",
"application/octet-stream", new ByteArrayInputStream(
"asdf".getBytes()));
new Datastream(dsNode).setLabel("Best datastream ever!");
session.save();
session.logout();

session = repo.login();
final Datastream ds = getDatastream("testObject", "testDatastreamNode");
assertEquals("Wrong label!", "Best datastream ever!", ds.getLabel());
}
}
Expand Up @@ -31,10 +31,9 @@ public class DatastreamServiceTest extends AbstractTest {
@Test
public void testCreateDatastreamNode() throws Exception {
Session session = repository.login();
InputStream is = new ByteArrayInputStream("asdf".getBytes());

createDatastreamNode(session, "testDatastreamNode",
"application/octet-stream", is);
"application/octet-stream", new ByteArrayInputStream("asdf"
.getBytes()));
session.save();
session.logout();
session = repository.login();
Expand Down
Expand Up @@ -249,11 +249,13 @@ private URI addDatastreamNode(final String pid, final String dsPath,
@GET
@Path("/{dsid}")
@Produces({TEXT_XML, APPLICATION_JSON})
public Response getDatastream(@PathParam("pid")
public DatastreamProfile getDatastream(@PathParam("pid")
final String pid, @PathParam("dsid")
final String dsid) throws RepositoryException, IOException {

return ok(getDSProfile(getDatastreamNode(pid, dsid))).build();
final String dsId) throws RepositoryException, IOException {
logger.trace("Executing getDatastream() with dsId: " + dsId);
Node dsNode = getDatastreamNode(pid, dsId);
logger.debug("Retrieved dsNode: " + dsNode.getName());
return getDSProfile(dsNode);

}

Expand Down Expand Up @@ -355,12 +357,17 @@ public Response deleteDatastream(@PathParam("pid")

private DatastreamProfile getDSProfile(Node ds) throws RepositoryException,
IOException {
logger.trace("Executing getDSProfile() with node: " + ds.getName());
final DatastreamProfile dsProfile = new DatastreamProfile();
dsProfile.dsID = ds.getName();
dsProfile.pid = ds.getParent().getName();
dsProfile.dsLabel = ds.getName();
logger.trace("Retrieved datastream " + ds.getName() + "'s parent: " +
dsProfile.pid);
dsProfile.dsLabel = new Datastream(ds).getLabel();
logger.trace("Retrieved datastream " + ds.getName() + "'s label: " +
new Datastream(ds).getLabel());
dsProfile.dsState = A;
dsProfile.dsMIME = getDSMimeType(ds);
dsProfile.dsMIME = new Datastream(ds).getMimeType();
dsProfile.dsSize =
getNodePropertySize(ds) +
ds.getNode(JCR_CONTENT).getProperty(JCR_DATA)
Expand Down

0 comments on commit efe1db1

Please sign in to comment.