]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
932487746ef87f875e5599d7e7da6e27f6d5cea8
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.advancedsearch;
2
3 import java.util.Collections;
4 import java.util.List;
5 import java.util.stream.Collectors;
6 import javax.ws.rs.Consumes;
7 import javax.ws.rs.GET;
8 import javax.ws.rs.Path;
9 import javax.ws.rs.Produces;
10 import javax.ws.rs.core.Context;
11 import javax.ws.rs.core.MultivaluedMap;
12 import javax.ws.rs.core.Request;
13 import javax.ws.rs.core.UriInfo;
14 import javax.xml.bind.JAXBException;
15 import javax.xml.bind.Unmarshaller;
16
17 import org.collectionspace.services.MediaJAXBSchema;
18 import org.collectionspace.services.advancedsearch.AdvancedsearchCommonList.AdvancedsearchListItem;
19 import org.collectionspace.services.advancedsearch.mapper.CollectionObjectMapper;
20 import org.collectionspace.services.client.IQueryManager;
21 import org.collectionspace.services.collectionobject.CollectionObjectResource;
22 import org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl;
23 import org.collectionspace.services.common.UriInfoWrapper;
24 import org.collectionspace.services.common.context.RemoteServiceContextFactory;
25 import org.collectionspace.services.common.context.ServiceContextFactory;
26 import org.collectionspace.services.common.relation.RelationResource;
27 import org.collectionspace.services.jaxb.AbstractCommonList;
28 import org.collectionspace.services.media.MediaResource;
29 import org.collectionspace.services.nuxeo.client.handler.CSDocumentModelList;
30 import org.collectionspace.services.nuxeo.client.handler.CSDocumentModelList.CSDocumentModelResponse;
31 import org.collectionspace.services.nuxeo.client.handler.UnfilteredDocumentModelHandler;
32 import org.collectionspace.services.relation.RelationsCommonList;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.w3c.dom.Element;
36
37 /**
38  * This class defines the advanced search endpoints.
39  */
40 @Path(AdvancedSearchConstants.SERVICE_PATH)
41 @Consumes("application/xml")
42 @Produces("application/xml")
43 public class AdvancedSearch
44                 extends AbstractCollectionSpaceResourceImpl<AdvancedsearchListItem, AdvancedsearchListItem> {
45         // FIXME: it's not great to hardcode either of these
46         private static final String FIELDS_RETURNED = "uri|csid|refName|blobCsid|updatedAt|objectId|objectNumber|objectName|title|computedCurrentLocation|responsibleDepartments|responsibleDepartment|contentConcepts|briefDescription";
47
48         private final Logger logger = LoggerFactory.getLogger(AdvancedSearch.class);
49         private final CollectionObjectResource cor = new CollectionObjectResource();
50         private final MediaResource mr = new MediaResource();
51         private final RelationResource relations = new RelationResource();
52
53         public AdvancedSearch() {
54                 super();
55         }
56
57         /**
58          * Primary advanced search API endpoint.
59          * 
60          * @param request The incoming request. Injected. 
61          * @param uriInfo The URI info of the incoming request, including query parameters and other search control parameters. Injected.
62          * @return A possibly-empty AbstractCommonList of the advanced search results corresponding to the query
63          */
64         @GET
65         public AbstractCommonList getList(@Context Request request, @Context UriInfo uriInfo) {
66                 logger.info("advancedsearch called with path: {}", uriInfo.getPath());
67                 MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters(true);
68                 logger.info("advancedsearch called with query params: {}", queryParams);
69                 final String markRelated = queryParams.getFirst(IQueryManager.MARK_RELATED_TO_CSID_AS_SUBJECT);
70
71                 cor.setDocumentHandlerClass(UnfilteredDocumentModelHandler.class);
72                 ObjectFactory objectFactory = new ObjectFactory();
73                 AdvancedsearchCommonList resultsList = objectFactory.createAdvancedsearchCommonList();
74
75                 AbstractCommonList abstractCommonList = cor.getList(uriInfo);
76                 if (!(abstractCommonList instanceof CSDocumentModelList)) {
77                         return resultsList;
78                 }
79
80                 Unmarshaller unmarshaller;
81                 CSDocumentModelList collectionObjectList = (CSDocumentModelList) abstractCommonList;
82                 try {
83                         unmarshaller = AdvancedSearchJAXBContext.getJaxbContext().createUnmarshaller();
84                 } catch (JAXBException e) {
85                         // this should result in a 500, need to verify from bigReThrow to see what exception it should be
86                         throw new RuntimeException("Unable to create unmarshaller for AdvancedSearch", e);
87                 }
88
89                 final CollectionObjectMapper responseMapper = new CollectionObjectMapper(unmarshaller);
90                 for (CSDocumentModelResponse response : collectionObjectList.getResponseList()) {
91                         String csid = response.getCsid();
92                         UriInfoWrapper wrappedUriInfo = new UriInfoWrapper(uriInfo);
93                         List<String> blobCsids = findBlobCsids(csid, wrappedUriInfo);
94
95                         AdvancedsearchListItem listItem = responseMapper.asListItem(response, blobCsids);
96                         if (listItem != null) {
97                                 if (markRelated != null) {
98                                         RelationsCommonList relationsList = relations.getRelationForSubject(markRelated, csid, uriInfo);
99                                         listItem.setRelated(!relationsList.getRelationListItem().isEmpty());
100                                 }
101                                 resultsList.getAdvancedsearchListItem().add(listItem);
102                         }
103                 }
104
105                 resultsList.setItemsInPage(collectionObjectList.getItemsInPage());
106                 resultsList.setPageNum(collectionObjectList.getPageNum());
107                 resultsList.setPageSize(collectionObjectList.getPageSize());
108                 resultsList.setTotalItems(collectionObjectList.getTotalItems());
109                 resultsList.setFieldsReturned(FIELDS_RETURNED);
110
111                 return resultsList;
112         }
113
114         /** 
115          * Retrieves the blob CSIDs associated with a given object's CSID
116          * 
117          * @param csid The CSID of an object whose associated blobs (thumbnails) is desired
118          * @param wrappedUriInfo The wrapped (mutable) UriInfo of the incoming query that ultimately triggered this call
119          * @return A possibly-empty list of strings of the blob CSIDs associated with CSID
120          */
121         private List<String> findBlobCsids(String csid, UriInfoWrapper wrappedUriInfo) {
122                 MultivaluedMap<String, String> wrappedQueryParams = wrappedUriInfo.getQueryParameters();
123                 wrappedQueryParams.clear();
124                 wrappedQueryParams.add(IQueryManager.SEARCH_RELATED_TO_CSID_AS_SUBJECT, csid);
125                 wrappedQueryParams.add("pgSz", "1");
126                 wrappedQueryParams.add("pgNum", "0");
127                 wrappedQueryParams.add("sortBy", "media_common:title");
128                 AbstractCommonList associatedMedia = mr.getList(wrappedUriInfo);
129                 if (associatedMedia == null || associatedMedia.getListItem() == null) {
130                         return Collections.emptyList();
131                 }
132
133                 return associatedMedia.getListItem().stream()
134                         .filter(item -> item != null && item.getAny() != null)
135                         .flatMap(li -> li.getAny().stream())
136                         .filter(element -> MediaJAXBSchema.blobCsid.equals(element.getTagName()))
137                         .map(Element::getTextContent)
138                         .filter(blobCsid -> blobCsid != null && !blobCsid.isEmpty())
139                         .collect(Collectors.toList());
140         }
141
142         @Override
143         public Class<?> getCommonPartClass() {
144                 return null;
145         }
146
147         @Override
148         public ServiceContextFactory<AdvancedsearchListItem, AdvancedsearchListItem> getServiceContextFactory() {
149                 return (ServiceContextFactory<AdvancedsearchListItem, AdvancedsearchListItem>) RemoteServiceContextFactory
150                                 .get();
151         }
152
153         @Override
154         public String getServiceName() {
155                 return AdvancedSearchConstants.SERVICE_NAME;
156         }
157
158         @Override
159         protected String getVersionString() {
160                 return "0.01";
161         }
162 }