This commit is contained in:
Francesco
2023-09-26 16:38:42 +02:00
parent ca657340c0
commit ca1931cda6
11 changed files with 202 additions and 166 deletions

View File

@@ -16,4 +16,5 @@ import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Filterable {
public String type() default "";
}

View File

@@ -19,7 +19,6 @@ import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

View File

@@ -90,7 +90,7 @@ public class AdvancedJpaRepository extends SimpleJpaRepository {
.collect(Collectors.toList());
List<Predicate> queryPredicates = new ArrayList<>();
if (q != null) {
if (q != null && !q.isBlank()) {
for (DbField f : stringFields) {
Path path = root.get(f.getJavaName());
queryPredicates.add(cb.like(cb.lower(cb.toString(path)), "%" + q.toLowerCase() + "%"));
@@ -118,15 +118,15 @@ public class AdvancedJpaRepository extends SimpleJpaRepository {
);
} else if (op == CompareOperator.EQ) {
finalPredicates.add(
cb.equal(root.get(field), value)
cb.equal(root.get(field), Double.parseDouble(value.toString()))
);
} else if (op == CompareOperator.GT) {
finalPredicates.add(
cb.greaterThan(root.get(field), value.toString())
cb.greaterThan(root.get(field), Double.parseDouble(value.toString()))
);
} else if (op == CompareOperator.LT) {
finalPredicates.add(
cb.lessThan(root.get(field), value.toString())
cb.lessThan(root.get(field), Double.parseDouble(value.toString()))
);
} else if (op == CompareOperator.AFTER) {
if (value instanceof LocalDate)

View File

@@ -1,10 +1,15 @@
package tech.ailef.dbadmin.external.dbmapping;
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import com.fasterxml.jackson.annotation.JsonIgnore;
import tech.ailef.dbadmin.external.annotations.DisplayImage;
import tech.ailef.dbadmin.external.annotations.Filterable;
public class DbField {
protected String dbName;
@@ -128,6 +133,25 @@ public class DbField {
return type == DbFieldType.TEXT;
}
public boolean isFilterable() {
return getPrimitiveField().getAnnotation(Filterable.class) != null;
}
public boolean isFilterableCategorical() {
Filterable filterable = getPrimitiveField().getAnnotation(Filterable.class);
return filterable != null && filterable.type().equalsIgnoreCase("categorical");
}
public Set<DbFieldValue> getAllValues() {
List findAll = schema.getJpaRepository().findAll();
Set<DbFieldValue> allValues = new HashSet<>();
for (Object o : findAll) {
DbFieldValue val = new DbObject(o, schema).get(this);
allValues.add(val);
}
return allValues;
}
@Override
public String toString() {
return "DbField [name=" + dbName + ", javaName=" + javaName + ", type=" + type + ", field=" + field
@@ -135,6 +159,23 @@ public class DbField {
+ ", schema=" + schema.getClassName() + "]";
}
@Override
public int hashCode() {
return Objects.hash(dbName, type);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
DbField other = (DbField) obj;
return Objects.equals(dbName, other.dbName) && type == other.type;
}
}

View File

@@ -1,5 +1,7 @@
package tech.ailef.dbadmin.external.dbmapping;
import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonIgnore;
public class DbFieldValue {
@@ -39,6 +41,22 @@ public class DbFieldValue {
public String toString() {
return "DbFieldValue [value=" + value + ", field=" + field + "]";
}
@Override
public int hashCode() {
return Objects.hash(field, value);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
DbFieldValue other = (DbFieldValue) obj;
return Objects.equals(field, other.field) && Objects.equals(value, other.value);
}
}

View File

@@ -7,6 +7,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
@@ -18,7 +19,6 @@ import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;
import tech.ailef.dbadmin.external.DbAdmin;
import tech.ailef.dbadmin.external.annotations.ComputedColumn;
import tech.ailef.dbadmin.external.annotations.Filterable;
import tech.ailef.dbadmin.external.exceptions.DbAdminException;
import tech.ailef.dbadmin.external.misc.Utils;
@@ -182,13 +182,41 @@ public class DbObjectSchema {
public List<DbField> getFilterableFields() {
return getSortedFields().stream().filter(f -> {
return !f.isBinary() && !f.isPrimaryKey()
&& f.getPrimitiveField().getAnnotation(Filterable.class) != null;
&& f.isFilterable();
}).toList();
}
public List<DbObject> findAll() {
List r = jpaRepository.findAll();
List<DbObject> results = new ArrayList<>();
for (Object o : r) {
results.add(new DbObject(o, this));
}
return results;
}
@Override
public String toString() {
return "DbObjectSchema [fields=" + fields + ", className=" + entityClass.getName() + "]";
}
@Override
public int hashCode() {
return Objects.hash(tableName);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
DbObjectSchema other = (DbObjectSchema) obj;
return Objects.equals(tableName, other.tableName);
}
}