]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
ba33300893617f76bad25cbac82a388cc7df2d64
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.listener;
2
3 import java.util.HashMap;
4 import java.util.Map;
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7 import org.collectionspace.services.common.api.RefNameUtils;
8 import org.collectionspace.services.common.api.Tools;
9 import org.nuxeo.ecm.core.api.ClientException;
10 import org.nuxeo.ecm.core.api.CoreSession;
11 import org.nuxeo.ecm.core.api.DocumentModel;
12
13 public class UpdateObjectLocationOnMove extends AbstractUpdateObjectLocationValues {
14
15     // FIXME: We might experiment here with using log4j instead of Apache Commons Logging;
16     // am using the latter to follow Ray's pattern for now
17     private final Log logger = LogFactory.getLog(UpdateObjectLocationOnMove.class);
18     
19     @Override
20     protected void updateAllLocationValues(CoreSession coreSession, String collectionObjectCsid)
21             throws ClientException {
22         updateCurrentLocationValue(coreSession, collectionObjectCsid);
23     }
24
25     private void updateCurrentLocationValue(CoreSession coreSession, String collectionObjectCsid)
26             throws ClientException {
27         DocumentModel collectionObjectDocModel;
28         String computedCurrentLocationRefName;
29         collectionObjectDocModel = getDocModelFromCsid(coreSession, collectionObjectCsid);
30         if (collectionObjectDocModel == null) {
31             return;
32         }
33         // Verify that the CollectionObject record is active.
34         if (!isActiveDocument(collectionObjectDocModel)) {
35             return;
36         }
37         // Obtain the computed current location of that CollectionObject.
38         computedCurrentLocationRefName = computeCurrentLocation(coreSession, collectionObjectCsid);
39         if (logger.isTraceEnabled()) {
40             logger.trace("computedCurrentLocation refName=" + computedCurrentLocationRefName);
41         }
42
43         // Check that the value returned, which is expected to be a
44         // reference (refName) to a storage location authority term,
45         // is, at a minimum:
46         // * Non-null and non-blank. (We need to verify this assumption; can a
47         //   CollectionObject's computed current location value ever meaningfully
48         //   be 'un-set' by returning it to a null value?)
49         // * Capable of being successfully parsed by an authority item parser;
50         //   that is, returning a non-null parse result.
51         if ((Tools.isBlank(computedCurrentLocationRefName)
52                 || (RefNameUtils.parseAuthorityTermInfo(computedCurrentLocationRefName) == null))) {
53             logger.warn("Could not parse computed current location refName '" + computedCurrentLocationRefName + "'");
54             return;
55         } else {
56             if (logger.isTraceEnabled()) {
57                 logger.trace("computed current location refName passes basic validation tests.");
58             }
59         }
60
61         // If the value returned from the function passes validation,
62         // compare it to the value in the computedCurrentLocation
63         // field of that CollectionObject.
64         String existingComputedCurrentLocationRefName =
65                 (String) collectionObjectDocModel.getProperty(COLLECTIONOBJECTS_COMMON_SCHEMA, COMPUTED_CURRENT_LOCATION_PROPERTY);
66         if (logger.isTraceEnabled()) {
67             logger.trace("Existing computedCurrentLocation refName=" + existingComputedCurrentLocationRefName);
68         }
69         // If the CollectionObject lacks a computed current location value,
70         // or if the new computed value differs from its existing value ...
71         if (Tools.isBlank(existingComputedCurrentLocationRefName)
72                 || (!computedCurrentLocationRefName.equals(existingComputedCurrentLocationRefName))) {
73             if (logger.isTraceEnabled()) {
74                 logger.trace("computedCurrentLocation refName requires updating.");
75             }
76             // ... update that value and then save the updated CollectionObject.
77             collectionObjectDocModel.setProperty(COLLECTIONOBJECTS_COMMON_SCHEMA, COMPUTED_CURRENT_LOCATION_PROPERTY, computedCurrentLocationRefName);
78             coreSession.saveDocument(collectionObjectDocModel);
79             if (logger.isTraceEnabled()) {
80                 String afterUpdateComputedCurrentLocationRefName =
81                         (String) collectionObjectDocModel.getProperty(COLLECTIONOBJECTS_COMMON_SCHEMA, COMPUTED_CURRENT_LOCATION_PROPERTY);
82                 logger.trace("Following update, new computedCurrentLocation refName value=" + afterUpdateComputedCurrentLocationRefName);
83
84             }
85         } else {
86             if (logger.isTraceEnabled()) {
87                 logger.trace("computedCurrentLocation refName does NOT require updating.");
88             }
89         }
90     }
91 }