Skip to content

Commit

Permalink
Feat : add list filter by key
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarbet committed Jun 20, 2024
1 parent 1206f04 commit 333c8dc
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Pair<Long, List<Data>> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ public FileSystemPersistenceServiceImpl(String localFolder) {
}

@Override
public Pair<Long, List<Data>> list(String zone, IdentityParam identityParam, Integer size, Integer page, SortOrder order) throws ArlasException {
public Pair<Long, List<Data>> list(String zone, IdentityParam identityParam, Integer size, Integer page, SortOrder order, String key) throws ArlasException {
List<FileWrapper> 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<FileWrapper> pub = getByFilenameFilter(prefixFilter(zone, ""), null,false);
List<FileWrapper> 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()))
Expand All @@ -80,7 +80,7 @@ public Pair<Long, List<Data>> list(String zone, IdentityParam identityParam, Int
}

public Data getById(String id, IdentityParam identityParam) throws ArlasException {
List<FileWrapper> list = getByFilenameFilter(suffixFilter(id), identityParam, false);
List<FileWrapper> 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)) {
Expand Down Expand Up @@ -118,7 +118,7 @@ public Data create(String zone, String key, IdentityParam identityParam, Set<Str

@Override
public Data update(String id, String key, IdentityParam identityParam, Set<String> readers, Set<String> writers, String value, Date lastUpdate) throws ArlasException {
List<FileWrapper> list = getByFilenameFilter(suffixFilter(id), identityParam, false);
List<FileWrapper> list = getByFilenameFilter(suffixFilter(id), identityParam, false, null);
if (list.size() == 1) {
Data data = list.get(0).data;
String zone = data.getDocZone();
Expand Down Expand Up @@ -151,7 +151,7 @@ public Data update(String id, String key, IdentityParam identityParam, Set<Strin

@Override
public Data deleteById(String id, IdentityParam identityParam) throws ArlasException {
List<FileWrapper> list = getByFilenameFilter(suffixFilter(id), identityParam, false);
List<FileWrapper> list = getByFilenameFilter(suffixFilter(id), identityParam, false, null);
if (list.size() == 1) {
if (PersistenceService.isWriterOnData(identityParam, list.get(0).data)) {
try {
Expand Down Expand Up @@ -187,9 +187,10 @@ private Predicate<Path> suffixFilter(String suffix) {
return p -> p.getFileName().toString().endsWith(suffix);
}

private List<FileWrapper> getByFilenameFilter(Predicate<Path> fileFilter, IdentityParam identityParam, boolean filterOnRights) throws ArlasException {
private List<FileWrapper> getByFilenameFilter(Predicate<Path> fileFilter, IdentityParam identityParam, boolean filterOnRights, String key) throws ArlasException {
try (Stream<Path> paths = Files.walk(Paths.get(storageFolder))) {
return paths

Stream<FileWrapper> pathsStream = paths
.filter(Files::isRegularFile)
.filter(fileFilter)
.map(Path::toFile)
Expand All @@ -201,8 +202,14 @@ private List<FileWrapper> getByFilenameFilter(Predicate<Path> 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().contains(key));
}
return pathsStream.collect(Collectors.toList());


} catch (Exception e) {
throw new ArlasException("Error");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,31 +138,39 @@ private Data toData(String id, DocumentSnapshot d) throws NotFoundException {
}

@Override
public Pair<Long, List<Data>> list(String zone, IdentityParam identityParam, Integer size, Integer page, SortOrder order) throws ArlasException {
public Pair<Long, List<Data>> list(String zone, IdentityParam identityParam, Integer size, Integer page, SortOrder order, String key) throws ArlasException {
List<String> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,37 @@ public HibernatePersistenceServiceImpl(SessionFactory factory) {
}

@Override
public Pair<Long, List<Data>> list(String zone, IdentityParam identityParam, Integer size, Integer page, SortOrder order) {
public Pair<Long, List<Data>> 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"
+ " and (ud." + Data.ownerColumn + "=:userId"
+ " 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<Long> 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<Data> query = currentSession().createQuery(from
+ " order by ud." + Data.lastUpdateDateColumn + " " + order.toString(), Data.class)
.setParameter("zone", zone)
.setParameter("organization", identityParam.organisation)
.setParameter("userId", identityParam.userId)
.setMaxResults(size)
.setFirstResult((page - 1) * size);

if(key != null) {
query = query.setParameter("searchKey", "%"+key+"%");
}
return Pair.of(totalCount, list(query));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,31 @@ 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));
}

@Test
Expand Down

0 comments on commit 333c8dc

Please sign in to comment.