]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
5fbe3b660ee2085f57310033713903477161a69c
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.nuxeo.elasticsearch.materials;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Iterator;
6 import java.util.LinkedHashSet;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.Set;
10
11 import org.apache.commons.lang3.StringUtils;
12
13 import org.codehaus.jackson.JsonNode;
14 import org.codehaus.jackson.map.ObjectMapper;
15 import org.codehaus.jackson.node.ObjectNode;
16 import org.codehaus.jackson.node.TextNode;
17
18 import org.collectionspace.services.common.api.RefNameUtils;
19 import org.collectionspace.services.nuxeo.elasticsearch.DefaultESDocumentWriter;
20 import org.nuxeo.ecm.core.api.CoreSession;
21 import org.nuxeo.ecm.core.api.DocumentModel;
22 import org.nuxeo.ecm.core.api.DocumentModelList;
23
24 public class MaterialsESDocumentWriter extends DefaultESDocumentWriter {
25
26         @Override
27         public ObjectNode getDenormValues(DocumentModel doc) {
28                 ObjectMapper objectMapper = new ObjectMapper();
29                 ObjectNode denormValues = objectMapper.createObjectNode();
30
31                 String docType = doc.getType();
32
33                 if (docType.startsWith("Materialitem")) {
34                         CoreSession session = doc.getCoreSession();
35
36                         // Store the csids of media records that reference this material authority item via the
37                         // coverage field.
38
39                         String refName = (String) doc.getProperty("collectionspace_core", "refName");
40
41                         if (StringUtils.isNotEmpty(refName)) {
42                                 String escapedRefName = refName.replace("'", "\\'");
43                                 String tenantId = (String) doc.getProperty("collectionspace_core", "tenantId");
44                                 String mediaQuery = String.format("SELECT * FROM Media WHERE media_common:coverage = '%s' AND ecm:currentLifeCycleState = 'project' AND collectionspace_core:tenantId = '%s' ORDER BY media_common:identificationNumber", escapedRefName, tenantId);
45
46                                 DocumentModelList mediaDocs = session.query(mediaQuery);
47                                 List<JsonNode> mediaCsids = new ArrayList<JsonNode>();
48
49                                 if (mediaDocs.size() > 0) {
50                                         Iterator<DocumentModel> iterator = mediaDocs.iterator();
51
52                                         while (iterator.hasNext()) {
53                                                 DocumentModel mediaDoc = iterator.next();
54
55                                                 if (isMediaPublished(mediaDoc)) {
56                                                         String mediaCsid = (String) mediaDoc.getName();
57
58                                                         mediaCsids.add(new TextNode(mediaCsid));
59                                                 }
60                                         }
61                                 }
62
63                                 denormValues.putArray("mediaCsid").addAll(mediaCsids);
64                         }
65
66                         // Compute the title of the record for the public browser, and store it so that it can
67                         // be used for sorting ES query results.
68
69                         String title = computeTitle(doc);
70
71                         if (title != null) {
72                                 denormValues.put("title", title);
73                         }
74
75                         List<Map<String, Object>> termGroups = (List<Map<String, Object>>) doc.getProperty("materials_common", "materialTermGroupList");
76                         List<String> commercialNames = findTermDisplayNamesWithFlag(termGroups, "commercial");
77                         List<String> commonNames = findTermDisplayNamesWithFlag(termGroups, "common");
78
79                         // Find and store the commercial names and common names for this item. This simplifies
80                         // search and display in the Material Order application.
81
82                         if (commercialNames.size() > 0) {
83                                 denormValues.putArray("commercialNames").addAll(jsonNodes(commercialNames));
84                         }
85
86                         if (commonNames.size() > 0) {
87                                 denormValues.putArray("commonNames").addAll(jsonNodes(commonNames));
88                         }
89
90                         // Combine term creator organizations and term editor organizations into a holding
91                         // institutions field.
92
93                         Set<String> holdingInstitutions = new LinkedHashSet<String>();
94
95                         holdingInstitutions.addAll(getTermAttributionContributors(doc));
96                         holdingInstitutions.addAll(getTermAttributionEditors(doc));
97
98                         if (holdingInstitutions.size() > 0) {
99                                 denormValues.putArray("holdingInstitutions").addAll(jsonNodes(holdingInstitutions));
100                         }
101                 }
102
103                 // Below is sample code for denormalizing fields from the computed current location (place
104                 // item) into collection object documents. This was written for the public browser
105                 // prototype for public art.
106
107                 /*
108                 if (docType.startsWith("CollectionObject")) {
109                         CoreSession session = doc.getCoreSession();
110
111                         String refName = (String) doc.getProperty("collectionobjects_common", "computedCurrentLocation");
112
113                         if (StringUtils.isNotEmpty(refName)) {
114                                 String escapedRefName = refName.replace("'", "\\'");
115                                 String placeQuery = String.format("SELECT * FROM PlaceitemTenant5000 WHERE places_common:refName = '%s'", escapedRefName);
116
117                                 DocumentModelList placeDocs = session.query(placeQuery, 1);
118
119                                 if (placeDocs.size() > 0) {
120                                         DocumentModel placeDoc = placeDocs.get(0);
121
122                                         String placementType = (String) placeDoc.getProperty("places_publicart:placementType").getValue();
123
124                                         if (placementType != null) {
125                                                 denormValues.put("placementType", placementType);
126                                         }
127
128                                         Property geoRefGroup;
129
130                                         try {
131                                                 geoRefGroup = placeDoc.getProperty("places_common:placeGeoRefGroupList/0");
132                                         } catch (PropertyNotFoundException e) {
133                                                 geoRefGroup = null;
134                                         }
135
136                                         if (geoRefGroup != null) {
137                                                 Double decimalLatitude = (Double) geoRefGroup.getValue("decimalLatitude");
138                                                 Double decimalLongitude = (Double) geoRefGroup.getValue("decimalLongitude");
139
140                                                 if (decimalLatitude != null && decimalLongitude != null) {
141                                                         ObjectNode geoPointNode = objectMapper.createObjectNode();
142
143                                                         geoPointNode.put("lat", decimalLatitude);
144                                                         geoPointNode.put("lon", decimalLongitude);
145
146                                                         denormValues.put("geoPoint", geoPointNode);
147                                                 }
148                                         }
149                                 }
150                         }
151
152                         String uri = (String) doc.getProperty("collectionobjects_core", "uri");
153                         String csid = uri.substring(uri.lastIndexOf('/') + 1);
154                         String mediaQuery = String.format("SELECT media_common:blobCsid, media_common:title FROM Relation WHERE relations_common:subjectCsid = '%s' AND relations_common:objectDocumentType = 'Media'", csid);
155
156                         DocumentModelList mediaDocs = session.query(mediaQuery, 1);
157
158                         if (mediaDocs.size() > 0) {
159
160                         }
161                 }
162                 */
163
164                 return denormValues;
165         }
166
167         /**
168          * Compute a title for the public browser. This needs to be indexed in ES so that it can
169          * be used for sorting. (Even if it's just extracting the primary value.)
170          */
171         private String computeTitle(DocumentModel doc) {
172                 List<Map<String, Object>> termGroups = (List<Map<String, Object>>) doc.getProperty("materials_common", "materialTermGroupList");
173                 String primaryDisplayName = null;
174
175                 if (termGroups.size() > 0) {
176                         Map<String, Object> primaryTermGroup = termGroups.get(0);
177                         primaryDisplayName = (String) primaryTermGroup.get("termDisplayName");
178                 }
179
180                 return primaryDisplayName;
181         }
182
183         private List<String> findTermDisplayNamesWithFlag(List<Map<String, Object>> termGroups, String flagShortId) {
184                 List<String> termDisplayNames = new ArrayList<String>();
185
186                 for (Map<String, Object> termGroup : termGroups) {
187                         String termFlag = (String) termGroup.get("termFlag");
188
189                         if (termFlag != null && termFlag.contains("(" + flagShortId + ")")) {
190                                 String candidateTermDisplayName = (String) termGroup.get("termDisplayName");
191
192                                 if (StringUtils.isNotEmpty(candidateTermDisplayName)) {
193                                         termDisplayNames.add(candidateTermDisplayName);
194                                 }
195                         }
196                 }
197
198                 return termDisplayNames;
199         }
200
201         private Set<String> getTermAttributionContributors(DocumentModel doc) {
202                 Set orgs = new LinkedHashSet<String>();
203
204                 List<Map<String, Object>> groups = (List<Map<String, Object>>) doc.getProperty("materials_common", "materialTermAttributionContributingGroupList");
205
206                 for (Map<String, Object> group : groups) {
207                         String org = (String) group.get("materialTermAttributionContributingOrganization");
208
209                         if (StringUtils.isNotEmpty(org)) {
210                                 orgs.add(org);
211                         }
212                 }
213
214                 return orgs;
215         }
216
217         private Set<String> getTermAttributionEditors(DocumentModel doc) {
218                 Set orgs = new LinkedHashSet<String>();
219
220                 List<Map<String, Object>> groups = (List<Map<String, Object>>) doc.getProperty("materials_common", "materialTermAttributionEditingGroupList");
221
222                 for (Map<String, Object> group : groups) {
223                         String org = (String) group.get("materialTermAttributionEditingOrganization");
224
225                         if (StringUtils.isNotEmpty(org)) {
226                                 orgs.add(org);
227                         }
228                 }
229
230                 return orgs;
231         }
232
233         private boolean isMediaPublished(DocumentModel mediaDoc) {
234                 List<String> publishToValues = (List<String>) mediaDoc.getProperty("media_materials", "publishToList");
235                 boolean isPublished = false;
236
237                 if (publishToValues != null) {
238                         for (int i=0; i<publishToValues.size(); i++) {
239                                 String value = publishToValues.get(i);
240                                 String shortId = RefNameUtils.getItemShortId(value);
241
242                                 if (shortId.equals("all") || shortId.equals("materialorder")) {
243                                         isPublished = true;
244                                         break;
245                                 }
246                         }
247                 }
248
249                 return isPublished;
250         }
251
252         private List<JsonNode> jsonNodes(Collection<String> values) {
253                 List<JsonNode> nodes = new ArrayList<JsonNode>();
254                 Iterator<String> iterator = values.iterator();
255
256                 while (iterator.hasNext()) {
257                         String value = iterator.next();
258
259                         nodes.add(new TextNode(value));
260                 }
261
262                 return nodes;
263         }
264 }