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 (the "new" value)
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):
40 // * Is capable of being successfully parsed by an authority item parser.
41 if (Tools.isBlank(currentLocationRefName)) {
42 if (logger.isTraceEnabled()) {
43 logger.trace("Current location in Movement record was blank");
45 return collectionObjectDocModel;
46 } else if (RefNameUtils.parseAuthorityTermInfo(currentLocationRefName) == null) {
47 logger.warn(String.format("Could not parse current location refName '%s' in Movement record",
48 currentLocationRefName));
49 return collectionObjectDocModel;
51 if (logger.isTraceEnabled()) {
52 logger.trace("current location refName passes basic validation tests.");
53 logger.trace("currentLocation refName=" + currentLocationRefName);
57 // Get the computed current location value of the CollectionObject
58 // (the "existing" value)
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 new value is not blank (redundant with a check just above, but
67 // a quick, extra guard) and the new value is different than the existing value ...
68 if ( (Tools.notBlank(currentLocationRefName)) && (!currentLocationRefName.equals(existingComputedCurrentLocationRefName))) {
69 if (logger.isTraceEnabled()) {
70 logger.trace("computedCurrentLocation refName requires updating.");
72 // ... update the existing value (in the CollectionObject) with the
73 // new value (from the Movement).
74 collectionObjectDocModel.setProperty(COLLECTIONOBJECTS_COMMON_SCHEMA,
75 COMPUTED_CURRENT_LOCATION_PROPERTY, currentLocationRefName);
78 if (logger.isTraceEnabled()) {
79 logger.trace("computedCurrentLocation refName does NOT require updating.");
82 return collectionObjectDocModel;