2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
24 package org.collectionspace.services.common.vocabulary.nuxeo;
26 import org.collectionspace.services.client.AuthorityClient;
27 import org.collectionspace.services.client.IQueryManager;
28 import org.collectionspace.services.client.PayloadInputPart;
29 import org.collectionspace.services.client.PoxPayloadIn;
30 import org.collectionspace.services.client.PoxPayloadOut;
31 import org.collectionspace.services.client.RelationClient;
32 import org.collectionspace.services.client.workflow.WorkflowClient;
33 import org.collectionspace.services.common.api.RefName;
34 import org.collectionspace.services.common.api.Tools;
35 import org.collectionspace.services.common.authorityref.AuthorityRefDocList;
36 import org.collectionspace.services.common.context.MultipartServiceContext;
37 import org.collectionspace.services.common.context.ServiceContext;
38 import org.collectionspace.services.common.document.DocumentException;
39 import org.collectionspace.services.common.document.DocumentFilter;
40 import org.collectionspace.services.common.document.DocumentNotFoundException;
41 import org.collectionspace.services.common.document.DocumentReferenceException;
42 import org.collectionspace.services.common.document.DocumentWrapper;
43 import org.collectionspace.services.common.repository.RepositoryClient;
44 import org.collectionspace.services.common.vocabulary.AuthorityJAXBSchema;
45 import org.collectionspace.services.common.vocabulary.AuthorityItemJAXBSchema;
46 import org.collectionspace.services.common.vocabulary.AuthorityResource;
47 import org.collectionspace.services.common.vocabulary.AuthorityServiceUtils;
48 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils;
49 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.AuthorityItemSpecifier;
50 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.SpecifierForm;
51 import org.collectionspace.services.config.service.ListResultField;
52 import org.collectionspace.services.config.service.ObjectPartType;
53 import org.collectionspace.services.lifecycle.TransitionDef;
54 import org.collectionspace.services.nuxeo.client.java.NuxeoDocumentModelHandler;
55 import org.collectionspace.services.nuxeo.client.java.CoreSessionInterface;
56 import org.collectionspace.services.nuxeo.client.java.NuxeoRepositoryClientImpl;
57 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
58 import org.collectionspace.services.relation.RelationsCommonList;
59 import org.collectionspace.services.vocabulary.VocabularyItemJAXBSchema;
61 import org.nuxeo.ecm.core.api.ClientException;
62 import org.nuxeo.ecm.core.api.DocumentModel;
63 import org.nuxeo.ecm.core.api.model.PropertyException;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
68 import javax.ws.rs.core.MultivaluedMap;
70 import java.util.ArrayList;
71 import java.util.Collections;
72 import java.util.HashMap;
73 import java.util.List;
75 import java.util.regex.Matcher;
76 import java.util.regex.Pattern;
77 import java.util.regex.PatternSyntaxException;
79 //import org.collectionspace.services.common.authority.AuthorityItemRelations;
81 * AuthorityItemDocumentModelHandler
83 * $LastChangedRevision: $
86 public abstract class AuthorityItemDocumentModelHandler<AICommon>
87 extends NuxeoDocumentModelHandler<AICommon> {
89 private final Logger logger = LoggerFactory.getLogger(AuthorityItemDocumentModelHandler.class);
91 private static final Integer PAGE_SIZE_FROM_QUERYPARAMS = null;
92 private static final Integer PAGE_NUM_FROM_QUERYPARAMS = null;
94 protected String authorityCommonSchemaName;
95 protected String authorityItemCommonSchemaName;
96 private String authorityItemTermGroupXPathBase;
98 private boolean shouldUpdateSASFields = true;
99 private boolean syncHierarchicalRelationships = false;
100 private boolean isProposed = false; // used by local authority to propose a new shared item. Allows local deployments to use new terms until they become official
101 private boolean isSAS = false; // used to indicate if the authority item originated as a SAS item
102 private boolean shouldUpdateRevNumber = true; // by default we should update the revision number -not true on synchronization with SAS
104 * inVocabulary is the parent Authority for this context
106 protected String inAuthority = null;
107 protected boolean wildcardedAuthorityRequest = false;
108 protected String authorityRefNameBase = null;
109 // Used to determine when the displayName changes as part of the update.
110 protected String oldDisplayNameOnUpdate = null;
111 private final static String LIST_SUFFIX = "List";
112 private final static String ZERO_OR_MORE_ANY_CHAR_REGEX = ".*";
114 public AuthorityItemDocumentModelHandler(String authorityCommonSchemaName, String authorityItemCommonSchemaName) {
115 this.authorityItemCommonSchemaName = authorityItemCommonSchemaName;
118 abstract public String getParentCommonSchemaName();
121 // Getter and Setter for 'shouldUpdateSASFields'
123 public boolean getShouldUpdateSASFields() {
124 return shouldUpdateSASFields;
127 public void setshouldUpdateSASFields(boolean flag) {
128 shouldUpdateSASFields = flag;
132 // Getter and Setter for 'proposed'
134 public boolean getIsProposed() {
135 return this.isProposed;
138 public void setIsProposed(boolean flag) {
139 this.isProposed = flag;
143 // Getter and Setter for 'isSAS'
145 public boolean getIsSASItem() {
149 public void setIsSASItem(boolean flag) {
154 // Getter and Setter for 'shouldUpdateRevNumber'
156 public boolean getShouldUpdateRevNumber() {
157 return this.shouldUpdateRevNumber;
160 public void setShouldUpdateRevNumber(boolean flag) {
161 this.shouldUpdateRevNumber = flag;
165 // Getter and Setter for deciding if we need to synch hierarchical relationships
167 public boolean getShouldSyncHierarchicalRelationships() {
168 return this.syncHierarchicalRelationships;
171 public void setShouldSyncHierarchicalRelationships(boolean flag) {
172 this.syncHierarchicalRelationships = flag;
176 public void prepareSync() throws Exception {
177 this.setShouldUpdateRevNumber(AuthorityServiceUtils.DONT_UPDATE_REV); // Never update rev nums on sync operations
181 protected String getRefnameDisplayName(DocumentWrapper<DocumentModel> docWrapper) {
182 String result = null;
184 DocumentModel docModel = docWrapper.getWrappedObject();
185 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = this.getServiceContext();
186 RefName.AuthorityItem refname = (RefName.AuthorityItem)getRefName(ctx, docModel);
187 result = refname.getDisplayName();
193 * After calling this method successfully, the document model will contain an updated refname and short ID
195 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#getRefName(org.collectionspace.services.common.context.ServiceContext, org.nuxeo.ecm.core.api.DocumentModel)
198 public RefName.RefNameInterface getRefName(ServiceContext ctx,
199 DocumentModel docModel) {
200 RefName.RefNameInterface refname = null;
203 String displayName = getPrimaryDisplayName(docModel, authorityItemCommonSchemaName,
204 getItemTermInfoGroupXPathBase(), AuthorityItemJAXBSchema.TERM_DISPLAY_NAME);
205 if (Tools.isEmpty(displayName)) {
206 throw new Exception("The displayName for this authority term was empty or not set.");
209 String shortIdentifier = (String) docModel.getProperty(authorityItemCommonSchemaName, AuthorityItemJAXBSchema.SHORT_IDENTIFIER);
210 if (Tools.isEmpty(shortIdentifier)) {
211 // We didn't find a short ID in the payload request, so we need to synthesize one.
212 shortIdentifier = handleDisplayNameAsShortIdentifier(docModel); // updates the document model with the new short ID as a side-effect
215 String authorityRefBaseName = getAuthorityRefNameBase();
216 if (Tools.isEmpty(authorityRefBaseName)) {
217 throw new Exception("Could not create the refName for this authority term, because the refName for its authority parent was empty.");
220 // Create the items refname using the parent's as a base
221 RefName.Authority parentsRefName = RefName.Authority.parse(authorityRefBaseName);
222 refname = RefName.buildAuthorityItem(parentsRefName, shortIdentifier, displayName);
223 // Now update the document model with the refname value
224 String refNameStr = refname.toString();
225 docModel.setProperty(authorityItemCommonSchemaName, AuthorityItemJAXBSchema.REF_NAME, refNameStr); // REM - This field is deprecated now that the refName is part of the collection_space core schema
227 } catch (Exception e) {
228 logger.error(e.getMessage(), e);
234 public void setInAuthority(String inAuthority) {
235 this.inAuthority = inAuthority;
238 public String getInAuthorityCsid() {
239 return this.inAuthority;
242 /** Subclasses may override this to customize the URI segment. */
243 public String getAuthorityServicePath() {
244 return getServiceContext().getServiceName().toLowerCase(); // Laramie20110510 CSPACE-3932
248 public String getUri(DocumentModel docModel) {
249 // Laramie20110510 CSPACE-3932
250 String authorityServicePath = getAuthorityServicePath();
251 if(inAuthority==null) { // Only true with the first document model received, on queries to wildcarded authorities
252 wildcardedAuthorityRequest = true;
254 // If this search crosses multiple authorities, get the inAuthority value
255 // from each record, rather than using the cached value from the first record
256 if(wildcardedAuthorityRequest) {
258 inAuthority = (String) docModel.getProperty(authorityItemCommonSchemaName,
259 AuthorityItemJAXBSchema.IN_AUTHORITY);
260 } catch (ClientException pe) {
261 throw new RuntimeException("Could not get parent specifier for item!");
264 return "/" + authorityServicePath + '/' + inAuthority + '/' + AuthorityClient.ITEMS + '/' + getCsid(docModel);
267 protected String getAuthorityRefNameBase() {
268 return this.authorityRefNameBase;
271 public void setAuthorityRefNameBase(String value) {
272 this.authorityRefNameBase = value;
276 * Note: the Vocabulary service's VocabularyItemDocumentModelHandler class overrides this method.
278 protected ListResultField getListResultsDisplayNameField() {
279 ListResultField result = new ListResultField();
280 // Per CSPACE-5132, the name of this element remains 'displayName'
281 // for backwards compatibility, although its value is obtained
282 // from the termDisplayName field.
284 // Update: this name is now being changed to 'termDisplayName', both
285 // because this is the actual field name and because the app layer
286 // work to convert over to this field is underway. Per Patrick, the
287 // app layer treats lists, in at least some context(s), as sparse record
288 // payloads, and thus fields in list results must all be present in
289 // (i.e. represent a strict subset of the fields in) record schemas.
293 // In CSPACE-5134, these list results will change substantially
294 // to return display names for both the preferred term and for
295 // each non-preferred term (if any).
296 result.setElement(AuthorityItemJAXBSchema.TERM_DISPLAY_NAME);
297 result.setXpath(NuxeoUtils.getPrimaryXPathPropertyName(
298 authorityItemCommonSchemaName, getItemTermInfoGroupXPathBase(), AuthorityItemJAXBSchema.TERM_DISPLAY_NAME));
304 * Note: the Vocabulary service's VocabularyItemDocumentModelHandler class overrides this method.
306 protected ListResultField getListResultsTermStatusField() {
307 ListResultField result = new ListResultField();
309 result.setElement(AuthorityItemJAXBSchema.TERM_STATUS);
310 result.setXpath(NuxeoUtils.getPrimaryXPathPropertyName(
311 authorityItemCommonSchemaName, getItemTermInfoGroupXPathBase(), AuthorityItemJAXBSchema.TERM_STATUS));
316 private boolean isTermDisplayName(String elName) {
317 return AuthorityItemJAXBSchema.TERM_DISPLAY_NAME.equals(elName) || VocabularyItemJAXBSchema.DISPLAY_NAME.equals(elName);
322 * @see org.collectionspace.services.nuxeo.client.java.DocHandlerBase#getListItemsArray()
324 * Note: We're updating the "global" service and tenant bindings instance here -the list instance here is
325 * a reference to the tenant bindings instance in the singleton ServiceMain.
328 public List<ListResultField> getListItemsArray() throws DocumentException {
329 List<ListResultField> list = super.getListItemsArray();
331 // One-time initialization for each authority item service.
332 if (isListItemArrayExtended() == false) {
333 synchronized(AuthorityItemDocumentModelHandler.class) {
334 if (isListItemArrayExtended() == false) {
335 int nFields = list.size();
336 // Ensure that each item in a list of Authority items includes
337 // a set of common fields, so we do not depend upon configuration
338 // for general logic.
339 List<Integer> termDisplayNamePositionsInList = new ArrayList<>();
340 boolean hasShortId = false;
341 boolean hasTermStatus = false;
342 for (int i = 0; i < nFields; i++) {
343 ListResultField field = list.get(i);
344 String elName = field.getElement();
345 if (isTermDisplayName(elName) == true) {
346 termDisplayNamePositionsInList.add(i);
347 } else if (AuthorityItemJAXBSchema.SHORT_IDENTIFIER.equals(elName)) {
349 } else if (AuthorityItemJAXBSchema.TERM_STATUS.equals(elName)) {
350 hasTermStatus = true;
354 ListResultField field;
356 // Certain fields in authority item list results
357 // are handled specially here
361 // Ignore (throw out) any configuration entries that
362 // specify how the termDisplayName field should be
363 // emitted in authority item lists. This field will
364 // be handled in a standardized manner (see block below).
365 if (termDisplayNamePositionsInList.isEmpty() == false) {
366 // Remove matching items starting at the end of the list
367 // and moving towards the start, so that reshuffling of
368 // list order doesn't alter the positions of earlier items
369 Collections.sort(termDisplayNamePositionsInList, Collections.reverseOrder());
370 for (int i : termDisplayNamePositionsInList) {
374 // termDisplayName values in authority item lists
375 // will be handled via code that emits display names
376 // for both the preferred term and all non-preferred
377 // terms (if any). The following is a placeholder
378 // entry that will trigger this code. See the
379 // getListResultValue() method in this class.
380 field = getListResultsDisplayNameField();
385 field = new ListResultField();
386 field.setElement(AuthorityItemJAXBSchema.SHORT_IDENTIFIER);
387 field.setXpath(AuthorityItemJAXBSchema.SHORT_IDENTIFIER);
392 if (!hasTermStatus) {
393 field = getListResultsTermStatusField();
399 setListItemArrayExtended(true);
400 } // end of synchronized block
407 * We consider workflow state changes as a change that should bump the revision number.
408 * Warning: This method might change the transitionDef's transtionName value
411 public void handleWorkflowTransition(ServiceContext ctx, DocumentWrapper<DocumentModel> wrapDoc, TransitionDef transitionDef) throws Exception {
412 // Decide whether or not to update the revision number
413 if (this.getShouldUpdateRevNumber() == true) { // We don't update the rev number of synchronization requests
414 updateRevNumbers(wrapDoc);
417 // We can't delete an authority item that has referencing records.
419 DocumentModel docModel = wrapDoc.getWrappedObject();
420 if (transitionDef.getName().equalsIgnoreCase(WorkflowClient.WORKFLOWTRANSITION_DELETE)) {
421 AuthorityRefDocList refsToAllObjects = getReferencingObjectsForStateTransitions(ctx, docModel, RefObjsSearchType.ALL);
422 AuthorityRefDocList refsToSoftDeletedObjects = getReferencingObjectsForStateTransitions(ctx, docModel, RefObjsSearchType.DELETED_ONLY);
423 if (refsToAllObjects.getTotalItems() > 0) {
424 if (refsToAllObjects.getTotalItems() > refsToSoftDeletedObjects.getTotalItems()) {
426 // If the number of refs to active objects is greater than the number of refs to
427 // soft deleted objects then we can't delete the item.
429 logger.error(String.format("Cannot delete authority item CSID='%s' because it still has records in the system that are referencing it.",
430 docModel.getName()));
431 if (logger.isWarnEnabled() == true) {
432 logReferencingObjects(docModel, refsToAllObjects);
435 throw new DocumentReferenceException(String.format("Cannot delete authority item '%s' because it still has records in the system that are referencing it. See the service layer log file for details.",
436 docModel.getName()));
448 protected boolean handleRelationsSync(DocumentWrapper<Object> wrapDoc) throws Exception {
449 boolean result = false;
450 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = getServiceContext();
453 // Get information about the local authority item so we can compare with corresponding item on the shared authority server
455 AuthorityItemSpecifier authorityItemSpecifier = (AuthorityItemSpecifier) wrapDoc.getWrappedObject();
456 DocumentModel itemDocModel = NuxeoUtils.getDocFromSpecifier(ctx, getRepositorySession(), getAuthorityItemCommonSchemaName(),
457 authorityItemSpecifier);
458 if (itemDocModel == null) {
459 throw new DocumentNotFoundException(String.format("Could not find authority item resource with CSID='%s'",
460 authorityItemSpecifier.getItemSpecifier().value));
462 Long localItemRev = (Long) NuxeoUtils.getProperyValue(itemDocModel, AuthorityItemJAXBSchema.REV);
463 Boolean localIsProposed = (Boolean) NuxeoUtils.getProperyValue(itemDocModel, AuthorityItemJAXBSchema.PROPOSED);
464 String localItemCsid = itemDocModel.getName();
465 String localItemWorkflowState = itemDocModel.getCurrentLifeCycleState();
466 String itemShortId = (String) NuxeoUtils.getProperyValue(itemDocModel, AuthorityItemJAXBSchema.SHORT_IDENTIFIER);
469 // Now get the item's Authority (the parent) information
471 DocumentModel authorityDocModel = NuxeoUtils.getDocFromSpecifier(ctx, getRepositorySession(), authorityCommonSchemaName,
472 authorityItemSpecifier.getParentSpecifier());
473 String authorityShortId = (String)NuxeoUtils.getProperyValue(authorityDocModel, AuthorityJAXBSchema.SHORT_IDENTIFIER);
474 String localParentCsid = authorityDocModel.getName();
475 String remoteClientConfigName = (String)NuxeoUtils.getProperyValue(authorityDocModel, AuthorityJAXBSchema.REMOTECLIENT_CONFIG_NAME);
477 // Using the short IDs of the local authority and item, create URN specifiers and retrieve the SAS authority item
479 AuthorityItemSpecifier sasAuthorityItemSpecifier = new AuthorityItemSpecifier(SpecifierForm.URN_NAME, authorityShortId, itemShortId);
480 // Get the shared authority server's copy
481 PoxPayloadIn sasPayloadIn = AuthorityServiceUtils.requestPayloadInFromRemoteServer(sasAuthorityItemSpecifier,
482 remoteClientConfigName, getAuthorityServicePath(), getEntityResponseType(), AuthorityClient.INCLUDE_RELATIONS);
485 // Get the RelationsCommonList and remove the CSIDs since they are for remote items only. We'll use
486 // the refnames in the payload instead to find the local CSIDs
488 PayloadInputPart relationsCommonListPart = sasPayloadIn.getPart(RelationClient.SERVICE_COMMON_LIST_NAME);
489 relationsCommonListPart.clearElementBody(); // clear the existing DOM element that was created from the incoming XML payload
490 RelationsCommonList rcl = (RelationsCommonList) relationsCommonListPart.getBody(); // Get the JAX-B object and clear the CSID values
491 for (RelationsCommonList.RelationListItem listItem : rcl.getRelationListItem()) {
492 // clear the remote relation item's CSID
493 listItem.setCsid(null);
494 // clear the remote subject's CSID
495 listItem.setSubjectCsid(null);
496 listItem.getSubject().setCsid(null);
497 listItem.getSubject().setUri(null);
498 // clear the remote object's CSID
499 listItem.setObjectCsid(null);
500 listItem.getObject().setCsid(null);
501 listItem.getObject().setUri(null);
505 // Remove all the payload parts except the relations part since we only want to sync the relationships
507 ArrayList<PayloadInputPart> newPartList = new ArrayList<PayloadInputPart>();
508 newPartList.add(relationsCommonListPart); // add our CSID filtered RelationsCommonList part
509 sasPayloadIn.setParts(newPartList);
510 sasPayloadIn = new PoxPayloadIn(sasPayloadIn.toXML()); // Builds a new payload using the current set of parts -i.e., just the relations part
512 sasPayloadIn = AuthorityServiceUtils.localizeRefNameDomains(ctx, sasPayloadIn); // We need to filter the domain name part of any and all refnames in the payload
513 AuthorityResource authorityResource = (AuthorityResource) ctx.getResource(getAuthorityServicePath());
514 PoxPayloadOut payloadOut = authorityResource.updateAuthorityItem(ctx,
515 ctx.getResourceMap(),
517 localParentCsid, // parent's CSID
518 localItemCsid, // item's CSID
519 sasPayloadIn, // the payload from the remote SAS
520 AuthorityServiceUtils.DONT_UPDATE_REV, // don't update the parent's revision number
521 AuthorityServiceUtils.NOT_PROPOSED, // The items is not proposed, make it a real SAS item now
522 AuthorityServiceUtils.SAS_ITEM); // Since we're sync'ing, this must be a SAS item
523 if (payloadOut != null) {
524 ctx.setOutput(payloadOut);
532 public boolean handleSync(DocumentWrapper<Object> wrapDoc) throws Exception {
533 boolean result = false;
535 if (this.getShouldSyncHierarchicalRelationships() == true) {
536 result = handleRelationsSync(wrapDoc);
538 result = handlePayloadSync(wrapDoc);
550 @SuppressWarnings({ "rawtypes", "unchecked" })
551 protected boolean handlePayloadSync(DocumentWrapper<Object> wrapDoc) throws Exception {
552 boolean result = false;
553 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = getServiceContext();
556 // Get information about the local authority item so we can compare with corresponding item on the shared authority server
558 AuthorityItemSpecifier authorityItemSpecifier = (AuthorityItemSpecifier) wrapDoc.getWrappedObject();
559 DocumentModel itemDocModel = NuxeoUtils.getDocFromSpecifier(ctx, getRepositorySession(), getAuthorityItemCommonSchemaName(),
560 authorityItemSpecifier);
561 if (itemDocModel == null) {
562 throw new DocumentNotFoundException(String.format("Could not find authority item resource with CSID='%s'",
563 authorityItemSpecifier.getItemSpecifier().value));
565 Long localItemRev = (Long) NuxeoUtils.getProperyValue(itemDocModel, AuthorityItemJAXBSchema.REV);
566 Boolean localIsProposed = (Boolean) NuxeoUtils.getProperyValue(itemDocModel, AuthorityItemJAXBSchema.PROPOSED);
567 String localItemCsid = itemDocModel.getName();
568 String localItemWorkflowState = itemDocModel.getCurrentLifeCycleState();
569 String itemShortId = (String) NuxeoUtils.getProperyValue(itemDocModel, AuthorityItemJAXBSchema.SHORT_IDENTIFIER);
572 // Now get the item's Authority (the parent) information
574 DocumentModel authorityDocModel = NuxeoUtils.getDocFromSpecifier(ctx, getRepositorySession(), authorityCommonSchemaName,
575 authorityItemSpecifier.getParentSpecifier());
576 String authorityShortId = (String) NuxeoUtils.getProperyValue(authorityDocModel, AuthorityJAXBSchema.SHORT_IDENTIFIER);
577 String localParentCsid = authorityDocModel.getName();
578 String remoteClientConfigName = (String)NuxeoUtils.getProperyValue(authorityDocModel, AuthorityJAXBSchema.REMOTECLIENT_CONFIG_NAME);
581 // Using the short IDs of the local authority and item, create URN specifiers and retrieve the SAS authority item
583 AuthorityItemSpecifier sasAuthorityItemSpecifier = new AuthorityItemSpecifier(SpecifierForm.URN_NAME, authorityShortId, itemShortId);
584 // Get the shared authority server's copy
585 PoxPayloadIn sasPayloadIn = AuthorityServiceUtils.requestPayloadInFromRemoteServer(sasAuthorityItemSpecifier,
586 remoteClientConfigName, getAuthorityServicePath(), getEntityResponseType(), AuthorityClient.DONT_INCLUDE_RELATIONS);
587 Long sasRev = getRevision(sasPayloadIn);
588 String sasWorkflowState = getWorkflowState(sasPayloadIn);
590 // If the shared authority item is newer, update our local copy
592 if (sasRev > localItemRev || localIsProposed || ctx.shouldForceSync()) {
593 sasPayloadIn = AuthorityServiceUtils.localizeRefNameDomains(ctx, sasPayloadIn); // We need to filter the domain name part of any and all refnames in the payload
594 AuthorityResource authorityResource = (AuthorityResource) ctx.getResource(getAuthorityServicePath());
595 PoxPayloadOut payloadOut = authorityResource.updateAuthorityItem(ctx,
596 ctx.getResourceMap(),
598 localParentCsid, // parent's CSID
599 localItemCsid, // item's CSID
600 sasPayloadIn, // the payload from the remote SAS
601 AuthorityServiceUtils.DONT_UPDATE_REV, // don't update the parent's revision number
602 AuthorityServiceUtils.NOT_PROPOSED, // The items is not proposed, make it a real SAS item now
603 AuthorityServiceUtils.SAS_ITEM); // Since we're sync'ing, this must be a SAS item
604 if (payloadOut != null) {
605 ctx.setOutput(payloadOut);
610 // Check to see if we need to update the local items's workflow state to reflect that of the remote's
612 List<String> transitionList = getTransitionList(sasWorkflowState, localItemWorkflowState);
613 if (transitionList.isEmpty() == false) {
614 AuthorityResource authorityResource = (AuthorityResource) ctx.getResource(getAuthorityServicePath()); // Get the authority (parent) client not the item client
616 // We need to move the local item to the SAS workflow state. This might involve multiple transitions.
618 for (String transition:transitionList) {
620 authorityResource.updateItemWorkflowWithTransition(ctx, localParentCsid, localItemCsid, transition, AuthorityServiceUtils.DONT_UPDATE_REV);
621 } catch (DocumentReferenceException de) {
623 // This exception means we tried unsuccessfully to soft-delete (workflow transition 'delete') an item that still has references to it from other records.
625 AuthorityServiceUtils.setAuthorityItemDeprecated(ctx, itemDocModel, authorityItemCommonSchemaName, AuthorityServiceUtils.DEPRECATED); // Since we can't sof-delete it, we need to mark it as deprecated since it is soft-deleted on the SAS
626 logger.warn(String.format("Could not transition item CSID='%s' from workflow state '%s' to '%s'. Check the services log file for details.",
627 localItemCsid, localItemWorkflowState, sasWorkflowState));
637 * We need to change the local item's state to one that maps to the replication server's workflow
638 * state. This might involve making multiple transitions.
641 * See table at https://collectionspace.atlassian.net/wiki/spaces/SDR/pages/665886940/Workflow+transitions+to+map+SAS+item+states+to+Local+item+states
642 * (was https://wiki.collectionspace.org/pages/viewpage.action?pageId=162496564)
645 private List<String> getTransitionList(String sasWorkflowState, String localItemWorkflowState) throws DocumentException {
646 List<String> result = new ArrayList<String>();
648 // The first set of conditions maps a replication-server "project" state to a local client state of "replicated"
650 if (sasWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_PROJECT) && localItemWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_PROJECT)) {
651 result.add(WorkflowClient.WORKFLOWTRANSITION_REPLICATE);
652 } else if (sasWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_PROJECT) && localItemWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
653 result.add(WorkflowClient.WORKFLOWTRANSITION_UNDELETE);
654 result.add(WorkflowClient.WORKFLOWTRANSITION_REPLICATE);
655 } else if (sasWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_PROJECT) && localItemWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_REPLICATED)) {
656 // Do nothing. We're good with this state
657 } else if (sasWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_PROJECT) && localItemWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_REPLICATED_DELETED)) {
658 result.add(WorkflowClient.WORKFLOWTRANSITION_UNDELETE);
659 } else if (sasWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_PROJECT) && localItemWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_REPLICATED_DEPRECATED)) {
660 result.add(WorkflowClient.WORKFLOWTRANSITION_UNDEPRECATE);
661 } else if (sasWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_PROJECT) && localItemWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_REPLICATED_DEPRECATED_DELETED)) {
662 result.add(WorkflowClient.WORKFLOWTRANSITION_UNDELETE);
663 result.add(WorkflowClient.WORKFLOWTRANSITION_UNDEPRECATE);
665 // The second set of conditions maps a replication-server "deleted" state to a local client state of "deleted"
667 } else if (sasWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_DELETED) && localItemWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_PROJECT)) {
668 result.add(WorkflowClient.WORKFLOWTRANSITION_REPLICATE);
669 result.add(WorkflowClient.WORKFLOWTRANSITION_DELETE);
670 } else if (sasWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_DELETED) && localItemWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
671 result.add(WorkflowClient.WORKFLOWTRANSITION_REPLICATE);
672 } else if (sasWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_DELETED) && localItemWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_REPLICATED)) {
673 result.add(WorkflowClient.WORKFLOWTRANSITION_DELETE);
674 } else if (sasWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_DELETED) && localItemWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_REPLICATED_DELETED)) {
675 // Do nothing. We're good with this state
676 } else if (sasWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_DELETED) && localItemWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_REPLICATED_DEPRECATED)) {
677 result.add(WorkflowClient.WORKFLOWTRANSITION_UNDEPRECATE);
678 result.add(WorkflowClient.WORKFLOWTRANSITION_DELETE);
679 } else if (sasWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_DELETED) && localItemWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_REPLICATED_DEPRECATED_DELETED)) {
680 result.add(WorkflowClient.WORKFLOWTRANSITION_UNDEPRECATE);
682 // The third set of conditions maps a replication-server "replicated" state to a local state of "replicated"
684 } else if (sasWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_REPLICATED) && localItemWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_PROJECT)) {
685 result.add(WorkflowClient.WORKFLOWTRANSITION_REPLICATE);
686 } else if (sasWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_REPLICATED) && localItemWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
687 result.add(WorkflowClient.WORKFLOWTRANSITION_UNDELETE);
688 result.add(WorkflowClient.WORKFLOWTRANSITION_REPLICATE);
689 } else if (sasWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_REPLICATED) && localItemWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_REPLICATED)) {
690 // Do nothing. We're good with this state
691 } else if (sasWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_REPLICATED) && localItemWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_REPLICATED_DELETED)) {
692 result.add(WorkflowClient.WORKFLOWTRANSITION_UNDELETE);
693 } else if (sasWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_REPLICATED) && localItemWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_REPLICATED_DEPRECATED)) {
694 result.add(WorkflowClient.WORKFLOWTRANSITION_UNDEPRECATE);
695 } else if (sasWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_REPLICATED) && localItemWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_REPLICATED_DEPRECATED_DELETED)) {
696 result.add(WorkflowClient.WORKFLOWTRANSITION_UNDELETE);
697 result.add(WorkflowClient.WORKFLOWTRANSITION_UNDEPRECATE);
699 // The fourth set of conditions maps a replicatation-server "deprecated" state to a local state of "replicated_deprecated"
701 } else if (sasWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_DEPRECATED) && localItemWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_PROJECT)) {
702 result.add(WorkflowClient.WORKFLOWTRANSITION_REPLICATE);
703 result.add(WorkflowClient.WORKFLOWTRANSITION_DEPRECATE);
704 } else if (sasWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_DEPRECATED) && localItemWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
705 result.add(WorkflowClient.WORKFLOWTRANSITION_UNDELETE);
706 result.add(WorkflowClient.WORKFLOWTRANSITION_REPLICATE);
707 result.add(WorkflowClient.WORKFLOWTRANSITION_DEPRECATE);
708 } else if (sasWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_DEPRECATED) && localItemWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_REPLICATED)) {
709 result.add(WorkflowClient.WORKFLOWTRANSITION_DEPRECATE);
710 } else if (sasWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_DEPRECATED) && localItemWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_REPLICATED_DELETED)) {
711 result.add(WorkflowClient.WORKFLOWTRANSITION_UNDELETE);
712 result.add(WorkflowClient.WORKFLOWTRANSITION_DEPRECATE);
713 } else if (sasWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_DEPRECATED) && localItemWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_REPLICATED_DEPRECATED)) {
715 } else if (sasWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_DEPRECATED) && localItemWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_REPLICATED_DEPRECATED_DELETED)) {
716 result.add(WorkflowClient.WORKFLOWTRANSITION_UNDELETE);
718 // The last set of conditions maps a replication-server "replicated_deleted" state to a local client state of "deleted"
720 } else if (sasWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_REPLICATED_DELETED) && localItemWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_PROJECT)) {
721 result.add(WorkflowClient.WORKFLOWTRANSITION_REPLICATE);
722 result.add(WorkflowClient.WORKFLOWTRANSITION_DELETE);
723 } else if (sasWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_REPLICATED_DELETED) && localItemWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
724 result.add(WorkflowClient.WORKFLOWTRANSITION_REPLICATE);
725 } else if (sasWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_REPLICATED_DELETED) && localItemWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_REPLICATED)) {
726 result.add(WorkflowClient.WORKFLOWTRANSITION_DELETE);
727 } else if (sasWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_REPLICATED_DELETED) && localItemWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_REPLICATED_DELETED)) {
728 // Do nothing. We're good with this state
729 } else if (sasWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_REPLICATED_DELETED) && localItemWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_REPLICATED_DEPRECATED)) {
730 result.add(WorkflowClient.WORKFLOWTRANSITION_UNDEPRECATE);
731 result.add(WorkflowClient.WORKFLOWTRANSITION_DELETE);
732 } else if (sasWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_REPLICATED_DELETED) && localItemWorkflowState.equals(WorkflowClient.WORKFLOWSTATE_REPLICATED_DEPRECATED_DELETED)) {
733 result.add(WorkflowClient.WORKFLOWTRANSITION_UNDEPRECATE);
736 // If we get here, we've encountered a SAS workflow state that we don't recognize.
738 throw new DocumentException(String.format("Encountered an invalid workflow state of '%s' on a SAS authority item.", sasWorkflowState));
745 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
748 public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
749 // first fill all the parts of the document, refname and short ID get set as well
750 super.handleCreate(wrapDoc);
751 // Ensure we have required fields set properly
752 handleInAuthority(wrapDoc.getWrappedObject());
755 enum RefObjsSearchType {
756 ALL, NON_DELETED, DELETED_ONLY
760 * This method gets called after the primary update to an authority item has happened. If the authority item's refName
761 * has changed, then we need to updated all the records that use that refname with the new/updated version
765 @SuppressWarnings({ "rawtypes", "unchecked" })
767 public boolean handleDelete(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
768 boolean result = true;
770 ServiceContext ctx = getServiceContext();
771 DocumentModel docModel = wrapDoc.getWrappedObject();
773 AuthorityRefDocList refsToAllObjects = getReferencingObjectsForStateTransitions(ctx, docModel, RefObjsSearchType.ALL);
774 AuthorityRefDocList refsToSoftDeletedObjects = getReferencingObjectsForStateTransitions(ctx, docModel, RefObjsSearchType.DELETED_ONLY);
775 if (refsToAllObjects.getTotalItems() > 0) {
776 if (refsToAllObjects.getTotalItems() > refsToSoftDeletedObjects.getTotalItems()) {
778 // If the number of refs to active objects is greater than the number of refs to
779 // soft deleted objects then we can't delete the item.
781 logger.error(String.format("Cannot delete authority item CSID='%s' because it still has %d records in the system that are referencing it.",
782 docModel.getName(), refsToSoftDeletedObjects.getTotalItems()));
783 if (logger.isWarnEnabled() == true) {
784 logReferencingObjects(docModel, refsToAllObjects);
787 throw new DocumentReferenceException(String.format("Cannot delete authority item '%s' because it still has records in the system that are referencing it. See the service layer log file for details.",
788 docModel.getName()));
791 // If all the refs are to soft-deleted objects, we should soft-delete this authority item instead of hard-deleting it and instead of failing.
793 String parentCsid = (String) NuxeoUtils.getProperyValue(docModel, AuthorityItemJAXBSchema.IN_AUTHORITY);
794 String itemCsid = docModel.getName();
795 AuthorityResource authorityResource = (AuthorityResource) ctx.getResource(getAuthorityServicePath());
796 authorityResource.updateItemWorkflowWithTransition(ctx, parentCsid, itemCsid, WorkflowClient.WORKFLOWTRANSITION_DELETE,
797 this.getShouldUpdateRevNumber());
798 result = false; // Don't delete since we just soft-deleted it.
803 // Since we've changed the state of the parent by deleting (or soft-deleting) one of its items, we might need to update the parent rev number
805 if (getShouldUpdateRevNumber() == true) {
806 updateRevNumbers(wrapDoc);
813 * Checks to see if an authority item has referencing objects.
820 @SuppressWarnings("rawtypes")
821 private AuthorityRefDocList getReferencingObjectsForStateTransitions(
823 DocumentModel docModel,
824 RefObjsSearchType searchType) throws Exception {
825 AuthorityRefDocList referenceList = null;
827 if (ctx.getUriInfo() == null) {
829 // We need a UriInfo object so we can pass "query" params to the AuthorityResource's getReferencingObjects() method
831 ctx.setUriInfo(this.getServiceContext().getUriInfo()); // try to get a UriInfo instance from the handler's context
835 // Since the call to get referencing objects might indirectly use the WorkflowClient.WORKFLOW_QUERY_NONDELETED query param, we need to
836 // temporarily remove that query param if it is set. If set, we'll save the value and reset once we're finished.
838 boolean doesContainValue = ctx.getUriInfo().getQueryParameters().containsKey(WorkflowClient.WORKFLOW_QUERY_DELETED_QP);
839 String previousValue = ctx.getUriInfo().getQueryParameters().getFirst(WorkflowClient.WORKFLOW_QUERY_DELETED_QP);
842 if (doesContainValue) {
843 ctx.getUriInfo().getQueryParameters().remove(WorkflowClient.WORKFLOW_QUERY_DELETED_QP);
845 AuthorityResource authorityResource = (AuthorityResource)ctx.getResource(getAuthorityServicePath());
846 referenceList = getReferencingObjects(authorityResource, ctx, docModel, searchType, PAGE_NUM_FROM_QUERYPARAMS, PAGE_SIZE_FROM_QUERYPARAMS, true, true); // useDefaultOrderByClause=true, computeTotal=true
848 if (doesContainValue) {
849 ctx.getUriInfo().getQueryParameters().addFirst(WorkflowClient.WORKFLOW_QUERY_DELETED_QP, previousValue);
853 return referenceList;
856 @SuppressWarnings("rawtypes")
857 private AuthorityRefDocList getReferencingObjectsForMarkingTerm(
859 DocumentModel docModel,
860 RefObjsSearchType searchType) throws Exception {
861 AuthorityRefDocList referenceList = null;
863 if (ctx.getUriInfo() == null) {
865 // We need a UriInfo object so we can pass "query" params to the AuthorityResource's getReferencingObjects() method
867 ctx.setUriInfo(this.getServiceContext().getUriInfo()); // try to get a UriInfo instance from the handler's context
871 // Since the call to get referencing objects might indirectly use the WorkflowClient.WORKFLOW_QUERY_NONDELETED query param, we need to
872 // temporarily remove that query param if it is set. If set, we'll save the value and reset once we're finished.
874 boolean doesContainValue = ctx.getUriInfo().getQueryParameters().containsKey(WorkflowClient.WORKFLOW_QUERY_DELETED_QP);
875 String previousValue = ctx.getUriInfo().getQueryParameters().getFirst(WorkflowClient.WORKFLOW_QUERY_DELETED_QP);
878 if (doesContainValue) {
879 ctx.getUriInfo().getQueryParameters().remove(WorkflowClient.WORKFLOW_QUERY_DELETED_QP);
881 AuthorityResource authorityResource = (AuthorityResource)ctx.getResource(getAuthorityServicePath());
882 referenceList = getReferencingObjects(authorityResource, ctx, docModel, searchType, 0, 1, false, false); // pageNum=0, pageSize=1, useDefaultOrderClause=false, computeTotal=false
884 if (doesContainValue) {
885 ctx.getUriInfo().getQueryParameters().addFirst(WorkflowClient.WORKFLOW_QUERY_DELETED_QP, previousValue);
889 return referenceList;
892 @SuppressWarnings({ "rawtypes", "unchecked" })
893 private AuthorityRefDocList getReferencingObjects(
894 AuthorityResource authorityResource,
896 DocumentModel docModel,
897 RefObjsSearchType searchType,
900 boolean useDefaultOrderByClause,
901 boolean computeTotal) throws Exception {
902 AuthorityRefDocList result = null;
904 String inAuthorityCsid = (String) docModel.getProperty(authorityItemCommonSchemaName, AuthorityItemJAXBSchema.IN_AUTHORITY);
905 String itemCsid = docModel.getName();
908 switch (searchType) {
910 // By default, get get everything
913 // Get only non-deleted objects
914 ctx.getUriInfo().getQueryParameters().addFirst(WorkflowClient.WORKFLOW_QUERY_DELETED_QP, Boolean.FALSE.toString()); // Add the wf_deleted=false query param to exclude soft-deleted items
917 // Get only deleted objects
918 ctx.getUriInfo().getQueryParameters().addFirst(WorkflowClient.WORKFLOW_QUERY_ONLY_DELETED_QP, Boolean.TRUE.toString()); // Add the wf_only_deleted query param to get only soft-deleted items
921 result = authorityResource.getReferencingObjects(ctx, inAuthorityCsid, itemCsid, ctx.getUriInfo(), pageNum, pageSize, useDefaultOrderByClause, computeTotal);
925 // Cleanup query params
927 switch (searchType) {
931 ctx.getUriInfo().getQueryParameters().remove(WorkflowClient.WORKFLOWSTATE_DELETED);
934 ctx.getUriInfo().getQueryParameters().remove(WorkflowClient.WORKFLOW_QUERY_ONLY_DELETED_QP);
942 private void logReferencingObjects(DocumentModel docModel, AuthorityRefDocList refObjs) {
943 List<AuthorityRefDocList.AuthorityRefDocItem> items = refObjs.getAuthorityRefDocItem();
944 logger.warn(String.format("The authority item CSID='%s' has the following references:", docModel.getName()));
946 for (AuthorityRefDocList.AuthorityRefDocItem item : items) {
947 if (item.getWorkflowState().contains(WorkflowClient.WORKFLOWSTATE_DELETED) == false) {
948 logger.warn(docModel.getName() + " referenced by : list-item[" + i + "] "
949 + item.getDocType() + "("
950 + item.getDocId() + ") Name:["
951 + item.getDocName() + "] Number:["
952 + item.getDocNumber() + "] in field:["
953 + item.getSourceField() + "]");
960 * This method gets called after the primary update to an authority item has happened. If the authority item's refName
961 * has changed, then we need to updated all the records that use that refname with the new/updated version
964 * @see org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl#completeUpdate(org.collectionspace.services.common.document.DocumentWrapper)
966 public void completeUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
967 // Must call our super class' version first
968 super.completeUpdate(wrapDoc);
971 // Look for and update authority references with the updated refName
973 if (hasRefNameUpdate() == true) {
974 // We have work to do.
975 if (logger.isDebugEnabled()) {
976 final String EOL = System.getProperty("line.separator");
977 logger.debug("Need to find and update references to authority item." + EOL
978 + " Old refName" + oldRefNameOnUpdate + EOL
979 + " New refName" + newRefNameOnUpdate);
981 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = getServiceContext();
982 RepositoryClient<PoxPayloadIn, PoxPayloadOut> repoClient = getRepositoryClient(ctx);
983 CoreSessionInterface repoSession = this.getRepositorySession();
985 // Update all the existing records that have a field with the old refName in it
986 int nUpdated = RefNameServiceUtils.updateAuthorityRefDocs(ctx, repoClient, repoSession,
987 oldRefNameOnUpdate, newRefNameOnUpdate, getRefPropName());
989 // Finished so log a message.
990 if (logger.isDebugEnabled()) {
991 logger.debug("Updated " + nUpdated + " instances of oldRefName to newRefName");
997 * Note that the Vocabulary service's document-model for items overrides this method.
999 protected String getPrimaryDisplayName(DocumentModel docModel, String schema,
1000 String complexPropertyName, String fieldName) {
1001 String result = null;
1003 result = getStringValueInPrimaryRepeatingComplexProperty(docModel, schema, complexPropertyName, fieldName);
1009 * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
1012 // FIXME: Once we remove the refName field from the authority item schemas, we can remove this override method since our super does everthing for us now.
1014 public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
1015 // Must call our super's version first, this updates the core schema and the relationship records to deal with possible refName changes/update
1016 super.handleUpdate(wrapDoc);
1017 if (this.hasRefNameUpdate() == true) {
1018 DocumentModel docModel = wrapDoc.getWrappedObject();
1019 docModel.setProperty(authorityItemCommonSchemaName, AuthorityItemJAXBSchema.REF_NAME, this.newRefNameOnUpdate); // This field is deprecated since it is now a duplicate of what is in the collectionspace_core:refName field
1024 // Handles both update calls (PUTS) AND create calls (POSTS)
1026 public void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc, Action action) throws Exception {
1027 super.fillAllParts(wrapDoc, action);
1028 DocumentModel documentModel = wrapDoc.getWrappedObject();
1031 // Update the record's revision number on both CREATE and UPDATE actions (as long as it is NOT a SAS authority item)
1033 Boolean propertyValue = (Boolean) documentModel.getProperty(authorityItemCommonSchemaName, AuthorityItemJAXBSchema.SAS);
1034 boolean isMarkedAsSASItem = propertyValue != null ? propertyValue : false;
1035 if (this.getShouldUpdateRevNumber() == true && !isMarkedAsSASItem) { // We won't update rev numbers on synchronization with SAS items and on local changes to SAS items
1036 updateRevNumbers(wrapDoc);
1039 if (getShouldUpdateSASFields() == true) {
1041 // If this is a proposed item (not part of the SAS), mark it as such
1043 documentModel.setProperty(authorityItemCommonSchemaName, AuthorityItemJAXBSchema.PROPOSED,
1044 new Boolean(this.getIsProposed()));
1046 // If it is a SAS authority item, mark it as such
1048 documentModel.setProperty(authorityItemCommonSchemaName, AuthorityItemJAXBSchema.SAS,
1049 new Boolean(this.getIsSASItem()));
1054 * Update the revision number of both the item and the item's parent.
1058 protected void updateRevNumbers(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
1059 DocumentModel documentModel = wrapDoc.getWrappedObject();
1060 Long rev = (Long)documentModel.getProperty(authorityItemCommonSchemaName, AuthorityItemJAXBSchema.REV);
1066 documentModel.setProperty(authorityItemCommonSchemaName, AuthorityItemJAXBSchema.REV, rev);
1068 // Next, update the inAuthority (the parent's) rev number
1070 String inAuthorityCsid = this.getInAuthorityCsid();
1071 if (inAuthorityCsid == null) {
1072 // When inAuthorityCsid is null, it usually means we're performing and update or synch with the SAS
1073 inAuthorityCsid = (String)documentModel.getProperty(authorityItemCommonSchemaName, AuthorityItemJAXBSchema.IN_AUTHORITY);
1075 DocumentModel inAuthorityDocModel = NuxeoUtils.getDocFromCsid(getServiceContext(), getRepositorySession(), inAuthorityCsid);
1076 if (inAuthorityDocModel != null) {
1077 Long parentRev = (Long)inAuthorityDocModel.getProperty(getParentCommonSchemaName(), AuthorityJAXBSchema.REV);
1078 if (parentRev == null) {
1079 parentRev = new Long(0);
1082 inAuthorityDocModel.setProperty(getParentCommonSchemaName(), AuthorityJAXBSchema.REV, parentRev);
1083 getRepositorySession().saveDocument(inAuthorityDocModel);
1085 logger.warn(String.format("Containing authority '%s' for item '%s' has been deleted. Item is orphaned, so revision numbers can't be updated.",
1086 inAuthorityCsid, documentModel.getName()));
1091 * If no short identifier was provided in the input payload, generate a
1092 * short identifier from the preferred term display name or term name.
1094 private String handleDisplayNameAsShortIdentifier(DocumentModel docModel) throws Exception {
1095 String result = (String) docModel.getProperty(authorityItemCommonSchemaName,
1096 AuthorityItemJAXBSchema.SHORT_IDENTIFIER);
1098 if (Tools.isEmpty(result)) {
1099 String termDisplayName = getPrimaryDisplayName(
1100 docModel, authorityItemCommonSchemaName,
1101 getItemTermInfoGroupXPathBase(),
1102 AuthorityItemJAXBSchema.TERM_DISPLAY_NAME);
1104 String termName = getPrimaryDisplayName(
1105 docModel, authorityItemCommonSchemaName,
1106 getItemTermInfoGroupXPathBase(),
1107 AuthorityItemJAXBSchema.TERM_NAME);
1109 String generatedShortIdentifier = AuthorityIdentifierUtils.generateShortIdentifierFromDisplayName(termDisplayName,
1111 docModel.setProperty(authorityItemCommonSchemaName, AuthorityItemJAXBSchema.SHORT_IDENTIFIER,
1112 generatedShortIdentifier);
1113 result = generatedShortIdentifier;
1120 * Generate a refName for the authority item from the short identifier
1123 * All refNames for authority items are generated. If a client supplies
1124 * a refName, it will be overwritten during create (per this method)
1125 * or discarded during update (per filterReadOnlyPropertiesForPart).
1127 * @see #filterReadOnlyPropertiesForPart(Map<String, Object>, org.collectionspace.services.common.service.ObjectPartType)
1130 protected String updateRefnameForAuthorityItem(DocumentModel docModel,
1131 String schemaName) throws Exception {
1132 String result = null;
1134 RefName.RefNameInterface refname = getRefName(getServiceContext(), docModel);
1135 String refNameStr = refname.toString();
1136 docModel.setProperty(schemaName, AuthorityItemJAXBSchema.REF_NAME, refNameStr);
1137 result = refNameStr;
1143 * Check the logic around the parent pointer. Note that we only need do this on
1144 * create, since we have logic to make this read-only on update.
1148 * @throws Exception the exception
1150 private void handleInAuthority(DocumentModel docModel) throws Exception {
1151 if (inAuthority == null) { // Only happens on queries to wildcarded authorities
1152 throw new IllegalStateException("Trying to Create an object with no inAuthority value!");
1154 docModel.setProperty(authorityItemCommonSchemaName, AuthorityItemJAXBSchema.IN_AUTHORITY, inAuthority);
1158 * Returns a list of records that reference this authority item
1161 * @param uriTemplateRegistry
1162 * @param serviceTypes
1163 * @param propertyName
1168 public AuthorityRefDocList getReferencingObjects(
1169 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
1170 List<String> serviceTypes,
1171 String propertyName,
1175 boolean useDefaultOrderByClause,
1176 boolean computeTotal) throws Exception {
1177 AuthorityRefDocList authRefDocList = null;
1178 CoreSessionInterface repoSession = (CoreSessionInterface) ctx.getCurrentRepositorySession();
1179 boolean releaseRepoSession = false;
1182 NuxeoRepositoryClientImpl repoClient = (NuxeoRepositoryClientImpl)this.getRepositoryClient(ctx);
1183 repoSession = this.getRepositorySession();
1184 if (repoSession == null) {
1185 repoSession = repoClient.getRepositorySession(ctx);
1186 releaseRepoSession = true;
1188 DocumentFilter myFilter = getDocumentFilter();
1189 if (pageSize != null) {
1190 myFilter.setPageSize(pageSize);
1192 if (pageNum != null) {
1193 myFilter.setStartPage(pageNum);
1195 myFilter.setUseDefaultOrderByClause(useDefaultOrderByClause);
1198 DocumentWrapper<DocumentModel> wrapper = repoClient.getDoc(repoSession, ctx, itemcsid);
1199 DocumentModel docModel = wrapper.getWrappedObject();
1200 String refName = (String) NuxeoUtils.getProperyValue(docModel, AuthorityItemJAXBSchema.REF_NAME); //docModel.getPropertyValue(AuthorityItemJAXBSchema.REF_NAME);
1201 authRefDocList = RefNameServiceUtils.getAuthorityRefDocs(
1209 useDefaultOrderByClause,
1210 computeTotal /*computeTotal*/);
1211 } catch (PropertyException pe) {
1213 } catch (DocumentException de) {
1215 } catch (Exception e) {
1216 if (logger.isDebugEnabled()) {
1217 logger.debug("Caught exception ", e);
1219 throw new DocumentException(e);
1221 // If we got/aquired a new seesion then we're responsible for releasing it.
1222 if (releaseRepoSession && repoSession != null) {
1223 repoClient.releaseRepositorySession(ctx, repoSession);
1226 } catch (Exception e) {
1227 if (logger.isDebugEnabled()) {
1228 logger.debug("Caught exception ", e);
1230 throw new DocumentException(e);
1233 return authRefDocList;
1237 * @see org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl#extractPart(org.nuxeo.ecm.core.api.DocumentModel, java.lang.String, org.collectionspace.services.common.service.ObjectPartType)
1240 protected Map<String, Object> extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
1242 Map<String, Object> unQObjectProperties = super.extractPart(docModel, schema, partMeta);
1244 // Add the CSID to the common part, since they may have fetched via the shortId.
1245 if (partMeta.getLabel().equalsIgnoreCase(authorityItemCommonSchemaName)) {
1246 String csid = getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
1247 unQObjectProperties.put("csid", csid);
1250 return unQObjectProperties;
1254 * Filters out selected values supplied in an update request.
1256 * For example, filters out AuthorityItemJAXBSchema.IN_AUTHORITY, to ensure
1257 * that the link to the item's parent remains untouched.
1259 * @param objectProps the properties filtered out from the update payload
1260 * @param partMeta metadata for the object to fill
1263 public void filterReadOnlyPropertiesForPart(
1264 Map<String, Object> objectProps, ObjectPartType partMeta) {
1265 super.filterReadOnlyPropertiesForPart(objectProps, partMeta);
1266 String commonPartLabel = getServiceContext().getCommonPartLabel();
1267 if (partMeta.getLabel().equalsIgnoreCase(commonPartLabel)) {
1268 objectProps.remove(AuthorityItemJAXBSchema.IN_AUTHORITY);
1269 objectProps.remove(AuthorityItemJAXBSchema.CSID);
1270 if (getServiceContext().shouldForceUpdateRefnameReferences() == false) {
1271 objectProps.remove(AuthorityJAXBSchema.SHORT_IDENTIFIER);
1273 objectProps.remove(AuthorityItemJAXBSchema.REF_NAME);
1278 * Returns the items in a list of term display names whose names contain
1279 * a partial term (as might be submitted in a search query, for instance).
1280 * @param termDisplayNameList a list of term display names.
1281 * @param partialTerm a partial term display name; that is, a portion
1282 * of a display name that might be expected to match 0-n terms in the list.
1283 * @return a list of term display names that matches the partial term.
1284 * Matches are case-insensitive. As well, before matching is performed, any
1285 * special-purpose characters that may appear in the partial term (such as
1286 * wildcards and anchor characters) are filtered out from both compared terms.
1288 protected List<String> getPartialTermDisplayNameMatches(List<String> termDisplayNameList, String partialTerm) {
1289 List<String> result = new ArrayList<>();
1290 String partialTermMatchExpression = filterAnchorAndWildcardChars(partialTerm).toLowerCase();
1292 for (String termDisplayName : termDisplayNameList) {
1293 if (termDisplayName.toLowerCase()
1294 .matches(partialTermMatchExpression) == true) {
1295 result.add(termDisplayName);
1298 } catch (PatternSyntaxException pse) {
1299 logger.warn("Error in regex match pattern '%s' for term display names: %s",
1300 partialTermMatchExpression, pse.getMessage());
1306 * Filters user-supplied anchor and wildcard characters in a string,
1307 * replacing them with equivalent regular expressions.
1308 * @param term a term in which to filter anchor and wildcard characters.
1309 * @return the term with those characters filtered.
1311 protected String filterAnchorAndWildcardChars(String term) {
1312 if (Tools.isBlank(term)) {
1315 if (term.length() < 3) {
1318 if (logger.isTraceEnabled()) {
1319 logger.trace(String.format("Term = %s", term));
1321 Boolean anchorAtStart = false;
1322 Boolean anchorAtEnd = false;
1323 String filteredTerm;
1324 StringBuilder filteredTermBuilder = new StringBuilder(term);
1325 // Term contains no anchor or wildcard characters.
1326 if ( (! term.contains(NuxeoRepositoryClientImpl.USER_SUPPLIED_ANCHOR_CHAR))
1327 && (! term.contains(NuxeoRepositoryClientImpl.USER_SUPPLIED_WILDCARD)) ) {
1328 filteredTerm = term;
1330 // Term contains at least one such character.
1332 // Filter the starting anchor or wildcard character, if any.
1333 String firstChar = filteredTermBuilder.substring(0,1);
1334 switch (firstChar) {
1335 case NuxeoRepositoryClientImpl.USER_SUPPLIED_ANCHOR_CHAR:
1336 anchorAtStart = true;
1338 case NuxeoRepositoryClientImpl.USER_SUPPLIED_WILDCARD:
1339 filteredTermBuilder.deleteCharAt(0);
1342 if (logger.isTraceEnabled()) {
1343 logger.trace(String.format("After first char filtering = %s", filteredTermBuilder.toString()));
1345 // Filter the ending anchor or wildcard character, if any.
1346 int lastPos = filteredTermBuilder.length() - 1;
1347 String lastChar = filteredTermBuilder.substring(lastPos);
1349 case NuxeoRepositoryClientImpl.USER_SUPPLIED_ANCHOR_CHAR:
1350 filteredTermBuilder.deleteCharAt(lastPos);
1351 filteredTermBuilder.insert(filteredTermBuilder.length(), NuxeoRepositoryClientImpl.ENDING_ANCHOR_CHAR);
1354 case NuxeoRepositoryClientImpl.USER_SUPPLIED_WILDCARD:
1355 filteredTermBuilder.deleteCharAt(lastPos);
1358 if (logger.isTraceEnabled()) {
1359 logger.trace(String.format("After last char filtering = %s", filteredTermBuilder.toString()));
1361 filteredTerm = filteredTermBuilder.toString();
1362 // Filter all other wildcards, if any.
1363 filteredTerm = filteredTerm.replaceAll(NuxeoRepositoryClientImpl.USER_SUPPLIED_WILDCARD_REGEX, ZERO_OR_MORE_ANY_CHAR_REGEX);
1364 if (logger.isTraceEnabled()) {
1365 logger.trace(String.format("After replacing user wildcards = %s", filteredTerm));
1367 } catch (Exception e) {
1368 logger.warn(String.format("Error filtering anchor and wildcard characters from string: %s", e.getMessage()));
1372 // Wrap the term in beginning and ending regex wildcards, unless a
1373 // starting or ending anchor character was present.
1374 return (anchorAtStart ? "" : ZERO_OR_MORE_ANY_CHAR_REGEX)
1376 + (anchorAtEnd ? "" : ZERO_OR_MORE_ANY_CHAR_REGEX);
1379 @SuppressWarnings("unchecked")
1380 private List<String> getPartialTermDisplayNameMatches(DocumentModel docModel, // REM - CSPACE-5133
1381 String schema, ListResultField field, String partialTerm) {
1382 List<String> result = null;
1384 String xpath = field.getXpath(); // results in something like "persons_common:personTermGroupList/[0]/termDisplayName"
1385 int endOfTermGroup = xpath.lastIndexOf("/[0]/");
1386 String propertyName = endOfTermGroup != -1 ? xpath.substring(0, endOfTermGroup) : xpath; // it may not be multivalued so the xpath passed in would be the property name
1387 Object value = null;
1390 value = docModel.getProperty(schema, propertyName);
1391 } catch (Exception e) {
1392 logger.error("Could not extract term display name with property = "
1396 if (value != null && value instanceof ArrayList) {
1397 ArrayList<HashMap<String, Object>> termGroupList = (ArrayList<HashMap<String, Object>>)value;
1398 int arrayListSize = termGroupList.size();
1399 if (arrayListSize > 1) { // if there's only 1 element in the list then we've already matched the primary term's display name
1400 List<String> displayNameList = new ArrayList<String>();
1401 for (int i = 1; i < arrayListSize; i++) { // start at 1, skip the primary term's displayName since we will always return it
1402 HashMap<String, Object> map = (HashMap<String, Object>)termGroupList.get(i);
1403 String termDisplayName = (String) map.get(AuthorityItemJAXBSchema.TERM_DISPLAY_NAME);
1404 displayNameList.add(i - 1, termDisplayName);
1407 result = getPartialTermDisplayNameMatches(displayNameList, partialTerm);
1414 private boolean isTermReferenced(DocumentModel docModel) throws Exception {
1415 boolean result = false;
1417 AuthorityRefDocList referenceList = null;
1419 String wf_deletedStr = (String) getServiceContext().getQueryParams().getFirst(WorkflowClient.WORKFLOW_QUERY_DELETED_QP);
1420 if (wf_deletedStr != null && Tools.isFalse(wf_deletedStr)) {
1422 // if query param 'wf_deleted=false', we won't count references to soft-deleted records
1424 referenceList = getReferencingObjectsForMarkingTerm(getServiceContext(), docModel, RefObjsSearchType.NON_DELETED);
1427 // if query param 'wf_deleted=true' or missing, we count references to soft-deleted and active records
1429 referenceList = getReferencingObjectsForMarkingTerm(getServiceContext(), docModel, RefObjsSearchType.ALL);
1432 if (referenceList.getTotalItems() > 0) {
1439 @SuppressWarnings("unchecked")
1441 protected Object getListResultValue(DocumentModel docModel, // REM - CSPACE-5133
1442 String schema, ListResultField field) throws DocumentException {
1443 Object result = null;
1444 String fieldXPath = field.getXpath();
1446 if (fieldXPath.equalsIgnoreCase(AuthorityClient.REFERENCED) == false) {
1447 result = NuxeoUtils.getXPathValue(docModel, schema, field.getXpath());
1450 // Check to see if the request is asking us to mark terms as referenced or not.
1452 String markIfReferencedStr = (String) getServiceContext().getQueryParams().getFirst(AuthorityClient.MARK_IF_REFERENCED_QP);
1453 if (Tools.isTrue(markIfReferencedStr) == false) {
1454 return null; // leave the <referenced> element as null since they're not asking for it
1456 return Boolean.toString(isTermReferenced(docModel)); // set the <referenced> element to either 'true' or 'false'
1457 } catch (Exception e) {
1458 String msg = String.format("Failed while trying to find records referencing term CSID='%s'.", docModel.getName());
1459 throw new DocumentException(msg, e);
1464 // Special handling of list item values for authority items (only)
1465 // takes place here:
1467 // If the list result field is the termDisplayName element,
1468 // check whether a partial term matching query was made.
1469 // If it was, emit values for both the preferred (aka primary)
1470 // term and for all non-preferred terms, if any.
1472 String elName = field.getElement();
1473 if (isTermDisplayName(elName) == true) {
1474 MultivaluedMap<String, String> queryParams = this.getServiceContext().getQueryParams();
1475 String partialTerm = queryParams != null ? queryParams.getFirst(IQueryManager.SEARCH_TYPE_PARTIALTERM) : null;
1476 if (partialTerm != null && partialTerm.trim().isEmpty() == false) {
1477 String primaryTermDisplayName = (String)result;
1478 List<String> matches = getPartialTermDisplayNameMatches(docModel, schema, field, partialTerm);
1479 if (matches != null && matches.isEmpty() == false) {
1480 matches.add(0, primaryTermDisplayName); // insert the primary term's display name at the beginning of the list
1481 result = matches; // set the result to a list of matching term display names with the primary term's display name at the beginning
1490 public void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
1491 MultipartServiceContext ctx = (MultipartServiceContext) getServiceContext();
1492 super.extractAllParts(wrapDoc);
1495 protected List<RelationsCommonList.RelationListItem> cloneList(List<RelationsCommonList.RelationListItem> inboundList) {
1496 List<RelationsCommonList.RelationListItem> result = newRelationsCommonList();
1497 for (RelationsCommonList.RelationListItem item : inboundList) {
1504 /* don't even THINK of re-using this method.
1505 * String example_uri = "/locationauthorities/7ec60f01-84ab-4908-9a6a/items/a5466530-713f-43b4-bc05";
1508 private String extractInAuthorityCSID(String uri) {
1509 String IN_AUTHORITY_REGEX = "/(.*?)/(.*?)/(.*)";
1510 Pattern p = Pattern.compile(IN_AUTHORITY_REGEX);
1511 Matcher m = p.matcher(uri);
1513 if (m.groupCount() < 3) {
1514 logger.warn("REGEX-WRONG-GROUPCOUNT looking in " + uri);
1517 //String service = m.group(1);
1518 String inauth = m.group(2);
1519 //String theRest = m.group(3);
1521 //print("service:"+service+", inauth:"+inauth+", rest:"+rest);
1524 logger.warn("REGEX-NOT-MATCHED looking in " + uri);
1529 //ensures CSPACE-4042
1530 protected void uriPointsToSameAuthority(String thisURI, String inboundItemURI) throws Exception {
1531 String authorityCSID = extractInAuthorityCSID(thisURI);
1532 String authorityCSIDForInbound = extractInAuthorityCSID(inboundItemURI);
1533 if (Tools.isBlank(authorityCSID)
1534 || Tools.isBlank(authorityCSIDForInbound)
1535 || (!authorityCSID.equalsIgnoreCase(authorityCSIDForInbound))) {
1536 throw new Exception("Item URI " + thisURI + " must point to same authority as related item: " + inboundItemURI);
1540 public String getItemTermInfoGroupXPathBase() {
1541 return authorityItemTermGroupXPathBase;
1544 public void setItemTermInfoGroupXPathBase(String itemTermInfoGroupXPathBase) {
1545 authorityItemTermGroupXPathBase = itemTermInfoGroupXPathBase;
1548 protected String getAuthorityItemCommonSchemaName() {
1549 return authorityItemCommonSchemaName;
1553 public boolean isJDBCQuery() {
1554 boolean result = false;
1556 MultivaluedMap<String, String> queryParams = getServiceContext().getQueryParams();
1558 // Look the query params to see if we need to make a SQL query.
1560 String partialTerm = queryParams.getFirst(IQueryManager.SEARCH_TYPE_PARTIALTERM);
1561 if (partialTerm != null && partialTerm.trim().isEmpty() == false) {
1568 // By convention, the name of the database table that contains
1569 // repeatable term information group records is derivable from
1570 // an existing XPath base value, by removing a suffix and converting
1572 protected String getTermGroupTableName() {
1573 String termInfoGroupListName = getItemTermInfoGroupXPathBase();
1574 return termInfoGroupListName.substring(0, termInfoGroupListName.lastIndexOf(LIST_SUFFIX)).toLowerCase();
1577 protected String getInAuthorityValue() {
1578 String inAuthorityValue = getInAuthorityCsid();
1579 if (Tools.notBlank(inAuthorityValue)) {
1580 return inAuthorityValue;
1582 return AuthorityResource.PARENT_WILDCARD;
1587 public Map<String,String> getJDBCQueryParams() {
1588 // FIXME: Get all of the following values from appropriate external constants.
1589 // At present, these are duplicated in both RepositoryClientImpl
1590 // and in AuthorityItemDocumentModelHandler.
1591 final String TERM_GROUP_LIST_NAME = "TERM_GROUP_LIST_NAME";
1592 final String TERM_GROUP_TABLE_NAME_PARAM = "TERM_GROUP_TABLE_NAME";
1593 final String IN_AUTHORITY_PARAM = "IN_AUTHORITY";
1595 Map<String,String> params = super.getJDBCQueryParams();
1596 params.put(TERM_GROUP_LIST_NAME, getItemTermInfoGroupXPathBase());
1597 params.put(TERM_GROUP_TABLE_NAME_PARAM, getTermGroupTableName());
1598 params.put(IN_AUTHORITY_PARAM, getInAuthorityValue());