From 74539508ba11148e0c5513e270bffa58699e4d73 Mon Sep 17 00:00:00 2001 From: Patrick Schmitz Date: Tue, 2 Mar 2010 01:27:51 +0000 Subject: [PATCH] CSPACE-590, CSPACE-852. Added support to get item list for a named PersonAuthority, prototyping basic facility. Built using some common methods in the repository/RepositoryClient. Almost there. --- .../common/repository/RepositoryClient.java | 22 +++++ .../client/java/RepositoryJavaClientImpl.java | 84 +++++++++++++++++++ .../person/PersonAuthorityResource.java | 58 +++++++++++++ 3 files changed, 164 insertions(+) diff --git a/services/common/src/main/java/org/collectionspace/services/common/repository/RepositoryClient.java b/services/common/src/main/java/org/collectionspace/services/common/repository/RepositoryClient.java index dd23f69ca..892fb70ba 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/repository/RepositoryClient.java +++ b/services/common/src/main/java/org/collectionspace/services/common/repository/RepositoryClient.java @@ -71,4 +71,26 @@ public interface RepositoryClient extends StorageClient { ServiceContext ctx, String id) throws DocumentNotFoundException, DocumentException; + /** + * find wrapped documentModel from the Nuxeo repository + * @param ctx service context under which this method is invoked + * @param specifies docType. If null, uses ctx.getDocumentType() + * @param where NXQL where clause to get the document + * @throws DocumentException + */ + public DocumentWrapper findDoc( + ServiceContext ctx, String where) + throws DocumentNotFoundException, DocumentException; + + /** + * find doc and return CSID from the Nuxeo repository + * @param ctx service context under which this method is invoked + * @param specifies docType. If null, uses ctx.getDocumentType() + * @param where NXQL where clause to get the document + * @throws DocumentException + */ + public String findDocCSID( + ServiceContext ctx, String where) + throws DocumentNotFoundException, DocumentException; + } diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClientImpl.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClientImpl.java index fb0c9287e..98ca8e7aa 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClientImpl.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClientImpl.java @@ -294,6 +294,90 @@ public class RepositoryJavaClientImpl implements RepositoryClient { return wrapDoc; } + /** + * find wrapped documentModel from the Nuxeo repository + * @param ctx service context under which this method is invoked + * @param where NXQL where clause to get the document + * @throws DocumentException + */ + @Override + public DocumentWrapper findDoc( + ServiceContext ctx, String where) + throws DocumentNotFoundException, DocumentException { + RepositoryInstance repoSession = null; + DocumentWrapper wrapDoc = null; + + try { + String docType = ctx.getDocumentType(); + if (docType == null) { + throw new DocumentNotFoundException( + "Unable to find DocumentType for service " + ctx.getServiceName()); + } + String domain = ctx.getRepositoryDomainName(); + if (domain == null) { + throw new DocumentNotFoundException( + "Unable to find Domain for service " + ctx.getServiceName()); + } + repoSession = getRepositorySession(); + DocumentModelList docList = null; + // force limit to 1, and ignore totalSize + String query = buildNXQLQuery(docType, where, domain ); + docList = repoSession.query( query, null, 1, 0, false); + if(docList.size()!=1) { + if (logger.isDebugEnabled()) { + logger.debug("findDoc: Query found: "+docList.size()+" items."); + logger.debug(" Query: " + query); + } + throw new DocumentNotFoundException("No document found matching filter params."); + } + DocumentModel doc = docList.get(0); + wrapDoc = new DocumentWrapperImpl(doc); + } catch (IllegalArgumentException iae) { + throw iae; + } 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); + } + } + return wrapDoc; + } + + /** + * find doc and return CSID from the Nuxeo repository + * @param ctx service context under which this method is invoked + * @param where NXQL where clause to get the document + * @throws DocumentException + */ + public String findDocCSID( + ServiceContext ctx, String where) + throws DocumentNotFoundException, DocumentException { + String csid = null; + try { + DocumentWrapper wrapDoc = findDoc(ctx, where); + DocumentModel docModel = wrapDoc.getWrappedObject(); + csid = NuxeoUtils.extractId(docModel.getPathAsString()); + } catch (DocumentNotFoundException dnfe) { + throw dnfe; + } catch (IllegalArgumentException iae) { + throw iae; + } catch (DocumentException de) { + throw de; + } catch (Exception e) { + if (logger.isDebugEnabled()) { + logger.debug("Caught exception ", e); + } + throw new DocumentException(e); + } + return csid; + } + @Override public void get(ServiceContext ctx, List csidList, DocumentHandler handler) throws DocumentNotFoundException, DocumentException { 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 26e09c57a..772c3174f 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 @@ -53,6 +53,7 @@ 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.document.DocumentWrapper; import org.collectionspace.services.common.security.UnauthorizedException; import org.collectionspace.services.common.vocabulary.RefNameUtils; import org.collectionspace.services.common.query.IQueryManager; @@ -65,6 +66,7 @@ import org.collectionspace.services.person.nuxeo.PersonDocumentModelHandler; import org.jboss.resteasy.plugins.providers.multipart.MultipartInput; import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput; import org.jboss.resteasy.util.HttpResponseCodes; +import org.nuxeo.ecm.core.api.DocumentModel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -542,6 +544,62 @@ public class PersonAuthorityResource extends AbstractCollectionSpaceResourceImpl return personObjectList; } + @GET + @Path("urn:cspace:name({specifier})/items") + @Produces("application/xml") + public PersonsCommonList getPersonListByAuthName( + @PathParam("specifier") String parentSpecifier, + @QueryParam (IQueryManager.SEARCH_TYPE_PARTIALTERM) String partialTerm, + @Context UriInfo ui) { + PersonsCommonList personObjectList = new PersonsCommonList(); + try { + String whereClause = + PersonAuthorityJAXBSchema.PERSONAUTHORITIES_COMMON+ + ":"+PersonAuthorityJAXBSchema.DISPLAY_NAME+ + "='"+parentSpecifier+"'"; + // Need to get an Authoirty 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 "persons_common:inAuthority='" + parentcsid + "'" + myFilter.setWhereClause(PersonJAXBSchema.PERSONS_COMMON + ":" + + PersonJAXBSchema.IN_AUTHORITY + "='" + parentcsid + "'"); + + // AND persons_common:displayName LIKE '%partialTerm%' + if (partialTerm != null && !partialTerm.isEmpty()) { + String ptClause = "AND " + + PersonJAXBSchema.PERSONS_COMMON + ":" + + PersonJAXBSchema.DISPLAY_NAME + + " LIKE " + + "'%" + partialTerm + "%'"; + myFilter.appendWhereClause(ptClause); + } + + handler.setDocumentFilter(myFilter); + getRepositoryClient(ctx).getFiltered(ctx, handler); + personObjectList = (PersonsCommonList) 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 getPersonList", 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 updatePerson( -- 2.47.3