]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
1b9b6f04b88625229bfeb846039b1854367ee1d7
[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 (the "new" value)
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):
38         //
39         // * Is not blank
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");
44             }
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;
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         
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);
64         }
65
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.");
71             }
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);
76
77         } else {
78             if (logger.isTraceEnabled()) {
79                 logger.trace("computedCurrentLocation refName does NOT require updating.");
80             }
81         }
82         return collectionObjectDocModel;
83     }
84 }