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