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