]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
08c0311f9aaa74180d4a8d7bb42fe6adeb129849
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.advancedsearch.mapper;
2
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;
8
9 import java.util.Map;
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;
37
38 /**
39  * A class for mapping {@link CSDocumentModelResponse} to {@link AdvancedsearchListItem}.
40  *
41  * @since 8.3.0
42  */
43 public class CollectionObjectMapper {
44
45     private static final Logger logger = LoggerFactory.getLogger(CollectionObjectMapper.class);
46
47     private static final String COMMON_PART_NAME =
48             CollectionObjectClient.SERVICE_NAME + PART_LABEL_SEPARATOR + PART_COMMON_LABEL;
49
50     private static final String NATHIST_PART_NAME =
51             CollectionObjectClient.SERVICE_NAME + PART_LABEL_SEPARATOR + NATURALHISTORY_EXT_EXTENSION_NAME;
52
53     private static final String NAGPRA_PART_NAME =
54             CollectionObjectClient.SERVICE_NAME + PART_LABEL_SEPARATOR + NAGPRA_EXTENSION_NAME;
55
56     private final ObjectFactory objectFactory;
57     private final Unmarshaller unmarshaller;
58
59     public CollectionObjectMapper(Unmarshaller unmarshaller) {
60         this.unmarshaller = unmarshaller;
61         this.objectFactory = new ObjectFactory();
62     }
63
64     /**
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.
69      * <p>
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
75      */
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();
81         }
82
83         final AdvancedsearchListItem item = objectFactory.createAdvancedsearchCommonListAdvancedsearchListItem();
84
85         final PoxPayloadOut outputPayload = response.getPayload();
86
87         final CollectionSpaceCore core =
88                 unmarshall(CollectionSpaceCore.class, COLLECTIONSPACE_CORE_SCHEMA, outputPayload, unmarshaller);
89
90         final CollectionobjectsCommon collectionObject =
91                 unmarshall(CollectionobjectsCommon.class, COMMON_PART_NAME, outputPayload, unmarshaller);
92
93         final CollectionObjectsNAGPRA objectsNAGPRA =
94                 unmarshall(CollectionObjectsNAGPRA.class, NAGPRA_PART_NAME, outputPayload, unmarshaller);
95
96         final CollectionobjectsNaturalhistory naturalHistory =
97                 unmarshall(CollectionobjectsNaturalhistory.class, NATHIST_PART_NAME, outputPayload, unmarshaller);
98
99         final String csid = response.getCsid();
100         item.setCsid(csid);
101         if (core != null) {
102             item.setRefName(core.getRefName());
103             item.setUri(core.getUri());
104             item.setUpdatedAt(core.getUpdatedAt());
105         } else {
106             logger.warn("advancedsearch: could not find collectionspace_core associated with csid {}", csid);
107         }
108
109         if (collectionObject != null) {
110             item.setObjectNumber(collectionObject.getObjectNumber());
111             item.setBriefDescription(BriefDescriptionListModel.briefDescriptionListToDisplayString(
112                     collectionObject.getBriefDescriptions()));
113             item.setComputedCurrentLocation(collectionObject.getComputedCurrentLocation());
114
115             item.setTitle(TitleGroupListModel.titleGroupListToDisplayString(collectionObject.getTitleGroupList()));
116             item.setResponsibleDepartment(
117                     ResponsibleDepartmentsListModel.responsibleDepartmentString(collectionObject));
118
119             item.setObjectName(ObjectNameListModel.objectName(collectionObject));
120             item.setObjectNameControlled(ObjectNameListModel.objectNameControlled(collectionObject));
121
122             item.setContentConcepts(ContentConceptListModel.contentConceptList(collectionObject));
123
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?
131             });
132
133             // Object Production Information (place, date, agent, agent role)
134             item.setObjectProductionDate(ObjectProductionModel.objectProductionDate(collectionObject));
135             item.setObjectProductionPlace(ObjectProductionModel.objectProductionPlace(collectionObject));
136
137             AgentModel.agent(collectionObject).ifPresent(agent -> {
138                 item.setAgent(agent.getAgent());
139                 item.setAgentRole(agent.getRole());
140             });
141
142             item.setForm(TaxonModel.preservationForm(collectionObject));
143             item.setMaterial(MaterialModel.material(collectionObject));
144
145             // from media resource
146             if (!blobInfo.isEmpty()) {
147                 item.setBlobCsid(blobInfo.get(MediaJAXBSchema.blobCsid));
148                 item.setBlobAltText(blobInfo.get(MediaJAXBSchema.altText));
149             }
150         } else {
151             logger.warn("advancedsearch: could not find CollectionobjectsCommon associated with csid {}", csid);
152         }
153
154         if (naturalHistory != null) {
155             item.setTaxon(TaxonModel.taxon(naturalHistory));
156             item.setDeterminationTaxon(TaxonModel.determinationTaxon(naturalHistory));
157         }
158
159         if (objectsNAGPRA != null) {
160             item.setNagpraCategories(NAGPRACategoryModel.napgraCategories(objectsNAGPRA));
161         }
162
163         return item;
164     }
165
166     public <T> T unmarshall(Class<T> clazz, String namespace, PoxPayloadOut out, Unmarshaller unmarshaller) {
167         PayloadOutputPart part = out.getPart(namespace);
168         if (part == null) {
169             return null;
170         }
171
172         try {
173             return clazz.cast(unmarshaller.unmarshal((Document) part.getBody()));
174         } catch (JAXBException e) {
175             throw new RuntimeException(e);
176         }
177     }
178 }