From 1b714205431009c7342257406f72afec4221c076 Mon Sep 17 00:00:00 2001 From: Richard Millet Date: Thu, 11 Feb 2010 03:57:18 +0000 Subject: [PATCH] CSPACE-834: Service consumer can retreive a list Intake records (if any) associated to a CollectionObject from the CollectionObject service. --- services/collectionobject/service/pom.xml | 20 +- .../CollectionObjectResource.java | 63 +- .../relation/RelationListItemJAXBSchema.java | 3 + .../common/relation/nuxeo/RelationsUtils.java | 14 +- .../common/storage/StorageClient.java | 15 + .../common/storage/jpa/JpaStorageClient.java | 64 ++ .../client/java/RepositoryJavaClient.java | 44 +- .../src/main/resources/relations_common.xsd | 2 + .../services/intake/IntakeResource.java | 30 + .../relation/NewRelationResource.java | 724 +++++++++++------- .../nuxeo/RelationDocumentModelHandler.java | 2 +- 11 files changed, 687 insertions(+), 294 deletions(-) diff --git a/services/collectionobject/service/pom.xml b/services/collectionobject/service/pom.xml index e4ddeb619..e0975c72b 100644 --- a/services/collectionobject/service/pom.xml +++ b/services/collectionobject/service/pom.xml @@ -25,6 +25,22 @@ org.collectionspace.services.collectionobject.jaxb 1.0 + + org.collectionspace.services + org.collectionspace.services.intake.jaxb + 1.0 + + + org.collectionspace.services + org.collectionspace.services.intake.service + 1.0 + + + org.collectionspace.services + org.collectionspace.services.relation.service + 1.0 + + org.slf4j slf4j-api @@ -113,7 +129,7 @@ - + diff --git a/services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/CollectionObjectResource.java b/services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/CollectionObjectResource.java index 44e9ae78a..189f6612c 100644 --- a/services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/CollectionObjectResource.java +++ b/services/collectionobject/service/src/main/java/org/collectionspace/services/collectionobject/CollectionObjectResource.java @@ -25,6 +25,11 @@ */ package org.collectionspace.services.collectionobject; +import java.util.List; +import java.util.ArrayList; +import java.util.Iterator; +import java.lang.reflect.Type; + import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.Path; @@ -51,19 +56,30 @@ import org.collectionspace.services.common.document.DocumentNotFoundException; import org.collectionspace.services.common.document.DocumentHandler; import org.collectionspace.services.common.document.DocumentFilter; import org.collectionspace.services.common.security.UnauthorizedException; +import org.jboss.resteasy.plugins.providers.multipart.InputPart; import org.jboss.resteasy.plugins.providers.multipart.MultipartInput; import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput; +import org.jboss.resteasy.plugins.providers.multipart.OutputPart; import org.jboss.resteasy.util.HttpResponseCodes; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.collectionspace.services.intake.IntakesCommonList; +import org.collectionspace.services.intake.IntakeResource; + +import org.collectionspace.services.relation.NewRelationResource; +import org.collectionspace.services.relation.RelationshipType; +import org.collectionspace.services.relation.RelationsCommonList; +import org.collectionspace.services.relation.RelationsCommon; + + @Path("/collectionobjects") @Consumes("multipart/mixed") @Produces("multipart/mixed") public class CollectionObjectResource extends AbstractCollectionSpaceResource { - final private String serviceName = "collectionobjects"; + static final public String serviceName = "collectionobjects"; final Logger logger = LoggerFactory.getLogger(CollectionObjectResource.class); @Override @@ -276,6 +292,50 @@ public class CollectionObjectResource } + @GET + @Path("{csid}/intakes") + @Produces("application/xml") + public IntakesCommonList getIntakesCommonList(@Context UriInfo ui, + @PathParam("csid") String csid) { + IntakesCommonList result = null; + + try { + // + // Find all the intake-related relation records. + // + String subjectCsid = csid; + String predicate = RelationshipType.COLLECTIONOBJECT_INTAKE.value(); + String objectCsid = null; + NewRelationResource relationResource = new NewRelationResource(); + RelationsCommonList relationsCommonList = relationResource.getRelationList(subjectCsid, predicate, objectCsid); + + // + // Create an array of Intake csid's + // + List relationsListItems = relationsCommonList.getRelationListItem(); + List intakeCsidList = new ArrayList(); + for (RelationsCommonList.RelationListItem relationsListItem : relationsListItems) { + intakeCsidList.add(relationsListItem.getObjectCsid()); + } + + // + // Get a response list for the Intake records from the Intake resource + // + IntakeResource intakeResource = new IntakeResource(); + result = intakeResource.getIntakeList(intakeCsidList); + } catch (Exception e) { + if (logger.isDebugEnabled()) { + logger.debug("Caught exception in getIntakeList", e); + } + Response response = Response.status( + Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build(); + throw new WebApplicationException(response); + } + + return result; + } + + //FIXME: Replace this "search" resource with a keyword "kw" query parameter @GET @Path("/search") @Produces("application/xml") @@ -314,4 +374,5 @@ public class CollectionObjectResource } return collectionObjectList; } + } diff --git a/services/common/src/main/java/org/collectionspace/services/common/relation/RelationListItemJAXBSchema.java b/services/common/src/main/java/org/collectionspace/services/common/relation/RelationListItemJAXBSchema.java index 733ae2d5b..db7fa908b 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/relation/RelationListItemJAXBSchema.java +++ b/services/common/src/main/java/org/collectionspace/services/common/relation/RelationListItemJAXBSchema.java @@ -37,6 +37,9 @@ public interface RelationListItemJAXBSchema { /** The Constant CSID. */ final static String CSID = "csid"; + final static String SUBJECT_CSID = "subjectCsid"; + final static String OBJECT_CSID = "objectCsid"; + /** The Constant URI. */ final static String URI = "url"; } diff --git a/services/common/src/main/java/org/collectionspace/services/common/relation/nuxeo/RelationsUtils.java b/services/common/src/main/java/org/collectionspace/services/common/relation/nuxeo/RelationsUtils.java index ebf47e26e..af2cabd6c 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/relation/nuxeo/RelationsUtils.java +++ b/services/common/src/main/java/org/collectionspace/services/common/relation/nuxeo/RelationsUtils.java @@ -26,9 +26,12 @@ package org.collectionspace.services.common.relation.nuxeo; import java.util.Iterator; import java.util.List; +import org.collectionspace.services.common.relation.RelationListItemJAXBSchema; import org.collectionspace.services.common.relation.RelationJAXBSchema; import org.collectionspace.services.common.document.DocumentException; import org.collectionspace.services.common.document.DocumentWrapper; +import org.collectionspace.services.common.context.ServiceContext; + import org.collectionspace.services.nuxeo.util.NuxeoUtils; import org.collectionspace.services.relation.RelationsCommonList; import org.collectionspace.services.relation.RelationsCommonList.RelationListItem; @@ -47,7 +50,7 @@ public class RelationsUtils { private static final Logger logger = LoggerFactory.getLogger(RelationsUtils.class); - public static RelationsCommonList extractCommonPartList(DocumentWrapper wrapDoc, + public static RelationsCommonList extractCommonPartList(ServiceContext ctx, DocumentWrapper wrapDoc, String serviceContextPath) throws Exception { DocumentModelList docList = (DocumentModelList) wrapDoc.getWrappedObject(); @@ -60,18 +63,23 @@ public class RelationsUtils { Iterator iter = docList.iterator(); while (iter.hasNext()) { DocumentModel docModel = iter.next(); - RelationListItem relationListItem = getRelationListItem(docModel, + RelationListItem relationListItem = getRelationListItem(ctx, docModel, serviceContextPath); list.add(relationListItem); } return relList; } - public static RelationListItem getRelationListItem(DocumentModel docModel, + public static RelationListItem getRelationListItem(ServiceContext ctx, DocumentModel docModel, String serviceContextPath) throws Exception { RelationListItem relationListItem = new RelationListItem(); String id = NuxeoUtils.extractId(docModel.getPathAsString()); relationListItem.setCsid(id); + relationListItem.setSubjectCsid((String) docModel.getProperty(ctx.getCommonPartLabel(), + RelationJAXBSchema.DOCUMENT_ID_1)); + relationListItem.setObjectCsid((String) docModel.getProperty(ctx.getCommonPartLabel(), + RelationJAXBSchema.DOCUMENT_ID_2)); + relationListItem.setUri(serviceContextPath + id); return relationListItem; } diff --git a/services/common/src/main/java/org/collectionspace/services/common/storage/StorageClient.java b/services/common/src/main/java/org/collectionspace/services/common/storage/StorageClient.java index 63474c34d..3e34f29c8 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/storage/StorageClient.java +++ b/services/common/src/main/java/org/collectionspace/services/common/storage/StorageClient.java @@ -17,6 +17,8 @@ */ package org.collectionspace.services.common.storage; +import java.util.List; + import org.collectionspace.services.common.context.ServiceContext; import org.collectionspace.services.common.document.BadRequestException; import org.collectionspace.services.common.document.DocumentException; @@ -58,6 +60,19 @@ public interface StorageClient { */ void get(ServiceContext ctx, String id, DocumentHandler handler) throws DocumentNotFoundException, DocumentException; + /** + * Gets the. + * + * @param ctx the ctx + * @param csidList the csid list + * @param handler the handler + * + * @throws DocumentNotFoundException the document not found exception + * @throws DocumentException the document exception + */ + void get(ServiceContext ctx, List csidList, DocumentHandler handler) + throws DocumentNotFoundException, DocumentException; + /** * getAll get all entitys for an entity service from the persistence store * @param ctx service context under which this method is invoked diff --git a/services/common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaStorageClient.java b/services/common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaStorageClient.java index 9e71d811c..7e8495f4d 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaStorageClient.java +++ b/services/common/src/main/java/org/collectionspace/services/common/storage/jpa/JpaStorageClient.java @@ -48,12 +48,21 @@ import org.slf4j.LoggerFactory; */ public class JpaStorageClient implements StorageClient { + /** The logger. */ private final Logger logger = LoggerFactory.getLogger(JpaStorageClient.class); + + /** The Constant CS_PERSISTENCE_UNIT. */ protected final static String CS_PERSISTENCE_UNIT = "org.collectionspace.services"; + /** + * Instantiates a new jpa storage client. + */ public JpaStorageClient() { } + /* (non-Javadoc) + * @see org.collectionspace.services.common.storage.StorageClient#create(org.collectionspace.services.common.context.ServiceContext, org.collectionspace.services.common.document.DocumentHandler) + */ @Override public String create(ServiceContext ctx, DocumentHandler handler) throws BadRequestException, @@ -100,6 +109,17 @@ public class JpaStorageClient implements StorageClient { } + /* (non-Javadoc) + * @see org.collectionspace.services.common.storage.StorageClient#get(org.collectionspace.services.common.context.ServiceContext, java.util.List, org.collectionspace.services.common.document.DocumentHandler) + */ + public void get(ServiceContext ctx, List csidList, DocumentHandler handler) + throws DocumentNotFoundException, DocumentException { + throw new UnsupportedOperationException(); + } + + /* (non-Javadoc) + * @see org.collectionspace.services.common.storage.StorageClient#get(org.collectionspace.services.common.context.ServiceContext, java.lang.String, org.collectionspace.services.common.document.DocumentHandler) + */ @Override public void get(ServiceContext ctx, String id, DocumentHandler handler) throws DocumentNotFoundException, DocumentException { @@ -173,12 +193,18 @@ public class JpaStorageClient implements StorageClient { } } + /* (non-Javadoc) + * @see org.collectionspace.services.common.storage.StorageClient#getAll(org.collectionspace.services.common.context.ServiceContext, org.collectionspace.services.common.document.DocumentHandler) + */ @Override public void getAll(ServiceContext ctx, DocumentHandler handler) throws DocumentNotFoundException, DocumentException { throw new UnsupportedOperationException("use getFiltered instead"); } + /* (non-Javadoc) + * @see org.collectionspace.services.common.storage.StorageClient#getFiltered(org.collectionspace.services.common.context.ServiceContext, org.collectionspace.services.common.document.DocumentHandler) + */ @Override public void getFiltered(ServiceContext ctx, DocumentHandler handler) throws DocumentNotFoundException, DocumentException { @@ -243,6 +269,9 @@ public class JpaStorageClient implements StorageClient { } } + /* (non-Javadoc) + * @see org.collectionspace.services.common.storage.StorageClient#update(org.collectionspace.services.common.context.ServiceContext, java.lang.String, org.collectionspace.services.common.document.DocumentHandler) + */ @Override public void update(ServiceContext ctx, String id, DocumentHandler handler) throws BadRequestException, DocumentNotFoundException, @@ -293,6 +322,9 @@ public class JpaStorageClient implements StorageClient { } } + /* (non-Javadoc) + * @see org.collectionspace.services.common.storage.StorageClient#delete(org.collectionspace.services.common.context.ServiceContext, java.lang.String) + */ @Override public void delete(ServiceContext ctx, String id) throws DocumentNotFoundException, @@ -349,16 +381,33 @@ public class JpaStorageClient implements StorageClient { } } + /** + * Gets the entity manager factory. + * + * @return the entity manager factory + */ protected EntityManagerFactory getEntityManagerFactory() { return getEntityManagerFactory(CS_PERSISTENCE_UNIT); } + /** + * Gets the entity manager factory. + * + * @param persistenceUnit the persistence unit + * + * @return the entity manager factory + */ protected EntityManagerFactory getEntityManagerFactory( String persistenceUnit) { return Persistence.createEntityManagerFactory(persistenceUnit); } + /** + * Release entity manager factory. + * + * @param emf the emf + */ protected void releaseEntityManagerFactory(EntityManagerFactory emf) { if (emf != null) { emf.close(); @@ -428,6 +477,14 @@ public class JpaStorageClient implements StorageClient { return r; } + /** + * Sets the csid. + * + * @param o the o + * @param csid the csid + * + * @throws Exception the exception + */ protected void setCsid(Object o, String csid) throws Exception { //verify csid String id = (String) getValue(o, "getCsid"); @@ -446,6 +503,13 @@ public class JpaStorageClient implements StorageClient { setValue(o, "setCsid", java.lang.String.class, csid); } + /** + * Gets the entity name. + * + * @param ctx the ctx + * + * @return the entity name + */ protected String getEntityName(ServiceContext ctx) { Object o = ctx.getProperty("entity-name"); if (o == null) { diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClient.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClient.java index 27f475394..01f9ab81d 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClient.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClient.java @@ -18,6 +18,7 @@ package org.collectionspace.services.nuxeo.client.java; import java.util.UUID; +import java.util.List; import org.collectionspace.services.common.context.ServiceContext; import org.collectionspace.services.common.document.BadRequestException; @@ -34,6 +35,7 @@ import org.nuxeo.common.utils.IdUtils; import org.nuxeo.ecm.core.api.ClientException; import org.nuxeo.ecm.core.api.DocumentModel; import org.nuxeo.ecm.core.api.DocumentModelList; +import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl; import org.nuxeo.ecm.core.api.DocumentRef; import org.nuxeo.ecm.core.api.IdRef; import org.nuxeo.ecm.core.api.PathRef; @@ -121,7 +123,7 @@ public class RepositoryJavaClient implements RepositoryClient { } } - + /** * get document from the Nuxeo repository * @param ctx service context under which this method is invoked @@ -175,6 +177,46 @@ public class RepositoryJavaClient implements RepositoryClient { } } + @Override + public void get(ServiceContext ctx, List csidList, DocumentHandler handler) + throws DocumentNotFoundException, DocumentException { + if (handler == null) { + throw new IllegalArgumentException( + "RepositoryJavaClient.getAll: handler is missing"); + } + + RepositoryInstance repoSession = null; + + try { + handler.prepare(Action.GET_ALL); + repoSession = getRepositorySession(); + DocumentModelList docModelList = new DocumentModelListImpl(); + //FIXME: Should be using NuxeoUtils.createPathRef for security reasons + for (String csid : csidList) { + DocumentRef docRef = NuxeoUtils.createPathRef(ctx, csid); + DocumentModel docModel = repoSession.getDocument(docRef); + docModelList.add(docModel); + } + + //set reposession to handle the document + ((DocumentModelHandler) handler).setRepositorySession(repoSession); + DocumentWrapper wrapDoc = new DocumentWrapperImpl(docModelList); + handler.handle(Action.GET_ALL, wrapDoc); + handler.complete(Action.GET_ALL, wrapDoc); + } catch (DocumentException de) { + throw de; + } catch (Exception e) { + if (logger.isDebugEnabled()) { + logger.debug("Caught exception ", e); + } + throw new DocumentException(e); + } finally { + if (repoSession != null) { + releaseRepositorySession(repoSession); + } + } + } + /** * getAll get all documents for an entity entity service from the Nuxeo * repository diff --git a/services/common/src/main/resources/relations_common.xsd b/services/common/src/main/resources/relations_common.xsd index d0415d480..9ab41ebc6 100644 --- a/services/common/src/main/resources/relations_common.xsd +++ b/services/common/src/main/resources/relations_common.xsd @@ -55,6 +55,8 @@ + + diff --git a/services/intake/service/src/main/java/org/collectionspace/services/intake/IntakeResource.java b/services/intake/service/src/main/java/org/collectionspace/services/intake/IntakeResource.java index d437fb002..40714aae8 100644 --- a/services/intake/service/src/main/java/org/collectionspace/services/intake/IntakeResource.java +++ b/services/intake/service/src/main/java/org/collectionspace/services/intake/IntakeResource.java @@ -23,6 +23,7 @@ */ package org.collectionspace.services.intake; +import java.util.List; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.Path; @@ -193,6 +194,35 @@ public class IntakeResource extends AbstractCollectionSpaceResource { return intakeObjectList; } + /** + * Gets the intake list. + * + * @param csidList the csid list + * + * @return the intake list + */ + public IntakesCommonList getIntakeList(List csidList) { + IntakesCommonList intakeObjectList = new IntakesCommonList(); + try { + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName()); + DocumentHandler handler = createDocumentHandler(ctx); + getRepositoryClient(ctx).get(ctx, csidList, handler); + intakeObjectList = (IntakesCommonList) handler.getCommonPartList(); + } catch (UnauthorizedException ue) { + Response response = Response.status( + Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build(); + throw new WebApplicationException(response); + } catch (Exception e) { + if (logger.isDebugEnabled()) { + logger.debug("Caught exception in getIntakeList", e); + } + Response response = Response.status( + Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build(); + throw new WebApplicationException(response); + } + return intakeObjectList; + } + @PUT @Path("{csid}") public MultipartOutput updateIntake( diff --git a/services/relation/service/src/main/java/org/collectionspace/services/relation/NewRelationResource.java b/services/relation/service/src/main/java/org/collectionspace/services/relation/NewRelationResource.java index 6763ff0bb..1dc04fc0d 100644 --- a/services/relation/service/src/main/java/org/collectionspace/services/relation/NewRelationResource.java +++ b/services/relation/service/src/main/java/org/collectionspace/services/relation/NewRelationResource.java @@ -42,7 +42,6 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.UriBuilder; import javax.ws.rs.core.UriInfo; - import org.collectionspace.services.common.AbstractCollectionSpaceResource; import org.collectionspace.services.common.context.MultipartServiceContext; import org.collectionspace.services.common.context.MultipartServiceContextFactory; @@ -57,315 +56,468 @@ import org.jboss.resteasy.util.HttpResponseCodes; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +/** + * The Class NewRelationResource. + */ @Path("/relations") @Consumes("multipart/mixed") @Produces("multipart/mixed") public class NewRelationResource extends AbstractCollectionSpaceResource { - public final static String serviceName = "relations"; - final Logger logger = LoggerFactory.getLogger(NewRelationResource.class); + /** The Constant serviceName. */ + public final static String serviceName = "relations"; + + /** The logger. */ + final Logger logger = LoggerFactory.getLogger(NewRelationResource.class); + + /* (non-Javadoc) + * @see org.collectionspace.services.common.AbstractCollectionSpaceResource#getVersionString() + */ + @Override + protected String getVersionString() { + /** The last change revision. */ + final String lastChangeRevision = "$LastChangedRevision$"; + return lastChangeRevision; + } - @Override - protected String getVersionString() { - /** The last change revision. */ - final String lastChangeRevision = "$LastChangedRevision$"; - return lastChangeRevision; - } - - @Override - public String getServiceName() { - return serviceName; - } + /* (non-Javadoc) + * @see org.collectionspace.services.common.AbstractCollectionSpaceResource#getServiceName() + */ + @Override + public String getServiceName() { + return serviceName; + } - @Override - public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception { - DocumentHandler docHandler = ctx.getDocumentHandler(); - if (ctx.getInput() != null) { - Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), RelationsCommon.class); - if (obj != null) { - docHandler.setCommonPart((RelationsCommon) obj); - } - } - return docHandler; - } + /* (non-Javadoc) + * @see org.collectionspace.services.common.AbstractCollectionSpaceResource#createDocumentHandler(org.collectionspace.services.common.context.ServiceContext) + */ + @Override + public DocumentHandler createDocumentHandler(ServiceContext ctx) + throws Exception { + DocumentHandler docHandler = ctx.getDocumentHandler(); + if (ctx.getInput() != null) { + Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx + .getCommonPartLabel(), RelationsCommon.class); + if (obj != null) { + docHandler.setCommonPart((RelationsCommon) obj); + } + } + return docHandler; + } - @POST - public Response createRelation(MultipartInput input) { + /** + * Creates the relation. + * + * @param input the input + * + * @return the response + */ + @POST + public Response createRelation(MultipartInput input) { - try { - ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName()); - DocumentHandler handler = createDocumentHandler(ctx); - String csid = getRepositoryClient(ctx).create(ctx, handler); - UriBuilder path = UriBuilder.fromResource(NewRelationResource.class); - path.path("" + csid); - Response response = Response.created(path.build()).build(); - return response; - } catch (UnauthorizedException ue) { - Response response = Response.status( - Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build(); - throw new WebApplicationException(response); - } catch (Exception e) { - if (logger.isDebugEnabled()) { - logger.debug("Caught exception in createRelation", e); - } - Response response = Response.status( - Response.Status.INTERNAL_SERVER_ERROR).entity( - "Create failed").type("text/plain").build(); - throw new WebApplicationException(response); - } - } + try { + ServiceContext ctx = MultipartServiceContextFactory.get() + .createServiceContext(input, getServiceName()); + DocumentHandler handler = createDocumentHandler(ctx); + String csid = getRepositoryClient(ctx).create(ctx, handler); + UriBuilder path = UriBuilder + .fromResource(NewRelationResource.class); + path.path("" + csid); + Response response = Response.created(path.build()).build(); + return response; + } catch (UnauthorizedException ue) { + Response response = Response.status(Response.Status.UNAUTHORIZED) + .entity("Create failed reason " + ue.getErrorReason()) + .type("text/plain").build(); + throw new WebApplicationException(response); + } catch (Exception e) { + if (logger.isDebugEnabled()) { + logger.debug("Caught exception in createRelation", e); + } + Response response = Response.status( + Response.Status.INTERNAL_SERVER_ERROR).entity( + "Create failed").type("text/plain").build(); + throw new WebApplicationException(response); + } + } + + /** + * Gets the relation. + * + * @param csid the csid + * + * @return the relation + */ + @GET + @Path("{csid}") + public MultipartOutput getRelation(@PathParam("csid") String csid) { + if (logger.isDebugEnabled()) { + logger.debug("getRelation with csid=" + csid); + } + if (csid == null || "".equals(csid)) { + logger.error("getRelation: missing csid!"); + Response response = Response.status(Response.Status.BAD_REQUEST) + .entity("get failed on Relation csid=" + csid).type( + "text/plain").build(); + throw new WebApplicationException(response); + } + MultipartOutput result = null; + try { + ServiceContext ctx = MultipartServiceContextFactory.get() + .createServiceContext(null, getServiceName()); + DocumentHandler handler = createDocumentHandler(ctx); + getRepositoryClient(ctx).get(ctx, csid, handler); + result = (MultipartOutput) ctx.getOutput(); + } catch (UnauthorizedException ue) { + Response response = Response.status(Response.Status.UNAUTHORIZED) + .entity("Get failed reason " + ue.getErrorReason()).type( + "text/plain").build(); + throw new WebApplicationException(response); + } catch (DocumentNotFoundException dnfe) { + if (logger.isDebugEnabled()) { + logger.debug("getRelation", dnfe); + } + Response response = Response.status(Response.Status.NOT_FOUND) + .entity("Get failed on Relation csid=" + csid).type( + "text/plain").build(); + throw new WebApplicationException(response); + } catch (Exception e) { + if (logger.isDebugEnabled()) { + logger.debug("getRelation", e); + } + Response response = Response.status( + Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed") + .type("text/plain").build(); + throw new WebApplicationException(response); + } - @GET - @Path("{csid}") - public MultipartOutput getRelation(@PathParam("csid") String csid) { - if (logger.isDebugEnabled()) { - logger.debug("getRelation with csid=" + csid); - } - if (csid == null || "".equals(csid)) { - logger.error("getRelation: missing csid!"); - Response response = Response.status(Response.Status.BAD_REQUEST).entity("get failed on Relation csid=" + csid).type( - "text/plain").build(); - throw new WebApplicationException(response); - } - MultipartOutput result = null; - try { - ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName()); - DocumentHandler handler = createDocumentHandler(ctx); - getRepositoryClient(ctx).get(ctx, csid, handler); - result = (MultipartOutput) ctx.getOutput(); - } catch (UnauthorizedException ue) { - Response response = Response.status( - Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build(); - throw new WebApplicationException(response); - } catch (DocumentNotFoundException dnfe) { - if (logger.isDebugEnabled()) { - logger.debug("getRelation", dnfe); - } - Response response = Response.status(Response.Status.NOT_FOUND).entity("Get failed on Relation csid=" + csid).type( - "text/plain").build(); - throw new WebApplicationException(response); - } catch (Exception e) { - if (logger.isDebugEnabled()) { - logger.debug("getRelation", e); - } - Response response = Response.status( - Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build(); - throw new WebApplicationException(response); - } + if (result == null) { + Response response = Response.status(Response.Status.NOT_FOUND) + .entity( + "Get failed, the requested Relation CSID:" + csid + + ": was not found.").type("text/plain") + .build(); + throw new WebApplicationException(response); + } + return result; + } - if (result == null) { - Response response = Response.status(Response.Status.NOT_FOUND).entity( - "Get failed, the requested Relation CSID:" + csid + ": was not found.").type("text/plain").build(); - throw new WebApplicationException(response); - } - return result; - } + /* + * BEGIN OF GET LIST + */ + /** + * Gets the relation list. + * + * @param ui the ui + * + * @return the relation list + */ + @GET + @Produces("application/xml") + public RelationsCommonList getRelationList(@Context UriInfo ui) { + return this.getRelationList(null, null, null); + } - /* - * BEGIN OF GET LIST - */ - @GET - @Produces("application/xml") - public RelationsCommonList getRelationList(@Context UriInfo ui) { - return this.getRelationList(null, null, null); - } + /** + * Gets the relation list_ s. + * + * @param ui the ui + * @param subjectCsid the subject csid + * + * @return the relation list_ s + */ + @GET + @Path("subject/{subjectCsid}") + @Produces("application/xml") + public RelationsCommonList getRelationList_S(@Context UriInfo ui, + @PathParam("subjectCsid") String subjectCsid) { + return this.getRelationList(subjectCsid, null, null); + } - @GET - @Path("subject/{subjectCsid}") - @Produces("application/xml") - public RelationsCommonList getRelationList_S(@Context UriInfo ui, - @PathParam("subjectCsid") String subjectCsid) { - return this.getRelationList(subjectCsid, null, null); - } + /** + * Gets the relation list_ p. + * + * @param ui the ui + * @param predicate the predicate + * + * @return the relation list_ p + */ + @GET + @Path("type/{predicate}") + @Produces("application/xml") + public RelationsCommonList getRelationList_P(@Context UriInfo ui, + @PathParam("predicate") String predicate) { + return this.getRelationList(null, predicate, null); + } - @GET - @Path("type/{predicate}") - @Produces("application/xml") - public RelationsCommonList getRelationList_P(@Context UriInfo ui, - @PathParam("predicate") String predicate) { - return this.getRelationList(null, predicate, null); - } + /** + * Gets the relation list_ o. + * + * @param ui the ui + * @param objectCsid the object csid + * + * @return the relation list_ o + */ + @GET + @Path("object/{objectCsid}") + @Produces("application/xml") + public RelationsCommonList getRelationList_O(@Context UriInfo ui, + @PathParam("objectCsid") String objectCsid) { + return this.getRelationList(null, null, objectCsid); + } - @GET - @Path("object/{objectCsid}") - @Produces("application/xml") - public RelationsCommonList getRelationList_O(@Context UriInfo ui, - @PathParam("objectCsid") String objectCsid) { - return this.getRelationList(null, null, objectCsid); - } + /** + * Gets the relation list_ ps. + * + * @param ui the ui + * @param predicate the predicate + * @param subjectCsid the subject csid + * + * @return the relation list_ ps + */ + @GET + @Path("type/{predicate}/subject/{subjectCsid}") + @Produces("application/xml") + public RelationsCommonList getRelationList_PS(@Context UriInfo ui, + @PathParam("predicate") String predicate, + @PathParam("subjectCsid") String subjectCsid) { + return this.getRelationList(subjectCsid, predicate, null); + } - @GET - @Path("type/{predicate}/subject/{subjectCsid}") - @Produces("application/xml") - public RelationsCommonList getRelationList_PS(@Context UriInfo ui, - @PathParam("predicate") String predicate, - @PathParam("subjectCsid") String subjectCsid) { - return this.getRelationList(subjectCsid, predicate, null); - } + /** + * Gets the relation list_ sp. + * + * @param ui the ui + * @param subjectCsid the subject csid + * @param predicate the predicate + * + * @return the relation list_ sp + */ + @GET + @Path("subject/{subjectCsid}/type/{predicate}") + @Produces("application/xml") + public RelationsCommonList getRelationList_SP(@Context UriInfo ui, + @PathParam("subjectCsid") String subjectCsid, + @PathParam("predicate") String predicate) { + return this.getRelationList(subjectCsid, predicate, null); + } - @GET - @Path("subject/{subjectCsid}/type/{predicate}") - @Produces("application/xml") - public RelationsCommonList getRelationList_SP(@Context UriInfo ui, - @PathParam("subjectCsid") String subjectCsid, - @PathParam("predicate") String predicate) { - return this.getRelationList(subjectCsid, predicate, null); - } + /** + * Gets the relation list_ po. + * + * @param ui the ui + * @param predicate the predicate + * @param objectCsid the object csid + * + * @return the relation list_ po + */ + @GET + @Path("type/{predicate}/object/{objectCsid}") + @Produces("application/xml") + public RelationsCommonList getRelationList_PO(@Context UriInfo ui, + @PathParam("predicate") String predicate, + @PathParam("objectCsid") String objectCsid) { + return this.getRelationList(null, predicate, objectCsid); + } - @GET - @Path("type/{predicate}/object/{objectCsid}") - @Produces("application/xml") - public RelationsCommonList getRelationList_PO(@Context UriInfo ui, - @PathParam("predicate") String predicate, - @PathParam("objectCsid") String objectCsid) { - return this.getRelationList(null, predicate, objectCsid); - } + /** + * Gets the relation list_ op. + * + * @param ui the ui + * @param objectCsid the object csid + * @param predicate the predicate + * + * @return the relation list_ op + */ + @GET + @Path("object/{objectCsid}/type/{predicate}") + @Produces("application/xml") + public RelationsCommonList getRelationList_OP(@Context UriInfo ui, + @PathParam("objectCsid") String objectCsid, + @PathParam("predicate") String predicate) { + return this.getRelationList(null, predicate, objectCsid); + } - @GET - @Path("object/{objectCsid}/type/{predicate}") - @Produces("application/xml") - public RelationsCommonList getRelationList_OP(@Context UriInfo ui, - @PathParam("objectCsid") String objectCsid, - @PathParam("predicate") String predicate) { - return this.getRelationList(null, predicate, objectCsid); - } + /** + * Gets the relation list_ pso. + * + * @param ui the ui + * @param predicate the predicate + * @param subjectCsid the subject csid + * @param objectCsid the object csid + * + * @return the relation list_ pso + */ + @GET + @Path("type/{predicate}/subject/{subjectCsid}/object/{objectCsid}") + @Produces("application/xml") + public RelationsCommonList getRelationList_PSO(@Context UriInfo ui, + @PathParam("predicate") String predicate, + @PathParam("subjectCsid") String subjectCsid, + @PathParam("objectCsid") String objectCsid) { + return this.getRelationList(subjectCsid, predicate, objectCsid); + } - @GET - @Path("type/{predicate}/subject/{subjectCsid}/object/{objectCsid}") - @Produces("application/xml") - public RelationsCommonList getRelationList_PSO(@Context UriInfo ui, - @PathParam("predicate") String predicate, - @PathParam("subjectCsid") String subjectCsid, - @PathParam("objectCsid") String objectCsid) { - return this.getRelationList(subjectCsid, predicate, objectCsid); - } + /** + * Gets the relation list_ spo. + * + * @param ui the ui + * @param subjectCsid the subject csid + * @param predicate the predicate + * @param objectCsid the object csid + * + * @return the relation list_ spo + */ + @GET + @Path("subject/{subjectCsid}/type/{predicate}/object/{objectCsid}") + @Produces("application/xml") + public RelationsCommonList getRelationList_SPO(@Context UriInfo ui, + @PathParam("subjectCsid") String subjectCsid, + @PathParam("predicate") String predicate, + @PathParam("objectCsid") String objectCsid) { + return this.getRelationList(subjectCsid, predicate, objectCsid); + } - @GET - @Path("subject/{subjectCsid}/type/{predicate}/object/{objectCsid}") - @Produces("application/xml") - public RelationsCommonList getRelationList_SPO(@Context UriInfo ui, - @PathParam("subjectCsid") String subjectCsid, - @PathParam("predicate") String predicate, - @PathParam("objectCsid") String objectCsid) { - return this.getRelationList(subjectCsid, predicate, objectCsid); - } - /* - * END OF GET LIST - */ + /* + * END OF GET LIST + */ - @PUT - @Path("{csid}") - public MultipartOutput updateRelation(@PathParam("csid") String csid, - MultipartInput theUpdate) { - if (logger.isDebugEnabled()) { - logger.debug("updateRelation with csid=" + csid); - } - if (csid == null || "".equals(csid)) { - logger.error("updateRelation: missing csid!"); - Response response = Response.status(Response.Status.BAD_REQUEST).entity("update failed on Relation csid=" + csid).type( - "text/plain").build(); - throw new WebApplicationException(response); - } - MultipartOutput result = null; - try { - ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName()); - DocumentHandler handler = createDocumentHandler(ctx); - getRepositoryClient(ctx).update(ctx, csid, handler); - result = (MultipartOutput) ctx.getOutput(); - } catch (UnauthorizedException ue) { - Response response = Response.status( - Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build(); - throw new WebApplicationException(response); - } catch (DocumentNotFoundException dnfe) { - if (logger.isDebugEnabled()) { - logger.debug("caugth exception in updateRelation", dnfe); - } - Response response = Response.status(Response.Status.NOT_FOUND).entity("Update failed on Relation csid=" + csid).type( - "text/plain").build(); - throw new WebApplicationException(response); - } catch (Exception e) { - Response response = Response.status( - Response.Status.INTERNAL_SERVER_ERROR).entity( - "Update failed").type("text/plain").build(); - throw new WebApplicationException(response); - } - return result; - } + /** + * Update relation. + * + * @param csid the csid + * @param theUpdate the the update + * + * @return the multipart output + */ + @PUT + @Path("{csid}") + public MultipartOutput updateRelation(@PathParam("csid") String csid, + MultipartInput theUpdate) { + if (logger.isDebugEnabled()) { + logger.debug("updateRelation with csid=" + csid); + } + if (csid == null || "".equals(csid)) { + logger.error("updateRelation: missing csid!"); + Response response = Response.status(Response.Status.BAD_REQUEST) + .entity("update failed on Relation csid=" + csid).type( + "text/plain").build(); + throw new WebApplicationException(response); + } + MultipartOutput result = null; + try { + ServiceContext ctx = MultipartServiceContextFactory.get() + .createServiceContext(theUpdate, getServiceName()); + DocumentHandler handler = createDocumentHandler(ctx); + getRepositoryClient(ctx).update(ctx, csid, handler); + result = (MultipartOutput) ctx.getOutput(); + } catch (UnauthorizedException ue) { + Response response = Response.status(Response.Status.UNAUTHORIZED) + .entity("Update failed reason " + ue.getErrorReason()) + .type("text/plain").build(); + throw new WebApplicationException(response); + } catch (DocumentNotFoundException dnfe) { + if (logger.isDebugEnabled()) { + logger.debug("caugth exception in updateRelation", dnfe); + } + Response response = Response.status(Response.Status.NOT_FOUND) + .entity("Update failed on Relation csid=" + csid).type( + "text/plain").build(); + throw new WebApplicationException(response); + } catch (Exception e) { + Response response = Response.status( + Response.Status.INTERNAL_SERVER_ERROR).entity( + "Update failed").type("text/plain").build(); + throw new WebApplicationException(response); + } + return result; + } - @DELETE - @Path("{csid}") - public Response deleteRelation(@PathParam("csid") String csid) { + /** + * Delete relation. + * + * @param csid the csid + * + * @return the response + */ + @DELETE + @Path("{csid}") + public Response deleteRelation(@PathParam("csid") String csid) { - if (logger.isDebugEnabled()) { - logger.debug("deleteRelation with csid=" + csid); - } - if (csid == null || "".equals(csid)) { - logger.error("deleteRelation: missing csid!"); - Response response = Response.status(Response.Status.BAD_REQUEST).entity("delete failed on Relation csid=" + csid).type( - "text/plain").build(); - throw new WebApplicationException(response); - } - try { - ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName()); - getRepositoryClient(ctx).delete(ctx, csid); - return Response.status(HttpResponseCodes.SC_OK).build(); - } catch (UnauthorizedException ue) { - Response response = Response.status( - Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build(); - throw new WebApplicationException(response); - } catch (DocumentNotFoundException dnfe) { - if (logger.isDebugEnabled()) { - logger.debug("caught exception in deleteRelation", dnfe); - } - Response response = Response.status(Response.Status.NOT_FOUND).entity("Delete failed on Relation csid=" + csid).type( - "text/plain").build(); - throw new WebApplicationException(response); - } catch (Exception e) { - Response response = Response.status( - Response.Status.INTERNAL_SERVER_ERROR).entity( - "Delete failed").type("text/plain").build(); - throw new WebApplicationException(response); - } + if (logger.isDebugEnabled()) { + logger.debug("deleteRelation with csid=" + csid); + } + if (csid == null || "".equals(csid)) { + logger.error("deleteRelation: missing csid!"); + Response response = Response.status(Response.Status.BAD_REQUEST) + .entity("delete failed on Relation csid=" + csid).type( + "text/plain").build(); + throw new WebApplicationException(response); + } + try { + ServiceContext ctx = MultipartServiceContextFactory.get() + .createServiceContext(null, getServiceName()); + getRepositoryClient(ctx).delete(ctx, csid); + return Response.status(HttpResponseCodes.SC_OK).build(); + } catch (UnauthorizedException ue) { + Response response = Response.status(Response.Status.UNAUTHORIZED) + .entity("Delete failed reason " + ue.getErrorReason()) + .type("text/plain").build(); + throw new WebApplicationException(response); + } catch (DocumentNotFoundException dnfe) { + if (logger.isDebugEnabled()) { + logger.debug("caught exception in deleteRelation", dnfe); + } + Response response = Response.status(Response.Status.NOT_FOUND) + .entity("Delete failed on Relation csid=" + csid).type( + "text/plain").build(); + throw new WebApplicationException(response); + } catch (Exception e) { + Response response = Response.status( + Response.Status.INTERNAL_SERVER_ERROR).entity( + "Delete failed").type("text/plain").build(); + throw new WebApplicationException(response); + } - } + } - /* - * Private Methods - */ - /** - * Gets the relation list request. - * - * @return the relation list request - * - * @throws WebApplicationException the web application exception - */ - private RelationsCommonList getRelationList(String subjectCsid, - String predicate, - String objectCsid) - throws WebApplicationException { - RelationsCommonList relationList = new RelationsCommonList(); - try { - ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName()); - DocumentHandler handler = createDocumentHandler(ctx); - Map propsFromPath = handler.getProperties(); - propsFromPath.put(IRelationsManager.SUBJECT, subjectCsid); - propsFromPath.put(IRelationsManager.PREDICATE, predicate); - propsFromPath.put(IRelationsManager.OBJECT, objectCsid); - getRepositoryClient(ctx).getAll(ctx, handler); - relationList = (RelationsCommonList) handler.getCommonPartList(); - } catch (UnauthorizedException ue) { - Response response = Response.status( - Response.Status.UNAUTHORIZED).entity("Get relations failed reason " + ue.getErrorReason()).type("text/plain").build(); - throw new WebApplicationException(response); - } catch (Exception e) { - if (logger.isDebugEnabled()) { - logger.debug("Caught exception in getRelationList", e); - } - Response response = Response.status( - Response.Status.INTERNAL_SERVER_ERROR).entity( - "Index failed").type("text/plain").build(); - throw new WebApplicationException(response); - } - return relationList; - } + /** + * Gets the relation list request. + * + * @return the relation list request + * + * @throws WebApplicationException + * the web application exception + */ + public RelationsCommonList getRelationList(String subjectCsid, + String predicate, String objectCsid) throws WebApplicationException { + RelationsCommonList relationList = new RelationsCommonList(); + try { + ServiceContext ctx = MultipartServiceContextFactory.get() + .createServiceContext(null, getServiceName()); + DocumentHandler handler = createDocumentHandler(ctx); + Map propsFromPath = handler.getProperties(); + propsFromPath.put(IRelationsManager.SUBJECT, subjectCsid); + propsFromPath.put(IRelationsManager.PREDICATE, predicate); + propsFromPath.put(IRelationsManager.OBJECT, objectCsid); + getRepositoryClient(ctx).getAll(ctx, handler); + relationList = (RelationsCommonList) handler.getCommonPartList(); + } catch (UnauthorizedException ue) { + Response response = Response.status(Response.Status.UNAUTHORIZED) + .entity( + "Get relations failed reason " + + ue.getErrorReason()).type("text/plain") + .build(); + throw new WebApplicationException(response); + } catch (Exception e) { + if (logger.isDebugEnabled()) { + logger.debug("Caught exception in getRelationList", e); + } + Response response = Response.status( + Response.Status.INTERNAL_SERVER_ERROR).entity( + "Index failed").type("text/plain").build(); + throw new WebApplicationException(response); + } + return relationList; + } } diff --git a/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationDocumentModelHandler.java b/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationDocumentModelHandler.java index 550546515..5b6bb5658 100644 --- a/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationDocumentModelHandler.java +++ b/services/relation/service/src/main/java/org/collectionspace/services/relation/nuxeo/RelationDocumentModelHandler.java @@ -127,7 +127,7 @@ public class RelationDocumentModelHandler DocumentModel docModel = iter.next(); if(RelationsUtils.isQueryMatch(docModel, subjectCsid, predicate, objectCsid) == true){ - RelationListItem relListItem = RelationsUtils.getRelationListItem( + RelationListItem relListItem = RelationsUtils.getRelationListItem(getServiceContext(), docModel, getServiceContextPath()); itemList.add(relListItem); } -- 2.47.3