From 6d9a87e15bd7a914c3c5f354705b087e278c615d Mon Sep 17 00:00:00 2001 From: Joern Muehlencord Date: Fri, 27 Dec 2019 14:38:24 +0100 Subject: [PATCH] added basic or clause support --- .gitignore | 10 ++- .../shared/db/CommonAbstractController.java | 47 ++++++++++--- .../shared/db/DefaultSearchFilter.java | 67 ++++++++++++------- .../de/muehlencord/shared/db/Operator.java | 26 +++++++ 4 files changed, 115 insertions(+), 35 deletions(-) create mode 100644 db/src/main/java/de/muehlencord/shared/db/Operator.java diff --git a/.gitignore b/.gitignore index 53f619e..4766366 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ nbdist/ nbactions.xml **/nb-configuration.xml .nb-gradle/ +**/faces-config.NavData # ---> Eclipse .project @@ -24,6 +25,11 @@ dependency-reduced-pom.xml buildNumber.properties .mvn/timing.properties + +# --> Idea +**/.idea +**/*.iml + # ---> Java *.class @@ -178,6 +184,8 @@ sympy-plots-for-*.tex/ # localized versions of JBOSS command line interface *.local.cli -/source/office-parent/office-web/faces-config.NavData + + +## project specific /source/vvh-access-import/src/main/resources/hibernate.cfg.xml /source/office-parent/office-entities/src/main/resources/META-INF/persistence.xml 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 b34cee5..4b0c80b 100644 --- a/db/src/main/java/de/muehlencord/shared/db/CommonAbstractController.java +++ b/db/src/main/java/de/muehlencord/shared/db/CommonAbstractController.java @@ -203,8 +203,7 @@ public abstract class CommonAbstractController { * updates the audit field of the entity class. * * @param audit the audit to apply - * @param onCreate specifies, whether the the update should be applied for a new entity or not. If yes, then also the createdBy /createdOn fields - * are updated. Otherwise these fields are skipped. + * @param onCreate specifies, whether the the update should be applied for a new entity or not. If yes, then also the createdBy /createdOn fields are updated. Otherwise these fields are skipped. * @param changedBy the username to apply * @return an updated audit object to use for the updated entity. * @throws ControllerException if the audit object cannot be updated @@ -334,8 +333,7 @@ public abstract class CommonAbstractController { } /** - * Deletes an entity from the database. If the entity implements the {@link EndDateable} interface, the entity is not deleted but marked as deleted - * (end date set). + * Deletes an entity from the database. If the entity implements the {@link EndDateable} interface, the entity is not deleted but marked as deleted (end date set). * * @param the type of the entity to handle * @param entity the entity to delete @@ -423,8 +421,7 @@ public abstract class CommonAbstractController { * @param cb the criteria builder to use * @param root the root of the object to search for * @param filters the filters to apply - * @param include if set to true, the filter is used as include filter (equals, in). If set to false, the filter is inverted and used as exclude - * filter (not equals, not in etc) + * @param include if set to true, the filter is used as include filter (equals, in). If set to false, the filter is inverted and used as exclude filter (not equals, not in etc) * @return the created filter condition */ protected Predicate getFilterCondition(Predicate filterCondition, CriteriaBuilder cb, Root root, Map filters, boolean include) { @@ -559,6 +556,7 @@ public abstract class CommonAbstractController { } returnCondition = addFilterCondition(cb, returnCondition, predicate); } else if (currentFilter.getSearchValue() instanceof Date) { + // TODO - support Date, LocaleDate, LocalTime, LocalDateTime, ZoneDateTime Date searchValue = (Date) currentFilter.getSearchValue(); Predicate predicate; Path datePath = root.get(currentFilter.getFieldName()); @@ -615,6 +613,20 @@ public abstract class CommonAbstractController { throw new ControllerException(ControllerException.INTERNAL_ERROR, currentFilter.getComparator() + "not support for searchValue " + searchValue); } returnCondition = addFilterCondition(cb, returnCondition, predicate); + } else if (currentFilter.getSearchValue() instanceof List) { + List searchValues = (List) currentFilter.getSearchValue(); + Predicate predicate = null; + if (!searchValues.isEmpty()) { + for (Object obj : searchValues) { + SearchFilter sf = new DefaultSearchFilter(currentFilter.getFieldName(), currentFilter.getComparator(), obj); + List localFilterList = new ArrayList<>(); + localFilterList.add(sf); + Predicate localPredicate = getFilterCondition(null, cb, root, localFilterList); + predicate = orFilterCondition(cb, predicate, localPredicate); + } + returnCondition = addFilterCondition(cb, returnCondition, predicate); + } +// Path } else { throw new ControllerException(ControllerException.INTERNAL_ERROR, "Filter for " + currentFilter.getSearchValue().getClass().getSimpleName() + " not yet implemented"); } @@ -643,6 +655,26 @@ public abstract class CommonAbstractController { return filterCondition; } + /** + * Adds a filter condition to an existing condition + * + * @param cb the builder to use + * @param filterCondition the existing filter condition + * @param orCondition the condition to add to the existing condition. + * @return an updated filter condition. + */ + protected Predicate orFilterCondition(CriteriaBuilder cb, Predicate filterCondition, Predicate orCondition) { + if (orCondition == null) { + return filterCondition; + } + if (filterCondition == null) { + filterCondition = orCondition; + } else { + filterCondition = cb.or(filterCondition, orCondition); + } + return filterCondition; + } + private Path getPathElement(Root root, Map.Entry filter) { String[] pathElements = StringUtils.split(filter.getKey(), '.'); Path path = null; @@ -670,8 +702,7 @@ public abstract class CommonAbstractController { } /** - * 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 + * 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 * * @param the type of the entity to handle * @param entityList the list to validate diff --git a/db/src/main/java/de/muehlencord/shared/db/DefaultSearchFilter.java b/db/src/main/java/de/muehlencord/shared/db/DefaultSearchFilter.java index 05d08dd..86f19c0 100644 --- a/db/src/main/java/de/muehlencord/shared/db/DefaultSearchFilter.java +++ b/db/src/main/java/de/muehlencord/shared/db/DefaultSearchFilter.java @@ -15,39 +15,54 @@ */ package de.muehlencord.shared.db; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + /** * * @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; - } + private final String fieldName; + private final Comparator comparator; + private final Object searchValue; + private final Operator operator; - @Override - public String getFieldName() { - return fieldName; - } + public DefaultSearchFilter(String fieldName, Comparator comparator, Object fieldValue) { + this.fieldName = fieldName; + this.comparator = comparator; + this.searchValue = fieldValue; + this.operator = null; + } - @Override - public Comparator getComparator() { - return comparator; - } + public DefaultSearchFilter(String fieldName, Comparator comparator, Operator operator, Object... fieldValue) { + this.fieldName = fieldName; + this.comparator = comparator; + List searchValues = new ArrayList(); + searchValues.addAll(Arrays.asList(fieldValue)); + this.searchValue = searchValues; + this.operator = operator; + } + + @Override + public String getFieldName() { + return fieldName; + } + + @Override + public Comparator getComparator() { + return comparator; + } + + @Override + public Object getSearchValue() { + return searchValue; + } + + public Operator getOperator() { + return operator; + } - @Override - public Object getSearchValue() { - return searchValue; - } - - - - - } diff --git a/db/src/main/java/de/muehlencord/shared/db/Operator.java b/db/src/main/java/de/muehlencord/shared/db/Operator.java new file mode 100644 index 0000000..0404372 --- /dev/null +++ b/db/src/main/java/de/muehlencord/shared/db/Operator.java @@ -0,0 +1,26 @@ +/* + * 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 Operator { + + AND, OR; + +}