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