]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
6a342b80295f8715f62f204124909c5101da72b9
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.listener.botgarden;
2
3 import java.util.List;
4 import java.util.Map;
5
6 import org.collectionspace.services.batch.nuxeo.UpdateRareFlagBatchJob;
7 import org.collectionspace.services.client.workflow.WorkflowClient;
8 import org.collectionspace.services.collectionobject.nuxeo.CollectionObjectBotGardenConstants;
9 import org.collectionspace.services.collectionobject.nuxeo.CollectionObjectConstants;
10 import org.collectionspace.services.common.ResourceMap;
11 import org.collectionspace.services.common.invocable.InvocationResults;
12 import org.collectionspace.services.nuxeo.listener.AbstractCSEventListenerImpl;
13 import org.collectionspace.services.taxonomy.nuxeo.TaxonBotGardenConstants;
14 import org.collectionspace.services.taxonomy.nuxeo.TaxonConstants;
15 import org.jboss.resteasy.spi.ResteasyProviderFactory;
16 import org.nuxeo.ecm.core.api.DocumentModel;
17 import org.nuxeo.ecm.core.api.event.CoreEventConstants;
18 import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
19 import org.nuxeo.ecm.core.event.Event;
20 import org.nuxeo.ecm.core.event.EventContext;
21 import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * A listener that updates the rare flag on collectionobjects when collectionobjects
27  * are created or modified, and when taxon records are modified.
28  *
29  * @see org.collectionspace.services.batch.nuxeo.UpdateRareFlagBatchJob
30  * @author ray
31  *
32  */
33 public class UpdateRareFlagListener extends AbstractCSEventListenerImpl {
34         final Logger logger = LoggerFactory.getLogger(UpdateRareFlagListener.class);
35
36         public static final String PREVIOUS_TAXON_PROPERTY_NAME = "UpdateRareFlagListener.previousTaxon";
37         public static final String PREVIOUS_HAS_RARE_CONSERVATION_CATEGORY_PROPERTY_NAME = "UpdateRareFlagListener.previousHasRareConservationCategory";
38
39         private static final String[] CONSERVATION_CATEGORY_PATH_ELEMENTS = TaxonBotGardenConstants.CONSERVATION_CATEGORY_FIELD_NAME.split("/");
40         private static final String PLANT_ATTRIBUTES_GROUP_LIST_FIELD_NAME = CONSERVATION_CATEGORY_PATH_ELEMENTS[0];
41         private static final String CONSERVATION_CATEGORY_FIELD_NAME = CONSERVATION_CATEGORY_PATH_ELEMENTS[2];
42
43         @Override
44         public void handleEvent(Event event) {
45                 EventContext ec = event.getContext();
46
47                 if (isRegistered(event) && ec instanceof DocumentEventContext) {
48                         DocumentEventContext context = (DocumentEventContext) ec;
49                         DocumentModel doc = context.getSourceDocument();
50
51                         logger.debug("docType=" + doc.getType());
52
53                         if (doc.getType().startsWith(CollectionObjectConstants.NUXEO_DOCTYPE) &&
54                                         !doc.isVersion() &&
55                                         !doc.isProxy() &&
56                                         !doc.getCurrentLifeCycleState().equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
57
58                                 if (event.getName().equals(DocumentEventTypes.BEFORE_DOC_UPDATE)) {
59                                         // Stash the previous primary taxonomic ident, so it can be retrieved in the documentModified handler.
60
61                                         DocumentModel previousDoc = (DocumentModel) context.getProperty(CoreEventConstants.PREVIOUS_DOCUMENT_MODEL);
62                                         String previousTaxon = (String) previousDoc.getProperty(CollectionObjectBotGardenConstants.TAXON_SCHEMA_NAME,
63                                                         CollectionObjectBotGardenConstants.PRIMARY_TAXON_FIELD_NAME);
64
65                                         context.setProperty(PREVIOUS_TAXON_PROPERTY_NAME, previousTaxon);
66                                 }
67                                 else {
68                                         boolean updateRequired = false;
69
70                                         if (event.getName().equals(DocumentEventTypes.DOCUMENT_UPDATED)) {
71                                                 // A collectionobject was modified. As an optimization, check if the primary taxonomic determination
72                                                 // of the collectionobject has changed. We only need to update the rare flag if it has.
73
74                                                 String previousTaxon = (String) context.getProperty(PREVIOUS_TAXON_PROPERTY_NAME);
75                                                 String currentTaxon = (String) doc.getProperty(CollectionObjectBotGardenConstants.TAXON_SCHEMA_NAME,
76                                                                 CollectionObjectBotGardenConstants.PRIMARY_TAXON_FIELD_NAME);
77
78                                                 if (previousTaxon == null) {
79                                                         previousTaxon = "";
80                                                 }
81
82                                                 if (currentTaxon == null) {
83                                                         currentTaxon = "";
84                                                 }
85
86                                                 if (previousTaxon.equals(currentTaxon)) {
87                                                         logger.debug("update not required: previousTaxon=" + previousTaxon + " currentTaxon=" + currentTaxon);
88                                                 }
89                                                 else {
90                                                         logger.debug("update required: previousTaxon=" + previousTaxon + " currentTaxon=" + currentTaxon);
91                                                         updateRequired = true;
92                                                 }
93                                         }
94                                         else if (event.getName().equals(DocumentEventTypes.DOCUMENT_CREATED)) {
95                                                 // A collectionobject was created. Always update the rare flag.
96
97                                                 updateRequired = true;
98                                         }
99
100                                         if (updateRequired) {
101                                                 String collectionObjectCsid = doc.getName();
102
103                                                 try {
104                                                         InvocationResults results = createUpdater().updateRareFlag(collectionObjectCsid);
105
106                                                         logger.debug("updateRareFlag complete: numAffected=" + results.getNumAffected() + " userNote=" + results.getUserNote());
107                                                 } catch (Exception e) {
108                                                         logger.error(e.getMessage(), e);
109                                                 }
110                                         }
111                                 }
112                         }
113                         else if (doc.getType().startsWith(TaxonConstants.NUXEO_DOCTYPE) &&
114                                         !doc.isVersion() &&
115                                         !doc.isProxy() &&
116                                         !doc.getCurrentLifeCycleState().equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
117
118                                 if (event.getName().equals(DocumentEventTypes.BEFORE_DOC_UPDATE)) {
119                                         // Stash whether there was previously a non-empty conservation category, so it can be retrieved in the documentModified handler.
120
121                                         DocumentModel previousDoc = (DocumentModel) context.getProperty(CoreEventConstants.PREVIOUS_DOCUMENT_MODEL);
122                                         boolean previousHasRareConservationCategory = hasRareConservationCategory(previousDoc);
123
124                                         context.setProperty(PREVIOUS_HAS_RARE_CONSERVATION_CATEGORY_PROPERTY_NAME, new Boolean(previousHasRareConservationCategory));
125                                 }
126                                 else {
127                                         boolean updateRequired = false;
128
129                                         if (event.getName().equals(DocumentEventTypes.DOCUMENT_UPDATED)) {
130                                                 // A taxon record was modified. As an optimization, check if there is now a rare
131                                                 // conservation category when there wasn't before, or vice versa. We only need to update
132                                                 // the rare flags of referencing collectionobjects if there was a change.
133
134                                                 boolean previousHasRareConservationCategory = (Boolean) context.getProperty(PREVIOUS_HAS_RARE_CONSERVATION_CATEGORY_PROPERTY_NAME);
135                                                 boolean currentHasRareConservationCategory = hasRareConservationCategory(doc);
136
137                                                 if (previousHasRareConservationCategory == currentHasRareConservationCategory) {
138                                                         logger.debug("update not required: previousHasRareConservationCategory=" + previousHasRareConservationCategory +
139                                                                         " currentHasRareConservationCategory=" + currentHasRareConservationCategory);
140                                                 }
141                                                 else {
142                                                         logger.debug("update required: previousHasRareConservationCategory=" + previousHasRareConservationCategory +
143                                                                         " currentHasRareConservationCategory=" + currentHasRareConservationCategory);
144                                                         updateRequired = true;
145                                                 }
146                                         }
147
148                                         if (updateRequired) {
149                                                 String taxonCsid = doc.getName();
150
151                                                 try {
152                                                         InvocationResults results = createUpdater().updateReferencingRareFlags(taxonCsid);
153
154                                                         logger.debug("updateReferencingRareFlags complete: numAffected=" + results.getNumAffected() + " userNote=" + results.getUserNote());
155                                                 } catch (Exception e) {
156                                                         logger.error(e.getMessage(), e);
157                                                 }
158                                         }
159                                 }
160                         }
161                 }
162         }
163
164         private boolean hasRareConservationCategory(DocumentModel doc) {
165                 List<Map<String, Object>> plantAttributesGroupList = (List<Map<String, Object>>) doc.getProperty(TaxonBotGardenConstants.CONSERVATION_CATEGORY_SCHEMA_NAME,
166                                 PLANT_ATTRIBUTES_GROUP_LIST_FIELD_NAME);
167                 boolean hasRareConservationCategory = false;
168
169                 // UCBG-369: Changing this so that it only checks the primary conservation category.
170
171                 if (plantAttributesGroupList.size() > 0) {
172                         Map<String, Object> plantAttributesGroup = plantAttributesGroupList.get(0);
173                         String conservationCategory = (String) plantAttributesGroup.get(CONSERVATION_CATEGORY_FIELD_NAME);
174
175                         if (UpdateRareFlagBatchJob.isRare(conservationCategory)) {
176                                 hasRareConservationCategory = true;
177                         }
178                 }
179
180 //              for (Map<String, Object> plantAttributesGroup : plantAttributesGroupList) {
181 //                      String conservationCategory = (String) plantAttributesGroup.get(CONSERVATION_CATEGORY_FIELD_NAME);
182 //
183 //                      if (UpdateRareFlagBatchJob.isRare(conservationCategory)) {
184 //                              hasRareConservationCategory = true;
185 //                              break;
186 //                      }
187 //              }
188
189                 return hasRareConservationCategory;
190         }
191
192         private UpdateRareFlagBatchJob createUpdater() {
193                 ResourceMap resourceMap = ResteasyProviderFactory.getContextData(ResourceMap.class);
194
195                 UpdateRareFlagBatchJob updater = new UpdateRareFlagBatchJob();
196                 updater.setResourceMap(resourceMap);
197
198                 return updater;
199         }
200 }