From 113cb4a6416ae649665967cecb37ac4b637f18f3 Mon Sep 17 00:00:00 2001 From: Joern Muehlencord Date: Fri, 4 Oct 2019 13:50:22 +0200 Subject: [PATCH] started to add searchFilter support --- .../shared/db/AbstractController.java | 11 +--- .../shared/db/CommonAbstractController.java | 56 +++++++++++++++++-- .../de/muehlencord/shared/db/Comparator.java | 30 ++++++++++ .../shared/db/DefaultSearchFilter.java | 53 ++++++++++++++++++ .../muehlencord/shared/db/SearchFilter.java | 30 ++++++++++ 5 files changed, 165 insertions(+), 15 deletions(-) create mode 100644 db/src/main/java/de/muehlencord/shared/db/Comparator.java create mode 100644 db/src/main/java/de/muehlencord/shared/db/DefaultSearchFilter.java create mode 100644 db/src/main/java/de/muehlencord/shared/db/SearchFilter.java diff --git a/db/src/main/java/de/muehlencord/shared/db/AbstractController.java b/db/src/main/java/de/muehlencord/shared/db/AbstractController.java index 175074c..cdf9212 100644 --- a/db/src/main/java/de/muehlencord/shared/db/AbstractController.java +++ b/db/src/main/java/de/muehlencord/shared/db/AbstractController.java @@ -107,16 +107,7 @@ public abstract class AbstractController extends CommonA */ @Lock(LockType.READ) public List findAll(List orderFields) { - final CriteriaBuilder cb = em.getCriteriaBuilder(); - final CriteriaQuery criteria = cb.createQuery(entityClass); - - final Root r = criteria.from(entityClass); - - List orderList = new ArrayList<>(); - orderFields.stream().forEachOrdered(field -> orderList.add(cb.asc(r.get(field)))); - final TypedQuery query = em.createQuery(criteria.orderBy(orderList)); - - return query.getResultList(); + return findAll(entityClass, orderFields); } /** diff --git a/db/src/main/java/de/muehlencord/shared/db/CommonAbstractController.java b/db/src/main/java/de/muehlencord/shared/db/CommonAbstractController.java index 404e3c6..80cdff2 100644 --- a/db/src/main/java/de/muehlencord/shared/db/CommonAbstractController.java +++ b/db/src/main/java/de/muehlencord/shared/db/CommonAbstractController.java @@ -90,6 +90,7 @@ public abstract class CommonAbstractController { final Root r = criteria.from(entityClass); if (EndDateable.class.isAssignableFrom(entityClass.getClass())) { + List filterList = new ArrayList<>(); Map 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 Predicate getFilterCondition(CriteriaBuilder cb, Root root, Map filters, Map 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 Predicate getFilterCondition(CriteriaBuilder cb, Root root, List searchFilter) throws ControllerException { + return getFilterCondition(null, cb, root, searchFilter); + } + + protected Predicate getFilterCondition(final Predicate filterCondition, CriteriaBuilder cb, Root root, List 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 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 Path getPathElement(Root root, SearchFilter filter) { + String[] pathElements = StringUtils.split(filter.getFieldName(), '.'); + Path 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 diff --git a/db/src/main/java/de/muehlencord/shared/db/Comparator.java b/db/src/main/java/de/muehlencord/shared/db/Comparator.java new file mode 100644 index 0000000..bed3f61 --- /dev/null +++ b/db/src/main/java/de/muehlencord/shared/db/Comparator.java @@ -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; + + + +} diff --git a/db/src/main/java/de/muehlencord/shared/db/DefaultSearchFilter.java b/db/src/main/java/de/muehlencord/shared/db/DefaultSearchFilter.java new file mode 100644 index 0000000..05d08dd --- /dev/null +++ b/db/src/main/java/de/muehlencord/shared/db/DefaultSearchFilter.java @@ -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; + } + + + + + +} diff --git a/db/src/main/java/de/muehlencord/shared/db/SearchFilter.java b/db/src/main/java/de/muehlencord/shared/db/SearchFilter.java new file mode 100644 index 0000000..3c426bb --- /dev/null +++ b/db/src/main/java/de/muehlencord/shared/db/SearchFilter.java @@ -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(); + +}