1 package org.collectionspace.services.nuxeo.elasticsearch;
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.Iterator;
9 import javax.ws.rs.core.HttpHeaders;
11 import org.apache.commons.lang3.StringUtils;
12 import org.codehaus.jackson.JsonGenerator;
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;
18 import org.collectionspace.services.common.api.RefNameUtils;
20 import org.nuxeo.ecm.automation.jaxrs.io.documents.JsonESDocumentWriter;
21 import org.nuxeo.ecm.core.api.CoreSession;
22 import org.nuxeo.ecm.core.api.DocumentModel;
23 import org.nuxeo.ecm.core.api.DocumentModelList;
25 public class CSJsonESDocumentWriter extends JsonESDocumentWriter {
26 private static ObjectMapper objectMapper = new ObjectMapper();
29 public void writeDoc(JsonGenerator jg, DocumentModel doc, String[] schemas,
30 Map<String, String> contextParameters, HttpHeaders headers)
33 // Compute and store fields that should be indexed with this document in ElasticSearch.
34 // TODO: Make this configurable. This is currently hardcoded for the materials profile and
35 // the Material Order application.
37 ObjectNode denormValues = objectMapper.createObjectNode();
39 String docType = doc.getType();
41 if (docType.startsWith("Materialitem")) {
42 CoreSession session = doc.getCoreSession();
44 // Store the csids of media records that reference this material authority item via the
47 String refName = (String) doc.getProperty("collectionspace_core", "refName");
49 if (StringUtils.isNotEmpty(refName)) {
50 String escapedRefName = refName.replace("'", "\\'");
51 String tenantId = (String) doc.getProperty("collectionspace_core", "tenantId");
52 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);
54 DocumentModelList mediaDocs = session.query(mediaQuery);
55 List<JsonNode> mediaCsids = new ArrayList<JsonNode>();
57 if (mediaDocs.size() > 0) {
58 Iterator<DocumentModel> iterator = mediaDocs.iterator();
60 while (iterator.hasNext()) {
61 DocumentModel mediaDoc = iterator.next();
63 if (isMediaPublished(mediaDoc)) {
64 String mediaCsid = (String) mediaDoc.getName();
66 mediaCsids.add(new TextNode(mediaCsid));
71 denormValues.putArray("mediaCsid").addAll(mediaCsids);
74 // Compute the title of the record for the public browser, and store it so that it can
75 // be used for sorting ES query results.
77 String title = computeTitle(doc);
80 denormValues.put("title", title);
83 List<Map<String, Object>> termGroups = (List<Map<String, Object>>) doc.getProperty("materials_common", "materialTermGroupList");
84 List<String> commercialNames = findTermDisplayNamesWithFlag(termGroups, "commercial");
85 List<String> commonNames = findTermDisplayNamesWithFlag(termGroups, "common");
87 // Find and store the commercial names and common names for this item. This simplifies
88 // search and display in the Material Order application.
90 if (commercialNames.size() > 0) {
91 denormValues.putArray("commercialNames").addAll(jsonNodes(commercialNames));
94 if (commonNames.size() > 0) {
95 denormValues.putArray("commonNames").addAll(jsonNodes(commonNames));
99 // Below is sample code for denormalizing fields from the computed current location (place
100 // item) into collection object documents. This was written for the public browser
101 // prototype for public art.
104 if (docType.startsWith("CollectionObject")) {
105 CoreSession session = doc.getCoreSession();
107 String refName = (String) doc.getProperty("collectionobjects_common", "computedCurrentLocation");
109 if (StringUtils.isNotEmpty(refName)) {
110 String escapedRefName = refName.replace("'", "\\'");
111 String placeQuery = String.format("SELECT * FROM PlaceitemTenant5000 WHERE places_common:refName = '%s'", escapedRefName);
113 DocumentModelList placeDocs = session.query(placeQuery, 1);
115 if (placeDocs.size() > 0) {
116 DocumentModel placeDoc = placeDocs.get(0);
118 String placementType = (String) placeDoc.getProperty("places_publicart:placementType").getValue();
120 if (placementType != null) {
121 denormValues.put("placementType", placementType);
124 Property geoRefGroup;
127 geoRefGroup = placeDoc.getProperty("places_common:placeGeoRefGroupList/0");
128 } catch (PropertyNotFoundException e) {
132 if (geoRefGroup != null) {
133 Double decimalLatitude = (Double) geoRefGroup.getValue("decimalLatitude");
134 Double decimalLongitude = (Double) geoRefGroup.getValue("decimalLongitude");
136 if (decimalLatitude != null && decimalLongitude != null) {
137 ObjectNode geoPointNode = objectMapper.createObjectNode();
139 geoPointNode.put("lat", decimalLatitude);
140 geoPointNode.put("lon", decimalLongitude);
142 denormValues.put("geoPoint", geoPointNode);
148 String uri = (String) doc.getProperty("collectionobjects_core", "uri");
149 String csid = uri.substring(uri.lastIndexOf('/') + 1);
150 String mediaQuery = String.format("SELECT media_common:blobCsid, media_common:title FROM Relation WHERE relations_common:subjectCsid = '%s' AND relations_common:objectDocumentType = 'Media'", csid);
152 DocumentModelList mediaDocs = session.query(mediaQuery, 1);
154 if (mediaDocs.size() > 0) {
160 jg.writeStartObject();
162 writeSystemProperties(jg, doc);
163 writeSchemas(jg, doc, schemas);
164 writeContextParameters(jg, doc, contextParameters);
165 writeDenormValues(jg, doc, denormValues);
171 public void writeDenormValues(JsonGenerator jg, DocumentModel doc, ObjectNode denormValues) throws IOException {
172 if (denormValues != null && denormValues.size() > 0) {
173 if (jg.getCodec() == null) {
174 jg.setCodec(objectMapper);
177 Iterator<Map.Entry<String, JsonNode>> entries = denormValues.getFields();
179 while (entries.hasNext()) {
180 Map.Entry<String, JsonNode> entry = entries.next();
182 jg.writeFieldName("collectionspace_denorm:" + entry.getKey());
183 jg.writeTree(entry.getValue());
189 * Compute a title for the public browser. This needs to be indexed in ES so that it can
190 * be used for sorting. (Even if it's just extracting the primary value.)
192 private String computeTitle(DocumentModel doc) {
193 List<Map<String, Object>> termGroups = (List<Map<String, Object>>) doc.getProperty("materials_common", "materialTermGroupList");
194 String primaryDisplayName = null;
196 if (termGroups.size() > 0) {
197 Map<String, Object> primaryTermGroup = termGroups.get(0);
198 primaryDisplayName = (String) primaryTermGroup.get("termDisplayName");
201 return primaryDisplayName;
204 private String findFirstTermDisplayNameWithFlag(List<Map<String, Object>> termGroups, String flagShortId) {
205 String termDisplayName = null;
207 for (Map<String, Object> termGroup : termGroups) {
208 String termFlag = (String) termGroup.get("termFlag");
210 if (termFlag != null && termFlag.contains("(" + flagShortId + ")")) {
211 String candidateTermDisplayName = (String) termGroup.get("termDisplayName");
213 if (StringUtils.isNotEmpty(candidateTermDisplayName)) {
214 termDisplayName = candidateTermDisplayName;
220 return termDisplayName;
223 private List<String> findTermDisplayNamesWithFlag(List<Map<String, Object>> termGroups, String flagShortId) {
224 List<String> termDisplayNames = new ArrayList<String>();
226 for (Map<String, Object> termGroup : termGroups) {
227 String termFlag = (String) termGroup.get("termFlag");
229 if (termFlag != null && termFlag.contains("(" + flagShortId + ")")) {
230 String candidateTermDisplayName = (String) termGroup.get("termDisplayName");
232 if (StringUtils.isNotEmpty(candidateTermDisplayName)) {
233 termDisplayNames.add(candidateTermDisplayName);
238 return termDisplayNames;
241 private boolean isMediaPublished(DocumentModel mediaDoc) {
242 List<String> publishToValues = (List<String>) mediaDoc.getProperty("media_materials", "publishToList");
243 boolean isPublished = false;
245 for (int i=0; i<publishToValues.size(); i++) {
246 String value = publishToValues.get(i);
247 String shortId = RefNameUtils.getItemShortId(value);
249 if (shortId.equals("all") || shortId.equals("materialorder")) {
258 private List<JsonNode> jsonNodes(List<String> values) {
259 List<JsonNode> nodes = new ArrayList<JsonNode>();
260 Iterator<String> iterator = values.iterator();
262 while (iterator.hasNext()) {
263 String value = iterator.next();
265 nodes.add(new TextNode(value));