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.MediaJAXBSchema;
14 import org.collectionspace.services.advancedsearch.AdvancedsearchCommonList.AdvancedsearchListItem;
15 import org.collectionspace.services.advancedsearch.ObjectFactory;
16 import org.collectionspace.services.advancedsearch.model.AgentModel;
17 import org.collectionspace.services.advancedsearch.model.BriefDescriptionListModel;
18 import org.collectionspace.services.advancedsearch.model.ContentConceptListModel;
19 import org.collectionspace.services.advancedsearch.model.FieldCollectionModel;
20 import org.collectionspace.services.advancedsearch.model.MaterialModel;
21 import org.collectionspace.services.advancedsearch.model.NAGPRACategoryModel;
22 import org.collectionspace.services.advancedsearch.model.ObjectNameListModel;
23 import org.collectionspace.services.advancedsearch.model.ObjectProductionModel;
24 import org.collectionspace.services.advancedsearch.model.ResponsibleDepartmentsListModel;
25 import org.collectionspace.services.advancedsearch.model.TaxonModel;
26 import org.collectionspace.services.advancedsearch.model.TitleGroupListModel;
27 import org.collectionspace.services.client.CollectionObjectClient;
28 import org.collectionspace.services.client.PayloadOutputPart;
29 import org.collectionspace.services.client.PoxPayloadOut;
30 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
31 import org.collectionspace.services.collectionobject.domain.nagpra.CollectionObjectsNAGPRA;
32 import org.collectionspace.services.collectionobject.domain.naturalhistory_extension.CollectionobjectsNaturalhistory;
33 import org.collectionspace.services.nuxeo.client.handler.CSDocumentModelList.CSDocumentModelResponse;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.w3c.dom.Document;
39 * A class for mapping {@link CSDocumentModelResponse} to {@link AdvancedsearchListItem}.
43 public class CollectionObjectMapper {
45 private static final Logger logger = LoggerFactory.getLogger(CollectionObjectMapper.class);
47 private static final String COMMON_PART_NAME =
48 CollectionObjectClient.SERVICE_NAME + PART_LABEL_SEPARATOR + PART_COMMON_LABEL;
50 private static final String NATHIST_PART_NAME =
51 CollectionObjectClient.SERVICE_NAME + PART_LABEL_SEPARATOR + NATURALHISTORY_EXT_EXTENSION_NAME;
53 private static final String NAGPRA_PART_NAME =
54 CollectionObjectClient.SERVICE_NAME + PART_LABEL_SEPARATOR + NAGPRA_EXTENSION_NAME;
56 private final ObjectFactory objectFactory;
57 private final Unmarshaller unmarshaller;
59 public CollectionObjectMapper(Unmarshaller unmarshaller) {
60 this.unmarshaller = unmarshaller;
61 this.objectFactory = new ObjectFactory();
65 * Map a {@link CSDocumentModelResponse} to a {@link AdvancedsearchListItem}. This looks at the response for each
66 * of the collectionspace_core, collectionobjects_common, collectionobjects_nagpra, and
67 * collectionobjects_naturalhistory_extension parts and pulls fields out of each based on the search specification.
68 * We don't differentiate between profiles here and instead return everything available for the ui.
70 * Note that this doesn't handle the {@link AdvancedsearchListItem#setRelated(Boolean)} as that requires access to
71 * the RelationResource. Maybe worth doing through an additional parameter.
72 * @param response The response from the CollectionObjectResource for a single object
73 * @param blobCsids The blobs associated with the object
74 * @return the advanced search list item
76 public AdvancedsearchListItem asListItem(final CSDocumentModelResponse response,
77 final Map<String, String> blobInfo) {
78 // todo: what makes sense here?
79 if (response == null || response.getPayload() == null) {
80 return objectFactory.createAdvancedsearchCommonListAdvancedsearchListItem();
83 final AdvancedsearchListItem item = objectFactory.createAdvancedsearchCommonListAdvancedsearchListItem();
85 final PoxPayloadOut outputPayload = response.getPayload();
87 final CollectionSpaceCore core =
88 unmarshall(CollectionSpaceCore.class, COLLECTIONSPACE_CORE_SCHEMA, outputPayload, unmarshaller);
90 final CollectionobjectsCommon collectionObject =
91 unmarshall(CollectionobjectsCommon.class, COMMON_PART_NAME, outputPayload, unmarshaller);
93 final CollectionObjectsNAGPRA objectsNAGPRA =
94 unmarshall(CollectionObjectsNAGPRA.class, NAGPRA_PART_NAME, outputPayload, unmarshaller);
96 final CollectionobjectsNaturalhistory naturalHistory =
97 unmarshall(CollectionobjectsNaturalhistory.class, NATHIST_PART_NAME, outputPayload, unmarshaller);
99 final String csid = response.getCsid();
102 item.setRefName(core.getRefName());
103 item.setUri(core.getUri());
104 item.setUpdatedAt(core.getUpdatedAt());
106 logger.warn("advancedsearch: could not find collectionspace_core associated with csid {}", csid);
109 if (collectionObject != null) {
110 item.setObjectNumber(collectionObject.getObjectNumber());
111 item.setBriefDescription(BriefDescriptionListModel.briefDescriptionListToDisplayString(
112 collectionObject.getBriefDescriptions()));
113 item.setComputedCurrentLocation(collectionObject.getComputedCurrentLocation());
115 item.setTitle(TitleGroupListModel.titleGroupListToDisplayString(collectionObject.getTitleGroupList()));
116 item.setResponsibleDepartment(
117 ResponsibleDepartmentsListModel.responsibleDepartmentString(collectionObject));
119 item.setObjectName(ObjectNameListModel.objectName(collectionObject));
120 item.setObjectNameControlled(ObjectNameListModel.objectNameControlled(collectionObject));
122 item.setContentConcepts(ContentConceptListModel.contentConceptList(collectionObject));
124 // Field collection items (place, site, date, collector, role)
125 item.setFieldCollectionPlace(FieldCollectionModel.fieldCollectionPlace(collectionObject));
126 item.setFieldCollectionSite(FieldCollectionModel.fieldCollectionSite(collectionObject));
127 item.setFieldCollectionDate(FieldCollectionModel.fieldCollectionDate(collectionObject));
128 FieldCollectionModel.fieldCollector(collectionObject).ifPresent(collector -> {
129 item.setFieldCollector(collector);
130 item.setFieldCollectorRole("field collector"); // todo: how would we i18n this?
133 // Object Production Information (place, date, agent, agent role)
134 item.setObjectProductionDate(ObjectProductionModel.objectProductionDate(collectionObject));
135 item.setObjectProductionPlace(ObjectProductionModel.objectProductionPlace(collectionObject));
137 AgentModel.agent(collectionObject).ifPresent(agent -> {
138 item.setAgent(agent.getAgent());
139 item.setAgentRole(agent.getRole());
142 item.setForm(TaxonModel.preservationForm(collectionObject));
143 item.setMaterial(MaterialModel.material(collectionObject));
145 // from media resource
146 if (!blobInfo.isEmpty()) {
147 item.setBlobCsid(blobInfo.get(MediaJAXBSchema.blobCsid));
148 item.setBlobAltText(blobInfo.get(MediaJAXBSchema.altText));
151 logger.warn("advancedsearch: could not find CollectionobjectsCommon associated with csid {}", csid);
154 if (naturalHistory != null) {
155 item.setTaxon(TaxonModel.taxon(naturalHistory));
156 item.setDeterminationTaxon(TaxonModel.determinationTaxon(naturalHistory));
159 if (objectsNAGPRA != null) {
160 item.setNagpraCategories(NAGPRACategoryModel.napgraCategories(objectsNAGPRA));
166 public <T> T unmarshall(Class<T> clazz, String namespace, PoxPayloadOut out, Unmarshaller unmarshaller) {
167 PayloadOutputPart part = out.getPart(namespace);
173 return clazz.cast(unmarshaller.unmarshal((Document) part.getBody()));
174 } catch (JAXBException e) {
175 throw new RuntimeException(e);