1 package org.collectionspace.services.listener.naturalhistory;
3 import java.util.HashMap;
7 import org.apache.commons.lang.StringUtils;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
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;
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;
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);
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];
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];
40 public boolean shouldHandleEvent(Event event) {
41 EventContext ec = event.getContext();
43 if (ec instanceof DocumentEventContext) {
44 DocumentEventContext context = (DocumentEventContext) ec;
45 DocumentModel doc = context.getSourceDocument();
46 String docType = doc.getType();
48 logger.debug("docType=" + docType);
50 if (docType.startsWith(TaxonConstants.NUXEO_DOCTYPE) &&
51 !docType.startsWith(TaxonomyAuthorityConstants.NUXEO_DOCTYPE) &&
54 !doc.getCurrentLifeCycleState().equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
63 public void handleCSEvent(Event event) {
64 EventContext ec = event.getContext();
65 DocumentEventContext context = (DocumentEventContext) ec;
66 DocumentModel doc = context.getSourceDocument();
68 String docType = doc.getType();
69 logger.debug("docType=" + docType);
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();
75 logger.debug("parentShortId=" + parentShortId);
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);
82 else if (event.getName().equals(DocumentEventTypes.BEFORE_DOC_UPDATE)) {
83 DocumentModel previousDoc = (DocumentModel) context.getProperty(CoreEventConstants.PREVIOUS_DOCUMENT_MODEL);
85 updateFormattedDisplayNames(doc, previousDoc);
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);
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);
99 if (StringUtils.isBlank(formattedDisplayName)) {
100 formattedDisplayName = "";
102 if (StringUtils.isNotBlank(displayName)) {
103 formattedDisplayName = formatter.format(displayName);
106 termGroup.put(FORMATTED_DISPLAY_NAME_FIELD_NAME, formattedDisplayName);
110 Map<String, Object> updateMap = new HashMap<String, Object>();
111 updateMap.put(TERM_GROUP_LIST_FIELD_NAME, termGroupList);
113 doc.setProperties(TaxonConstants.DISPLAY_NAME_SCHEMA_NAME, updateMap);
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);
121 for (Map<String, Object> termGroup : termGroupList) {
122 String displayName = (String) termGroup.get(DISPLAY_NAME_FIELD_NAME);
124 if (displayName != null) {
125 displayNames.add(displayName);
134 public Logger getLogger() {