From 7ef619482bf46f43b1c2b9268d3cbd934f034931 Mon Sep 17 00:00:00 2001 From: Patrick Schmitz Date: Mon, 23 Jul 2012 16:26:24 -0700 Subject: [PATCH] CSPACE-5381 Added support to pass a new query param, which will mark list results to indicate whether they are related to a passed object. --- .../services/client/IQueryManager.java | 1 + .../nuxeo/client/java/DocHandlerBase.java | 149 ++++++++++++------ 2 files changed, 105 insertions(+), 45 deletions(-) diff --git a/services/client/src/main/java/org/collectionspace/services/client/IQueryManager.java b/services/client/src/main/java/org/collectionspace/services/client/IQueryManager.java index accc0cbbb..c2e9c0c54 100644 --- a/services/client/src/main/java/org/collectionspace/services/client/IQueryManager.java +++ b/services/client/src/main/java/org/collectionspace/services/client/IQueryManager.java @@ -51,6 +51,7 @@ public interface IQueryManager { final static String SEARCH_RELATED_MATCH_OBJ_DOCTYPES = "rtObjDocTypes"; final static String SELECT_DOC_TYPE_FIELD = "selectDocType"; + final static String MARK_RELATED_TO_CSID_AS_SUBJECT = "mkRtSbj"; // // Generic CMIS property mapping constants // diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/DocHandlerBase.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/DocHandlerBase.java index 9b81c8210..c8f4efbc7 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/DocHandlerBase.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/DocHandlerBase.java @@ -30,13 +30,20 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; +import javax.ws.rs.core.MultivaluedMap; + import org.collectionspace.services.client.CollectionSpaceClient; +import org.collectionspace.services.client.IQueryManager; +import org.collectionspace.services.client.IRelationsManager; import org.collectionspace.services.common.ReflectionMapper; import org.collectionspace.services.common.api.Tools; +import org.collectionspace.services.common.context.AbstractServiceContextImpl; import org.collectionspace.services.common.context.MultipartServiceContext; import org.collectionspace.services.common.datetime.DateTimeFormatUtils; import org.collectionspace.services.common.document.DocumentException; import org.collectionspace.services.common.document.DocumentWrapper; +import org.collectionspace.services.common.query.QueryContext; +import org.collectionspace.services.common.relation.nuxeo.RelationsUtils; import org.collectionspace.services.config.service.DocHandlerParams; import org.collectionspace.services.config.service.ListResultField; import org.collectionspace.services.config.service.ServiceBindingType; @@ -46,6 +53,7 @@ import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandler import org.collectionspace.services.nuxeo.util.NuxeoUtils; import org.nuxeo.ecm.core.api.DocumentModel; import org.nuxeo.ecm.core.api.DocumentModelList; +import org.nuxeo.ecm.core.api.repository.RepositoryInstance; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -71,6 +79,7 @@ public abstract class DocHandlerBase extends RemoteDocumentModelHandlerImpl extends RemoteDocumentModelHandlerImpl resultsFields = getListItemsArray(); - int nFields = resultsFields.size() + NUM_STANDARD_LIST_RESULT_FIELDS; - String fields[] = new String[nFields]; - fields[0] = STANDARD_LIST_CSID_FIELD; - fields[1] = STANDARD_LIST_URI_FIELD; - fields[2] = STANDARD_LIST_UPDATED_AT_FIELD; - fields[3] = STANDARD_LIST_WORKFLOW_FIELD; - for(int i = NUM_STANDARD_LIST_RESULT_FIELDS; i < nFields; i++) { - ListResultField field = resultsFields.get(i - NUM_STANDARD_LIST_RESULT_FIELDS); - fields[i] = field.getElement(); - } - commonList.setFieldsReturned(fields); - Iterator iter = wrapDoc.getWrappedObject().iterator(); - HashMap item = new HashMap(); - while (iter.hasNext()) { - DocumentModel docModel = iter.next(); - String id = NuxeoUtils.getCsid(docModel); - item.put(STANDARD_LIST_CSID_FIELD, id); - String uri = getUri(docModel); - item.put(STANDARD_LIST_URI_FIELD, uri); - item.put(STANDARD_LIST_UPDATED_AT_FIELD, - getUpdatedAtAsString(docModel)); - item.put(STANDARD_LIST_WORKFLOW_FIELD, - docModel.getCurrentLifeCycleState()); - - for (ListResultField field : resultsFields) { - String schema = field.getSchema(); - if (schema == null || schema.trim().isEmpty()) { - schema = commonSchema; - } - Object value = getListResultValue(docModel, schema, field); - if (value != null && value instanceof String) { - String strValue = (String) value; - if (strValue.trim().isEmpty() == true) { - value = null; + String markRtSbj = null; + RepositoryInstance repoSession = null; + RepositoryJavaClientImpl repoClient = null; + RepositoryJavaClientImpl nuxeoRepoClient = null; + boolean releaseRepoSession = false; + + AbstractServiceContextImpl sc = (AbstractServiceContextImpl)getServiceContext(); + MultivaluedMap queryParams = getServiceContext().getQueryParams(); + markRtSbj = queryParams.getFirst(IQueryManager.MARK_RELATED_TO_CSID_AS_SUBJECT); + if(markRtSbj!=null && markRtSbj.isEmpty()) + markRtSbj = null; + + try { + if(markRtSbj!=null) { + repoClient = (RepositoryJavaClientImpl)this.getRepositoryClient(sc); + nuxeoRepoClient = (RepositoryJavaClientImpl) repoClient; + repoSession = this.getRepositorySession(); + if (repoSession == null) { + repoSession = repoClient.getRepositorySession(sc); + releaseRepoSession = true; + } + } + + String commonSchema = getServiceContext().getCommonPartLabel(); + extractPagingInfo(commonList, wrapDoc); + List resultsFields = getListItemsArray(); + int nFields = resultsFields.size() + NUM_STANDARD_LIST_RESULT_FIELDS; + int baseFields = NUM_STANDARD_LIST_RESULT_FIELDS; + if(markRtSbj!=null) { + nFields++; + baseFields++; + } + String fields[] = new String[nFields]; + fields[0] = STANDARD_LIST_CSID_FIELD; + fields[1] = STANDARD_LIST_URI_FIELD; + fields[2] = STANDARD_LIST_UPDATED_AT_FIELD; + fields[3] = STANDARD_LIST_WORKFLOW_FIELD; + if(markRtSbj!=null) { + fields[4] = STANDARD_LIST_MARK_RT_FIELD; + } + for(int i = baseFields; i < nFields; i++) { + ListResultField field = resultsFields.get(i - baseFields); + fields[i] = field.getElement(); + } + commonList.setFieldsReturned(fields); + Iterator iter = wrapDoc.getWrappedObject().iterator(); + HashMap item = new HashMap(); + while (iter.hasNext()) { + DocumentModel docModel = iter.next(); + String id = NuxeoUtils.getCsid(docModel); + item.put(STANDARD_LIST_CSID_FIELD, id); + if(markRtSbj!=null) { + String relationClause = RelationsUtils.buildWhereClause(markRtSbj, null, null, id, null); + QueryContext queryContext = new QueryContext(sc, relationClause); + queryContext.setDocType(IRelationsManager.DOC_TYPE); + String query = NuxeoUtils.buildNXQLQuery(sc, queryContext); + // Search for 1 relation that matches. 1 is enough to fail the filter + DocumentModelList docList = repoSession.query(query, null, 1, 0, false); + item.put(STANDARD_LIST_MARK_RT_FIELD, docList.isEmpty()?"false":"true"); + } + String uri = getUri(docModel); + item.put(STANDARD_LIST_URI_FIELD, uri); + item.put(STANDARD_LIST_UPDATED_AT_FIELD, + getUpdatedAtAsString(docModel)); + item.put(STANDARD_LIST_WORKFLOW_FIELD, + docModel.getCurrentLifeCycleState()); + + for (ListResultField field : resultsFields) { + String schema = field.getSchema(); + if (schema == null || schema.trim().isEmpty()) { + schema = commonSchema; + } + Object value = getListResultValue(docModel, schema, field); + if (value != null && value instanceof String) { + String strValue = (String) value; + if (strValue.trim().isEmpty() == true) { + value = null; + } + } + if (value != null) { + item.put(field.getElement(), value); } } - if (value != null) { - item.put(field.getElement(), value); - } + commonList.addItem(item); + item.clear(); } - commonList.addItem(item); - item.clear(); - } + } catch (Exception e) { + if (logger.isDebugEnabled()) { + logger.debug("Caught exception ", e); + } + throw new DocumentException(e); + } finally { + // If we got/aquired a new seesion then we're responsible for releasing it. + if (releaseRepoSession && repoSession != null) { + repoClient.releaseRepositorySession(sc, repoSession); + } + } - return commonList; + return commonList; } // TODO - get rid of this if we can - appears to be unused. -- 2.47.3