]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
7a40b9b166eeb23765fc2c33a5961f9a25c739f7
[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 COLLECTIONOBJECTS_ANTHROPOLOGY_SCHEMA = "collectionobjects_anthropology";
21     private final static String MOVEMENTS_ANTHROPOLOGY_SCHEMA = "movements_anthropology";
22     private final static String CRATE_PROPERTY = "crate";
23     private final static String COMPUTED_CRATE_PROPERTY = "computedCrate";
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 =
41                 (String) movementDocModel.getProperty(MOVEMENTS_ANTHROPOLOGY_SCHEMA, 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 =
61                 (String) collectionObjectDocModel.getProperty(COLLECTIONOBJECTS_ANTHROPOLOGY_SCHEMA,
62                 COMPUTED_CRATE_PROPERTY);
63         if (logger.isTraceEnabled()) {
64             logger.trace("Existing crate refName=" + existingCrateRefName);
65         }
66
67         // If the new value is blank, any non-blank existing value should always
68         // be overwritten ('nulled out') with a blank value.
69         if (Tools.isBlank(crateRefName) && Tools.notBlank(existingCrateRefName)) {
70             collectionObjectDocModel.setProperty(COLLECTIONOBJECTS_ANTHROPOLOGY_SCHEMA,
71                     COMPUTED_CRATE_PROPERTY, (Serializable) null);
72             // Otherwise, if the new value is not blank, and
73             // * the existing value is blank, or
74             // * the new value is different than the existing value ...
75         } else if (Tools.notBlank(crateRefName) &&
76                     (Tools.isBlank(existingCrateRefName)
77                     || !crateRefName.equals(existingCrateRefName))) {
78             if (logger.isTraceEnabled()) {
79                 logger.trace("crate refName requires updating.");
80             }
81             // ... update the existing value in the CollectionObject with the
82             // new value from the Movement.
83             collectionObjectDocModel.setProperty(COLLECTIONOBJECTS_ANTHROPOLOGY_SCHEMA,
84                     COMPUTED_CRATE_PROPERTY, crateRefName);
85         } else {
86             if (logger.isTraceEnabled()) {
87                 logger.trace("crate refName does NOT require updating.");
88             }
89         }
90         
91         return collectionObjectDocModel;
92     }
93 }