Skip to content

Commit

Permalink
feat: reafactoring dao
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejpetras committed Dec 17, 2024
1 parent 9835345 commit 2dbe52a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 63 deletions.
57 changes: 13 additions & 44 deletions src/main/java/org/tkit/onecx/parameters/domain/daos/HistoryDAO.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.tkit.onecx.parameters.domain.daos;

import static org.tkit.quarkus.jpa.utils.QueryCriteriaUtil.addSearchStringPredicate;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -12,7 +14,6 @@
import org.tkit.onecx.parameters.domain.models.History;
import org.tkit.onecx.parameters.domain.models.HistoryCountTuple;
import org.tkit.onecx.parameters.domain.models.History_;
import org.tkit.onecx.parameters.domain.models.Parameter_;
import org.tkit.quarkus.jpa.daos.AbstractDAO;
import org.tkit.quarkus.jpa.daos.Page;
import org.tkit.quarkus.jpa.daos.PageResult;
Expand Down Expand Up @@ -44,20 +45,13 @@ public PageResult<History> searchByCriteria(HistorySearchCriteria criteria) {
List<Predicate> predicates = new ArrayList<>();
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();

if (criteria.getProductName() != null && !criteria.getProductName().isEmpty()) {
predicates.add(cb.like(cb.lower(root.get(Parameter_.PRODUCT_NAME)),
stringPattern(criteria.getProductName())));
}
addSearchStringPredicate(predicates, cb, root.get(History_.PRODUCT_NAME), criteria.getProductName());
addSearchStringPredicate(predicates, cb, root.get(History_.APPLICATION_ID), criteria.getApplicationId());
addSearchStringPredicate(predicates, cb, root.get(History_.NAME), criteria.getName());

if (criteria.getApplicationId() != null && !criteria.getApplicationId().isEmpty()) {
predicates.add(cb.like(cb.lower(root.get(Parameter_.APPLICATION_ID)),
stringPattern(criteria.getApplicationId())));
}
if (criteria.getName() != null && !criteria.getName().isEmpty()) {
predicates.add(cb.like(cb.lower(root.get(History_.NAME)), stringPattern(criteria.getName())));
}
if (!criteria.getType().isEmpty()) {
predicates.add(cb.lower(root.get(History_.TYPE)).in(toLowerCase(criteria.getType())));
var items = criteria.getType().stream().map(String::toLowerCase).toList();
predicates.add(cb.lower(root.get(History_.TYPE)).in(items));
}
if (!predicates.isEmpty()) {
cq.where(cb.and(predicates.toArray(new Predicate[0])));
Expand All @@ -83,18 +77,9 @@ public PageResult<History> searchOnlyLatestByCriteria(

List<Predicate> predicates = new ArrayList<>();
predicates.add(root.get(AbstractTraceableEntity_.CREATION_DATE).in(maxDateSubquery));

if (criteria.getProductName() != null && !criteria.getProductName().isEmpty()) {
predicates.add(cb.equal(cb.lower(root.get(Parameter_.PRODUCT_NAME)),
criteria.getProductName().toLowerCase()));
}
if (criteria.getApplicationId() != null && !criteria.getApplicationId().isEmpty()) {
predicates.add(cb.equal(cb.lower(root.get(Parameter_.APPLICATION_ID)),
criteria.getApplicationId().toLowerCase()));
}
if (criteria.getName() != null && !criteria.getName().isEmpty()) {
predicates.add(cb.equal(cb.lower(root.get(History_.NAME)), criteria.getName().toLowerCase()));
}
addSearchStringPredicate(predicates, cb, root.get(History_.PRODUCT_NAME), criteria.getProductName());
addSearchStringPredicate(predicates, cb, root.get(History_.APPLICATION_ID), criteria.getApplicationId());
addSearchStringPredicate(predicates, cb, root.get(History_.NAME), criteria.getName());

cq.select(root)
.where(cb.and(predicates.toArray(new Predicate[0])))
Expand All @@ -116,18 +101,10 @@ public List<HistoryCountTuple> searchCountsByCriteria(HistorySearchCriteria crit
root.get(History_.COUNT)));
List<Predicate> predicates = new ArrayList<>();

if (criteria.getProductName() != null && !criteria.getProductName().isEmpty()) {
predicates.add(cb.like(cb.lower(root.get(Parameter_.PRODUCT_NAME)),
stringPattern(criteria.getProductName())));
}
addSearchStringPredicate(predicates, cb, root.get(History_.PRODUCT_NAME), criteria.getProductName());
addSearchStringPredicate(predicates, cb, root.get(History_.APPLICATION_ID), criteria.getApplicationId());
addSearchStringPredicate(predicates, cb, root.get(History_.NAME), criteria.getName());

if (criteria.getApplicationId() != null && !criteria.getApplicationId().isEmpty()) {
predicates.add(cb.like(cb.lower(root.get(Parameter_.APPLICATION_ID)),
stringPattern(criteria.getApplicationId())));
}
if (criteria.getName() != null && !criteria.getName().isEmpty()) {
predicates.add(cb.like(cb.lower(root.get(History_.NAME)), stringPattern(criteria.getName())));
}
if (!predicates.isEmpty()) {
cq.where(cb.and(predicates.toArray(new Predicate[0])));
}
Expand All @@ -137,14 +114,6 @@ public List<HistoryCountTuple> searchCountsByCriteria(HistorySearchCriteria crit
}
}

private static String stringPattern(String value) {
return (value.toLowerCase() + "%");
}

private static List<String> toLowerCase(List<String> value) {
return value.stream().map(String::toLowerCase).toList();
}

public enum ErrorKeys {
DELETE_PARAMETER_HISTORY_OLDER_THAN_FAILED,
FIND_ALL_PARAMETERS_HISTORY_FAILED;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.tkit.onecx.parameters.domain.daos;

import static org.tkit.quarkus.jpa.utils.QueryCriteriaUtil.addSearchStringPredicate;

import java.util.*;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -46,22 +48,14 @@ public PageResult<Parameter> searchByCriteria(ParameterSearchCriteria criteria)
try {
CriteriaQuery<Parameter> cq = criteriaQuery();
Root<Parameter> root = cq.from(Parameter.class);
List<Predicate> predicates = new ArrayList<>();
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
if (criteria.getProductName() != null && !criteria.getProductName().isEmpty()) {
predicates.add(cb.like(cb.lower(root.get(Parameter_.PRODUCT_NAME)),
stringPattern(criteria.getProductName())));
}
if (criteria.getApplicationId() != null && !criteria.getApplicationId().isEmpty()) {
predicates.add(cb.like(cb.lower(root.get(Parameter_.APPLICATION_ID)),
stringPattern(criteria.getApplicationId())));
}
if (criteria.getName() != null && !criteria.getName().isEmpty()) {
predicates.add(cb.like(cb.lower(root.get(Parameter_.NAME)), stringPattern(criteria.getName())));
}
if (criteria.getDisplayName() != null && !criteria.getDisplayName().isEmpty()) {
predicates.add(cb.like(cb.lower(root.get(Parameter_.DISPLAY_NAME)), stringPattern(criteria.getDisplayName())));
}

List<Predicate> predicates = new ArrayList<>();
addSearchStringPredicate(predicates, cb, root.get(Parameter_.PRODUCT_NAME), criteria.getProductName());
addSearchStringPredicate(predicates, cb, root.get(Parameter_.APPLICATION_ID), criteria.getApplicationId());
addSearchStringPredicate(predicates, cb, root.get(Parameter_.NAME), criteria.getName());
addSearchStringPredicate(predicates, cb, root.get(Parameter_.DISPLAY_NAME), criteria.getDisplayName());

if (!predicates.isEmpty()) {
cq.where(cb.and(predicates.toArray(new Predicate[0])));
}
Expand Down Expand Up @@ -108,10 +102,6 @@ public List<ApplicationTuple> searchAllProductNamesAndApplicationIds() {
}
}

private static String stringPattern(String value) {
return (value.toLowerCase() + "%");
}

public enum ErrorKeys {

FIND_ALL_PARAMETERS_BY_APPLICATION_ID_FAILED,
Expand Down

0 comments on commit 2dbe52a

Please sign in to comment.