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