1 package org.collectionspace.services.nuxeo.elasticsearch;
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.Collection;
6 import java.util.HashSet;
7 import java.util.Iterator;
12 import javax.ws.rs.core.HttpHeaders;
14 import org.apache.commons.lang3.StringUtils;
15 import org.codehaus.jackson.JsonGenerator;
16 import org.codehaus.jackson.JsonNode;
17 import org.codehaus.jackson.map.ObjectMapper;
18 import org.codehaus.jackson.node.ObjectNode;
19 import org.codehaus.jackson.node.TextNode;
21 import org.collectionspace.services.common.api.RefNameUtils;
23 import org.nuxeo.ecm.automation.jaxrs.io.documents.JsonESDocumentWriter;
24 import org.nuxeo.ecm.core.api.CoreSession;
25 import org.nuxeo.ecm.core.api.DocumentModel;
26 import org.nuxeo.ecm.core.api.DocumentModelList;
28 public class CSJsonESDocumentWriter extends JsonESDocumentWriter {
29 private static ObjectMapper objectMapper = new ObjectMapper();
32 public void writeDoc(JsonGenerator jg, DocumentModel doc, String[] schemas,
33 Map<String, String> contextParameters, HttpHeaders headers)
36 // Compute and store fields that should be indexed with this document in ElasticSearch.
37 // TODO: Make this configurable. This is currently hardcoded for the materials profile and
38 // the Material Order application.
40 ObjectNode denormValues = objectMapper.createObjectNode();
42 String docType = doc.getType();
44 if (docType.startsWith("Materialitem")) {
45 CoreSession session = doc.getCoreSession();
47 // Store the csids of media records that reference this material authority item via the
50 String refName = (String) doc.getProperty("collectionspace_core", "refName");
52 if (StringUtils.isNotEmpty(refName)) {
53 String escapedRefName = refName.replace("'", "\\'");
54 String tenantId = (String) doc.getProperty("collectionspace_core", "tenantId");
55 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);
57 DocumentModelList mediaDocs = session.query(mediaQuery);
58 List<JsonNode> mediaCsids = new ArrayList<JsonNode>();
60 if (mediaDocs.size() > 0) {
61 Iterator<DocumentModel> iterator = mediaDocs.iterator();
63 while (iterator.hasNext()) {
64 DocumentModel mediaDoc = iterator.next();
66 if (isMediaPublished(mediaDoc)) {
67 String mediaCsid = (String) mediaDoc.getName();
69 mediaCsids.add(new TextNode(mediaCsid));
74 denormValues.putArray("mediaCsid").addAll(mediaCsids);
77 // Compute the title of the record for the public browser, and store it so that it can
78 // be used for sorting ES query results.
80 String title = computeTitle(doc);
83 denormValues.put("title", title);
86 List<Map<String, Object>> termGroups = (List<Map<String, Object>>) doc.getProperty("materials_common", "materialTermGroupList");
87 List<String> commercialNames = findTermDisplayNamesWithFlag(termGroups, "commercial");
88 List<String> commonNames = findTermDisplayNamesWithFlag(termGroups, "common");
90 // Find and store the commercial names and common names for this item. This simplifies
91 // search and display in the Material Order application.
93 if (commercialNames.size() > 0) {
94 denormValues.putArray("commercialNames").addAll(jsonNodes(commercialNames));
97 if (commonNames.size() > 0) {
98 denormValues.putArray("commonNames").addAll(jsonNodes(commonNames));
101 // Combine term creator organizations and term editor organizations into a holding
102 // institutions field.
104 Set<String> holdingInstitutions = new HashSet<String>();
106 holdingInstitutions.addAll(getTermAttributionContributors(doc));
107 holdingInstitutions.addAll(getTermAttributionEditors(doc));
109 if (holdingInstitutions.size() > 0) {
110 denormValues.putArray("holdingInstitutions").addAll(jsonNodes(holdingInstitutions));
114 // Below is sample code for denormalizing fields from the computed current location (place
115 // item) into collection object documents. This was written for the public browser
116 // prototype for public art.
119 if (docType.startsWith("CollectionObject")) {
120 CoreSession session = doc.getCoreSession();
122 String refName = (String) doc.getProperty("collectionobjects_common", "computedCurrentLocation");
124 if (StringUtils.isNotEmpty(refName)) {
125 String escapedRefName = refName.replace("'", "\\'");
126 String placeQuery = String.format("SELECT * FROM PlaceitemTenant5000 WHERE places_common:refName = '%s'", escapedRefName);
128 DocumentModelList placeDocs = session.query(placeQuery, 1);
130 if (placeDocs.size() > 0) {
131 DocumentModel placeDoc = placeDocs.get(0);
133 String placementType = (String) placeDoc.getProperty("places_publicart:placementType").getValue();
135 if (placementType != null) {
136 denormValues.put("placementType", placementType);
139 Property geoRefGroup;
142 geoRefGroup = placeDoc.getProperty("places_common:placeGeoRefGroupList/0");
143 } catch (PropertyNotFoundException e) {
147 if (geoRefGroup != null) {
148 Double decimalLatitude = (Double) geoRefGroup.getValue("decimalLatitude");
149 Double decimalLongitude = (Double) geoRefGroup.getValue("decimalLongitude");
151 if (decimalLatitude != null && decimalLongitude != null) {
152 ObjectNode geoPointNode = objectMapper.createObjectNode();
154 geoPointNode.put("lat", decimalLatitude);
155 geoPointNode.put("lon", decimalLongitude);
157 denormValues.put("geoPoint", geoPointNode);
163 String uri = (String) doc.getProperty("collectionobjects_core", "uri");
164 String csid = uri.substring(uri.lastIndexOf('/') + 1);
165 String mediaQuery = String.format("SELECT media_common:blobCsid, media_common:title FROM Relation WHERE relations_common:subjectCsid = '%s' AND relations_common:objectDocumentType = 'Media'", csid);
167 DocumentModelList mediaDocs = session.query(mediaQuery, 1);
169 if (mediaDocs.size() > 0) {
175 jg.writeStartObject();
177 writeSystemProperties(jg, doc);
178 writeSchemas(jg, doc, schemas);
179 writeContextParameters(jg, doc, contextParameters);
180 writeDenormValues(jg, doc, denormValues);
186 public void writeDenormValues(JsonGenerator jg, DocumentModel doc, ObjectNode denormValues) throws IOException {
187 if (denormValues != null && denormValues.size() > 0) {
188 if (jg.getCodec() == null) {
189 jg.setCodec(objectMapper);
192 Iterator<Map.Entry<String, JsonNode>> entries = denormValues.getFields();
194 while (entries.hasNext()) {
195 Map.Entry<String, JsonNode> entry = entries.next();
197 jg.writeFieldName("collectionspace_denorm:" + entry.getKey());
198 jg.writeTree(entry.getValue());
204 * Compute a title for the public browser. This needs to be indexed in ES so that it can
205 * be used for sorting. (Even if it's just extracting the primary value.)
207 private String computeTitle(DocumentModel doc) {
208 List<Map<String, Object>> termGroups = (List<Map<String, Object>>) doc.getProperty("materials_common", "materialTermGroupList");
209 String primaryDisplayName = null;
211 if (termGroups.size() > 0) {
212 Map<String, Object> primaryTermGroup = termGroups.get(0);
213 primaryDisplayName = (String) primaryTermGroup.get("termDisplayName");
216 return primaryDisplayName;
219 private String findFirstTermDisplayNameWithFlag(List<Map<String, Object>> termGroups, String flagShortId) {
220 String termDisplayName = null;
222 for (Map<String, Object> termGroup : termGroups) {
223 String termFlag = (String) termGroup.get("termFlag");
225 if (termFlag != null && termFlag.contains("(" + flagShortId + ")")) {
226 String candidateTermDisplayName = (String) termGroup.get("termDisplayName");
228 if (StringUtils.isNotEmpty(candidateTermDisplayName)) {
229 termDisplayName = candidateTermDisplayName;
235 return termDisplayName;
238 private List<String> findTermDisplayNamesWithFlag(List<Map<String, Object>> termGroups, String flagShortId) {
239 List<String> termDisplayNames = new ArrayList<String>();
241 for (Map<String, Object> termGroup : termGroups) {
242 String termFlag = (String) termGroup.get("termFlag");
244 if (termFlag != null && termFlag.contains("(" + flagShortId + ")")) {
245 String candidateTermDisplayName = (String) termGroup.get("termDisplayName");
247 if (StringUtils.isNotEmpty(candidateTermDisplayName)) {
248 termDisplayNames.add(candidateTermDisplayName);
253 return termDisplayNames;
256 private Set<String> getTermAttributionContributors(DocumentModel doc) {
257 Set orgs = new HashSet<String>();
259 List<Map<String, Object>> groups = (List<Map<String, Object>>) doc.getProperty("materials_common", "materialTermAttributionContributingGroupList");
261 for (Map<String, Object> group : groups) {
262 String org = (String) group.get("materialTermAttributionContributingOrganization");
270 private Set<String> getTermAttributionEditors(DocumentModel doc) {
271 Set orgs = new HashSet<String>();
273 List<Map<String, Object>> groups = (List<Map<String, Object>>) doc.getProperty("materials_common", "materialTermAttributionEditingGroupList");
275 for (Map<String, Object> group : groups) {
276 String org = (String) group.get("materialTermAttributionEditingOrganization");
284 private boolean isMediaPublished(DocumentModel mediaDoc) {
285 List<String> publishToValues = (List<String>) mediaDoc.getProperty("media_materials", "publishToList");
286 boolean isPublished = false;
288 for (int i=0; i<publishToValues.size(); i++) {
289 String value = publishToValues.get(i);
290 String shortId = RefNameUtils.getItemShortId(value);
292 if (shortId.equals("all") || shortId.equals("materialorder")) {
301 private List<JsonNode> jsonNodes(Collection<String> values) {
302 List<JsonNode> nodes = new ArrayList<JsonNode>();
303 Iterator<String> iterator = values.iterator();
305 while (iterator.hasNext()) {
306 String value = iterator.next();
308 nodes.add(new TextNode(value));