1 package org.collectionspace.services.advancedsearch.mapper;
3 import static org.collectionspace.services.client.CollectionSpaceClient.COLLECTIONSPACE_CORE_SCHEMA;
4 import static org.collectionspace.services.client.CollectionSpaceClient.NAGPRA_EXTENSION_NAME;
5 import static org.collectionspace.services.client.CollectionSpaceClient.NATURALHISTORY_EXT_EXTENSION_NAME;
6 import static org.collectionspace.services.client.CollectionSpaceClient.PART_COMMON_LABEL;
7 import static org.collectionspace.services.client.CollectionSpaceClient.PART_LABEL_SEPARATOR;
10 import javax.xml.bind.JAXBException;
11 import javax.xml.bind.Unmarshaller;
12 import org.collectionspace.collectionspace_core.CollectionSpaceCore;
13 import org.collectionspace.services.advancedsearch.AdvancedsearchCommonList.AdvancedsearchListItem;
14 import org.collectionspace.services.advancedsearch.ObjectFactory;
15 import org.collectionspace.services.advancedsearch.model.AgentModel;
16 import org.collectionspace.services.advancedsearch.model.BriefDescriptionListModel;
17 import org.collectionspace.services.advancedsearch.model.ContentConceptListModel;
18 import org.collectionspace.services.advancedsearch.model.FieldCollectionModel;
19 import org.collectionspace.services.advancedsearch.model.NAGPRACategoryModel;
20 import org.collectionspace.services.advancedsearch.model.ObjectNameListModel;
21 import org.collectionspace.services.advancedsearch.model.ObjectProductionModel;
22 import org.collectionspace.services.advancedsearch.model.ResponsibleDepartmentsListModel;
23 import org.collectionspace.services.advancedsearch.model.TaxonModel;
24 import org.collectionspace.services.advancedsearch.model.TitleGroupListModel;
25 import org.collectionspace.services.client.CollectionObjectClient;
26 import org.collectionspace.services.client.PayloadOutputPart;
27 import org.collectionspace.services.client.PoxPayloadOut;
28 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
29 import org.collectionspace.services.collectionobject.domain.nagpra.CollectionObjectsNAGPRA;
30 import org.collectionspace.services.collectionobject.domain.naturalhistory_extension.CollectionobjectsNaturalhistory;
31 import org.collectionspace.services.nuxeo.client.handler.CSDocumentModelList.CSDocumentModelResponse;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.w3c.dom.Document;
37 * A class for mapping {@link CSDocumentModelResponse} to {@link AdvancedsearchListItem}.
41 public class CollectionObjectMapper {
43 private static final Logger logger = LoggerFactory.getLogger(CollectionObjectMapper.class);
45 private static final String COMMON_PART_NAME =
46 CollectionObjectClient.SERVICE_NAME + PART_LABEL_SEPARATOR + PART_COMMON_LABEL;
48 private static final String NATHIST_PART_NAME =
49 CollectionObjectClient.SERVICE_NAME + PART_LABEL_SEPARATOR + NATURALHISTORY_EXT_EXTENSION_NAME;
51 private static final String NAGPRA_PART_NAME =
52 CollectionObjectClient.SERVICE_NAME + PART_LABEL_SEPARATOR + NAGPRA_EXTENSION_NAME;
54 private final ObjectFactory objectFactory;
55 private final Unmarshaller unmarshaller;
57 public CollectionObjectMapper(Unmarshaller unmarshaller) {
58 this.unmarshaller = unmarshaller;
59 this.objectFactory = new ObjectFactory();
63 * Map a {@link CSDocumentModelResponse} to a {@link AdvancedsearchListItem}. This looks at the response for each
64 * of the collectionspace_core, collectionobjects_common, collectionobjects_nagpra, and
65 * collectionobjects_naturalhistory_extension parts and pulls fields out of each based on the search specification.
66 * We don't differentiate between profiles here and instead return everything available for the ui.
68 * Note that this doesn't handle the {@link AdvancedsearchListItem#setRelated(Boolean)} as that requires access to
69 * the RelationResource. Maybe worth doing through an additional parameter.
70 * @param response The response from the CollectionObjectResource for a single object
71 * @param blobCsids The blobs associated with the object
72 * @return the advanced search list item
74 public AdvancedsearchListItem asListItem(final CSDocumentModelResponse response, final List<String> blobCsids) {
75 // todo: what makes sense here?
76 if (response == null || response.getPayload() == null) {
77 return objectFactory.createAdvancedsearchCommonListAdvancedsearchListItem();
80 final AdvancedsearchListItem item = objectFactory.createAdvancedsearchCommonListAdvancedsearchListItem();
82 final PoxPayloadOut outputPayload = response.getPayload();
84 final CollectionSpaceCore core =
85 unmarshall(CollectionSpaceCore.class, COLLECTIONSPACE_CORE_SCHEMA, outputPayload, unmarshaller);
87 final CollectionobjectsCommon collectionObject =
88 unmarshall(CollectionobjectsCommon.class, COMMON_PART_NAME, outputPayload, unmarshaller);
90 final CollectionObjectsNAGPRA objectsNAGPRA =
91 unmarshall(CollectionObjectsNAGPRA.class, NAGPRA_PART_NAME, outputPayload, unmarshaller);
93 final CollectionobjectsNaturalhistory naturalHistory =
94 unmarshall(CollectionobjectsNaturalhistory.class, NATHIST_PART_NAME, outputPayload, unmarshaller);
96 final String csid = response.getCsid();
99 item.setRefName(core.getRefName());
100 item.setUri(core.getUri());
101 item.setUpdatedAt(core.getUpdatedAt());
103 logger.warn("advancedsearch: could not find collectionspace_core associated with csid {}", csid);
106 if (collectionObject != null) {
107 item.setObjectNumber(collectionObject.getObjectNumber());
108 item.setBriefDescription(BriefDescriptionListModel.briefDescriptionListToDisplayString(
109 collectionObject.getBriefDescriptions()));
110 item.setComputedCurrentLocation(collectionObject.getComputedCurrentLocation());
112 item.setTitle(TitleGroupListModel.titleGroupListToDisplayString(collectionObject.getTitleGroupList()));
113 item.setResponsibleDepartment(
114 ResponsibleDepartmentsListModel.responsibleDepartmentString(collectionObject));
116 item.setObjectName(ObjectNameListModel.objectName(collectionObject));
117 item.setObjectNameControlled(ObjectNameListModel.objectNameControlled(collectionObject));
119 item.setContentConcepts(ContentConceptListModel.contentConceptList(collectionObject));
121 // Field collection items (place, site, date, collector, role)
122 item.setFieldCollectionPlace(FieldCollectionModel.fieldCollectionPlace(collectionObject));
123 item.setFieldCollectionSite(FieldCollectionModel.fieldCollectionSite(collectionObject));
124 item.setFieldCollectionDate(FieldCollectionModel.fieldCollectionDate(collectionObject));
125 FieldCollectionModel.fieldCollector(collectionObject).ifPresent(collector -> {
126 item.setFieldCollector(collector);
127 item.setFieldCollectorRole("field collector"); // todo: how would we i18n this?
130 // Object Production Information (place, date, agent, agent role)
131 item.setObjectProductionDate(ObjectProductionModel.objectProductionDate(collectionObject));
132 item.setObjectProductionPlace(ObjectProductionModel.objectProductionPlace(collectionObject));
134 AgentModel.agent(collectionObject).ifPresent(agent -> {
135 item.setAgent(agent.getAgent());
136 item.setAgentRole(agent.getRole());
139 item.setForm(TaxonModel.preservationForm(collectionObject));
141 // from media resource
142 if (blobCsids.size() > 0) {
143 item.setBlobCsid(blobCsids.get(0));
146 logger.warn("advancedsearch: could not find CollectionobjectsCommon associated with csid {}", csid);
149 if (naturalHistory != null) {
150 item.setTaxon(TaxonModel.taxon(naturalHistory));
153 if (objectsNAGPRA != null) {
154 item.setNagpraCategories(NAGPRACategoryModel.napgraCategories(objectsNAGPRA));
160 public <T> T unmarshall(Class<T> clazz, String namespace, PoxPayloadOut out, Unmarshaller unmarshaller) {
161 PayloadOutputPart part = out.getPart(namespace);
167 return clazz.cast(unmarshaller.unmarshal((Document) part.getBody()));
168 } catch (JAXBException e) {
169 throw new RuntimeException(e);