]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
46a9c84c17304249d05c9de058a9051680fd9c49
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.listener;
2
3 import java.io.Serializable;
4
5 import org.slf4j.Logger;
6 import org.slf4j.LoggerFactory;
7
8 import org.collectionspace.services.common.api.RefNameUtils;
9 import org.collectionspace.services.common.api.Tools;
10 import org.nuxeo.ecm.core.api.ClientException;
11 import org.nuxeo.ecm.core.api.DocumentModel;
12
13 public class UpdateObjectLocationAndCrateOnMove extends UpdateObjectLocationOnMove {
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 static final Logger logger = LoggerFactory.getLogger(UpdateObjectLocationAndCrateOnMove.class);
18     
19     // FIXME: Get values below from external constants
20     private final static String TENANT_COLLECTIONOBJECTS_SCHEMANAME_KEY = "TENANT_COLLECTIONOBJECTS_SCHEMANAME_KEY"; // For this listener, this is the key value to find the Nuxeo document schema name for the CollectionObject document from the tenant binding's parameter list.
21     private final static String COMPUTED_CRATE_PROPERTY = "computedCrate";
22     private final static String TENANT_MOVEMENTS_SCHEMANAME_KEY = "TENANT_MOVEMENTS_SCHEMANAME_KEY"; // For this listener, this is the key value to find the Nuxeo document schema name for the Movement document from the tenant binding's parameter list.
23     private final static String CRATE_PROPERTY = "crate";
24
25     @Override
26     protected boolean updateCollectionObjectLocation(DocumentModel collectionObjectDocModel,
27             DocumentModel movementDocModel,
28             String mostRecentLocation) throws ClientException {
29         boolean flag = super.updateCollectionObjectLocation(collectionObjectDocModel, movementDocModel, mostRecentLocation);
30         collectionObjectDocModel = updateComputedCrateValue(collectionObjectDocModel, movementDocModel);
31         
32         return flag;
33     }
34
35     private DocumentModel updateComputedCrateValue(DocumentModel collectionObjectDocModel,
36             DocumentModel movementDocModel)
37             throws ClientException {
38
39         // Get the current crate value from the Movement (the "new" value)
40         String crateRefName = (String) movementDocModel.getProperty(getParamValue(TENANT_MOVEMENTS_SCHEMANAME_KEY),
41                                 CRATE_PROPERTY);
42
43         // Check that the value returned, which is expected to be a
44         // reference (refName) to an authority term:
45         //
46         // * If it is not blank ...
47         // * Is then capable of being successfully parsed by an authority item parser.
48         if (Tools.notBlank(crateRefName)
49                 && RefNameUtils.parseAuthorityTermInfo(crateRefName) == null) {
50             logger.warn("Could not parse crate refName '" + crateRefName + "'");
51             return collectionObjectDocModel;
52         } else {
53             if (logger.isTraceEnabled()) {
54                 logger.trace("crate refName passes basic validation tests.");
55                 logger.trace("crate refName=" + crateRefName);
56             }
57         }
58         // Get the computed crate value of the CollectionObject
59         // (the "existing" value)
60         String existingCrateRefName = (String) collectionObjectDocModel.getProperty(getParamValue(TENANT_COLLECTIONOBJECTS_SCHEMANAME_KEY),
61                 COMPUTED_CRATE_PROPERTY);
62         if (logger.isTraceEnabled()) {
63             logger.trace("Existing crate refName=" + existingCrateRefName);
64         }
65
66         // If the new value is blank, any non-blank existing value should always
67         // be overwritten ('nulled out') with a blank value.
68         if (Tools.isBlank(crateRefName) && Tools.notBlank(existingCrateRefName)) {
69             collectionObjectDocModel.setProperty(TENANT_COLLECTIONOBJECTS_SCHEMANAME_KEY,
70                     COMPUTED_CRATE_PROPERTY, (Serializable) null);
71             // Otherwise, if the new value is not blank, and
72             // * the existing value is blank, or
73             // * the new value is different than the existing value ...
74         } else if (Tools.notBlank(crateRefName) &&
75                     (Tools.isBlank(existingCrateRefName)
76                     || !crateRefName.equals(existingCrateRefName))) {
77             if (logger.isTraceEnabled()) {
78                 logger.trace("crate refName requires updating.");
79             }
80             // ... update the existing value in the CollectionObject with the
81             // new value from the Movement.
82             collectionObjectDocModel.setProperty(TENANT_COLLECTIONOBJECTS_SCHEMANAME_KEY,
83                     COMPUTED_CRATE_PROPERTY, crateRefName);
84         } else {
85             if (logger.isTraceEnabled()) {
86                 logger.trace("crate refName does NOT require updating.");
87             }
88         }
89         
90         return collectionObjectDocModel;
91     }
92 }