Skip to content

Commit

Permalink
feat: hide programmatic fields in filterfield
Browse files Browse the repository at this point in the history
  • Loading branch information
Idane committed Feb 17, 2023
1 parent f3af8a4 commit 9802af5
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public <ID extends Serializable, Entity extends BaseCrudEntity<ID>> void decorat
public <ID extends Serializable, Entity extends BaseCrudEntity<ID>> void validateAndFillFilterFieldMetadata(List<FilterField> filterFields, Class<Entity> entityClazz) {
EntityMetadataDTO metadataDTO = getEntityMetadata(entityClazz);
for (FilterField filterField : filterFields) {
if (filterField.isValidated()) {
if (filterField.validated()) {
continue;
}
if (filterField.getOperation() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import dev.krud.crudframework.modelfilter.enums.FilterFieldDataType;
import dev.krud.crudframework.modelfilter.enums.FilterFieldOperation;

import jakarta.annotation.Nullable;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;
Expand All @@ -16,19 +18,24 @@
*/
public class FilterField implements Serializable {

@Nullable
private String fieldName;

private FilterFieldOperation operation;

@Nullable
private FilterFieldDataType dataType;

@Nullable
private String enumType;

@Nullable
private Object[] values;

@Nullable
private List<FilterField> children;

private transient boolean validated = false;
private boolean validated = false;

public FilterField() {
}
Expand Down Expand Up @@ -63,11 +70,12 @@ public FilterField(String fieldName, FilterFieldOperation operation, String enum
this.values = values;
}

@Nullable
public String getFieldName() {
return fieldName;
}

public void setFieldName(String fieldName) {
public void setFieldName(@Nullable String fieldName) {
this.fieldName = fieldName;
}

Expand All @@ -79,62 +87,66 @@ public void setOperation(FilterFieldOperation operation) {
this.operation = operation;
}

public FilterFieldDataType getDataType() {
@Nullable
public FilterFieldDataType getDataType() {
return dataType;
}

public void setDataType(FilterFieldDataType dataType) {
public void setDataType(@Nullable FilterFieldDataType dataType) {
this.dataType = dataType;
}

public String getEnumType() {
@Nullable
public String getEnumType() {
return enumType;
}

public void setEnumType(String enumType) {
public void setEnumType(@Nullable String enumType) {
this.enumType = enumType;
}

public boolean isValidated() {
public boolean validated() {
return validated;
}

public Object[] getValues() {
@Nullable
public Object[] getValues() {
if(values != null) {
return Arrays.stream(values).map(x -> castToType(x)).collect(Collectors.toList()).toArray();
}

return null;
}

public void setValues(Object[] values) {
public void setValues(@Nullable Object[] values) {
this.values = values;
}

public Object getValue1() {
@Nullable
public List<FilterField> getChildren() {
return children;
}

public void setChildren(@Nullable List<FilterField> children) {
this.children = children;
}

public Object value1() {
if(values == null || values.length == 0) {
return null;
}

return castToType(values[0]);
}

public Object getValue2() {
public Object value2() {
if(values == null || values.length < 2) {
return null;
}

return castToType(values[1]);
}

public List<FilterField> getChildren() {
return children;
}

public void setChildren(List<FilterField> children) {
this.children = children;
}

public void validate() {
this.validated = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,46 +58,46 @@ fun FilterField.filtersMatch(target: Any): Boolean {
}
when (this.operation) {
FilterFieldOperation.Equal -> {
return actualValue == this.value1
return actualValue == this.value1()
}
FilterFieldOperation.NotEqual -> {
return actualValue != this.value1
return actualValue != this.value1()
}
FilterFieldOperation.In -> {
return this.value1 in actualValue as Collection<*>
return this.value1() in actualValue as Collection<*>
}
FilterFieldOperation.NotIn -> {
return this.value1 !in actualValue as Collection<*>
return this.value1() !in actualValue as Collection<*>
}
FilterFieldOperation.GreaterThan -> {
actualValue as Comparable<Any>
val value = this.value1 as Comparable<Any>
val value = this.value1() as Comparable<Any>
return actualValue > value
}
FilterFieldOperation.GreaterEqual -> {
actualValue as Comparable<Any>
val value = this.value1 as Comparable<Any>
val value = this.value1() as Comparable<Any>
return actualValue >= value
}
FilterFieldOperation.LowerThan -> {
actualValue as Comparable<Any>
val value = this.value1 as Comparable<Any>
val value = this.value1() as Comparable<Any>
return actualValue < value
}
FilterFieldOperation.LowerEqual -> {
actualValue as Comparable<Any>
val value = this.value1 as Comparable<Any>
val value = this.value1() as Comparable<Any>
return actualValue <= value
}
FilterFieldOperation.Between -> {
actualValue as Comparable<Any>
val value1 = this.value1 as Comparable<Any>
val value2 = this.value2 as Comparable<Any>
val value1 = this.value1() as Comparable<Any>
val value2 = this.value2() as Comparable<Any>
return actualValue >= value1 && actualValue < value2
}
FilterFieldOperation.Contains -> {
actualValue as String
val value = this.value1 as String
val value = this.value1() as String
return actualValue.contains(value)
}
FilterFieldOperation.IsNull -> {
Expand All @@ -107,11 +107,11 @@ fun FilterField.filtersMatch(target: Any): Boolean {
return actualValue != null
}
FilterFieldOperation.IsEmpty -> {
val value = this.value1 as Collection<*>
val value = this.value1() as Collection<*>
return value.isEmpty()
}
FilterFieldOperation.IsNotEmpty -> {
val value = this.value1 as Collection<*>
val value = this.value1() as Collection<*>
return value.isNotEmpty()
}
FilterFieldOperation.And -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,59 +91,59 @@ class JpaDaoImpl : CrudDao {
private fun CriteriaBuilder.processFilterField(filterField: FilterField, root: Root<*>): Predicate {
val predicate: Predicate = when (filterField.operation) {
FilterFieldOperation.Equal -> {
equal(root.getExpressionByFieldName(filterField.fieldName), filterField.value1)
equal(root.getExpressionByFieldName(filterField.fieldName), filterField.value1())
}

FilterFieldOperation.NotEqual -> {
notEqual(root.getExpressionByFieldName(filterField.fieldName), filterField.value1)
notEqual(root.getExpressionByFieldName(filterField.fieldName), filterField.value1())
}

FilterFieldOperation.In -> {
`in`(root.getExpressionByFieldName(filterField.fieldName)).value(filterField.value1)
`in`(root.getExpressionByFieldName(filterField.fieldName)).value(filterField.value1())
}

FilterFieldOperation.NotIn -> {
not(`in`(root.getExpressionByFieldName(filterField.fieldName)).value(filterField.value1))
not(`in`(root.getExpressionByFieldName(filterField.fieldName)).value(filterField.value1()))
}

FilterFieldOperation.GreaterThan -> {
greaterThan(
root.getExpressionByFieldName(filterField.fieldName) as Expression<out Comparable<Any>>,
filterField.value1 as Comparable<Any>
filterField.value1() as Comparable<Any>
);
}

FilterFieldOperation.GreaterEqual -> {
greaterThanOrEqualTo(
root.getExpressionByFieldName(filterField.fieldName) as Expression<out Comparable<Any>>,
filterField.value1 as Comparable<Any>
filterField.value1() as Comparable<Any>
)
}

FilterFieldOperation.LowerThan -> {
lessThan(
root.getExpressionByFieldName(filterField.fieldName) as Expression<out Comparable<Any>>,
filterField.value1 as Comparable<Any>
filterField.value1() as Comparable<Any>
)
}

FilterFieldOperation.LowerEqual -> {
lessThanOrEqualTo(
root.getExpressionByFieldName(filterField.fieldName) as Expression<out Comparable<Any>>,
filterField.value1 as Comparable<Any>
filterField.value1() as Comparable<Any>
)
}

FilterFieldOperation.Between -> {
between(
root.getExpressionByFieldName(filterField.fieldName) as Expression<out Comparable<Any>>,
filterField.value1 as Comparable<Any>,
filterField.value2 as Comparable<Any>
filterField.value1() as Comparable<Any>,
filterField.value2() as Comparable<Any>
)
}

FilterFieldOperation.Contains -> {
like(root.getExpressionByFieldName(filterField.fieldName) as Path<String>, "%${filterField.value1}%")
like(root.getExpressionByFieldName(filterField.fieldName) as Path<String>, "%${filterField.value1()}%")
}

FilterFieldOperation.IsNull -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ private Criteria buildCriterion(Criteria criteria, FilterField filterField) {

switch(filterField.getOperation()) {
case Equal:
criteria.and(filterField.getFieldName()).is(filterField.getValue1());
criteria.and(filterField.getFieldName()).is(filterField.value1());
break;
case NotEqual:
criteria.and(filterField.getFieldName()).ne(filterField.getValue1());
criteria.and(filterField.getFieldName()).ne(filterField.value1());
break;
case In:
criteria.and(filterField.getFieldName()).in(filterField.getValues());
Expand All @@ -125,26 +125,26 @@ private Criteria buildCriterion(Criteria criteria, FilterField filterField) {
criteria.and(filterField.getFieldName()).nin(filterField.getValues());
break;
case GreaterEqual:
criteria.and(filterField.getFieldName()).gte(filterField.getValue1());
criteria.and(filterField.getFieldName()).gte(filterField.value1());
break;
case GreaterThan:
criteria.and(filterField.getFieldName()).gt(filterField.getValue1());
criteria.and(filterField.getFieldName()).gt(filterField.value1());
break;
case LowerEqual:
criteria.and(filterField.getFieldName()).lte(filterField.getValue1());
criteria.and(filterField.getFieldName()).lte(filterField.value1());
break;
case LowerThan:
criteria.and(filterField.getFieldName()).lt(filterField.getValue1());
criteria.and(filterField.getFieldName()).lt(filterField.value1());
break;
case Between:
criteria.and(filterField.getFieldName())
.gte(filterField.getValue1())
.gte(filterField.value1())
.and(filterField.getFieldName())
.lte(filterField.getValue2());
.lte(filterField.value2());
break;
case Contains:
if(filterField.getValue1() != null && !filterField.getValue1().toString().trim().isEmpty()) {
criteria.and(filterField.getFieldName()).regex(Pattern.compile(filterField.getValue1().toString(), Pattern.LITERAL & Pattern.CASE_INSENSITIVE));
if(filterField.value1() != null && !filterField.value1().toString().trim().isEmpty()) {
criteria.and(filterField.getFieldName()).regex(Pattern.compile(filterField.value1().toString(), Pattern.LITERAL & Pattern.CASE_INSENSITIVE));
}
break;
case IsNull:
Expand Down

0 comments on commit 9802af5

Please sign in to comment.