]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
f508de6c5e4a4b19829a0221b8d175123b12e240
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.advancedsearch;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.stream.Collectors;
8
9 import javax.ws.rs.Consumes;
10 import javax.ws.rs.GET;
11 import javax.ws.rs.Path;
12 import javax.ws.rs.Produces;
13 import javax.ws.rs.core.Context;
14 import javax.ws.rs.core.MultivaluedMap;
15 import javax.ws.rs.core.Request;
16 import javax.ws.rs.core.UriInfo;
17 import javax.xml.bind.JAXBException;
18 import javax.xml.bind.Unmarshaller;
19
20 import org.collectionspace.collectionspace_core.CollectionSpaceCore;
21 import org.collectionspace.services.MediaJAXBSchema;
22 import org.collectionspace.services.advancedsearch.AdvancedsearchCommonList.AdvancedsearchListItem;
23 import org.collectionspace.services.advancedsearch.model.BriefDescriptionListModel;
24 import org.collectionspace.services.advancedsearch.model.ContentConceptListModel;
25 import org.collectionspace.services.advancedsearch.model.ObjectNameListModel;
26 import org.collectionspace.services.advancedsearch.model.ResponsibleDepartmentsListModel;
27 import org.collectionspace.services.advancedsearch.model.TitleGroupListModel;
28 import org.collectionspace.services.client.CollectionObjectClient;
29 import org.collectionspace.services.client.CollectionSpaceClient;
30 import org.collectionspace.services.client.IQueryManager;
31 import org.collectionspace.services.client.PayloadOutputPart;
32 import org.collectionspace.services.client.PoxPayloadOut;
33 import org.collectionspace.services.collectionobject.CollectionObjectResource;
34 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
35 import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl;
36 import org.collectionspace.services.common.UriInfoWrapper;
37 import org.collectionspace.services.common.context.RemoteServiceContextFactory;
38 import org.collectionspace.services.common.context.ServiceContextFactory;
39 import org.collectionspace.services.jaxb.AbstractCommonList;
40 import org.collectionspace.services.jaxb.AbstractCommonList.ListItem;
41 import org.collectionspace.services.jaxb.BlobJAXBSchema;
42 import org.collectionspace.services.media.MediaResource;
43 import org.collectionspace.services.nuxeo.client.handler.CSDocumentModelList;
44 import org.collectionspace.services.nuxeo.client.handler.CSDocumentModelList.CSDocumentModelResponse;
45 import org.collectionspace.services.nuxeo.client.handler.UnfilteredDocumentModelHandler;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.w3c.dom.Document;
49 import org.w3c.dom.Element;
50 import org.w3c.dom.NodeList;
51
52 /**
53  * This class defines the advanced search endpoints.
54  */
55 @Path(AdvancedSearchConstants.SERVICE_PATH)
56 @Consumes("application/xml")
57 @Produces("application/xml")
58 public class AdvancedSearch
59                 extends AbstractCollectionSpaceResourceImpl<AdvancedsearchListItem, AdvancedsearchListItem> {
60         private static final String FIELDS_RETURNED = "uri|csid|refName|blobCsid|updatedAt|objectId|objectNumber|objectName|title|computedCurrentLocation|responsibleDepartments|responsibleDepartment|contentConcepts|briefDescription";
61         // FIXME: it's not great to hardcode this here
62         private static final String COMMON_PART_NAME = CollectionObjectClient.SERVICE_NAME
63                                                        + CollectionSpaceClient.PART_LABEL_SEPARATOR
64                                                        + CollectionSpaceClient.PART_COMMON_LABEL;
65
66         private final Logger logger = LoggerFactory.getLogger(AdvancedSearch.class);
67         private final CollectionObjectResource cor = new CollectionObjectResource();
68         private final MediaResource mr = new MediaResource();
69
70         public AdvancedSearch() {
71                 super();
72         }
73
74         /**
75          * Primary advanced search API endpoint.
76          * 
77          * @param request The incoming request. Injected. 
78          * @param uriInfo The URI info of the incoming request, including query parameters and other search control parameters. Injected.
79          * @return A possibly-empty AbstractCommonList of the advanced search results corresponding to the query
80          */
81         @GET
82         public AbstractCommonList getList(@Context Request request, @Context UriInfo uriInfo) {
83                 logger.info("advancedsearch called with path: {}", uriInfo.getPath());
84                 MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters(true);
85                 logger.info("advancedsearch called with query params: {}", queryParams);
86
87                 cor.setDocumentHandlerClass(UnfilteredDocumentModelHandler.class);
88                 ObjectFactory objectFactory = new ObjectFactory();
89                 // the list to return
90                 AdvancedsearchCommonList resultsList = objectFactory.createAdvancedsearchCommonList();
91                 // FIXME: this shouldn't be necessary?
92                 resultsList.advancedsearchListItem = new ArrayList<>();
93
94                 AbstractCommonList abstractCommonList = cor.getList(uriInfo);
95                 if (!(abstractCommonList instanceof CSDocumentModelList)) {
96                         return resultsList;
97                 }
98
99                 Unmarshaller unmarshaller;
100                 CSDocumentModelList collectionObjectList = (CSDocumentModelList) abstractCommonList;
101                 try {
102                         unmarshaller = AdvancedSearchJAXBContext.getJaxbContext().createUnmarshaller();
103                 } catch (JAXBException e) {
104                         // this should result in a 500, need to verify from bigReThrow to see what exception it should be
105                         throw new RuntimeException("Unable to create unmarshaller for AdvancedSearch", e);
106                 }
107
108                 for (CSDocumentModelResponse response : collectionObjectList.getResponseList()) {
109                         PoxPayloadOut outputPayload = response.getPayload();
110                         PayloadOutputPart corePart = outputPayload.getPart(CollectionSpaceClient.COLLECTIONSPACE_CORE_SCHEMA);
111                         PayloadOutputPart commonPart = outputPayload.getPart(COMMON_PART_NAME);
112                         CollectionSpaceCore core;
113                         CollectionobjectsCommon collectionObject;
114
115                         try {
116                                 core = (CollectionSpaceCore) unmarshaller.unmarshal((Document) corePart.getBody());
117                                 collectionObject = (CollectionobjectsCommon) unmarshaller.unmarshal((Document) commonPart.getBody());
118                         } catch (JAXBException e) {
119                                 throw new RuntimeException(e);
120                         }
121
122                         String csid = response.getCsid();
123                         UriInfoWrapper wrappedUriInfo = new UriInfoWrapper(uriInfo);
124                         List<String> blobCsids = findBlobCsids(csid, wrappedUriInfo);
125
126                         AdvancedsearchListItem listItem = objectFactory.createAdvancedsearchCommonListAdvancedsearchListItem();
127                         if (core != null) {
128                                 listItem.setCsid(csid);
129                                 listItem.setRefName(core.getRefName());
130                                 listItem.setUri(core.getUri());
131                                 listItem.setUpdatedAt(core.getUpdatedAt());
132                         } else {
133                                 logger.warn("advancedsearch: could not find collectionspace_core associated with csid {}", csid);
134                         }
135
136                         if (collectionObject != null) {
137                                 listItem.setObjectNumber(collectionObject.getObjectNumber());
138                                 listItem.setBriefDescription(BriefDescriptionListModel
139                                         .briefDescriptionListToDisplayString(collectionObject.getBriefDescriptions()));
140                                 listItem.setComputedCurrentLocation(collectionObject.getComputedCurrentLocation());
141                                 listItem.setObjectName(
142                                         ObjectNameListModel.objectNameListToDisplayString(collectionObject.getObjectNameList()));
143                                 listItem.setTitle(
144                                         TitleGroupListModel.titleGroupListToDisplayString(collectionObject.getTitleGroupList()));
145                                 ResponsibleDepartmentsList rdl = ResponsibleDepartmentsListModel
146                                         .responsibleDepartmentListToResponsibleDepartmentsList(
147                                                 collectionObject.getResponsibleDepartments());
148                                 listItem.setResponsibleDepartments(rdl);
149                                 listItem.setResponsibleDepartment(
150                                         ResponsibleDepartmentsListModel.responsibleDepartmentsListDisplayString(rdl));
151
152                                 listItem.setContentConcepts(
153                                         ContentConceptListModel.contentConceptListDisplayString(collectionObject.getContentConcepts()));
154
155                                 // from media resource
156                                 if (blobCsids.size() > 0) {
157                                         listItem.setBlobCsid(blobCsids.get(0));
158                                 }
159                                 // add the populated item to the results
160                                 resultsList.getAdvancedsearchListItem().add(listItem);
161                         } else {
162                                 logger.warn("advancedsearch: could not find CollectionobjectsCommon associated with csid {}", csid);
163                         }
164
165                 }
166
167                 // NOTE: I think this is necessary for the front end to know what to do with
168                 // what's returned (?)
169                 AbstractCommonList abstractList = (AbstractCommonList) resultsList;
170                 abstractList.setItemsInPage(collectionObjectList.getItemsInPage());
171                 abstractList.setPageNum(collectionObjectList.getPageNum());
172                 abstractList.setPageSize(collectionObjectList.getPageSize());
173                 abstractList.setTotalItems(collectionObjectList.getTotalItems());
174                 // FIXME: is there a way to generate this rather than hardcode it?
175                 abstractList.setFieldsReturned(FIELDS_RETURNED);
176
177                 return resultsList;
178         }
179
180         /** 
181          * Retrieves the blob CSIDs associated with a given object's CSID
182          * 
183          * @param csid The CSID of an object whose associated blobs (thumbnails) is desired
184          * @param wrappedUriInfo The wrapped (mutable) UriInfo of the incoming query that ultimately triggered this call
185          * @return A possibly-empty list of strings of the blob CSIDs associated with CSID
186          */
187         private List<String> findBlobCsids(String csid, UriInfoWrapper wrappedUriInfo) {
188                 MultivaluedMap<String, String> wrappedQueryParams = wrappedUriInfo.getQueryParameters();
189                 wrappedQueryParams.clear();
190                 wrappedQueryParams.add(IQueryManager.SEARCH_RELATED_TO_CSID_AS_SUBJECT, csid);
191                 wrappedQueryParams.add("pgSz", "1");
192                 wrappedQueryParams.add("pgNum", "0");
193                 wrappedQueryParams.add("sortBy", "media_common:title");
194                 AbstractCommonList associatedMedia = mr.getList(wrappedUriInfo);
195                 if (associatedMedia == null || associatedMedia.getListItem() == null) {
196                         return Collections.emptyList();
197                 }
198
199                 return associatedMedia.getListItem().stream()
200                         .filter(item -> item != null && item.getAny() != null)
201                         .flatMap(li -> li.getAny().stream())
202                         .filter(element -> MediaJAXBSchema.blobCsid.equals(element.getTagName()))
203                         .map(Element::getTextContent)
204                         .filter(blobCsid -> blobCsid != null && !blobCsid.isEmpty())
205                         .collect(Collectors.toList());
206         }
207
208         @Override
209         public Class<?> getCommonPartClass() {
210                 return null;
211         }
212
213         @Override
214         public ServiceContextFactory<AdvancedsearchListItem, AdvancedsearchListItem> getServiceContextFactory() {
215                 return (ServiceContextFactory<AdvancedsearchListItem, AdvancedsearchListItem>) RemoteServiceContextFactory
216                                 .get();
217         }
218
219         @Override
220         public String getServiceName() {
221                 return AdvancedSearchConstants.SERVICE_NAME;
222         }
223
224         @Override
225         protected String getVersionString() {
226                 return "0.01";
227         }
228 }