View Javadoc
1   package de.dlr.shepard.common.search.services;
2   
3   import de.dlr.shepard.auth.users.entities.User;
4   import de.dlr.shepard.auth.users.services.UserService;
5   import de.dlr.shepard.common.neo4j.io.BasicEntityIO;
6   import de.dlr.shepard.common.search.daos.SearchDAO;
7   import de.dlr.shepard.common.search.io.ResponseBody;
8   import de.dlr.shepard.common.search.io.ResultTriple;
9   import de.dlr.shepard.common.search.io.SearchBody;
10  import de.dlr.shepard.common.search.io.SearchScope;
11  import de.dlr.shepard.common.search.query.Neo4jQueryBuilder;
12  import de.dlr.shepard.common.util.SearchConstants;
13  import de.dlr.shepard.context.references.basicreference.entities.BasicReference;
14  import jakarta.enterprise.context.RequestScoped;
15  import jakarta.inject.Inject;
16  import java.util.HashSet;
17  import java.util.Set;
18  
19  @RequestScoped
20  public class ReferenceSearchService {
21  
22    @Inject
23    SearchDAO searchDAO;
24  
25    @Inject
26    UserService userService;
27  
28    public ResponseBody search(SearchBody searchBody) {
29      User user = userService.getCurrentUser();
30  
31      Set<BasicReference> resultsSet = new HashSet<>();
32      SearchScope[] scopes = searchBody.getScopes();
33      String searchBodyQuery = searchBody.getSearchParams().getQuery();
34      for (SearchScope scope : scopes) {
35        // no CollectionId and no DataObjectId given
36        if (scope.getCollectionId() == null && scope.getDataObjectId() == null) {
37          String selectionQuery = Neo4jQueryBuilder.basicReferenceSelectionQueryWithNeo4jId(
38            searchBodyQuery,
39            user.getUsername()
40          );
41          var res = searchDAO.findReferences(selectionQuery, SearchConstants.REFERENCE_IN_QUERY);
42          resultsSet.addAll(res);
43        }
44        // CollectionId given but no DataObjectId
45        else if (scope.getCollectionId() != null && scope.getDataObjectId() == null) {
46          String selectionQuery = Neo4jQueryBuilder.collectionBasicReferenceSelectionQueryWithNeo4jId(
47            searchBodyQuery,
48            scope.getCollectionId(),
49            user.getUsername()
50          );
51          var res = searchDAO.findReferences(selectionQuery, SearchConstants.REFERENCE_IN_QUERY);
52          resultsSet.addAll(res);
53        }
54        // CollectionId and DataObjectId given
55        else if (scope.getCollectionId() != null && scope.getDataObjectId() != null) {
56          // search according to TraversalRules
57          if (scope.getTraversalRules().length != 0) {
58            for (int j = 0; j < scope.getTraversalRules().length; j++) {
59              String selectionQuery = Neo4jQueryBuilder.collectionDataObjectBasicReferenceSelectionQueryWithNeo4jId(
60                scope,
61                scope.getTraversalRules()[j],
62                searchBodyQuery,
63                user.getUsername()
64              );
65              var res = searchDAO.findReferences(selectionQuery, SearchConstants.REFERENCE_IN_QUERY);
66              resultsSet.addAll(res);
67            }
68          } else {
69            // no TraversalRules given
70            String selectionQuery = Neo4jQueryBuilder.collectionDataObjectReferenceSelectionQueryWithNeo4jId(
71              scope,
72              searchBodyQuery,
73              user.getUsername()
74            );
75            var res = searchDAO.findReferences(selectionQuery, SearchConstants.REFERENCE_IN_QUERY);
76            resultsSet.addAll(res);
77          }
78        }
79      }
80      BasicReference[] references = resultsSet.toArray(new BasicReference[0]);
81      ResultTriple[] resultTriples = new ResultTriple[resultsSet.size()];
82      BasicEntityIO[] results = new BasicEntityIO[resultsSet.size()];
83      for (var i = 0; i < resultsSet.size(); i++) {
84        resultTriples[i] = new ResultTriple(
85          references[i].getDataObject().getCollection().getShepardId(),
86          references[i].getDataObject().getShepardId(),
87          references[i].getShepardId()
88        );
89        results[i] = new BasicEntityIO(references[i]);
90      }
91      ResponseBody ret = new ResponseBody(resultTriples, results, searchBody.getSearchParams());
92      return ret;
93    }
94  }