]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
4be3f82bb452bb66bacc40ce69052bbd6d6fbcf4
[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         //
32         // First see if we're being asked to clear the computed location field
33         //
34         if (movementDocModel.getCoreSession() == null) {
35                 collectionObjectDocModel.setProperty(COLLECTIONOBJECTS_COMMON_SCHEMA,
36                     COMPUTED_CURRENT_LOCATION_PROPERTY, null);
37                 return collectionObjectDocModel;
38         }
39
40         // Get the current location value from the Movement (the "new" value)
41         String currentLocationRefName =
42                 (String) movementDocModel.getProperty(MOVEMENTS_COMMON_SCHEMA, CURRENT_LOCATION_PROPERTY);
43
44         // Check that the value returned, which is expected to be a
45         // reference (refName) to an authority term (such as a storage
46         // location or organization term):
47         //
48         // * Is not blank
49         // * Is capable of being successfully parsed by an authority item parser.
50         if (Tools.isBlank(currentLocationRefName)) {
51             if (logger.isTraceEnabled()) {
52                 logger.trace("Current location in Movement record was blank");
53             }
54             return collectionObjectDocModel;
55         } else if (RefNameUtils.parseAuthorityTermInfo(currentLocationRefName) == null) {
56             logger.warn(String.format("Could not parse current location refName '%s' in Movement record",
57                     currentLocationRefName));
58             return collectionObjectDocModel;
59         } else {
60             if (logger.isTraceEnabled()) {
61                 logger.trace("current location refName passes basic validation tests.");
62                 logger.trace("currentLocation refName=" + currentLocationRefName);
63             }
64         }
65         
66         // Get the computed current location value of the CollectionObject
67         // (the "existing" value)
68         String existingComputedCurrentLocationRefName =
69                 (String) collectionObjectDocModel.getProperty(COLLECTIONOBJECTS_COMMON_SCHEMA,
70                 COMPUTED_CURRENT_LOCATION_PROPERTY);
71         if (logger.isTraceEnabled()) {
72             logger.trace("Existing computedCurrentLocation refName=" + existingComputedCurrentLocationRefName);
73         }
74
75         // If the new value is not blank (redundant with a check just above, but
76         // a quick, extra guard) and the new value is different than the existing value ...
77         if ( (Tools.notBlank(currentLocationRefName)) && (!currentLocationRefName.equals(existingComputedCurrentLocationRefName))) {
78             if (logger.isTraceEnabled()) {
79                 logger.trace("computedCurrentLocation refName requires updating.");
80             }
81             // ... update the existing value (in the CollectionObject) with the
82             // new value (from the Movement).
83             collectionObjectDocModel.setProperty(COLLECTIONOBJECTS_COMMON_SCHEMA,
84                     COMPUTED_CURRENT_LOCATION_PROPERTY, currentLocationRefName);
85
86         } else {
87             if (logger.isTraceEnabled()) {
88                 logger.trace("computedCurrentLocation refName does NOT require updating.");
89             }
90         }
91         
92         return collectionObjectDocModel;
93     }
94 }