]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
78d10acde8234f910d0053ecd480cbb069becb59
[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
63                         // Compute the title of the record for the public browser, and store it so that it can
64                         // be used for sorting ES query results.
65
66                         String title = computeTitle(doc);
67
68                         if (title != null) {
69                                 denormValues.put("title", title);
70                         }
71
72                         // Create a list of production years from the production date structured dates.
73
74                         List<Map<String, Object>> prodDateGroupList = (List<Map<String, Object>>) doc.getProperty("collectionobjects_common", "objectProductionDateGroupList");
75
76                         denormValues.putArray("prodYears").addAll(structDatesToYearNodes(prodDateGroupList));
77                 }
78
79                 return denormValues;
80         }
81
82         public void writeDenormValues(JsonGenerator jg, DocumentModel doc, ObjectNode denormValues) throws IOException {
83                 if (denormValues != null && denormValues.size() > 0) {
84                         if (jg.getCodec() == null) {
85                                 jg.setCodec(objectMapper);
86                         }
87
88                         Iterator<Map.Entry<String, JsonNode>> entries = denormValues.getFields();
89
90                         while (entries.hasNext()) {
91                                 Map.Entry<String, JsonNode> entry = entries.next();
92
93                                 jg.writeFieldName("collectionspace_denorm:" + entry.getKey());
94                                 jg.writeTree(entry.getValue());
95                         }
96                 }
97         }
98
99         private void denormMediaRecords(CoreSession session, String csid, String tenantId, ObjectNode denormValues) {
100                 // Store the csids of media records that are related to this object.
101
102                 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);
103                 DocumentModelList relationDocs = session.query(relatedRecordQuery);
104                 List<JsonNode> mediaCsids = new ArrayList<JsonNode>();
105
106                 if (relationDocs.size() > 0) {
107                         Iterator<DocumentModel> iterator = relationDocs.iterator();
108
109                         while (iterator.hasNext()) {
110                                 DocumentModel relationDoc = iterator.next();
111                                 String mediaCsid = (String) relationDoc.getProperty("relations_common", "objectCsid");
112
113                                 if (isMediaPublished(session, tenantId, mediaCsid)) {
114                                         mediaCsids.add(new TextNode(mediaCsid));
115                                 }
116                         }
117                 }
118
119                 denormValues.putArray("mediaCsid").addAll(mediaCsids);
120                 denormValues.put("hasMedia", mediaCsids.size() > 0);
121         }
122
123         private void denormAcquisitionRecords(CoreSession session, String csid, String tenantId, ObjectNode denormValues) {
124                 // Store the credit lines of acquisition records that are related to this object.
125
126                 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);
127                 DocumentModelList relationDocs = session.query(relatedRecordQuery);
128                 List<JsonNode> creditLines = new ArrayList<JsonNode>();
129
130                 if (relationDocs.size() > 0) {
131                         Iterator<DocumentModel> iterator = relationDocs.iterator();
132
133                         while (iterator.hasNext()) {
134                                 DocumentModel relationDoc = iterator.next();
135                                 String acquisitionCsid = (String) relationDoc.getProperty("relations_common", "objectCsid");
136                                 String creditLine = getCreditLine(session, tenantId, acquisitionCsid);
137
138                                 if (creditLine != null && creditLine.length() > 0) {
139                                         creditLines.add(new TextNode(creditLine));
140                                 }
141                         }
142                 }
143
144                 denormValues.putArray("creditLine").addAll(creditLines);
145 }
146
147         /**
148          * Compute a title for the public browser. This needs to be indexed in ES so that it can
149          * be used for sorting. (Even if it's just extracting the primary value.)
150          */
151         private String computeTitle(DocumentModel doc) {
152                 List<Map<String, Object>> titleGroups = (List<Map<String, Object>>) doc.getProperty("collectionobjects_common", "titleGroupList");
153                 String primaryTitle = null;
154
155                 if (titleGroups.size() > 0) {
156                         Map<String, Object> primaryTitleGroup = titleGroups.get(0);
157                         primaryTitle = (String) primaryTitleGroup.get("title");
158                 }
159
160                 if (StringUtils.isNotEmpty(primaryTitle)) {
161                         return primaryTitle;
162                 }
163
164                 List<Map<String, Object>> objectNameGroups = (List<Map<String, Object>>) doc.getProperty("collectionobjects_common", "objectNameList");
165                 String primaryObjectName = null;
166
167                 if (objectNameGroups.size() > 0) {
168                         Map<String, Object> primaryObjectNameGroup = objectNameGroups.get(0);
169                         primaryObjectName = (String) primaryObjectNameGroup.get("objectName");
170                 }
171
172                 return primaryObjectName;
173         }
174
175         private boolean isMediaPublished(CoreSession session, String tenantId, String mediaCsid) {
176                 boolean isPublished = false;
177                 DocumentModel mediaDoc = getRecordByCsid(session, tenantId, "Media", mediaCsid);
178
179                 if (mediaDoc != null) {
180                         List<String> publishToValues = (List<String>) mediaDoc.getProperty("media_common", "publishToList");
181
182                         if (publishToValues != null) {
183                                 for (int i=0; i<publishToValues.size(); i++) {
184                                         String value = publishToValues.get(i);
185                                         String shortId = RefNameUtils.getItemShortId(value);
186
187                                         if (shortId.equals("all") || shortId.equals("cspacepub")) {
188                                                 isPublished = true;
189                                                 break;
190                                         }
191                                 }
192                         }
193                 }
194
195                 return isPublished;
196         }
197
198         private String getCreditLine(CoreSession session, String tenantId, String acquisitionCsid) {
199                 String creditLine = null;
200                 DocumentModel acquisitionDoc = getRecordByCsid(session, tenantId, "Acquisition", acquisitionCsid);
201
202                 if (acquisitionDoc != null) {
203                         creditLine = (String) acquisitionDoc.getProperty("acquisitions_common", "creditLine");
204                 }
205
206                 return creditLine;
207         }
208
209         protected DocumentModel getRecordByCsid(CoreSession session, String tenantId, String recordType, String csid) {
210                 String getRecordQuery = String.format("SELECT * FROM %s WHERE ecm:name = '%s' AND ecm:currentLifeCycleState = 'project' AND collectionspace_core:tenantId = '%s'", recordType, csid, tenantId);
211
212                 DocumentModelList docs = session.query(getRecordQuery);
213
214                 if (docs != null && docs.size() > 0) {
215                         return docs.get(0);
216                 }
217
218                 return null;
219         }
220
221         protected List<JsonNode> structDateToYearNodes(Map<String, Object> structDate) {
222                 return structDatesToYearNodes(Arrays.asList(structDate));
223         }
224
225         protected List<JsonNode> structDatesToYearNodes(List<Map<String, Object>> structDates) {
226                 Set<Integer> years = new HashSet<Integer>();
227
228                 for (Map<String, Object> structDate : structDates) {
229                         if (structDate != null) {
230                                 GregorianCalendar earliestCalendar = (GregorianCalendar) structDate.get("dateEarliestScalarValue");
231                                 GregorianCalendar latestCalendar = (GregorianCalendar) structDate.get("dateLatestScalarValue");
232
233                                 if (earliestCalendar != null && latestCalendar != null) {
234                                         // Grr @ latest scalar value historically being exclusive.
235                                         // Subtract one day to make it inclusive.
236                                         latestCalendar.add(Calendar.DATE, -1);
237
238                                         Integer earliestYear = earliestCalendar.get(Calendar.YEAR);
239                                         Integer latestYear = latestCalendar.get(Calendar.YEAR);;
240
241                                         for (int year = earliestYear; year <= latestYear; year++) {
242                                                 years.add(year);
243                                         }
244                                 }
245                         }
246                 }
247
248                 List<Integer> yearList = new ArrayList<Integer>(years);
249                 Collections.sort(yearList);
250
251                 List<JsonNode> yearNodes = new ArrayList<JsonNode>();
252
253                 for (Integer year : yearList) {
254                         yearNodes.add(new IntNode(year));
255                 }
256
257                 return yearNodes;
258         }
259 }