1 package org.collectionspace.services.listener;
3 import java.io.Serializable;
4 import org.apache.commons.logging.Log;
5 import org.apache.commons.logging.LogFactory;
6 import org.collectionspace.services.common.api.RefNameUtils;
7 import org.collectionspace.services.common.api.Tools;
8 import org.nuxeo.ecm.core.api.ClientException;
9 import org.nuxeo.ecm.core.api.DocumentModel;
11 public class UpdateObjectLocationOnMove extends AbstractUpdateObjectLocationValues {
13 // FIXME: We might experiment here with using log4j instead of Apache Commons Logging;
14 // am using the latter to follow Ray's pattern for now
15 private final Log logger = LogFactory.getLog(UpdateObjectLocationOnMove.class);
18 protected DocumentModel updateCollectionObjectValuesFromMovement(DocumentModel collectionObjectDocModel,
19 DocumentModel movementDocModel) throws ClientException {
21 collectionObjectDocModel = updateComputedCurrentLocationValue(collectionObjectDocModel, movementDocModel);
22 // This method can be overridden and extended by adding or removing method
23 // calls here, to update a custom set of values in the CollectionObject
24 // record by pulling in values from the related Movement record.
25 return collectionObjectDocModel;
28 protected DocumentModel updateComputedCurrentLocationValue(DocumentModel collectionObjectDocModel,
29 DocumentModel movementDocModel)
30 throws ClientException {
32 // Get the current location value from the Movement (the "new" value)
33 String currentLocationRefName =
34 (String) movementDocModel.getProperty(MOVEMENTS_COMMON_SCHEMA, CURRENT_LOCATION_PROPERTY);
36 // Check that the value returned, which is expected to be a
37 // reference (refName) to an authority term (such as a storage
38 // location or organization term):
40 // * If it is not blank ...
41 // * Is then capable of being successfully parsed by an authority item parser.
42 if (Tools.notBlank(currentLocationRefName)
43 && RefNameUtils.parseAuthorityTermInfo(currentLocationRefName) == null) {
44 logger.warn("Could not parse current location refName '" + currentLocationRefName + "'");
45 return collectionObjectDocModel;
47 if (logger.isTraceEnabled()) {
48 logger.trace("current location refName passes basic validation tests.");
49 logger.trace("currentLocation refName=" + currentLocationRefName);
53 // Get the computed current location value of the CollectionObject
54 // (the "existing" value)
55 String existingComputedCurrentLocationRefName =
56 (String) collectionObjectDocModel.getProperty(COLLECTIONOBJECTS_COMMON_SCHEMA,
57 COMPUTED_CURRENT_LOCATION_PROPERTY);
58 if (logger.isTraceEnabled()) {
59 logger.trace("Existing computedCurrentLocation refName=" + existingComputedCurrentLocationRefName);
62 // If the new value is blank, any non-blank existing value should always
63 // be overwritten ('nulled out') with a blank value.
64 if (Tools.isBlank(currentLocationRefName) && Tools.notBlank(existingComputedCurrentLocationRefName)) {
65 collectionObjectDocModel.setProperty(COLLECTIONOBJECTS_COMMON_SCHEMA,
66 COMPUTED_CURRENT_LOCATION_PROPERTY, (Serializable) null);
67 // Otherwise, if the new value is not blank, and
68 // * the existing value is blank, or
69 // * the new value is different than the existing value ...
70 } else if (Tools.notBlank(currentLocationRefName) &&
71 (Tools.isBlank(existingComputedCurrentLocationRefName)
72 || !currentLocationRefName.equals(existingComputedCurrentLocationRefName))) {
73 if (logger.isTraceEnabled()) {
74 logger.trace("computedCurrentLocation refName requires updating.");
76 // ... update the existing value in the CollectionObject with the
77 // new value from the Movement.
78 collectionObjectDocModel.setProperty(COLLECTIONOBJECTS_COMMON_SCHEMA,
79 COMPUTED_CURRENT_LOCATION_PROPERTY, currentLocationRefName);
82 if (logger.isTraceEnabled()) {
83 logger.trace("computedCurrentLocation refName does NOT require updating.");
86 return collectionObjectDocModel;