1 package org.collectionspace.services.listener.naturalhistory;
3 import java.util.HashMap;
7 import org.apache.commons.lang.StringUtils;
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
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;
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;
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);
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];
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];
39 public boolean shouldHandleEvent(Event event) {
40 EventContext ec = event.getContext();
42 if (ec instanceof DocumentEventContext) {
43 DocumentEventContext context = (DocumentEventContext) ec;
44 DocumentModel doc = context.getSourceDocument();
45 String docType = doc.getType();
47 logger.debug("docType=" + docType);
49 if (docType.startsWith(TaxonConstants.NUXEO_DOCTYPE) &&
50 !docType.startsWith(TaxonomyAuthorityConstants.NUXEO_DOCTYPE) &&
53 !doc.getCurrentLifeCycleState().equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
62 public void handleCSEvent(Event event) {
63 EventContext ec = event.getContext();
64 DocumentEventContext context = (DocumentEventContext) ec;
65 DocumentModel doc = context.getSourceDocument();
67 String docType = doc.getType();
68 logger.debug("docType=" + docType);
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();
74 logger.debug("parentShortId=" + parentShortId);
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);
81 else if (event.getName().equals(DocumentEventTypes.BEFORE_DOC_UPDATE)) {
82 DocumentModel previousDoc = (DocumentModel) context.getProperty(CoreEventConstants.PREVIOUS_DOCUMENT_MODEL);
84 updateFormattedDisplayNames(doc, previousDoc);
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);
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);
98 if (StringUtils.isBlank(formattedDisplayName)) {
99 formattedDisplayName = "";
101 if (StringUtils.isNotBlank(displayName)) {
102 formattedDisplayName = formatter.format(displayName);
105 termGroup.put(FORMATTED_DISPLAY_NAME_FIELD_NAME, formattedDisplayName);
109 Map<String, Object> updateMap = new HashMap<String, Object>();
110 updateMap.put(TERM_GROUP_LIST_FIELD_NAME, termGroupList);
112 doc.setProperties(TaxonConstants.DISPLAY_NAME_SCHEMA_NAME, updateMap);
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);
120 for (Map<String, Object> termGroup : termGroupList) {
121 String displayName = (String) termGroup.get(DISPLAY_NAME_FIELD_NAME);
123 if (displayName != null) {
124 displayNames.add(displayName);
133 public Log getLogger() {