]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
80ead39adb1a48134a81f56ad8ec8097d957b836
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.listener;
2
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;
9
10 public class UpdateObjectLocationOnMove extends AbstractUpdateObjectLocationValues {
11
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);
15
16     @Override
17     protected DocumentModel updateCollectionObjectValuesFromMovement(DocumentModel collectionObjectDocModel,
18             DocumentModel movementDocModel) throws ClientException {
19
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;
25     }
26
27     protected DocumentModel updateComputedCurrentLocationValue(DocumentModel collectionObjectDocModel,
28             DocumentModel movementDocModel)
29             throws ClientException {
30
31         // Get the current location value from the Movement.
32         String currentLocationRefName =
33                 (String) movementDocModel.getProperty(MOVEMENTS_COMMON_SCHEMA, CURRENT_LOCATION_PROPERTY);
34
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:
38         //
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?)
43         //
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;
50         } else {
51             if (logger.isTraceEnabled()) {
52                 logger.trace("current location refName passes basic validation tests.");
53                 logger.trace("currentLocation refName=" + currentLocationRefName);
54             }
55         }
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);
64         }
65
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.");
72             }
73             // ... update that value.
74             collectionObjectDocModel.setProperty(COLLECTIONOBJECTS_COMMON_SCHEMA,
75                     COMPUTED_CURRENT_LOCATION_PROPERTY, currentLocationRefName);
76         } else {
77             if (logger.isTraceEnabled()) {
78                 logger.trace("computedCurrentLocation refName does NOT require updating.");
79             }
80         }
81         return collectionObjectDocModel;
82     }
83 }