]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
3b8b4608c51c6aed7381354fb6a08e8827231927
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.listener.naturalhistory;
2
3 import java.util.HashMap;
4 import java.util.List;
5 import java.util.Map;
6
7 import org.apache.commons.lang.StringUtils;
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10
11 import org.collectionspace.services.client.workflow.WorkflowClient;
12 import org.collectionspace.services.common.api.RefName;
13 import org.collectionspace.services.common.api.TaxonFormatter;
14 import org.collectionspace.services.nuxeo.listener.AbstractCSEventSyncListenerImpl;
15 import org.collectionspace.services.taxonomy.nuxeo.TaxonBotGardenConstants;
16 import org.collectionspace.services.taxonomy.nuxeo.TaxonConstants;
17 import org.collectionspace.services.taxonomy.nuxeo.TaxonomyAuthorityConstants;
18
19 import org.nuxeo.ecm.core.api.DocumentModel;
20 import org.nuxeo.ecm.core.api.event.CoreEventConstants;
21 import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
22 import org.nuxeo.ecm.core.event.Event;
23 import org.nuxeo.ecm.core.event.EventContext;
24 import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
25
26 public class UpdateFormattedDisplayNameListener extends AbstractCSEventSyncListenerImpl {
27         public static final String RUN_AFTER_MODIFIED_PROPERTY = "UpdateFormattedDisplayNameListener.RUN_AFTER_MODIFIED";
28         static final Log logger = LogFactory.getLog(UpdateFormattedDisplayNameListener.class);
29
30         private static final String[] DISPLAY_NAME_PATH_ELEMENTS = TaxonConstants.DISPLAY_NAME_FIELD_NAME.split("/");
31         private static final String TERM_GROUP_LIST_FIELD_NAME = DISPLAY_NAME_PATH_ELEMENTS[0];
32         private static final String DISPLAY_NAME_FIELD_NAME = DISPLAY_NAME_PATH_ELEMENTS[2];
33
34         private static final String[] FORMATTED_DISPLAY_NAME_PATH_ELEMENTS = TaxonConstants.FORMATTED_DISPLAY_NAME_FIELD_NAME.split("/");
35         private static final String FORMATTED_DISPLAY_NAME_FIELD_NAME = FORMATTED_DISPLAY_NAME_PATH_ELEMENTS[2];
36
37
38     @Override
39         public boolean shouldHandleEvent(Event event) {
40                 EventContext ec = event.getContext();
41
42                 if (ec instanceof DocumentEventContext) {
43                         DocumentEventContext context = (DocumentEventContext) ec;
44                         DocumentModel doc = context.getSourceDocument();
45                         String docType = doc.getType();
46
47                         logger.debug("docType=" + docType);
48
49                         if (docType.startsWith(TaxonConstants.NUXEO_DOCTYPE) &&
50                                         !docType.startsWith(TaxonomyAuthorityConstants.NUXEO_DOCTYPE) &&
51                                         !doc.isVersion() &&
52                                         !doc.isProxy() &&
53                                         !doc.getCurrentLifeCycleState().equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
54                                 return true;
55                         }
56                 }
57
58                 return false;
59     }
60
61         @Override
62         public void handleCSEvent(Event event) {
63                 EventContext ec = event.getContext();
64                 DocumentEventContext context = (DocumentEventContext) ec;
65                 DocumentModel doc = context.getSourceDocument();
66                 
67                 String docType = doc.getType();
68                 logger.debug("docType=" + docType);
69
70                 String refName = (String) doc.getProperty(TaxonConstants.REFNAME_SCHEMA_NAME, TaxonConstants.REFNAME_FIELD_NAME);
71                 RefName.AuthorityItem item = RefName.AuthorityItem.parse(refName);
72                 String parentShortId = item.getParentShortIdentifier();
73
74                 logger.debug("parentShortId=" + parentShortId);
75
76                 if (!parentShortId.equals(TaxonBotGardenConstants.COMMON_VOCABULARY_SHORTID)) {
77                         if (event.getName().equals(DocumentEventTypes.DOCUMENT_CREATED)) {
78                                 // Save the document, to get the BEFORE_DOC_UPDATE branch to run.
79                                 doc.getCoreSession().saveDocument(doc);
80                         }
81                         else if (event.getName().equals(DocumentEventTypes.BEFORE_DOC_UPDATE)) {
82                                 DocumentModel previousDoc = (DocumentModel) context.getProperty(CoreEventConstants.PREVIOUS_DOCUMENT_MODEL);
83
84                                 updateFormattedDisplayNames(doc, previousDoc);
85                         }
86                 }
87         }
88
89         private void updateFormattedDisplayNames(DocumentModel doc, DocumentModel previousDoc) {
90                 //Set<String> previousDisplayNames = getDisplayNames(previousDoc);
91                 TaxonFormatter formatter = new TaxonFormatter();
92                 List<Map<String, Object>> termGroupList = (List<Map<String, Object>>) doc.getProperty(TaxonConstants.DISPLAY_NAME_SCHEMA_NAME, TERM_GROUP_LIST_FIELD_NAME);
93
94                 for (Map<String, Object> termGroup : termGroupList) {
95                         String displayName = (String) termGroup.get(DISPLAY_NAME_FIELD_NAME);
96                         String formattedDisplayName = (String) termGroup.get(FORMATTED_DISPLAY_NAME_FIELD_NAME);
97
98                         if (StringUtils.isBlank(formattedDisplayName)) {
99                                 formattedDisplayName = "";
100
101                                 if (StringUtils.isNotBlank(displayName)) {
102                                         formattedDisplayName = formatter.format(displayName);
103                                 }
104
105                                 termGroup.put(FORMATTED_DISPLAY_NAME_FIELD_NAME, formattedDisplayName);
106                         }
107                 }
108
109                 Map<String, Object> updateMap = new HashMap<String, Object>();
110                 updateMap.put(TERM_GROUP_LIST_FIELD_NAME, termGroupList);
111
112                 doc.setProperties(TaxonConstants.DISPLAY_NAME_SCHEMA_NAME, updateMap);
113         }
114
115         /*
116         private Set<String> getDisplayNames(DocumentModel doc) throws ClientException {
117                 Set<String> displayNames = new HashSet<String>();
118                 List<Map<String, Object>> termGroupList = (List<Map<String, Object>>) doc.getProperty(TaxonConstants.DISPLAY_NAME_SCHEMA_NAME, TERM_GROUP_LIST_FIELD_NAME);
119
120                 for (Map<String, Object> termGroup : termGroupList) {
121                         String displayName = (String) termGroup.get(DISPLAY_NAME_FIELD_NAME);
122
123                         if (displayName != null) {
124                                 displayNames.add(displayName);
125                         }
126                 }
127
128                 return displayNames;
129         }
130         */
131         
132         @Override
133         public Log getLogger() {
134                 return logger;
135         }
136 }