]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
fcf6a4a69252c64e584461c6ea1a92e90910071f
[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 import org.collectionspace.services.client.workflow.WorkflowClient;
11 import org.collectionspace.services.common.api.RefName;
12 import org.collectionspace.services.common.api.TaxonFormatter;
13 import org.collectionspace.services.nuxeo.listener.AbstractCSEventListenerImpl;
14 import org.collectionspace.services.taxonomy.nuxeo.TaxonBotGardenConstants;
15 import org.collectionspace.services.taxonomy.nuxeo.TaxonConstants;
16 import org.nuxeo.ecm.core.api.DocumentModel;
17 import org.nuxeo.ecm.core.api.event.CoreEventConstants;
18 import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
19 import org.nuxeo.ecm.core.event.Event;
20 import org.nuxeo.ecm.core.event.EventContext;
21 import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
22
23 public class UpdateFormattedDisplayNameListener extends AbstractCSEventListenerImpl {
24         public static final String RUN_AFTER_MODIFIED_PROPERTY = "UpdateFormattedDisplayNameListener.RUN_AFTER_MODIFIED";
25
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];
29         
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];
32
33         final Log logger = LogFactory.getLog(UpdateFormattedDisplayNameListener.class);
34         
35         @Override
36         public void handleEvent(Event event) {
37                 EventContext ec = event.getContext();
38
39                 if (isRegistered(event) && ec instanceof DocumentEventContext) {
40                         DocumentEventContext context = (DocumentEventContext) ec;
41                         DocumentModel doc = context.getSourceDocument();
42
43                         logger.debug("docType=" + doc.getType());
44                         
45                         if (doc.getType().startsWith(TaxonConstants.NUXEO_DOCTYPE) && 
46                                         !doc.isVersion() && 
47                                         !doc.isProxy() && 
48                                         !doc.getCurrentLifeCycleState().equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
49                                 
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();
53                                 
54                                 logger.debug("parentShortId=" + parentShortId);
55                                 
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);
60                                         }
61                                         else if (event.getName().equals(DocumentEventTypes.BEFORE_DOC_UPDATE)) {
62                                                 DocumentModel previousDoc = (DocumentModel) context.getProperty(CoreEventConstants.PREVIOUS_DOCUMENT_MODEL);                    
63         
64                                                 updateFormattedDisplayNames(doc, previousDoc);
65                                         }
66                                 }
67                         }
68                 }
69         }
70         
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);
75
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);
79                         
80                         if (StringUtils.isBlank(formattedDisplayName)) {
81                                 formattedDisplayName = "";
82                                 
83                                 if (StringUtils.isNotBlank(displayName)) {
84                                         formattedDisplayName = formatter.format(displayName);
85                                 }
86         
87                                 termGroup.put(FORMATTED_DISPLAY_NAME_FIELD_NAME, formattedDisplayName);
88                         }
89                 }
90                 
91                 Map<String, Object> updateMap = new HashMap<String, Object>();
92                 updateMap.put(TERM_GROUP_LIST_FIELD_NAME, termGroupList);
93                 
94                 doc.setProperties(TaxonConstants.DISPLAY_NAME_SCHEMA_NAME, updateMap);
95         }
96         
97         /*
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);
101
102                 for (Map<String, Object> termGroup : termGroupList) {
103                         String displayName = (String) termGroup.get(DISPLAY_NAME_FIELD_NAME);
104                         
105                         if (displayName != null) {
106                                 displayNames.add(displayName);
107                         }
108                 }
109                 
110                 return displayNames;
111         }
112         */
113 }