From fa7bcbb062b3d5a51d4915d19fd9700bbdeeda6f Mon Sep 17 00:00:00 2001 From: Patrick Schmitz Date: Wed, 10 Mar 2010 00:33:25 +0000 Subject: [PATCH] CSPACE-590. Added support to get item list for a named OrgAuthority, based upon PersonAuthority work. Built using some common methods in the repository/RepositoryClient. --- .../services/client/OrgAuthorityClient.java | 18 +++ .../services/client/OrgAuthorityProxy.java | 11 ++ .../client/test/OrgAuthorityServiceTest.java | 58 ++++++++- .../services/OrgAuthorityJAXBSchema.java | 1 + .../organization/OrgAuthorityResource.java | 118 +++++++++++++++++- .../person/PersonAuthorityResource.java | 2 +- 6 files changed, 200 insertions(+), 8 deletions(-) diff --git a/services/organization/client/src/main/java/org/collectionspace/services/client/OrgAuthorityClient.java b/services/organization/client/src/main/java/org/collectionspace/services/client/OrgAuthorityClient.java index e21dbfe3b..b4c071c7a 100644 --- a/services/organization/client/src/main/java/org/collectionspace/services/client/OrgAuthorityClient.java +++ b/services/organization/client/src/main/java/org/collectionspace/services/client/OrgAuthorityClient.java @@ -1,5 +1,6 @@ package org.collectionspace.services.client; +import javax.ws.rs.PathParam; import javax.ws.rs.core.Response; import org.collectionspace.services.contact.ContactsCommonList; @@ -90,6 +91,15 @@ public class OrgAuthorityClient extends AbstractServiceClientImpl { return orgAuthorityProxy.read(csid); } + /** + * @param name + * @return + * @see org.collectionspace.services.client.OrgAuthorityProxy#readByName(java.lang.String) + */ + public ClientResponse readByName(String name) { + return orgAuthorityProxy.readByName(name); + } + /** * @param orgAuthority * @return @@ -127,6 +137,14 @@ public class OrgAuthorityClient extends AbstractServiceClientImpl { return orgAuthorityProxy.readItemList(vcsid); } + /** + * @return + * @see org.collectionspace.services.client.OrgAuthorityProxy#readItemListForNamedAuthority() + */ + public ClientResponse readItemListForNamedAuthority(String specifier) { + return orgAuthorityProxy.readItemListForNamedAuthority(specifier); + } + /** * @param csid * @return diff --git a/services/organization/client/src/main/java/org/collectionspace/services/client/OrgAuthorityProxy.java b/services/organization/client/src/main/java/org/collectionspace/services/client/OrgAuthorityProxy.java index 97415e41b..74d88d01f 100644 --- a/services/organization/client/src/main/java/org/collectionspace/services/client/OrgAuthorityProxy.java +++ b/services/organization/client/src/main/java/org/collectionspace/services/client/OrgAuthorityProxy.java @@ -39,6 +39,11 @@ public interface OrgAuthorityProxy { @Path("/{csid}") ClientResponse read(@PathParam("csid") String csid); + //(R)ead by name + @GET + @Path("/urn:cspace:name({name})") + ClientResponse readByName(@PathParam("name") String name); + //(U)pdate @PUT @Path("/{csid}") @@ -55,6 +60,12 @@ public interface OrgAuthorityProxy { @Path("/{vcsid}/items/") ClientResponse readItemList(@PathParam("vcsid") String vcsid); + // List Items for a named authority + @GET + @Produces({"application/xml"}) + @Path("/urn:cspace:name({specifier})/items/") + ClientResponse readItemListForNamedAuthority(@PathParam("specifier") String specifier); + //(C)reate Item @POST @Path("/{vcsid}/items/") diff --git a/services/organization/client/src/test/java/org/collectionspace/services/client/test/OrgAuthorityServiceTest.java b/services/organization/client/src/test/java/org/collectionspace/services/client/test/OrgAuthorityServiceTest.java index 2f3a2fceb..e749b73c4 100644 --- a/services/organization/client/src/test/java/org/collectionspace/services/client/test/OrgAuthorityServiceTest.java +++ b/services/organization/client/src/test/java/org/collectionspace/services/client/test/OrgAuthorityServiceTest.java @@ -72,6 +72,7 @@ public class OrgAuthorityServiceTest extends AbstractServiceTestImpl { private final String TEST_ORG_SHORTNAME = "Test Org"; private final String TEST_ORG_FOUNDING_PLACE = "Anytown, USA"; private String knownResourceId = null; + private String knownResourceDisplayName = null; private String knownResourceRefName = null; private String knownItemResourceId = null; private String knownContactResourceId = null; @@ -128,6 +129,7 @@ public class OrgAuthorityServiceTest extends AbstractServiceTestImpl { // for additional tests below. if (knownResourceId == null){ knownResourceId = newID; + knownResourceDisplayName = displayName; if (logger.isDebugEnabled()) { logger.debug(testName + ": knownResourceId=" + knownResourceId); } @@ -393,6 +395,36 @@ public class OrgAuthorityServiceTest extends AbstractServiceTestImpl { } } + @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class, + groups = {"read"}, dependsOnGroups = {"create"}) + public void readByName(String testName) throws Exception { + + // Perform setup. + setupRead(); + + // Submit the request to the service and store the response. + ClientResponse res = client.readByName(knownResourceDisplayName); + int statusCode = res.getStatus(); + + // Check the status code of the response: does it match + // the expected response(s)? + if(logger.isDebugEnabled()){ + logger.debug(testName + ": status = " + statusCode); + } + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); + //FIXME: remove the following try catch once Aron fixes signatures + try { + MultipartInput input = (MultipartInput) res.getEntity(); + OrgauthoritiesCommon orgAuthority = (OrgauthoritiesCommon) extractPart(input, + client.getCommonPartName(), OrgauthoritiesCommon.class); + Assert.assertNotNull(orgAuthority); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + /* @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class, groups = {"read"}, dependsOnMethods = {"read"}) @@ -752,7 +784,7 @@ public class OrgAuthorityServiceTest extends AbstractServiceTestImpl { item.getDisplayName()); logger.debug(testName + ": list-item[" + i + "] URI=" + item.getUri()); - readItemList(csid); + readItemList(csid, null); i++; } } @@ -760,10 +792,15 @@ public class OrgAuthorityServiceTest extends AbstractServiceTestImpl { @Test(groups = {"readList"}, dependsOnMethods = {"readList"}) public void readItemList() { - readItemList(knownResourceId); + readItemList(knownResourceId, null); } - private void readItemList(String vcsid) { + @Test(groups = {"readList"}, dependsOnMethods = {"readItemList"}) + public void readItemListByAuthorityName() { + readItemList(null, knownResourceDisplayName); + } + + private void readItemList(String vcsid, String name) { final String testName = "readItemList"; @@ -771,8 +808,17 @@ public class OrgAuthorityServiceTest extends AbstractServiceTestImpl { setupReadList(testName); // Submit the request to the service and store the response. - ClientResponse res = - client.readItemList(vcsid); + ClientResponse res = null; + + if(vcsid!= null) { + // Submit the request to the service and store the response. + res = client.readItemList(vcsid); + } else if(name!= null) { + // Submit the request to the service and store the response. + res = client.readItemListForNamedAuthority(name); + } else { + Assert.fail("readItemList passed null csid and name!"); + } OrganizationsCommonList list = res.getEntity(); int statusCode = res.getStatus(); @@ -888,7 +934,7 @@ public class OrgAuthorityServiceTest extends AbstractServiceTestImpl { // Success outcomes @Override @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class, - groups = {"update"}, dependsOnGroups = {"create"}) + groups = {"update"}, dependsOnGroups = {"read", "readList"}) public void update(String testName) throws Exception { // Perform setup. diff --git a/services/organization/jaxb/src/main/java/org/collectionspace/services/OrgAuthorityJAXBSchema.java b/services/organization/jaxb/src/main/java/org/collectionspace/services/OrgAuthorityJAXBSchema.java index ad674843b..90798f988 100644 --- a/services/organization/jaxb/src/main/java/org/collectionspace/services/OrgAuthorityJAXBSchema.java +++ b/services/organization/jaxb/src/main/java/org/collectionspace/services/OrgAuthorityJAXBSchema.java @@ -8,6 +8,7 @@ package org.collectionspace.services; * */ public interface OrgAuthorityJAXBSchema { + final static String ORGAUTHORITIES_COMMON = "orgauthorities_common"; final static String DISPLAY_NAME = "displayName"; final static String REF_NAME = "refName"; final static String VOCAB_TYPE = "vocabType"; diff --git a/services/organization/service/src/main/java/org/collectionspace/services/organization/OrgAuthorityResource.java b/services/organization/service/src/main/java/org/collectionspace/services/organization/OrgAuthorityResource.java index f41add84e..122867b2c 100644 --- a/services/organization/service/src/main/java/org/collectionspace/services/organization/OrgAuthorityResource.java +++ b/services/organization/service/src/main/java/org/collectionspace/services/organization/OrgAuthorityResource.java @@ -39,6 +39,7 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.UriBuilder; import javax.ws.rs.core.UriInfo; +import org.collectionspace.services.OrgAuthorityJAXBSchema; import org.collectionspace.services.OrganizationJAXBSchema; import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl; import org.collectionspace.services.common.ClientType; @@ -50,13 +51,13 @@ import org.collectionspace.services.common.document.BadRequestException; import org.collectionspace.services.common.document.DocumentFilter; import org.collectionspace.services.common.document.DocumentHandler; import org.collectionspace.services.common.document.DocumentNotFoundException; -import org.collectionspace.services.common.security.UnauthorizedException; import org.collectionspace.services.common.query.IQueryManager; import org.collectionspace.services.contact.ContactResource; import org.collectionspace.services.contact.ContactsCommon; import org.collectionspace.services.contact.ContactsCommonList; import org.collectionspace.services.contact.ContactJAXBSchema; import org.collectionspace.services.contact.nuxeo.ContactDocumentModelHandler; +import org.collectionspace.services.common.security.UnauthorizedException; import org.collectionspace.services.organization.nuxeo.OrganizationDocumentModelHandler; import org.jboss.resteasy.plugins.providers.multipart.MultipartInput; import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput; @@ -183,6 +184,65 @@ public class OrgAuthorityResource extends AbstractCollectionSpaceResourceImpl { } } + + @GET + @Path("urn:cspace:name({specifier})") + public MultipartOutput getOrgAuthorityByName(@PathParam("specifier") String specifier) { + String idValue = null; + if (specifier == null) { + logger.error("getOrgAuthority: missing name!"); + Response response = Response.status(Response.Status.BAD_REQUEST).entity( + "get failed on OrgAuthority (missing specifier)").type( + "text/plain").build(); + throw new WebApplicationException(response); + } + String whereClause = + OrgAuthorityJAXBSchema.ORGAUTHORITIES_COMMON+ + ":"+OrgAuthorityJAXBSchema.DISPLAY_NAME+ + "='"+specifier+"'"; + // We only get a single doc - if there are multiple, + // it is an error in use. + + if (logger.isDebugEnabled()) { + logger.debug("getOrgAuthority with name=" + specifier); + } + MultipartOutput result = null; + try { + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName()); + DocumentHandler handler = createDocumentHandler(ctx); + DocumentFilter myFilter = new DocumentFilter(whereClause, 0, 1); + handler.setDocumentFilter(myFilter); + getRepositoryClient(ctx).get(ctx, 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("getOrgAuthority", dnfe); + } + Response response = Response.status(Response.Status.NOT_FOUND).entity( + "Get failed on OrgAuthority spec=" + specifier).type( + "text/plain").build(); + throw new WebApplicationException(response); + } catch (Exception e) { + if (logger.isDebugEnabled()) { + logger.debug("getOrgAuthority", 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 OrgAuthority spec:" + specifier + ": was not found.").type( + "text/plain").build(); + throw new WebApplicationException(response); + } + return result; + } + @GET @Path("{csid}") public MultipartOutput getOrgAuthority(@PathParam("csid") String csid) { @@ -482,6 +542,62 @@ public class OrgAuthorityResource extends AbstractCollectionSpaceResourceImpl { return organizationObjectList; } + @GET + @Path("urn:cspace:name({specifier})/items") + @Produces("application/xml") + public OrganizationsCommonList getOrganizationListByAuthName( + @PathParam("specifier") String parentSpecifier, + @QueryParam (IQueryManager.SEARCH_TYPE_PARTIALTERM) String partialTerm, + @Context UriInfo ui) { + OrganizationsCommonList personObjectList = new OrganizationsCommonList(); + try { + String whereClause = + OrgAuthorityJAXBSchema.ORGAUTHORITIES_COMMON+ + ":"+OrgAuthorityJAXBSchema.DISPLAY_NAME+ + "='"+parentSpecifier+"'"; + // Need to get an Authority by name + ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName()); + String parentcsid = + getRepositoryClient(ctx).findDocCSID(ctx, whereClause); + + ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName()); + DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid); + MultivaluedMap queryParams = ui.getQueryParameters(); + DocumentFilter myFilter = new DocumentFilter(); + myFilter.setPagination(queryParams); + + // Add the where clause "organizations_common:inAuthority='" + parentcsid + "'" + myFilter.setWhereClause(OrganizationJAXBSchema.ORGANIZATIONS_COMMON + ":" + + OrganizationJAXBSchema.IN_AUTHORITY + "='" + parentcsid + "'"); + + // AND organizations_common:displayName LIKE '%partialTerm%' + if (partialTerm != null && !partialTerm.isEmpty()) { + String ptClause = "AND " + + OrganizationJAXBSchema.ORGANIZATIONS_COMMON + ":" + + OrganizationJAXBSchema.DISPLAY_NAME + + " LIKE " + + "'%" + partialTerm + "%'"; + myFilter.appendWhereClause(ptClause); + } + + handler.setDocumentFilter(myFilter); + getRepositoryClient(ctx).getFiltered(ctx, handler); + personObjectList = (OrganizationsCommonList) 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 getOrganizationListByAuthName", e); + } + Response response = Response.status( + Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build(); + throw new WebApplicationException(response); + } + return personObjectList; + } + @PUT @Path("{csid}/items/{itemcsid}") public MultipartOutput updateOrganization( diff --git a/services/person/service/src/main/java/org/collectionspace/services/person/PersonAuthorityResource.java b/services/person/service/src/main/java/org/collectionspace/services/person/PersonAuthorityResource.java index 772c3174f..cfb962a7f 100644 --- a/services/person/service/src/main/java/org/collectionspace/services/person/PersonAuthorityResource.java +++ b/services/person/service/src/main/java/org/collectionspace/services/person/PersonAuthorityResource.java @@ -557,7 +557,7 @@ public class PersonAuthorityResource extends AbstractCollectionSpaceResourceImpl PersonAuthorityJAXBSchema.PERSONAUTHORITIES_COMMON+ ":"+PersonAuthorityJAXBSchema.DISPLAY_NAME+ "='"+parentSpecifier+"'"; - // Need to get an Authoirty by name + // Need to get an Authority by name ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName()); String parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause); -- 2.47.3