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