1 package org.collectionspace.services.nuxeo.elasticsearch.materials;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Iterator;
6 import java.util.LinkedHashSet;
11 import org.apache.commons.lang3.StringUtils;
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;
19 import org.collectionspace.services.nuxeo.elasticsearch.DefaultESDocumentWriter;
20 import org.nuxeo.ecm.core.api.CoreSession;
21 import org.nuxeo.ecm.core.api.DocumentModel;
22 import org.nuxeo.ecm.core.api.DocumentModelList;
24 public class MaterialsESDocumentWriter extends DefaultESDocumentWriter {
27 public ObjectNode getDenormValues(DocumentModel doc) {
28 ObjectMapper objectMapper = new ObjectMapper();
29 ObjectNode denormValues = objectMapper.createObjectNode();
31 String docType = doc.getType();
33 if (docType.startsWith("Materialitem")) {
34 CoreSession session = doc.getCoreSession();
36 // Store the csids of media records that reference this material authority item via the
39 String refName = (String) doc.getProperty("collectionspace_core", "refName");
41 if (StringUtils.isNotEmpty(refName)) {
42 String escapedRefName = refName.replace("'", "\\'");
43 String tenantId = (String) doc.getProperty("collectionspace_core", "tenantId");
44 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);
46 DocumentModelList mediaDocs = session.query(mediaQuery);
47 List<JsonNode> mediaCsids = new ArrayList<JsonNode>();
49 if (mediaDocs.size() > 0) {
50 Iterator<DocumentModel> iterator = mediaDocs.iterator();
52 while (iterator.hasNext()) {
53 DocumentModel mediaDoc = iterator.next();
55 if (isMediaPublished(mediaDoc)) {
56 String mediaCsid = (String) mediaDoc.getName();
58 mediaCsids.add(new TextNode(mediaCsid));
63 denormValues.putArray("mediaCsid").addAll(mediaCsids);
66 // Compute the title of the record for the public browser, and store it so that it can
67 // be used for sorting ES query results.
69 String title = computeTitle(doc);
72 denormValues.put("title", title);
75 List<Map<String, Object>> termGroups = (List<Map<String, Object>>) doc.getProperty("materials_common", "materialTermGroupList");
76 List<String> commercialNames = findTermDisplayNamesWithFlag(termGroups, "commercial");
77 List<String> commonNames = findTermDisplayNamesWithFlag(termGroups, "common");
79 // Find and store the commercial names and common names for this item. This simplifies
80 // search and display in the Material Order application.
82 if (commercialNames.size() > 0) {
83 denormValues.putArray("commercialNames").addAll(jsonNodes(commercialNames));
86 if (commonNames.size() > 0) {
87 denormValues.putArray("commonNames").addAll(jsonNodes(commonNames));
90 // Combine term creator organizations and term editor organizations into a holding
91 // institutions field.
93 Set<String> holdingInstitutions = new LinkedHashSet<String>();
95 holdingInstitutions.addAll(getTermAttributionContributors(doc));
96 holdingInstitutions.addAll(getTermAttributionEditors(doc));
98 if (holdingInstitutions.size() > 0) {
99 denormValues.putArray("holdingInstitutions").addAll(jsonNodes(holdingInstitutions));
103 // Below is sample code for denormalizing fields from the computed current location (place
104 // item) into collection object documents. This was written for the public browser
105 // prototype for public art.
108 if (docType.startsWith("CollectionObject")) {
109 CoreSession session = doc.getCoreSession();
111 String refName = (String) doc.getProperty("collectionobjects_common", "computedCurrentLocation");
113 if (StringUtils.isNotEmpty(refName)) {
114 String escapedRefName = refName.replace("'", "\\'");
115 String placeQuery = String.format("SELECT * FROM PlaceitemTenant5000 WHERE places_common:refName = '%s'", escapedRefName);
117 DocumentModelList placeDocs = session.query(placeQuery, 1);
119 if (placeDocs.size() > 0) {
120 DocumentModel placeDoc = placeDocs.get(0);
122 String placementType = (String) placeDoc.getProperty("places_publicart:placementType").getValue();
124 if (placementType != null) {
125 denormValues.put("placementType", placementType);
128 Property geoRefGroup;
131 geoRefGroup = placeDoc.getProperty("places_common:placeGeoRefGroupList/0");
132 } catch (PropertyNotFoundException e) {
136 if (geoRefGroup != null) {
137 Double decimalLatitude = (Double) geoRefGroup.getValue("decimalLatitude");
138 Double decimalLongitude = (Double) geoRefGroup.getValue("decimalLongitude");
140 if (decimalLatitude != null && decimalLongitude != null) {
141 ObjectNode geoPointNode = objectMapper.createObjectNode();
143 geoPointNode.put("lat", decimalLatitude);
144 geoPointNode.put("lon", decimalLongitude);
146 denormValues.put("geoPoint", geoPointNode);
152 String uri = (String) doc.getProperty("collectionobjects_core", "uri");
153 String csid = uri.substring(uri.lastIndexOf('/') + 1);
154 String mediaQuery = String.format("SELECT media_common:blobCsid, media_common:title FROM Relation WHERE relations_common:subjectCsid = '%s' AND relations_common:objectDocumentType = 'Media'", csid);
156 DocumentModelList mediaDocs = session.query(mediaQuery, 1);
158 if (mediaDocs.size() > 0) {
168 * Compute a title for the public browser. This needs to be indexed in ES so that it can
169 * be used for sorting. (Even if it's just extracting the primary value.)
171 private String computeTitle(DocumentModel doc) {
172 List<Map<String, Object>> termGroups = (List<Map<String, Object>>) doc.getProperty("materials_common", "materialTermGroupList");
173 String primaryDisplayName = null;
175 if (termGroups.size() > 0) {
176 Map<String, Object> primaryTermGroup = termGroups.get(0);
177 primaryDisplayName = (String) primaryTermGroup.get("termDisplayName");
180 return primaryDisplayName;
183 private List<String> findTermDisplayNamesWithFlag(List<Map<String, Object>> termGroups, String flagShortId) {
184 List<String> termDisplayNames = new ArrayList<String>();
186 for (Map<String, Object> termGroup : termGroups) {
187 String termFlag = (String) termGroup.get("termFlag");
189 if (termFlag != null && termFlag.contains("(" + flagShortId + ")")) {
190 String candidateTermDisplayName = (String) termGroup.get("termDisplayName");
192 if (StringUtils.isNotEmpty(candidateTermDisplayName)) {
193 termDisplayNames.add(candidateTermDisplayName);
198 return termDisplayNames;
201 private Set<String> getTermAttributionContributors(DocumentModel doc) {
202 Set orgs = new LinkedHashSet<String>();
204 List<Map<String, Object>> groups = (List<Map<String, Object>>) doc.getProperty("materials_common", "materialTermAttributionContributingGroupList");
206 for (Map<String, Object> group : groups) {
207 String org = (String) group.get("materialTermAttributionContributingOrganization");
209 if (StringUtils.isNotEmpty(org)) {
217 private Set<String> getTermAttributionEditors(DocumentModel doc) {
218 Set orgs = new LinkedHashSet<String>();
220 List<Map<String, Object>> groups = (List<Map<String, Object>>) doc.getProperty("materials_common", "materialTermAttributionEditingGroupList");
222 for (Map<String, Object> group : groups) {
223 String org = (String) group.get("materialTermAttributionEditingOrganization");
225 if (StringUtils.isNotEmpty(org)) {
233 private boolean isMediaPublished(DocumentModel mediaDoc) {
234 List<String> publishToValues = (List<String>) mediaDoc.getProperty("media_materials", "publishToList");
235 boolean isPublished = false;
237 if (publishToValues != null) {
238 for (int i=0; i<publishToValues.size(); i++) {
239 String value = publishToValues.get(i);
240 String shortId = RefNameUtils.getItemShortId(value);
242 if (shortId.equals("all") || shortId.equals("materialorder")) {
252 private List<JsonNode> jsonNodes(Collection<String> values) {
253 List<JsonNode> nodes = new ArrayList<JsonNode>();
254 Iterator<String> iterator = values.iterator();
256 while (iterator.hasNext()) {
257 String value = iterator.next();
259 nodes.add(new TextNode(value));