started to add searchFilter support
This commit is contained in:
@ -107,16 +107,7 @@ public abstract class AbstractController<T extends Serializable> extends CommonA
|
||||
*/
|
||||
@Lock(LockType.READ)
|
||||
public List<T> findAll(List<String> orderFields) {
|
||||
final CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||
final CriteriaQuery<T> criteria = cb.createQuery(entityClass);
|
||||
|
||||
final Root<T> r = criteria.from(entityClass);
|
||||
|
||||
List<Order> orderList = new ArrayList<>();
|
||||
orderFields.stream().forEachOrdered(field -> orderList.add(cb.asc(r.get(field))));
|
||||
final TypedQuery<T> query = em.createQuery(criteria.orderBy(orderList));
|
||||
|
||||
return query.getResultList();
|
||||
return findAll(entityClass, orderFields);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -90,6 +90,7 @@ public abstract class CommonAbstractController {
|
||||
final Root<T> r = criteria.from(entityClass);
|
||||
|
||||
if (EndDateable.class.isAssignableFrom(entityClass.getClass())) {
|
||||
List<DefaultSearchFilter> filterList = new ArrayList<>();
|
||||
Map<String, Object> filters = new HashMap<>();
|
||||
// FIXME - add filter to validFrom / validTo
|
||||
// need to support different conditions, currently only equals supported, need to support e.g. gt, lt, ...
|
||||
@ -282,7 +283,6 @@ public abstract class CommonAbstractController {
|
||||
em.refresh(entity);
|
||||
}
|
||||
|
||||
|
||||
/* *** filter methods *** */
|
||||
/**
|
||||
* Creates a filter condition.
|
||||
@ -308,10 +308,6 @@ public abstract class CommonAbstractController {
|
||||
* @return the created filter condition
|
||||
*/
|
||||
protected <T extends Serializable> Predicate getFilterCondition(CriteriaBuilder cb, Root<T> root, Map<String, Object> filters, Map<String, Object> excludeFilters) {
|
||||
// Predicate filterCondition = null;
|
||||
// filterCondition = getFilterCondition(filterCondition, cb, root, filters, true);
|
||||
// filterCondition = getFilterCondition(filterCondition, cb, root, excludeFilters, false);
|
||||
// return filterCondition;
|
||||
Predicate includeFilterCondition = getFilterCondition(null, cb, root, filters, true);
|
||||
Predicate excludeFilterCondition = getFilterCondition(null, cb, root, excludeFilters, false);
|
||||
|
||||
@ -422,6 +418,43 @@ public abstract class CommonAbstractController {
|
||||
return filterCondition;
|
||||
}
|
||||
|
||||
protected <T extends Serializable> Predicate getFilterCondition(CriteriaBuilder cb, Root<T> root, List<SearchFilter> searchFilter) throws ControllerException {
|
||||
return getFilterCondition(null, cb, root, searchFilter);
|
||||
}
|
||||
|
||||
protected <T extends Serializable> Predicate getFilterCondition(final Predicate filterCondition, CriteriaBuilder cb, Root<T> root, List<SearchFilter> searchFilter) throws ControllerException {
|
||||
Predicate returnCondition = filterCondition;
|
||||
for (SearchFilter currentFilter : searchFilter) {
|
||||
if (currentFilter.getComparator() == null) {
|
||||
throw new ControllerException(ControllerException.INTERNAL_ERROR, "Comparator must not be null");
|
||||
}
|
||||
if (currentFilter.getFieldName() == null) {
|
||||
throw new ControllerException(ControllerException.INTERNAL_ERROR, "Fieldname must not be null");
|
||||
}
|
||||
|
||||
Path<String> path = getPathElement(root, currentFilter);
|
||||
|
||||
if (currentFilter.getSearchValue() == null) {
|
||||
Predicate predicate;
|
||||
switch (currentFilter.getComparator()) {
|
||||
case EQUAL:
|
||||
predicate = cb.isNull(path);
|
||||
break;
|
||||
case NOT_EQUAL:
|
||||
predicate = cb.isNotNull(path);
|
||||
break;
|
||||
default:
|
||||
throw new ControllerException(ControllerException.INTERNAL_ERROR, currentFilter.getComparator() + " not supported for searchValue null");
|
||||
}
|
||||
returnCondition = addFilterCondition(cb, returnCondition, predicate);
|
||||
} else {
|
||||
throw new ControllerException (ControllerException.INTERNAL_ERROR, "Not yet implemented");
|
||||
}
|
||||
} // for all filters
|
||||
|
||||
return returnCondition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a filter condition to an existing condition
|
||||
*
|
||||
@ -455,6 +488,19 @@ public abstract class CommonAbstractController {
|
||||
return path;
|
||||
}
|
||||
|
||||
private <T extends Serializable> Path<String> getPathElement(Root<T> root, SearchFilter filter) {
|
||||
String[] pathElements = StringUtils.split(filter.getFieldName(), '.');
|
||||
Path<String> path = null;
|
||||
for (String element : pathElements) {
|
||||
if (path == null) {
|
||||
path = root.get(element);
|
||||
} else {
|
||||
path = path.get(element);
|
||||
}
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns null, if the list is empty or null itself.Returns the one element if there is exactly one element in the list. Otherwise an exception
|
||||
* is thrown
|
||||
|
||||
30
db/src/main/java/de/muehlencord/shared/db/Comparator.java
Normal file
30
db/src/main/java/de/muehlencord/shared/db/Comparator.java
Normal file
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package de.muehlencord.shared.db;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Joern Muehlencord (joern@muehlencord.de)
|
||||
*/
|
||||
public enum Comparator {
|
||||
|
||||
EQUAL,
|
||||
NOT_EQUAL,
|
||||
CONTAINS;
|
||||
|
||||
|
||||
|
||||
}
|
||||
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package de.muehlencord.shared.db;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Joern Muehlencord (joern@muehlencord.de)
|
||||
*/
|
||||
public class DefaultSearchFilter implements SearchFilter {
|
||||
|
||||
private final String fieldName;
|
||||
private final Comparator comparator;
|
||||
private final Object searchValue;
|
||||
|
||||
public DefaultSearchFilter(String fieldName, Comparator comparator, Object fieldValue) {
|
||||
this.fieldName = fieldName;
|
||||
this.comparator = comparator;
|
||||
this.searchValue = fieldValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFieldName() {
|
||||
return fieldName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Comparator getComparator() {
|
||||
return comparator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getSearchValue() {
|
||||
return searchValue;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
30
db/src/main/java/de/muehlencord/shared/db/SearchFilter.java
Normal file
30
db/src/main/java/de/muehlencord/shared/db/SearchFilter.java
Normal file
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright 2019 Joern Muehlencord (joern@muehlencord.de).
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package de.muehlencord.shared.db;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Joern Muehlencord (joern@muehlencord.de)
|
||||
*/
|
||||
public interface SearchFilter {
|
||||
|
||||
Comparator getComparator();
|
||||
|
||||
String getFieldName();
|
||||
|
||||
Object getSearchValue();
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user