1 package org.collectionspace.services.listener;
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.collectionspace.services.common.api.RefNameUtils;
6 import org.collectionspace.services.common.api.Tools;
7 import org.nuxeo.ecm.core.api.ClientException;
8 import org.nuxeo.ecm.core.api.DocumentModel;
10 public class UpdateObjectLocationOnMove extends AbstractUpdateObjectLocationValues {
12 // FIXME: We might experiment here with using log4j instead of Apache Commons Logging;
13 // am using the latter to follow Ray's pattern for now
14 private final Log logger = LogFactory.getLog(UpdateObjectLocationOnMove.class);
17 protected DocumentModel updateCollectionObjectValuesFromMovement(DocumentModel collectionObjectDocModel,
18 DocumentModel movementDocModel) throws ClientException {
20 collectionObjectDocModel = updateComputedCurrentLocationValue(collectionObjectDocModel, movementDocModel);
21 // This method can be overridden and extended by adding or removing method
22 // calls here, to update a custom set of values in the CollectionObject
23 // record by pulling in values from the related Movement record.
24 return collectionObjectDocModel;
27 protected DocumentModel updateComputedCurrentLocationValue(DocumentModel collectionObjectDocModel,
28 DocumentModel movementDocModel)
29 throws ClientException {
31 // Get the current location value from the Movement.
32 String currentLocationRefName =
33 (String) movementDocModel.getProperty(MOVEMENTS_COMMON_SCHEMA, CURRENT_LOCATION_PROPERTY);
35 // Check that the value returned, which is expected to be a
36 // reference (refName) to an authority term (such as a storage
37 // location or organization term) is, at a minimum:
39 // * Non-null and non-blank.
40 // (Note: we need to verify this assumption; can a CollectionObject's
41 // computed current location value ever meaningfully be 'un-set'
42 // by returning it to a null value?)
44 // * Capable of being successfully parsed by an authority item parser;
45 // that is, returning a non-null parse result.
46 if ((Tools.isBlank(currentLocationRefName)
47 || (RefNameUtils.parseAuthorityTermInfo(currentLocationRefName) == null))) {
48 logger.warn("Could not parse current location refName '" + currentLocationRefName + "'");
49 return collectionObjectDocModel;
51 if (logger.isTraceEnabled()) {
52 logger.trace("current location refName passes basic validation tests.");
53 logger.trace("currentLocation refName=" + currentLocationRefName);
56 // If the value returned from the function passes validation,
57 // compare it to the value in the computed current location
58 // field of the CollectionObject.
59 String existingComputedCurrentLocationRefName =
60 (String) collectionObjectDocModel.getProperty(COLLECTIONOBJECTS_COMMON_SCHEMA,
61 COMPUTED_CURRENT_LOCATION_PROPERTY);
62 if (logger.isTraceEnabled()) {
63 logger.trace("Existing computedCurrentLocation refName=" + existingComputedCurrentLocationRefName);
66 // If the CollectionObject lacks a computed current location value,
67 // or if the new value differs from its existing value ...
68 if (Tools.isBlank(existingComputedCurrentLocationRefName)
69 || (!currentLocationRefName.equals(existingComputedCurrentLocationRefName))) {
70 if (logger.isTraceEnabled()) {
71 logger.trace("computedCurrentLocation refName requires updating.");
73 // ... update that value.
74 collectionObjectDocModel.setProperty(COLLECTIONOBJECTS_COMMON_SCHEMA,
75 COMPUTED_CURRENT_LOCATION_PROPERTY, currentLocationRefName);
77 if (logger.isTraceEnabled()) {
78 logger.trace("computedCurrentLocation refName does NOT require updating.");
81 return collectionObjectDocModel;