Skip to content

Commit

Permalink
add filter
Browse files Browse the repository at this point in the history
  • Loading branch information
ErmiasG committed May 17, 2024
1 parent d34dfd7 commit 4fe59b3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ private void setFilter(Set<? extends AbstractFacade.FilterBy> filter, Query q) {
private void setFilterQuery(AbstractFacade.FilterBy filterBy, Query q) {
switch (Filters.valueOf(filterBy.getValue())) {
case NAME:
case NAME_NEQ:
case NAME_LIKE:
q.setParameter(filterBy.getField(), filterBy.getParam());
break;
case ACCEPTED:
Expand Down Expand Up @@ -259,7 +261,9 @@ public String toString() {
}

public enum Filters {
NAME("NAME", "d.name LIKE CONCAT(:name, '%') ", "name", " "),
NAME("NAME", "LOWER(d.name) LIKE LOWER(CONCAT(:name, '%')) ", "name", " "),
NAME_NEQ("NAME_NEQ", "LOWER(d.name) NOT LIKE LOWER(CONCAT(:name_not_eq, '%')) ", "name_not_eq", " "),
NAME_LIKE("NAME_LIKE", "LOWER(d.name) LIKE LOWER(CONCAT('%', :name_like, '%')) ", "name_like", " "),
ACCEPTED("ACCEPTED", "true =:accepted ", "accepted", "true"),//return all if true
SHARED("SHARED", "NOT :shared ", "shared", "true"),//return none if true
SEARCHABLE("SEARCHABLE", "d.searchable =:searchable ", "searchable", "0"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,16 @@
package io.hops.hopsworks.common.dao.dataset;

import io.hops.hopsworks.common.dao.AbstractFacade;
import io.hops.hopsworks.common.dao.hdfs.inode.InodeFacade;
import io.hops.hopsworks.persistence.entity.project.Project;
import io.hops.hopsworks.persistence.entity.dataset.Dataset;
import io.hops.hopsworks.persistence.entity.dataset.DatasetSharedWith;
import io.hops.hopsworks.persistence.entity.dataset.DatasetType;
import io.hops.hopsworks.persistence.entity.project.Project;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.Date;
import java.util.List;
import java.util.Set;

Expand All @@ -38,8 +35,6 @@ public class DatasetSharedWithFacade extends AbstractFacade<DatasetSharedWith> {
@PersistenceContext(unitName = "kthfsPU")
private EntityManager em;

@EJB
private InodeFacade inodeFacade;

@Override
protected EntityManager getEntityManager() {
Expand Down Expand Up @@ -76,56 +71,37 @@ public CollectionInfo findAllDatasetByProject(Integer offset, Integer limit, Set
"d.project = :project ");
Query query = em.createQuery(queryStr, DatasetSharedWith.class).setParameter("project", project);
Query queryCount = em.createQuery(queryCountStr, DatasetSharedWith.class).setParameter("project", project);
setFilter(filter, query, project);
setFilter(filter, queryCount, project);
setFilter(filter, query);
setFilter(filter, queryCount);
setOffsetAndLim(offset, limit, query);
return new CollectionInfo((Long) queryCount.getSingleResult(), query.getResultList());
}

private void setFilter(Set<? extends AbstractFacade.FilterBy> filter, Query q, Project project) {
private void setFilter(Set<? extends AbstractFacade.FilterBy> filter, Query q) {
if (filter == null || filter.isEmpty()) {
return;
}
for (FilterBy aFilter : filter) {
setFilterQuery(aFilter, q, project);
setFilterQuery(aFilter, q);
}
}

private void setFilterQuery(AbstractFacade.FilterBy filterBy, Query q, Project project) {
private void setFilterQuery(AbstractFacade.FilterBy filterBy, Query q) {
switch (Filters.valueOf(filterBy.getValue())) {
case NAME:
case NAME_NEQ:
case NAME_LIKE:
q.setParameter(filterBy.getField(), filterBy.getParam());
break;
case ACCEPTED:
case PUBLIC:
case SHARED:
case SEARCHABLE:
case UNDER_CONSTRUCTION:
q.setParameter(filterBy.getField(), getBooleanValue(filterBy.getParam()));
break;
case TYPE:
q.setParameter(filterBy.getField(), getEnumValue(filterBy.getField(), filterBy.getValue(), DatasetType.class));
break;
case HDFS_USER:
q.setParameter(filterBy.getField(), inodeFacade.gethdfsUser(filterBy.getParam()));
break;
case USER_EMAIL:
q.setParameter(filterBy.getField(), inodeFacade.getUsers(filterBy.getParam(), project));
break;
case ACCESS_TIME:
case ACCESS_TIME_GT:
case ACCESS_TIME_LT:
case MODIFICATION_TIME:
case MODIFICATION_TIME_GT:
case MODIFICATION_TIME_LT:
Date date = getDate(filterBy.getField(), filterBy.getParam());
q.setParameter(filterBy.getField(), date.getTime());
break;
case SIZE:
case SIZE_LT:
case SIZE_GT:
q.setParameter(filterBy.getField(), getIntValue(filterBy));
break;
default:
break;
}
Expand All @@ -135,10 +111,8 @@ public enum Sorts {
ID("ID", "d.dataset.id ", "ASC"),
NAME("NAME", "LOWER(d.dataset.name) ", "ASC"),
SEARCHABLE("SEARCHABLE", "d.dataset.searchable ", "ASC"),
MODIFICATION_TIME("MODIFICATION_TIME", "d.dataset.inode.modificationTime ", "ASC"),
ACCESS_TIME("ACCESS_TIME", "d.dataset.inode.accessTime ", "ASC"),
PUBLIC("PUBLIC", "d.dataset.public ", "ASC"),
SIZE("SIZE", "d.dataset.inode.size ", "ASC"),

TYPE("TYPE", "d.dataset.dsType ", "ASC");

private final String value;
Expand Down Expand Up @@ -175,30 +149,14 @@ public String toString() {
}

public enum Filters {
NAME("NAME", "d.dataset.name LIKE CONCAT(:name, '%') ", "name", " "),
USER_EMAIL("USER_EMAIL", "d.dataset.inode.hdfsUser =:user ", "user", " "),
HDFS_USER("HDFS_USER", "d.dataset.inode.hdfsUser =:hdfsUser ", "hdfsUser", " "),
UNDER_CONSTRUCTION("UNDER_CONSTRUCTION", "d.dataset.inode.underConstruction =:underConstruction ",
"underConstruction", "true"),
NAME("NAME", "LOWER(d.dataset.name) LIKE LOWER(CONCAT(:name, '%')) ", "name", " "),
NAME_NEQ("NAME_NEQ", "LOWER(d.dataset.name) NOT LIKE LOWER(CONCAT(:name_not_eq, '%')) ", "name_not_eq", " "),
NAME_LIKE("NAME_LIKE", "LOWER(d.dataset.name) LIKE LOWER(CONCAT('%', :name_like, '%')) ", "name_like", " "),
ACCEPTED("ACCEPTED", "d.accepted =:accepted ", "accepted", "true"),
SHARED("SHARED", "true =:shared ", "shared", "true"),//return all if true nothing if false
SEARCHABLE("SEARCHABLE", "d.dataset.searchable =:searchable ", "searchable", "0"),
TYPE("TYPE", "d.dataset.dsType =:dsType ", "dsType", "DATASET"),
PUBLIC("PUBLIC", "d.dataset.public =:public ", "public", "0"),
MODIFICATION_TIME("MODIFICATION_TIME", "d.dataset.inode.modificationTime =:modificationTime ", "modificationTime",
""),
MODIFICATION_TIME_LT("MODIFICATION_TIME_LT", "d.dataset.inode.modificationTime <:modificationTime_lt ",
"modificationTime_lt",
""),
MODIFICATION_TIME_GT("MODIFICATION_TIME_GT", "d.dataset.inode.modificationTime >:modificationTime_gt ",
"modificationTime_gt",
""),
ACCESS_TIME("ACCESS_TIME", "d.dataset.inode.accessTime =:accessTime ", "accessTime", ""),
ACCESS_TIME_LT("ACCESS_TIME_LT", "d.dataset.inode.accessTime <:accessTime_lt ", "accessTime_lt", ""),
ACCESS_TIME_GT("ACCESS_TIME_GT", "d.dataset.inode.accessTime >:accessTime_gt ", "accessTime_gt", ""),
SIZE("SIZE", "d.dataset.inode.size =:size_eq ", "size_eq", "0"),
SIZE_LT("SIZE_LT", "d.dataset.inode.size <:size_lt ", "size_lt", "1"),
SIZE_GT("SIZE_GT", "d.dataset.inode.size >:size_gt ", "size_gt", "0");
PUBLIC("PUBLIC", "d.dataset.public =:public ", "public", "0");

private final String value;
private final String sql;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ private void setFilter(Set<? extends AbstractFacade.FilterBy> filter, Query q, P
private void setFilterQuery(AbstractFacade.FilterBy filterBy, Query q, Project project) {
switch (Filters.valueOf(filterBy.getValue())) {
case NAME:
case NAME_NEQ:
case NAME_LIKE:
q.setParameter(filterBy.getField(), filterBy.getParam());
break;
case UNDER_CONSTRUCTION:
Expand All @@ -301,6 +303,9 @@ private void setFilterQuery(AbstractFacade.FilterBy filterBy, Query q, Project p
case SIZE_GT:
q.setParameter(filterBy.getField(), getIntValue(filterBy));
break;
case TYPE:
q.setParameter(filterBy.getField(), filterBy.getParam().equalsIgnoreCase("dir"));
break;
default:
break;
}
Expand Down Expand Up @@ -365,7 +370,9 @@ public String toString() {
}

public enum Filters {
NAME("NAME", "i.inodePK.name LIKE CONCAT(:name, '%') ", "name", " "),//case
NAME("NAME", "LOWER(i.inodePK.name) LIKE LOWER(CONCAT(:name, '%')) ", "name", " "),
NAME_NEQ("NAME_NEQ", "LOWER(i.inodePK.name) NOT LIKE LOWER(CONCAT(:name_not_eq, '%')) ", "name_not_eq", " "),
NAME_LIKE("NAME_LIKE", "LOWER(i.inodePK.name) LIKE LOWER(CONCAT('%', :name_like, '%')) ", "name_like", " "),
USER_EMAIL("USER_EMAIL", "i.hdfsUser =:user ", "user", " "),
HDFS_USER("HDFS_USER", "i.hdfsUser =:hdfsUser ", "hdfsUser", " "),
UNDER_CONSTRUCTION("UNDER_CONSTRUCTION", "i.underConstruction =:underConstruction ", "underConstruction", "true"),
Expand All @@ -377,6 +384,7 @@ public enum Filters {
ACCESS_TIME("ACCESS_TIME", "i.accessTime =:accessTime ", "accessTime", ""),
ACCESS_TIME_LT("ACCESS_TIME_LT", "i.accessTime <:accessTime_lt ", "accessTime_lt", ""),
ACCESS_TIME_GT("ACCESS_TIME_GT", "i.accessTime >:accessTime_gt ", "accessTime_gt", ""),
TYPE("TYPE", "i.dir =:is_dir ", "is_dir", "dir"),
SIZE("SIZE", "i.size =:size_eq ", "size_eq", "0"),
SIZE_LT("SIZE_LT", "i.size <:size_lt ", "size_lt", "1"),
SIZE_GT("SIZE_GT", "i.size >:size_gt ", "size_gt", "0");
Expand Down

0 comments on commit 4fe59b3

Please sign in to comment.