View Javadoc
1   package de.dlr.shepard.common.search.services;
2   
3   import de.dlr.shepard.common.search.endpoints.BasicCollectionAttributes;
4   import de.dlr.shepard.common.search.io.CollectionSearchBody;
5   import de.dlr.shepard.common.search.io.CollectionSearchResult;
6   import de.dlr.shepard.common.search.io.ResponseBody;
7   import de.dlr.shepard.common.search.io.SearchBody;
8   import de.dlr.shepard.common.search.query.QueryValidator;
9   import de.dlr.shepard.context.collection.io.CollectionIO;
10  import jakarta.enterprise.context.RequestScoped;
11  import jakarta.inject.Inject;
12  import java.util.Optional;
13  
14  @RequestScoped
15  public class SearchService {
16  
17    @Inject
18    StructuredDataSearchService structuredDataSearcher;
19  
20    @Inject
21    CollectionSearchService collectionSearcher;
22  
23    @Inject
24    DataObjectSearchService dataObjectSearcher;
25  
26    @Inject
27    ReferenceSearchService referenceSearcher;
28  
29    public ResponseBody search(SearchBody searchBody) {
30      QueryValidator.checkQuery(searchBody.getSearchParams().getQuery());
31      ResponseBody ret =
32        switch (searchBody.getSearchParams().getQueryType()) {
33          case StructuredData -> structuredDataSearcher.search(searchBody);
34          case Collection -> searchCollections(searchBody);
35          case DataObject -> dataObjectSearcher.search(searchBody);
36          case Reference -> referenceSearcher.search(searchBody);
37          default -> null;
38        };
39      return ret;
40    }
41  
42    private ResponseBody searchCollections(SearchBody searchBody) {
43      PaginatedCollectionList paginatedCollectionList = collectionSearcher.search(
44        searchBody.getSearchParams().getQuery(),
45        Optional.empty(),
46        Optional.empty(),
47        BasicCollectionAttributes.createdAt,
48        true
49      );
50  
51      return new CollectionSearchResult(
52        paginatedCollectionList.getResults().stream().map(CollectionIO::new).toList(),
53        new CollectionSearchBody(searchBody).getSearchParams(),
54        paginatedCollectionList.getTotalResults()
55      ).toResponseBody();
56    }
57  }