]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
91fb3432a0238cb6da9d23108b0eec46d4db9d29
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.nuxeo.elasticsearch;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.Calendar;
7 import java.util.Collections;
8 import java.util.GregorianCalendar;
9 import java.util.HashSet;
10 import java.util.Iterator;
11 import java.util.List;
12 import java.util.Map;
13 import java.util.Set;
14
15 import javax.ws.rs.core.HttpHeaders;
16
17 import org.apache.commons.lang3.StringUtils;
18 import org.codehaus.jackson.JsonGenerator;
19 import org.codehaus.jackson.JsonNode;
20 import org.codehaus.jackson.map.ObjectMapper;
21 import org.codehaus.jackson.node.IntNode;
22 import org.codehaus.jackson.node.ObjectNode;
23 import org.codehaus.jackson.node.TextNode;
24 import org.collectionspace.services.common.api.RefNameUtils;
25 import org.nuxeo.ecm.automation.jaxrs.io.documents.JsonESDocumentWriter;
26 import org.nuxeo.ecm.core.api.CoreSession;
27 import org.nuxeo.ecm.core.api.DocumentModel;
28 import org.nuxeo.ecm.core.api.DocumentModelList;
29
30 public class DefaultESDocumentWriter extends JsonESDocumentWriter {
31         private static ObjectMapper objectMapper = new ObjectMapper();
32
33         @Override
34         public void writeDoc(JsonGenerator jg, DocumentModel doc, String[] schemas,
35                         Map<String, String> contextParameters, HttpHeaders headers)
36                         throws IOException {
37
38                 ObjectNode denormValues = getDenormValues(doc);
39
40                 jg.writeStartObject();
41
42                 writeSystemProperties(jg, doc);
43                 writeSchemas(jg, doc, schemas);
44                 writeContextParameters(jg, doc, contextParameters);
45                 writeDenormValues(jg, doc, denormValues);
46
47                 jg.writeEndObject();
48                 jg.flush();
49         }
50
51         public ObjectNode getDenormValues(DocumentModel doc) {
52                 ObjectNode denormValues = objectMapper.createObjectNode();
53                 String docType = doc.getType();
54
55                 if (docType.startsWith("CollectionObject")) {
56                         CoreSession session = doc.getCoreSession();
57                         String csid = doc.getName();
58                         String tenantId = (String) doc.getProperty("collectionspace_core", "tenantId");
59
60                         denormMediaRecords(session, csid, tenantId, denormValues);
61                         denormAcquisitionRecords(session, csid, tenantId, denormValues);
62                         denormExhibitionRecords(session, csid, tenantId, denormValues);
63                         denormMaterialFields(doc, denormValues);
64
65                         // Compute the title of the record for the public browser, and store it so that it can
66                         // be used for sorting ES query results.
67
68                         String title = computeTitle(doc);
69
70                         if (title != null) {
71                                 denormValues.put("title", title);
72                         }
73
74                         // Create a list of production years from the production date structured dates.
75
76                         List<Map<String, Object>> prodDateGroupList = (List<Map<String, Object>>) doc.getProperty("collectionobjects_common", "objectProductionDateGroupList");
77
78                         denormValues.putArray("prodYears").addAll(structDatesToYearNodes(prodDateGroupList));
79                 }
80
81                 return denormValues;
82         }
83
84         public void writeDenormValues(JsonGenerator jg, DocumentModel doc, ObjectNode denormValues) throws IOException {
85                 if (denormValues != null && denormValues.size() > 0) {
86                         if (jg.getCodec() == null) {
87                                 jg.setCodec(objectMapper);
88                         }
89
90                         Iterator<Map.Entry<String, JsonNode>> entries = denormValues.getFields();
91
92                         while (entries.hasNext()) {
93                                 Map.Entry<String, JsonNode> entry = entries.next();
94
95                                 jg.writeFieldName("collectionspace_denorm:" + entry.getKey());
96                                 jg.writeTree(entry.getValue());
97                         }
98                 }
99         }
100
101         private void denormMediaRecords(CoreSession session, String csid, String tenantId, ObjectNode denormValues) {
102                 // Store the csid and alt text of media records that are related to this object.
103
104                 String relatedRecordQuery = String.format("SELECT * FROM Relation WHERE relations_common:subjectCsid = '%s' AND relations_common:objectDocumentType = 'Media' AND ecm:currentLifeCycleState = 'project' AND collectionspace_core:tenantId = '%s'", csid, tenantId);
105                 DocumentModelList relationDocs = session.query(relatedRecordQuery);
106                 List<JsonNode> mediaCsids = new ArrayList<JsonNode>();
107                 List<JsonNode> mediaAltTexts = new ArrayList<JsonNode>();
108
109                 if (relationDocs.size() > 0) {
110                         Iterator<DocumentModel> iterator = relationDocs.iterator();
111
112                         while (iterator.hasNext()) {
113                                 DocumentModel relationDoc = iterator.next();
114                                 String mediaCsid = (String) relationDoc.getProperty("relations_common", "objectCsid");
115                                 DocumentModel mediaDoc = getRecordByCsid(session, tenantId, "Media", mediaCsid);
116
117                                 if (isMediaPublished(mediaDoc)) {
118                                         mediaCsids.add(new TextNode(mediaCsid));
119
120                                         String altText = (String) mediaDoc.getProperty("media_common", "altText");
121
122                                         if (altText == null) {
123                                                 altText = "";
124                                         }
125
126                                         mediaAltTexts.add(new TextNode(altText));
127                                 }
128                         }
129                 }
130
131                 denormValues.putArray("mediaCsid").addAll(mediaCsids);
132                 denormValues.putArray("mediaAltText").addAll(mediaAltTexts);
133                 denormValues.put("hasMedia", mediaCsids.size() > 0);
134         }
135
136         private void denormAcquisitionRecords(CoreSession session, String csid, String tenantId, ObjectNode denormValues) {
137                 // Store the credit lines of acquisition records that are related to this object.
138
139                 String relatedRecordQuery = String.format("SELECT * FROM Relation WHERE relations_common:subjectCsid = '%s' AND relations_common:objectDocumentType = 'Acquisition' AND ecm:currentLifeCycleState = 'project' AND collectionspace_core:tenantId = '%s'", csid, tenantId);
140                 DocumentModelList relationDocs = session.query(relatedRecordQuery);
141                 List<JsonNode> creditLines = new ArrayList<JsonNode>();
142
143                 if (relationDocs.size() > 0) {
144                         Iterator<DocumentModel> iterator = relationDocs.iterator();
145
146                         while (iterator.hasNext()) {
147                                 DocumentModel relationDoc = iterator.next();
148                                 String acquisitionCsid = (String) relationDoc.getProperty("relations_common", "objectCsid");
149                                 String creditLine = getCreditLine(session, tenantId, acquisitionCsid);
150
151                                 if (creditLine != null && creditLine.length() > 0) {
152                                         creditLines.add(new TextNode(creditLine));
153                                 }
154                         }
155                 }
156
157                 denormValues.putArray("creditLine").addAll(creditLines);
158 }
159
160 private void denormExhibitionRecords(CoreSession session, String csid, String tenantId, ObjectNode denormValues) {
161         // Store the title, general note, and curatorial note of exhibition records that are published, and related to this object.
162
163         String relatedRecordQuery = String.format("SELECT * FROM Relation WHERE relations_common:subjectCsid = '%s' AND relations_common:objectDocumentType = 'Exhibition' AND ecm:currentLifeCycleState = 'project' AND collectionspace_core:tenantId = '%s'", csid, tenantId);
164         DocumentModelList relationDocs = session.query(relatedRecordQuery);
165         List<JsonNode> exhibitions = new ArrayList<JsonNode>();
166
167         if (relationDocs.size() > 0) {
168                 Iterator<DocumentModel> iterator = relationDocs.iterator();
169
170                 while (iterator.hasNext()) {
171                         DocumentModel relationDoc = iterator.next();
172                         String exhibitionCsid = (String) relationDoc.getProperty("relations_common", "objectCsid");
173                         DocumentModel exhibitionDoc = getRecordByCsid(session, tenantId, "Exhibition", exhibitionCsid);
174
175                         if (exhibitionDoc != null && isExhibitionPublished(exhibitionDoc)) {
176                                 ObjectNode exhibitionNode = objectMapper.createObjectNode();
177
178                                 String title = (String) exhibitionDoc.getProperty("exhibitions_common", "title");
179                                 String generalNote = (String) exhibitionDoc.getProperty("exhibitions_common", "generalNote");
180                                 String curatorialNote = (String) exhibitionDoc.getProperty("exhibitions_common", "curatorialNote");
181
182                                 exhibitionNode.put("title", title);
183                                 exhibitionNode.put("generalNote", generalNote);
184                                 exhibitionNode.put("curatorialNote", curatorialNote);
185
186                                 exhibitions.add(exhibitionNode);
187                         }
188                 }
189         }
190
191         denormValues.putArray("exhibition").addAll(exhibitions);
192 }
193
194         /**
195          * Denormalize the material group list for a collectionobject in order to index the controlled or uncontrolled term
196          *
197          * @param doc the collectionobject document
198          * @param denormValues the json node for denormalized fields
199          */
200         private void denormMaterialFields(DocumentModel doc, ObjectNode denormValues) {
201                 List<Map<String, Object>> materialGroupList =
202                         (List<Map<String, Object>>) doc.getProperty("collectionobjects_common", "materialGroupList");
203
204                 List<JsonNode> denormMaterials = new ArrayList<>();
205                 for (Map<String, Object> materialGroup : materialGroupList) {
206                         String controlledMaterial = (String) materialGroup.get("materialControlled");
207                         if (controlledMaterial != null) {
208                                 final ObjectNode node = objectMapper.createObjectNode();
209                                 node.put("material", RefNameUtils.getDisplayName(controlledMaterial));
210                                 denormMaterials.add(node);
211                         }
212
213                         String material = (String) materialGroup.get("material");
214                         if (material != null) {
215                                 final ObjectNode node = objectMapper.createObjectNode();
216                                 node.put("material", material);
217                                 denormMaterials.add(node);
218                         }
219                 }
220
221                 denormValues.putArray("materialGroupList").addAll(denormMaterials);
222         }
223
224         /**
225          * Compute a title for the public browser. This needs to be indexed in ES so that it can
226          * be used for sorting. (Even if it's just extracting the primary value.)
227          */
228         protected String computeTitle(DocumentModel doc) {
229                 List<Map<String, Object>> titleGroups = (List<Map<String, Object>>) doc.getProperty("collectionobjects_common", "titleGroupList");
230                 String primaryTitle = null;
231
232                 if (titleGroups.size() > 0) {
233                         Map<String, Object> primaryTitleGroup = titleGroups.get(0);
234                         primaryTitle = (String) primaryTitleGroup.get("title");
235                 }
236
237                 if (StringUtils.isNotEmpty(primaryTitle)) {
238                         return primaryTitle;
239                 }
240
241                 List<Map<String, Object>> objectNameGroups = (List<Map<String, Object>>) doc.getProperty("collectionobjects_common", "objectNameList");
242                 String primaryObjectName = null;
243
244                 if (objectNameGroups.size() > 0) {
245                         Map<String, Object> primaryObjectNameGroup = objectNameGroups.get(0);
246                         primaryObjectName = (String) primaryObjectNameGroup.get("objectNameControlled");
247                         if (primaryObjectName == null) {
248                                 primaryObjectName = (String) primaryObjectNameGroup.get("objectName");
249                         }
250
251                         // The object might be a refname in some profiles/tenants. If it is, use only the display name.
252
253                         try {
254                                 String displayName = RefNameUtils.getDisplayName(primaryObjectName);
255
256                                 if (displayName != null) {
257                                         primaryObjectName = displayName;
258                                 }
259                         }
260                         catch (Exception e) {}
261                 }
262
263                 return primaryObjectName;
264         }
265
266         private boolean isPublished(DocumentModel doc, String publishedFieldPart, String publishedFieldName) {
267                 boolean isPublished = false;
268
269                 if (doc != null) {
270                         List<String> publishToValues = (List<String>) doc.getProperty(publishedFieldPart, publishedFieldName);
271
272                         if (publishToValues != null) {
273                                 for (int i=0; i<publishToValues.size(); i++) {
274                                         String value = publishToValues.get(i);
275                                         String shortId = RefNameUtils.getItemShortId(value);
276
277                                         if (shortId.equals("all") || shortId.equals("cspacepub")) {
278                                                 isPublished = true;
279                                                 break;
280                                         }
281                                 }
282                         }
283                 }
284
285                 return isPublished;
286
287         }
288
289         private boolean isMediaPublished(DocumentModel mediaDoc) {
290                 return isPublished(mediaDoc, "media_common", "publishToList");
291         }
292
293         private boolean isExhibitionPublished(DocumentModel exhibitionDoc) {
294                 return isPublished(exhibitionDoc, "exhibitions_common", "publishToList");
295         }
296
297         private String getCreditLine(CoreSession session, String tenantId, String acquisitionCsid) {
298                 String creditLine = null;
299                 DocumentModel acquisitionDoc = getRecordByCsid(session, tenantId, "Acquisition", acquisitionCsid);
300
301                 if (acquisitionDoc != null) {
302                         creditLine = (String) acquisitionDoc.getProperty("acquisitions_common", "creditLine");
303                 }
304
305                 return creditLine;
306         }
307
308         protected DocumentModel getRecordByCsid(CoreSession session, String tenantId, String recordType, String csid) {
309                 String getRecordQuery = String.format("SELECT * FROM %s WHERE ecm:name = '%s' AND ecm:currentLifeCycleState = 'project' AND collectionspace_core:tenantId = '%s'", recordType, csid, tenantId);
310
311                 DocumentModelList docs = session.query(getRecordQuery);
312
313                 if (docs != null && docs.size() > 0) {
314                         return docs.get(0);
315                 }
316
317                 return null;
318         }
319
320         protected List<JsonNode> structDateToYearNodes(Map<String, Object> structDate) {
321                 return structDatesToYearNodes(Arrays.asList(structDate));
322         }
323
324         protected List<JsonNode> structDatesToYearNodes(List<Map<String, Object>> structDates) {
325                 Set<Integer> years = new HashSet<Integer>();
326
327                 for (Map<String, Object> structDate : structDates) {
328                         if (structDate != null) {
329                                 GregorianCalendar earliestCalendar = (GregorianCalendar) structDate.get("dateEarliestScalarValue");
330                                 GregorianCalendar latestCalendar = (GregorianCalendar) structDate.get("dateLatestScalarValue");
331
332                                 if (earliestCalendar != null && latestCalendar != null) {
333                                         // Grr @ latest scalar value historically being exclusive.
334                                         // Subtract one day to make it inclusive.
335                                         latestCalendar.add(Calendar.DATE, -1);
336
337                                         Integer earliestYear = earliestCalendar.get(Calendar.YEAR);
338                                         Integer latestYear = latestCalendar.get(Calendar.YEAR);;
339
340                                         for (int year = earliestYear; year <= latestYear; year++) {
341                                                 years.add(year);
342                                         }
343                                 }
344                         }
345                 }
346
347                 List<Integer> yearList = new ArrayList<Integer>(years);
348                 Collections.sort(yearList);
349
350                 List<JsonNode> yearNodes = new ArrayList<JsonNode>();
351
352                 for (Integer year : yearList) {
353                         yearNodes.add(new IntNode(year));
354                 }
355
356                 return yearNodes;
357         }
358 }