1 package org.collectionspace.services.listener.naturalhistory;
3 import java.util.HashMap;
7 import org.apache.commons.lang.StringUtils;
8 import org.collectionspace.services.client.workflow.WorkflowClient;
9 import org.collectionspace.services.common.api.RefName;
10 import org.collectionspace.services.common.api.TaxonFormatter;
11 import org.collectionspace.services.nuxeo.listener.AbstractCSEventListenerImpl;
12 import org.collectionspace.services.taxonomy.nuxeo.TaxonBotGardenConstants;
13 import org.collectionspace.services.taxonomy.nuxeo.TaxonConstants;
14 import org.nuxeo.ecm.core.api.DocumentModel;
15 import org.nuxeo.ecm.core.api.event.CoreEventConstants;
16 import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
17 import org.nuxeo.ecm.core.event.Event;
18 import org.nuxeo.ecm.core.event.EventContext;
19 import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 public class UpdateFormattedDisplayNameListener extends AbstractCSEventListenerImpl {
24 public static final String RUN_AFTER_MODIFIED_PROPERTY = "UpdateFormattedDisplayNameListener.RUN_AFTER_MODIFIED";
26 private static final String[] DISPLAY_NAME_PATH_ELEMENTS = TaxonConstants.DISPLAY_NAME_FIELD_NAME.split("/");
27 private static final String TERM_GROUP_LIST_FIELD_NAME = DISPLAY_NAME_PATH_ELEMENTS[0];
28 private static final String DISPLAY_NAME_FIELD_NAME = DISPLAY_NAME_PATH_ELEMENTS[2];
30 private static final String[] FORMATTED_DISPLAY_NAME_PATH_ELEMENTS = TaxonConstants.FORMATTED_DISPLAY_NAME_FIELD_NAME.split("/");
31 private static final String FORMATTED_DISPLAY_NAME_FIELD_NAME = FORMATTED_DISPLAY_NAME_PATH_ELEMENTS[2];
33 final Logger logger = LoggerFactory.getLogger(UpdateFormattedDisplayNameListener.class);
36 public void handleEvent(Event event) {
37 EventContext ec = event.getContext();
39 if (isRegistered(event) && ec instanceof DocumentEventContext) {
40 DocumentEventContext context = (DocumentEventContext) ec;
41 DocumentModel doc = context.getSourceDocument();
43 logger.debug("docType=" + doc.getType());
45 if (doc.getType().startsWith(TaxonConstants.NUXEO_DOCTYPE) &&
48 !doc.getCurrentLifeCycleState().equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
50 String refName = (String) doc.getProperty(TaxonConstants.REFNAME_SCHEMA_NAME, TaxonConstants.REFNAME_FIELD_NAME);
51 RefName.AuthorityItem item = RefName.AuthorityItem.parse(refName);
52 String parentShortId = item.getParentShortIdentifier();
54 logger.debug("parentShortId=" + parentShortId);
56 if (!parentShortId.equals(TaxonBotGardenConstants.COMMON_VOCABULARY_SHORTID)) {
57 if (event.getName().equals(DocumentEventTypes.DOCUMENT_CREATED)) {
58 // Save the document, to get the BEFORE_DOC_UPDATE branch to run.
59 doc.getCoreSession().saveDocument(doc);
61 else if (event.getName().equals(DocumentEventTypes.BEFORE_DOC_UPDATE)) {
62 DocumentModel previousDoc = (DocumentModel) context.getProperty(CoreEventConstants.PREVIOUS_DOCUMENT_MODEL);
64 updateFormattedDisplayNames(doc, previousDoc);
71 private void updateFormattedDisplayNames(DocumentModel doc, DocumentModel previousDoc) {
72 //Set<String> previousDisplayNames = getDisplayNames(previousDoc);
73 TaxonFormatter formatter = new TaxonFormatter();
74 List<Map<String, Object>> termGroupList = (List<Map<String, Object>>) doc.getProperty(TaxonConstants.DISPLAY_NAME_SCHEMA_NAME, TERM_GROUP_LIST_FIELD_NAME);
76 for (Map<String, Object> termGroup : termGroupList) {
77 String displayName = (String) termGroup.get(DISPLAY_NAME_FIELD_NAME);
78 String formattedDisplayName = (String) termGroup.get(FORMATTED_DISPLAY_NAME_FIELD_NAME);
80 if (StringUtils.isBlank(formattedDisplayName)) {
81 formattedDisplayName = "";
83 if (StringUtils.isNotBlank(displayName)) {
84 formattedDisplayName = formatter.format(displayName);
87 termGroup.put(FORMATTED_DISPLAY_NAME_FIELD_NAME, formattedDisplayName);
91 Map<String, Object> updateMap = new HashMap<String, Object>();
92 updateMap.put(TERM_GROUP_LIST_FIELD_NAME, termGroupList);
94 doc.setProperties(TaxonConstants.DISPLAY_NAME_SCHEMA_NAME, updateMap);
98 private Set<String> getDisplayNames(DocumentModel doc) throws ClientException {
99 Set<String> displayNames = new HashSet<String>();
100 List<Map<String, Object>> termGroupList = (List<Map<String, Object>>) doc.getProperty(TaxonConstants.DISPLAY_NAME_SCHEMA_NAME, TERM_GROUP_LIST_FIELD_NAME);
102 for (Map<String, Object> termGroup : termGroupList) {
103 String displayName = (String) termGroup.get(DISPLAY_NAME_FIELD_NAME);
105 if (displayName != null) {
106 displayNames.add(displayName);