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