1 package org.collectionspace.services.nuxeo.elasticsearch;
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;
15 import javax.ws.rs.core.HttpHeaders;
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;
30 public class DefaultESDocumentWriter extends JsonESDocumentWriter {
31 private static ObjectMapper objectMapper = new ObjectMapper();
34 public void writeDoc(JsonGenerator jg, DocumentModel doc, String[] schemas,
35 Map<String, String> contextParameters, HttpHeaders headers)
38 ObjectNode denormValues = getDenormValues(doc);
40 jg.writeStartObject();
42 writeSystemProperties(jg, doc);
43 writeSchemas(jg, doc, schemas);
44 writeContextParameters(jg, doc, contextParameters);
45 writeDenormValues(jg, doc, denormValues);
51 public ObjectNode getDenormValues(DocumentModel doc) {
52 ObjectNode denormValues = objectMapper.createObjectNode();
53 String docType = doc.getType();
55 if (docType.startsWith("CollectionObject")) {
56 CoreSession session = doc.getCoreSession();
57 String csid = doc.getName();
58 String tenantId = (String) doc.getProperty("collectionspace_core", "tenantId");
60 denormMediaRecords(session, csid, tenantId, denormValues);
61 denormAcquisitionRecords(session, csid, tenantId, denormValues);
62 denormExhibitionRecords(session, csid, tenantId, denormValues);
63 denormMaterialFields(doc, denormValues);
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.
68 String title = computeTitle(doc);
71 denormValues.put("title", title);
74 // Create a list of production years from the production date structured dates.
76 List<Map<String, Object>> prodDateGroupList = (List<Map<String, Object>>) doc.getProperty("collectionobjects_common", "objectProductionDateGroupList");
78 denormValues.putArray("prodYears").addAll(structDatesToYearNodes(prodDateGroupList));
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);
90 Iterator<Map.Entry<String, JsonNode>> entries = denormValues.getFields();
92 while (entries.hasNext()) {
93 Map.Entry<String, JsonNode> entry = entries.next();
95 jg.writeFieldName("collectionspace_denorm:" + entry.getKey());
96 jg.writeTree(entry.getValue());
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.
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>();
109 if (relationDocs.size() > 0) {
110 Iterator<DocumentModel> iterator = relationDocs.iterator();
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);
117 if (isMediaPublished(mediaDoc)) {
118 mediaCsids.add(new TextNode(mediaCsid));
120 String altText = (String) mediaDoc.getProperty("media_common", "altText");
122 if (altText == null) {
126 mediaAltTexts.add(new TextNode(altText));
131 denormValues.putArray("mediaCsid").addAll(mediaCsids);
132 denormValues.putArray("mediaAltText").addAll(mediaAltTexts);
133 denormValues.put("hasMedia", mediaCsids.size() > 0);
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.
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>();
143 if (relationDocs.size() > 0) {
144 Iterator<DocumentModel> iterator = relationDocs.iterator();
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);
151 if (creditLine != null && creditLine.length() > 0) {
152 creditLines.add(new TextNode(creditLine));
157 denormValues.putArray("creditLine").addAll(creditLines);
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.
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>();
167 if (relationDocs.size() > 0) {
168 Iterator<DocumentModel> iterator = relationDocs.iterator();
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);
175 if (exhibitionDoc != null && isExhibitionPublished(exhibitionDoc)) {
176 ObjectNode exhibitionNode = objectMapper.createObjectNode();
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");
182 exhibitionNode.put("title", title);
183 exhibitionNode.put("generalNote", generalNote);
184 exhibitionNode.put("curatorialNote", curatorialNote);
186 exhibitions.add(exhibitionNode);
191 denormValues.putArray("exhibition").addAll(exhibitions);
195 * Denormalize the material group list for a collectionobject in order to index the controlled or uncontrolled term
197 * @param doc the collectionobject document
198 * @param denormValues the json node for denormalized fields
200 private void denormMaterialFields(DocumentModel doc, ObjectNode denormValues) {
201 List<Map<String, Object>> materialGroupList =
202 (List<Map<String, Object>>) doc.getProperty("collectionobjects_common", "materialGroupList");
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);
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);
221 denormValues.putArray("materialGroupList").addAll(denormMaterials);
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.)
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;
232 if (titleGroups.size() > 0) {
233 Map<String, Object> primaryTitleGroup = titleGroups.get(0);
234 primaryTitle = (String) primaryTitleGroup.get("title");
237 if (StringUtils.isNotEmpty(primaryTitle)) {
241 List<Map<String, Object>> objectNameGroups = (List<Map<String, Object>>) doc.getProperty("collectionobjects_common", "objectNameList");
242 String primaryObjectName = null;
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");
251 // The object might be a refname in some profiles/tenants. If it is, use only the display name.
254 String displayName = RefNameUtils.getDisplayName(primaryObjectName);
256 if (displayName != null) {
257 primaryObjectName = displayName;
260 catch (Exception e) {}
263 return primaryObjectName;
266 private boolean isPublished(DocumentModel doc, String publishedFieldPart, String publishedFieldName) {
267 boolean isPublished = false;
270 List<String> publishToValues = (List<String>) doc.getProperty(publishedFieldPart, publishedFieldName);
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);
277 if (shortId.equals("all") || shortId.equals("cspacepub")) {
289 private boolean isMediaPublished(DocumentModel mediaDoc) {
290 return isPublished(mediaDoc, "media_common", "publishToList");
293 private boolean isExhibitionPublished(DocumentModel exhibitionDoc) {
294 return isPublished(exhibitionDoc, "exhibitions_common", "publishToList");
297 private String getCreditLine(CoreSession session, String tenantId, String acquisitionCsid) {
298 String creditLine = null;
299 DocumentModel acquisitionDoc = getRecordByCsid(session, tenantId, "Acquisition", acquisitionCsid);
301 if (acquisitionDoc != null) {
302 creditLine = (String) acquisitionDoc.getProperty("acquisitions_common", "creditLine");
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);
311 DocumentModelList docs = session.query(getRecordQuery);
313 if (docs != null && docs.size() > 0) {
320 protected List<JsonNode> structDateToYearNodes(Map<String, Object> structDate) {
321 return structDatesToYearNodes(Arrays.asList(structDate));
324 protected List<JsonNode> structDatesToYearNodes(List<Map<String, Object>> structDates) {
325 Set<Integer> years = new HashSet<Integer>();
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");
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);
337 Integer earliestYear = earliestCalendar.get(Calendar.YEAR);
338 Integer latestYear = latestCalendar.get(Calendar.YEAR);;
340 for (int year = earliestYear; year <= latestYear; year++) {
347 List<Integer> yearList = new ArrayList<Integer>(years);
348 Collections.sort(yearList);
350 List<JsonNode> yearNodes = new ArrayList<JsonNode>();
352 for (Integer year : yearList) {
353 yearNodes.add(new IntNode(year));