From 6681e85038d162437fa7209bd9830d61a27be495 Mon Sep 17 00:00:00 2001 From: MatthieuBarbet Date: Wed, 19 Jun 2024 16:30:26 +0200 Subject: [PATCH] Feat : add list filter by key --- .../persistence/server/app/Documentation.java | 1 + .../server/core/PersistenceService.java | 2 +- .../FileSystemPersistenceServiceImpl.java | 27 +++++++++----- ...GoogleFirestorePersistenceServiceImpl.java | 34 ++++++++++------- .../impl/HibernatePersistenceServiceImpl.java | 18 +++++++-- .../rest/PersistenceRestService.java | 12 +++++- .../arlas/persistence/rest/PersistenceIT.java | 37 +++++++++++++++++++ 7 files changed, 101 insertions(+), 30 deletions(-) diff --git a/arlas-persistence-core/src/main/java/io/arlas/persistence/server/app/Documentation.java b/arlas-persistence-core/src/main/java/io/arlas/persistence/server/app/Documentation.java index bd78f9b..d70e63a 100644 --- a/arlas-persistence-core/src/main/java/io/arlas/persistence/server/app/Documentation.java +++ b/arlas-persistence-core/src/main/java/io/arlas/persistence/server/app/Documentation.java @@ -38,5 +38,6 @@ public class Documentation { public static final String WRITERS = "Comma separated values of groups authorized to modify the data."; public static final String LAST_UPDATE = "Previous date value of last modification known by client."; public static final String FORM_PRETTY = "Pretty print"; + public static final String SEARCH_KEY = "Filter by key value"; } diff --git a/arlas-persistence-core/src/main/java/io/arlas/persistence/server/core/PersistenceService.java b/arlas-persistence-core/src/main/java/io/arlas/persistence/server/core/PersistenceService.java index 0714893..8189d33 100644 --- a/arlas-persistence-core/src/main/java/io/arlas/persistence/server/core/PersistenceService.java +++ b/arlas-persistence-core/src/main/java/io/arlas/persistence/server/core/PersistenceService.java @@ -39,7 +39,7 @@ Pair> list(String zone, IdentityParam identityParam, Integer size, Integer page, - SortOrder order) throws ArlasException; + SortOrder order, String key) throws ArlasException; Data getById(String id, IdentityParam identityParam) throws ArlasException; diff --git a/arlas-persistence-core/src/main/java/io/arlas/persistence/server/impl/FileSystemPersistenceServiceImpl.java b/arlas-persistence-core/src/main/java/io/arlas/persistence/server/impl/FileSystemPersistenceServiceImpl.java index 77fee59..c84d815 100644 --- a/arlas-persistence-core/src/main/java/io/arlas/persistence/server/impl/FileSystemPersistenceServiceImpl.java +++ b/arlas-persistence-core/src/main/java/io/arlas/persistence/server/impl/FileSystemPersistenceServiceImpl.java @@ -53,14 +53,14 @@ public FileSystemPersistenceServiceImpl(String localFolder) { } @Override - public Pair> list(String zone, IdentityParam identityParam, Integer size, Integer page, SortOrder order) throws ArlasException { + public Pair> list(String zone, IdentityParam identityParam, Integer size, Integer page, SortOrder order, String key) throws ArlasException { List fileWrappers = new ArrayList<>(); for (String org : identityParam.organisation) { - fileWrappers.addAll(getByFilenameFilter(prefixFilter(zone, org), identityParam, true)); + fileWrappers.addAll(getByFilenameFilter(prefixFilter(zone, org), identityParam, true, key)); } if (!identityParam.isAnonymous) { // add public from other orgs - List pub = getByFilenameFilter(prefixFilter(zone, ""), null,false); + List pub = getByFilenameFilter(prefixFilter(zone, ""), null,false, key); fileWrappers.addAll(pub.stream() .filter(f -> PersistenceService.isPublic(f.data)) .filter(f -> !identityParam.organisation.contains(f.data.getDocOrganization())) @@ -80,7 +80,7 @@ public Pair> list(String zone, IdentityParam identityParam, Int } public Data getById(String id, IdentityParam identityParam) throws ArlasException { - List list = getByFilenameFilter(suffixFilter(id), identityParam, false); + List list = getByFilenameFilter(suffixFilter(id), identityParam, false, null); if (list.size() == 1) { if (PersistenceService.isReaderOnData(identityParam, list.get(0).data) || PersistenceService.isWriterOnData(identityParam, list.get(0).data)) { @@ -118,7 +118,7 @@ public Data create(String zone, String key, IdentityParam identityParam, Set readers, Set writers, String value, Date lastUpdate) throws ArlasException { - List list = getByFilenameFilter(suffixFilter(id), identityParam, false); + List list = getByFilenameFilter(suffixFilter(id), identityParam, false, null); if (list.size() == 1) { Data data = list.get(0).data; String zone = data.getDocZone(); @@ -151,7 +151,7 @@ public Data update(String id, String key, IdentityParam identityParam, Set list = getByFilenameFilter(suffixFilter(id), identityParam, false); + List list = getByFilenameFilter(suffixFilter(id), identityParam, false, null); if (list.size() == 1) { if (PersistenceService.isWriterOnData(identityParam, list.get(0).data)) { try { @@ -187,9 +187,10 @@ private Predicate suffixFilter(String suffix) { return p -> p.getFileName().toString().endsWith(suffix); } - private List getByFilenameFilter(Predicate fileFilter, IdentityParam identityParam, boolean filterOnRights) throws ArlasException { + private List getByFilenameFilter(Predicate fileFilter, IdentityParam identityParam, boolean filterOnRights, String key) throws ArlasException { try (Stream paths = Files.walk(Paths.get(storageFolder))) { - return paths + + Stream pathsStream = paths .filter(Files::isRegularFile) .filter(fileFilter) .map(Path::toFile) @@ -201,8 +202,14 @@ private List getByFilenameFilter(Predicate fileFilter, Identi } }) .filter(fw -> !filterOnRights || PersistenceService.isReaderOnData(identityParam, fw.data) || - PersistenceService.isWriterOnData(identityParam, fw.data)) - .collect(Collectors.toList()); + PersistenceService.isWriterOnData(identityParam, fw.data)); + + if(key != null){ + pathsStream = pathsStream.filter(fw -> fw.data.getDocKey().toLowerCase().contains(key.toLowerCase())); + } + return pathsStream.collect(Collectors.toList()); + + } catch (Exception e) { throw new ArlasException("Error"); } diff --git a/arlas-persistence-core/src/main/java/io/arlas/persistence/server/impl/GoogleFirestorePersistenceServiceImpl.java b/arlas-persistence-core/src/main/java/io/arlas/persistence/server/impl/GoogleFirestorePersistenceServiceImpl.java index cdba77c..dea6158 100644 --- a/arlas-persistence-core/src/main/java/io/arlas/persistence/server/impl/GoogleFirestorePersistenceServiceImpl.java +++ b/arlas-persistence-core/src/main/java/io/arlas/persistence/server/impl/GoogleFirestorePersistenceServiceImpl.java @@ -138,31 +138,39 @@ private Data toData(String id, DocumentSnapshot d) throws NotFoundException { } @Override - public Pair> list(String zone, IdentityParam identityParam, Integer size, Integer page, SortOrder order) throws ArlasException { + public Pair> list(String zone, IdentityParam identityParam, Integer size, Integer page, SortOrder order, String key) throws ArlasException { List entities = new ArrayList<>(identityParam.groups); entities.add(identityParam.userId); + Filter filter = Filter.or( + Filter.arrayContainsAny(Data.docEntitiesColumn, List.of(GROUP_PUBLIC)), + Filter.and( + Filter.inArray(Data.organizationColumn, identityParam.organisation), + Filter.arrayContainsAny(Data.docEntitiesColumn, entities) + )); + if(key != null){ + filter = Filter.and( + Filter.or( + Filter.arrayContainsAny(Data.docEntitiesColumn, List.of(GROUP_PUBLIC)), + Filter.and( + Filter.inArray(Data.organizationColumn, identityParam.organisation), + Filter.arrayContainsAny(Data.docEntitiesColumn, entities) + ), + // We can't make %like% request with firestore + Filter.equalTo(Data.keyColumn, key) + )); + } try { return Pair.of( (long) db.collection(this.collection) .whereEqualTo(Data.zoneColumn, zone) - .where(Filter.or( - Filter.arrayContainsAny(Data.docEntitiesColumn, List.of(GROUP_PUBLIC)), - Filter.and( - Filter.inArray(Data.organizationColumn, identityParam.organisation), - Filter.arrayContainsAny(Data.docEntitiesColumn, entities) - ))) + .where(filter) .get() .get() .size(), db.collection(this.collection) .whereEqualTo(Data.zoneColumn, zone) - .where(Filter.or( - Filter.arrayContainsAny(Data.docEntitiesColumn, List.of(GROUP_PUBLIC)), - Filter.and( - Filter.inArray(Data.organizationColumn, identityParam.organisation), - Filter.arrayContainsAny(Data.docEntitiesColumn, entities) - ))) + .where(filter) .orderBy(Data.lastUpdateDateColumn, order == SortOrder.ASC ? Query.Direction.ASCENDING : Query.Direction.DESCENDING) .limit(size) .offset((page - 1) * size) diff --git a/arlas-persistence-core/src/main/java/io/arlas/persistence/server/impl/HibernatePersistenceServiceImpl.java b/arlas-persistence-core/src/main/java/io/arlas/persistence/server/impl/HibernatePersistenceServiceImpl.java index 0374f4d..66a88fb 100644 --- a/arlas-persistence-core/src/main/java/io/arlas/persistence/server/impl/HibernatePersistenceServiceImpl.java +++ b/arlas-persistence-core/src/main/java/io/arlas/persistence/server/impl/HibernatePersistenceServiceImpl.java @@ -48,7 +48,7 @@ public HibernatePersistenceServiceImpl(SessionFactory factory) { } @Override - public Pair> list(String zone, IdentityParam identityParam, Integer size, Integer page, SortOrder order) { + public Pair> list(String zone, IdentityParam identityParam, Integer size, Integer page, SortOrder order, String key) { String from = " from Data ud " + " where ud." + Data.zoneColumn + "=:zone" + " and ((ud." + Data.organizationColumn + " in :organization" @@ -56,12 +56,18 @@ public Pair> list(String zone, IdentityParam identityParam, Int + " or " + getGroupsRequest(identityParam.groups) + "))" + " or (" + getGroupsRequest(List.of(GROUP_PUBLIC)) + "))"; - Long totalCount = currentSession().createQuery("SELECT count(ud) " + from, Long.class) + if(key != null){ + from = from + " and ud." + Data.keyColumn + " ilike :searchKey"; + } + Query countQuery = currentSession().createQuery("SELECT count(ud) " + from, Long.class) .setParameter("zone", zone) .setParameter("organization", identityParam.organisation) - .setParameter("userId", identityParam.userId) - .uniqueResult(); + .setParameter("userId", identityParam.userId); + if(key != null) { + countQuery = countQuery.setParameter("searchKey", "%"+key+"%"); + } + Long totalCount = countQuery.uniqueResult(); Query query = currentSession().createQuery(from + " order by ud." + Data.lastUpdateDateColumn + " " + order.toString(), Data.class) .setParameter("zone", zone) @@ -69,6 +75,10 @@ public Pair> list(String zone, IdentityParam identityParam, Int .setParameter("userId", identityParam.userId) .setMaxResults(size) .setFirstResult((page - 1) * size); + + if(key != null) { + query = query.setParameter("searchKey", "%"+key+"%"); + } return Pair.of(totalCount, list(query)); } diff --git a/arlas-persistence-rest/src/main/java/io/arlas/persistence/rest/PersistenceRestService.java b/arlas-persistence-rest/src/main/java/io/arlas/persistence/rest/PersistenceRestService.java index 211e601..0669623 100644 --- a/arlas-persistence-rest/src/main/java/io/arlas/persistence/rest/PersistenceRestService.java +++ b/arlas-persistence-rest/src/main/java/io/arlas/persistence/rest/PersistenceRestService.java @@ -139,12 +139,20 @@ public Response list( @Parameter(name = "pretty", description = Documentation.FORM_PRETTY, schema = @Schema(defaultValue = "false")) - @QueryParam(value = "pretty") Boolean pretty + @QueryParam(value = "pretty") Boolean pretty, + + // -------------------------------------------------------- + // ----------------------- Filter ----------------------- + // -------------------------------------------------------- + @Parameter(name = "key", + description = Documentation.SEARCH_KEY, + schema = @Schema(type = "string"), required = false) + @QueryParam(value = "key") String key ) throws ArlasException { IdentityParam identityparam = getIdentityParam(headers); return ResponseFormatter.getResultResponse( halService.dataListToResource( - persistenceService.list(zone, identityparam, size, page, order), uriInfo, page, size, order, identityparam)); + persistenceService.list(zone, identityparam, size, page, order, key), uriInfo, page, size, order, identityparam)); } @Timed diff --git a/arlas-persistence-tests/src/test/java/io/arlas/persistence/rest/PersistenceIT.java b/arlas-persistence-tests/src/test/java/io/arlas/persistence/rest/PersistenceIT.java index 46a794f..da14ae8 100644 --- a/arlas-persistence-tests/src/test/java/io/arlas/persistence/rest/PersistenceIT.java +++ b/arlas-persistence-tests/src/test/java/io/arlas/persistence/rest/PersistenceIT.java @@ -174,7 +174,44 @@ public void test06ListWithPagination() { .body("count", equalTo(1)) .body("total", equalTo(7)); + givenForUser(technical) + .pathParam("zone", dataZone) + .param("order", "asc") + .param("size", "10") + .param("page", "1") + .param("key", "document") + .when() + .get(arlasAppPath.concat("resources/{zone}")) + .then().statusCode(200) + .contentType(ContentType.JSON) + .body("count", equalTo(7)) + .body("total", equalTo(7)); + givenForUser(technical) + .pathParam("zone", dataZone) + .param("order", "asc") + .param("size", "10") + .param("page", "1") + .param("key", "ment6") + .when() + .get(arlasAppPath.concat("resources/{zone}")) + .then().statusCode(200) + .contentType(ContentType.JSON) + .body("count", equalTo(1)) + .body("total", equalTo(1)); + + givenForUser(technical) + .pathParam("zone", dataZone) + .param("order", "asc") + .param("size", "10") + .param("page", "1") + .param("key", "MenT6") + .when() + .get(arlasAppPath.concat("resources/{zone}")) + .then().statusCode(200) + .contentType(ContentType.JSON) + .body("count", equalTo(1)) + .body("total", equalTo(1)); } @Test