-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1554 from thehyve/fix-files-api
Fix file /api/search endpoint when collections not configured as a postgres view.
- Loading branch information
Showing
15 changed files
with
567 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
projects/saturn/src/main/java/io/fairspace/saturn/services/search/FileSearchService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package io.fairspace.saturn.services.search; | ||
|
||
import java.util.List; | ||
|
||
public interface FileSearchService { | ||
List<SearchResultDTO> searchFiles(FileSearchRequest request); | ||
} |
49 changes: 49 additions & 0 deletions
49
projects/saturn/src/main/java/io/fairspace/saturn/services/search/JdbcFileSearchService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package io.fairspace.saturn.services.search; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import io.milton.resource.CollectionResource; | ||
import lombok.SneakyThrows; | ||
import lombok.extern.log4j.Log4j2; | ||
|
||
import io.fairspace.saturn.config.Config; | ||
import io.fairspace.saturn.config.ViewsConfig; | ||
import io.fairspace.saturn.rdf.transactions.Transactions; | ||
import io.fairspace.saturn.services.views.ViewStoreClientFactory; | ||
import io.fairspace.saturn.services.views.ViewStoreReader; | ||
|
||
import static io.fairspace.saturn.webdav.PathUtils.getCollectionNameByUri; | ||
|
||
@Log4j2 | ||
public class JdbcFileSearchService implements FileSearchService { | ||
private final Transactions transactions; | ||
private final CollectionResource rootSubject; | ||
private final Config.Search searchConfig; | ||
private final ViewsConfig viewsConfig; | ||
private final ViewStoreClientFactory viewStoreClientFactory; | ||
|
||
public JdbcFileSearchService( | ||
Config.Search searchConfig, | ||
ViewsConfig viewsConfig, | ||
ViewStoreClientFactory viewStoreClientFactory, | ||
Transactions transactions, | ||
CollectionResource rootSubject) { | ||
this.searchConfig = searchConfig; | ||
this.viewStoreClientFactory = viewStoreClientFactory; | ||
this.transactions = transactions; | ||
this.rootSubject = rootSubject; | ||
this.viewsConfig = viewsConfig; | ||
} | ||
|
||
@SneakyThrows | ||
public List<SearchResultDTO> searchFiles(FileSearchRequest request) { | ||
var collectionsForUser = transactions.calculateRead(m -> rootSubject.getChildren().stream() | ||
.map(collection -> getCollectionNameByUri(rootSubject.getUniqueId(), collection.getUniqueId())) | ||
.collect(Collectors.toList())); | ||
|
||
try (var viewStoreReader = new ViewStoreReader(searchConfig, viewsConfig, viewStoreClientFactory)) { | ||
return viewStoreReader.searchFiles(request, collectionsForUser); | ||
} | ||
} | ||
} |
9 changes: 4 additions & 5 deletions
9
projects/saturn/src/main/java/io/fairspace/saturn/services/search/SearchApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...cts/saturn/src/main/java/io/fairspace/saturn/services/search/SparqlFileSearchService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package io.fairspace.saturn.services.search; | ||
|
||
import java.util.List; | ||
|
||
import lombok.extern.log4j.Log4j2; | ||
import org.apache.jena.query.Dataset; | ||
import org.apache.jena.query.Query; | ||
import org.apache.jena.query.QueryFactory; | ||
import org.apache.jena.query.QuerySolutionMap; | ||
|
||
import io.fairspace.saturn.rdf.SparqlUtils; | ||
import io.fairspace.saturn.vocabulary.FS; | ||
|
||
import static io.fairspace.saturn.util.ValidationUtils.validateIRI; | ||
|
||
import static org.apache.jena.rdf.model.ResourceFactory.createStringLiteral; | ||
|
||
@Log4j2 | ||
public class SparqlFileSearchService implements FileSearchService { | ||
private final Dataset ds; | ||
|
||
public SparqlFileSearchService(Dataset ds) { | ||
this.ds = ds; | ||
} | ||
|
||
public List<SearchResultDTO> searchFiles(FileSearchRequest request) { | ||
var query = getSearchForFilesQuery(request.getParentIRI()); | ||
var binding = new QuerySolutionMap(); | ||
binding.add("regexQuery", createStringLiteral(SparqlUtils.getQueryRegex(request.getQuery()))); | ||
return SparqlUtils.getByQuery(query, binding, ds); | ||
} | ||
|
||
private Query getSearchForFilesQuery(String parentIRI) { | ||
var builder = new StringBuilder("PREFIX fs: <") | ||
.append(FS.NS) | ||
.append(">\nPREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n\n") | ||
.append("SELECT ?id ?label ?comment ?type\n") | ||
.append("WHERE {\n"); | ||
|
||
if (parentIRI != null && !parentIRI.trim().isEmpty()) { | ||
validateIRI(parentIRI); | ||
builder.append("?id fs:belongsTo* <").append(parentIRI).append("> .\n"); | ||
} | ||
|
||
builder.append("?id rdfs:label ?label ; a ?type .\n") | ||
.append("FILTER (?type in (fs:File, fs:Directory, fs:Collection))\n") | ||
.append("OPTIONAL { ?id rdfs:comment ?comment }\n") | ||
.append("FILTER NOT EXISTS { ?id fs:dateDeleted ?anydate }\n") | ||
.append("FILTER (regex(?label, ?regexQuery, \"i\") || regex(?comment, ?regexQuery, \"i\"))\n") | ||
.append("}\nLIMIT 10000"); | ||
|
||
return QueryFactory.create(builder.toString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.