Skip to content

Commit

Permalink
Added FedoraDatastreams resource, changed schema to include datastrea…
Browse files Browse the repository at this point in the history
…m records and links from object records to datastream records
  • Loading branch information
ajs6f committed Jan 26, 2013
1 parent f1492cd commit ff0b8d1
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 22 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Expand Up @@ -41,6 +41,11 @@
<version>${org.springframework.version}</version>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava-collections</artifactId>
<version>r03</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/org/fcrepo/lily/AbstractResource.java
Expand Up @@ -8,6 +8,8 @@
import org.lilyproject.client.LilyClient;
import org.lilyproject.repository.api.QName;
import org.lilyproject.repository.api.Repository;
import org.lilyproject.repository.impl.id.IdGeneratorImpl;
import org.lilyproject.repository.impl.id.UserRecordIdFactory;
import org.lilyproject.tools.import_.cli.JsonImport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -17,9 +19,15 @@ public abstract class AbstractResource {
static final String fedoraNamespace = "fedora";
static final QName fedoraObjectRecordTypeName = new QName(fedoraNamespace,
"object");
static final QName fedoraDatastreamRecordTypeName = new QName(fedoraNamespace,
"datastream");
static final QName label = new QName(fedoraNamespace, "label");
static final QName datastreams = new QName(fedoraNamespace, "datastreams");
static final QName datastreamId = new QName(fedoraNamespace, "datastreamId");

static public Repository repo = null;
static public Repository repo;
static public IdGeneratorImpl idGenerator;
UserRecordIdFactory userRecordIdFactory;

@Resource
public LilyClient lilyClient = null;
Expand All @@ -41,6 +49,10 @@ public abstract class AbstractResource {
void loadSchema() throws IOException, Exception {
logger.debug("Loading Fedora schema");
JsonImport.load(getRepo(), schemaResource.getInputStream(), true);
repo = getRepo();
idGenerator = (IdGeneratorImpl) repo.getIdGenerator();
userRecordIdFactory = new UserRecordIdFactory();

}

public Repository getRepo() {
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/org/fcrepo/lily/FedoraDatastreams.java
@@ -0,0 +1,57 @@
package org.fcrepo.lily;

import java.io.InputStream;
import java.net.URI;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

import org.lilyproject.repository.api.Link;
import org.lilyproject.repository.api.Record;
import org.lilyproject.repository.api.RecordId;
import org.lilyproject.repository.api.RepositoryException;

import com.google.common.collect.ImmutableSet;

@Path("/objects/{pid}/datastreams")
public class FedoraDatastreams extends AbstractResource {

@Path("/")
@GET
public Response getDatastreams(@PathParam("pid") final String pid)
throws RepositoryException, InterruptedException {
RecordId pidRecId = userRecordIdFactory.fromString(pid, idGenerator);
Record rec = repo.read(pidRecId, label, datastreams);
List dses = (List) rec.getField(datastreams);
ImmutableSet.Builder dsset = new ImmutableSet.Builder();
for (final Object ds : dses) {
dsset.add(ds);
}
return Response.ok(dsset.build().toString()).build();
}

@Path("/{dsid}")
@POST
public Response addDatastream(@PathParam("pid") final String pid,
@PathParam("dsid") final String dsid, InputStream content)
throws RepositoryException, InterruptedException {
// create datastream record
Record dsRec = repo.recordBuilder()
.recordType(fedoraDatastreamRecordTypeName)
.id(pid + "/" + dsid).field(label, dsid)
.field(datastreamId, dsid).createOrUpdate();
// add link to object
RecordId pidRecId = userRecordIdFactory.fromString(pid, idGenerator);
Record objRec = repo.read(pidRecId, label, datastreams);
List<Link> dses = (List<Link>) objRec.getField(datastreams);
dses.add(new Link(dsRec.getId()));
objRec.setField(datastreams, dses);
repo.createOrUpdate(objRec);
return Response.created(URI.create(dsid)).build();
}

}
11 changes: 5 additions & 6 deletions src/main/java/org/fcrepo/lily/FedoraObjects.java
@@ -1,6 +1,7 @@
package org.fcrepo.lily;

import java.net.URI;
import java.util.ArrayList;

import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
Expand All @@ -10,19 +11,16 @@
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

import org.lilyproject.repository.api.Link;
import org.lilyproject.repository.api.Record;
import org.lilyproject.repository.api.RecordId;
import org.lilyproject.repository.api.Repository;
import org.lilyproject.repository.api.RepositoryException;
import org.lilyproject.repository.impl.id.IdGeneratorImpl;
import org.lilyproject.repository.impl.id.UserRecordIdFactory;

@Path("/objects")
public class FedoraObjects extends AbstractResource {

Repository repo = getRepo();
IdGeneratorImpl idGenerator = (IdGeneratorImpl) repo.getIdGenerator();
UserRecordIdFactory userRecordIdFactory = new UserRecordIdFactory();

@Path("/{pid}")
@GET
Expand All @@ -39,8 +37,9 @@ public Response addObject(@PathParam("pid") final String pid,
@QueryParam("label") @DefaultValue("test") final String objLabel)
throws RepositoryException, InterruptedException {

repo.recordBuilder().recordType(fedoraObjectRecordTypeName).id(pid).field(label, objLabel)
.createOrUpdate();
repo.recordBuilder().recordType(fedoraObjectRecordTypeName).id(pid)
.field(datastreams, new ArrayList<Link>())
.field(label, objLabel).createOrUpdate();
return Response.created(URI.create(pid)).build();
}
}
41 changes: 32 additions & 9 deletions src/main/resources/org/fcrepo/lily/fedoraSchema.json
@@ -1,21 +1,44 @@
/* First sketch at modeling Fedora in Lily */

{
namespaces: {
"namespaces": {
"fedora": "fedora"
},
fieldTypes: [
"fieldTypes": [
{
name: "fedora$label",
valueType: "STRING",
scope: "non_versioned"
"name": "fedora$label",
"valueType": "STRING",
"scope": "non_versioned"
},
{
"name": "fedora$datastreams",
"valueType": "LIST<LINK>",
"scope": "non_versioned"
},
{
"name": "fedora$datastreamId",
"valueType": "STRING",
"scope": "non_versioned"
},
{
"name": "fedora$content",
"valueType": "BLOB",
"scope": "non_versioned"
}
],
recordTypes: [
"recordTypes": [
{
"name": "fedora$object",
"fields": [
{"name": "fedora$label", "mandatory": true },
{"name": "fedora$datastreams"}
]
},
{
name: "fedora$object",
fields: [
{name: "fedora$label", mandatory: true }
"name": "fedora$datastream",
"fields": [
{"name": "fedora$datastreamId", "mandatory": true },
{"name": "fedora$content"}
]
}
]
Expand Down
12 changes: 6 additions & 6 deletions src/main/webapp/WEB-INF/rest.xml
Expand Up @@ -15,21 +15,21 @@

<jaxrs:server address="/">
<jaxrs:serviceBeans>
<bean id="fedoraRepository" class="org.fcrepo.lily.FedoraRepository"/>
<bean id="fedoraObjects" class="org.fcrepo.lily.FedoraObjects"/>
<bean class="org.fcrepo.lily.FedoraRepository"/>
<bean class="org.fcrepo.lily.FedoraObjects"/>
<bean class="org.fcrepo.lily.FedoraDatastreams"/>
</jaxrs:serviceBeans>
</jaxrs:server>

<bean id="lilyClient" class="org.lilyproject.client.LilyClient">
<bean id="lilyClient" class="org.lilyproject.client.LilyClient" destroy-method="close">
<!-- Zookeeper connection string -->
<constructor-arg value="localhost:2181"/>
<!-- timeout -->
<constructor-arg value="20000"/>
</bean>

<bean name="fedoraSchema" class="org.springframework.core.io.ClassPathResource">
<constructor-arg type="String"
value="org/fcrepo/lily/fedoraSchema.json"/>
<constructor-arg type="String" value="org/fcrepo/lily/fedoraSchema.json"/>
</bean>

</beans>

0 comments on commit ff0b8d1

Please sign in to comment.