import java.util.List;
import java.util.Map;
+import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import org.collectionspace.services.nuxeo.client.java.CommonList;
import org.collectionspace.services.jaxb.AbstractCommonList;
import org.collectionspace.services.client.CollectionSpaceClient;
import org.collectionspace.services.client.IQueryManager;
+import org.collectionspace.services.client.IRelationsManager;
import org.collectionspace.services.client.PoxPayloadIn;
import org.collectionspace.services.client.PoxPayloadOut;
import org.collectionspace.services.common.NuxeoBasedResource;
import org.collectionspace.services.common.api.RefNameUtils;
import org.collectionspace.services.common.api.RefNameUtils.AuthorityTermInfo;
import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
+import org.collectionspace.services.common.context.AbstractServiceContextImpl;
import org.collectionspace.services.common.context.ServiceBindingUtils;
import org.collectionspace.services.common.context.ServiceContext;
import org.collectionspace.services.common.document.DocumentException;
import org.collectionspace.services.common.document.DocumentFilter;
import org.collectionspace.services.common.document.DocumentNotFoundException;
import org.collectionspace.services.common.document.DocumentWrapper;
+import org.collectionspace.services.common.document.TransactionException;
import org.collectionspace.services.common.security.SecurityUtils;
+import org.collectionspace.services.common.query.QueryContext;
import org.collectionspace.services.common.query.nuxeo.QueryManagerNuxeoImpl;
+import org.collectionspace.services.common.relation.nuxeo.RelationsUtils;
+import org.collectionspace.services.config.service.ListResultField;
import org.collectionspace.services.config.service.ServiceBindingType;
import org.collectionspace.services.config.service.ServiceObjectType;
import org.collectionspace.services.servicegroup.ServicegroupsCommon;
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
- protected static final int NUM_META_FIELDS = 3;
+ protected static final int NUM_META_FIELDS = 4; // the number of meta fields -i.e., DOC_TYPE_FIELD, DOC_NUMBER_FIELD, DOC_NAME_FIELD, STANDARD_LIST_MARK_RT_FIELD
protected static final String DOC_TYPE_FIELD = "docType";
protected static final String DOC_NUMBER_FIELD = "docNumber";
protected static final String DOC_NAME_FIELD = "docName";
+ protected static final String STANDARD_LIST_MARK_RT_FIELD = "related";
//
// Returns a service payload for an authority item
if (docList == null) { // found no authRef fields - nothing to process
return list;
}
- processDocList(ctx.getTenantId(), docList, queriedServiceBindings, commonList);
+ processDocList(ctx, docList, queriedServiceBindings, commonList);
list.setItemsInPage(docList.size());
list.setTotalItems(docList.totalSize());
} catch (DocumentException de) {
return "/" + sb.getName().toLowerCase() + "/" + csid;
}
- private void processDocList(String tenantId,
+ private void processDocList(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
DocumentModelList docList,
Map<String, ServiceBindingType> queriedServiceBindings,
- CommonList list) {
- String fields[] = new String[NUM_META_FIELDS + NUM_STANDARD_LIST_RESULT_FIELDS];
- 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;
- fields[4] = STANDARD_LIST_REFNAME_FIELD;
- fields[5] = DOC_NAME_FIELD;
- fields[6] = DOC_NUMBER_FIELD;
- fields[7] = DOC_TYPE_FIELD;
- list.setFieldsReturned(fields);
-
- Iterator<DocumentModel> iter = docList.iterator();
- HashMap<String, Object> item = new HashMap<String, Object>();
- while (iter.hasNext()) {
- DocumentModel docModel = iter.next();
- String docType = docModel.getDocumentType().getName();
- docType = ServiceBindingUtils.getUnqualifiedTenantDocType(docType);
- ServiceBindingType sb = queriedServiceBindings.get(docType);
- if (sb == null) {
- throw new RuntimeException("processDocList: No Service Binding for docType: " + docType);
- }
-
- String csid = NuxeoUtils.getCsid(docModel);
- item.put(STANDARD_LIST_CSID_FIELD, csid);
-
- UriTemplateRegistry uriTemplateRegistry = ServiceMain.getInstance().getUriTemplateRegistry();
- StoredValuesUriTemplate storedValuesResourceTemplate = uriTemplateRegistry.get(new UriTemplateRegistryKey(tenantId, docType));
- Map<String, String> additionalValues = new HashMap<String, String>();
- if (storedValuesResourceTemplate.getUriTemplateType() == UriTemplateFactory.ITEM) {
- try {
- String inAuthorityCsid = (String) NuxeoUtils.getProperyValue(docModel, "inAuthority"); //docModel.getPropertyValue("inAuthority"); // AuthorityItemJAXBSchema.IN_AUTHORITY
- additionalValues.put(UriTemplateFactory.IDENTIFIER_VAR, inAuthorityCsid);
- additionalValues.put(UriTemplateFactory.ITEM_IDENTIFIER_VAR, csid);
- } catch (Exception e) {
- String msg = String.format("Could not extract inAuthority property from authority item with CSID = ", docModel.getName());
- logger.warn(msg, e);
- }
- } else {
- additionalValues.put(UriTemplateFactory.IDENTIFIER_VAR, csid);
- }
-
- String uriStr = storedValuesResourceTemplate.buildUri(additionalValues);
- item.put(STANDARD_LIST_URI_FIELD, uriStr);
- try {
- item.put(STANDARD_LIST_UPDATED_AT_FIELD, getUpdatedAtAsString(docModel));
- item.put(STANDARD_LIST_WORKFLOW_FIELD, docModel.getCurrentLifeCycleState());
- item.put(STANDARD_LIST_REFNAME_FIELD, getRefname(docModel));
- } catch(Exception e) {
- logger.error("Error getting core values for doc ["+csid+"]: "+e.getLocalizedMessage());
- }
+ CommonList list) throws DocumentException {
+
+ String tenantId = ctx.getTenantId();
+ CoreSessionInterface repoSession = null;
+ RepositoryClientImpl repoClient = null;
+ boolean releaseRepoSession = false;
+
+ MultivaluedMap<String, String> queryParams = getServiceContext().getQueryParams();
+ String markRtSbj = queryParams.getFirst(IQueryManager.MARK_RELATED_TO_CSID_AS_SUBJECT);
+ if (markRtSbj != null && markRtSbj.isEmpty()) {
+ markRtSbj = null;
+ }
+
+ String markRtSbjOrObj = queryParams.getFirst(IQueryManager.MARK_RELATED_TO_CSID_AS_EITHER);
+ if (markRtSbjOrObj != null && markRtSbjOrObj.isEmpty()) {
+ markRtSbjOrObj = null;
+ }
+
+ try {
+ if (markRtSbj != null || markRtSbjOrObj != null) {
+ repoClient = (RepositoryClientImpl) this.getRepositoryClient(ctx);
+ RepositoryClientImpl nuxeoRepoClient = (RepositoryClientImpl) repoClient;
+ repoSession = this.getRepositorySession();
+ if (repoSession == null) {
+ repoSession = repoClient.getRepositorySession(ctx);
+ releaseRepoSession = true;
+ }
+ }
+
+ String fields[] = new String[NUM_META_FIELDS + NUM_STANDARD_LIST_RESULT_FIELDS];
+ 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;
+ fields[4] = STANDARD_LIST_REFNAME_FIELD;
+ fields[5] = DOC_NAME_FIELD;
+ fields[6] = DOC_NUMBER_FIELD;
+ fields[7] = DOC_TYPE_FIELD;
+ if (markRtSbj != null || markRtSbjOrObj != null) {
+ fields[8] = STANDARD_LIST_MARK_RT_FIELD;
+ }
- String value = ServiceBindingUtils.getMappedFieldInDoc(sb, ServiceBindingUtils.OBJ_NUMBER_PROP, docModel);
- if (value != null) {
- item.put(DOC_NUMBER_FIELD, value);
- }
-
- value = ServiceBindingUtils.getMappedFieldInDoc(sb, ServiceBindingUtils.OBJ_NAME_PROP, docModel);
- if (value != null) {
- item.put(DOC_NAME_FIELD, value);
- }
-
- item.put(DOC_TYPE_FIELD, docType);
- // add the item to the list
- list.addItem(item);
- item.clear();
- }
+ list.setFieldsReturned(fields);
+
+ Iterator<DocumentModel> iter = docList.iterator();
+ HashMap<String, Object> item = new HashMap<String, Object>();
+ while (iter.hasNext()) {
+ DocumentModel docModel = iter.next();
+ String docType = docModel.getDocumentType().getName();
+ docType = ServiceBindingUtils.getUnqualifiedTenantDocType(docType);
+ ServiceBindingType sb = queriedServiceBindings.get(docType);
+ if (sb == null) {
+ throw new RuntimeException("processDocList: No Service Binding for docType: " + docType);
+ }
+
+ String csid = NuxeoUtils.getCsid(docModel);
+ item.put(STANDARD_LIST_CSID_FIELD, csid);
+
+ //
+ // If the mark-related query param was set, check to see if the doc we're processing
+ // is related to the value specified in the mark-related query param.
+ //
+ if (markRtSbj != null || markRtSbjOrObj != null) {
+ String relationClause = RelationsUtils.buildWhereClause(markRtSbj, null, null, csid, null, markRtSbjOrObj);
+ String whereClause = relationClause + IQueryManager.SEARCH_QUALIFIER_AND
+ + NuxeoUtils.buildWorkflowNotDeletedWhereClause();
+ QueryContext queryContext = new QueryContext(ctx, whereClause);
+ queryContext.setDocType(IRelationsManager.DOC_TYPE);
+ String query = NuxeoUtils.buildNXQLQuery(queryContext);
+ // Search for 1 relation that matches. 1 is enough to fail
+ // the filter
+ DocumentModelList queryDocList = repoSession.query(query, null, 1, 0, false);
+ item.put(STANDARD_LIST_MARK_RT_FIELD, queryDocList.isEmpty() ? "false" : "true");
+ }
+
+ UriTemplateRegistry uriTemplateRegistry = ServiceMain.getInstance().getUriTemplateRegistry();
+ StoredValuesUriTemplate storedValuesResourceTemplate = uriTemplateRegistry.get(new UriTemplateRegistryKey(tenantId, docType));
+ Map<String, String> additionalValues = new HashMap<String, String>();
+ if (storedValuesResourceTemplate.getUriTemplateType() == UriTemplateFactory.ITEM) {
+ try {
+ String inAuthorityCsid = (String) NuxeoUtils.getProperyValue(docModel, "inAuthority"); //docModel.getPropertyValue("inAuthority"); // AuthorityItemJAXBSchema.IN_AUTHORITY
+ additionalValues.put(UriTemplateFactory.IDENTIFIER_VAR, inAuthorityCsid);
+ additionalValues.put(UriTemplateFactory.ITEM_IDENTIFIER_VAR, csid);
+ } catch (Exception e) {
+ String msg = String.format("Could not extract inAuthority property from authority item with CSID = ", docModel.getName());
+ logger.warn(msg, e);
+ }
+ } else {
+ additionalValues.put(UriTemplateFactory.IDENTIFIER_VAR, csid);
+ }
+
+ String uriStr = storedValuesResourceTemplate.buildUri(additionalValues);
+ item.put(STANDARD_LIST_URI_FIELD, uriStr);
+ try {
+ item.put(STANDARD_LIST_UPDATED_AT_FIELD, getUpdatedAtAsString(docModel));
+ item.put(STANDARD_LIST_WORKFLOW_FIELD, docModel.getCurrentLifeCycleState());
+ item.put(STANDARD_LIST_REFNAME_FIELD, getRefname(docModel));
+ } catch(Exception e) {
+ logger.error("Error getting core values for doc ["+csid+"]: "+e.getLocalizedMessage());
+ }
+
+ String value = ServiceBindingUtils.getMappedFieldInDoc(sb, ServiceBindingUtils.OBJ_NUMBER_PROP, docModel);
+ if (value != null) {
+ item.put(DOC_NUMBER_FIELD, value);
+ }
+
+ value = ServiceBindingUtils.getMappedFieldInDoc(sb, ServiceBindingUtils.OBJ_NAME_PROP, docModel);
+ if (value != null) {
+ item.put(DOC_NAME_FIELD, value);
+ }
+
+ item.put(DOC_TYPE_FIELD, docType);
+ // add the item to the list
+ list.addItem(item);
+ item.clear();
+ }
+ } catch (Exception e) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("Caught exception ", e);
+ }
+ throw new DocumentException(e);
+ } finally {
+ // If we got/acquired a new session then we're responsible for releasing it.
+ if (releaseRepoSession && repoSession != null) {
+ repoClient.releaseRepositorySession(ctx, repoSession);
+ }
+ }
}
}