Skip to content

Commit

Permalink
feat: refactoring model (#81)
Browse files Browse the repository at this point in the history
* feat: rename ParameterHistory to History

* feat: rename history table

* feat: update liquibase script and database model

* feat: rename name to displayName

* feat: rename key to name

* feat: reafactoring dao
  • Loading branch information
andrejpetras authored Dec 17, 2024
1 parent 11f0c17 commit 6f4e7e3
Show file tree
Hide file tree
Showing 28 changed files with 359 additions and 386 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
@Getter
@Setter
public class ParameterHistorySearchCriteria {
public class HistorySearchCriteria {

/**
* The application ID.
Expand All @@ -23,9 +23,9 @@ public class ParameterHistorySearchCriteria {
private String productName;

/**
* The application parameter key.
* The parameter name.
*/
private String key;
private String name;

private List<String> type;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import lombok.Setter;

/**
* The application parameter search criteria.
* The parameter search criteria.
*/
@Getter
@Setter
Expand All @@ -21,12 +21,12 @@ public class ParameterSearchCriteria {
private String productName;

/**
* The application parameter key.
* The parameter key.
*/
private String key;

private String name;

private String displayName;

private Integer pageNumber;

private Integer pageSize;
Expand Down
121 changes: 121 additions & 0 deletions src/main/java/org/tkit/onecx/parameters/domain/daos/HistoryDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
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;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.persistence.criteria.*;
import jakarta.transaction.Transactional;

import org.tkit.onecx.parameters.domain.criteria.HistorySearchCriteria;
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.quarkus.jpa.daos.AbstractDAO;
import org.tkit.quarkus.jpa.daos.Page;
import org.tkit.quarkus.jpa.daos.PageResult;
import org.tkit.quarkus.jpa.exceptions.DAOException;
import org.tkit.quarkus.jpa.models.AbstractTraceableEntity_;
import org.tkit.quarkus.jpa.models.TraceableEntity_;

@ApplicationScoped
@Transactional(value = Transactional.TxType.NOT_SUPPORTED, rollbackOn = DAOException.class)
public class HistoryDAO extends AbstractDAO<History> {

@Transactional(value = Transactional.TxType.REQUIRED, rollbackOn = DAOException.class)
public void deleteApplicationHistoryOlderThan(LocalDateTime date) {
try {
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
CriteriaDelete<History> cd = deleteQuery();
Root<History> root = cd.from(History.class);
cd.where(cb.lessThanOrEqualTo(root.get(AbstractTraceableEntity_.CREATION_DATE), date));
getEntityManager().createQuery(cd).executeUpdate();
} catch (Exception e) {
throw new DAOException(ErrorKeys.DELETE_PARAMETER_HISTORY_OLDER_THAN_FAILED, e);
}
}

public PageResult<History> searchByCriteria(HistorySearchCriteria criteria) {
try {
CriteriaQuery<History> cq = criteriaQuery();
Root<History> root = cq.from(History.class);
List<Predicate> predicates = new ArrayList<>();
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();

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.getType().isEmpty()) {
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])));
}
cq.orderBy(cb.asc(root.get(AbstractTraceableEntity_.CREATION_DATE)));
return createPageQuery(cq, Page.of(criteria.getPageNumber(), criteria.getPageSize())).getPageResult();
} catch (Exception exception) {
throw new DAOException(ErrorKeys.FIND_ALL_PARAMETERS_HISTORY_FAILED, exception);
}
}

public PageResult<History> searchOnlyLatestByCriteria(
HistorySearchCriteria criteria) {
try {
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
CriteriaQuery<History> cq = cb.createQuery(History.class);
Root<History> root = cq.from(History.class);

Subquery<Number> maxDateSubquery = cq.subquery(Number.class);
Root<History> maxDateSubqueryRoot = maxDateSubquery.from(History.class);
maxDateSubquery.select(cb.max(maxDateSubqueryRoot.get(AbstractTraceableEntity_.CREATION_DATE)))
.groupBy(maxDateSubqueryRoot.get(History_.INSTANCE_ID));

List<Predicate> predicates = new ArrayList<>();
predicates.add(root.get(AbstractTraceableEntity_.CREATION_DATE).in(maxDateSubquery));
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])))
.groupBy(root.get(History_.INSTANCE_ID), root.get(TraceableEntity_.ID));

return createPageQuery(cq, Page.of(criteria.getPageNumber(), criteria.getPageSize())).getPageResult();
} catch (Exception exception) {
throw new DAOException(ErrorKeys.FIND_ALL_PARAMETERS_HISTORY_FAILED, exception);
}
}

public List<HistoryCountTuple> searchCountsByCriteria(HistorySearchCriteria criteria) {
try {
var cb = getEntityManager().getCriteriaBuilder();
CriteriaQuery<HistoryCountTuple> cq = cb.createQuery(HistoryCountTuple.class);
Root<History> root = cq.from(History.class);
cq.select(
cb.construct(HistoryCountTuple.class, root.get(AbstractTraceableEntity_.CREATION_DATE),
root.get(History_.COUNT)));
List<Predicate> predicates = new ArrayList<>();

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 (!predicates.isEmpty()) {
cq.where(cb.and(predicates.toArray(new Predicate[0])));
}
return em.createQuery(cq).getResultList();
} catch (Exception exception) {
throw new DAOException(ErrorKeys.FIND_ALL_PARAMETERS_HISTORY_FAILED, exception);
}
}

public enum ErrorKeys {
DELETE_PARAMETER_HISTORY_OLDER_THAN_FAILED,
FIND_ALL_PARAMETERS_HISTORY_FAILED;
}
}
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 @@ -34,7 +36,7 @@ public Map<String, String> findAllByProductNameAndApplicationId(String productNa
return getEntityManager()
.createQuery(cq)
.getResultStream()
.collect(Collectors.toMap(Parameter::getKey,
.collect(Collectors.toMap(Parameter::getName,
p -> p.getValue() != null ? p.getValue() : p.getImportValue()));

} catch (Exception e) {
Expand All @@ -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.getKey() != null && !criteria.getKey().isEmpty()) {
predicates.add(cb.like(cb.lower(root.get(Parameter_.KEY)), stringPattern(criteria.getKey())));
}
if (criteria.getName() != null && !criteria.getName().isEmpty()) {
predicates.add(cb.like(cb.lower(root.get(Parameter_.NAME)), stringPattern(criteria.getName())));
}

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 All @@ -76,7 +70,7 @@ public PageResult<String> searchAllKeys(KeysSearchCriteria criteria) {
var cb = getEntityManager().getCriteriaBuilder();
CriteriaQuery<String> cq = cb.createQuery(String.class);
Root<Parameter> root = cq.from(Parameter.class);
cq.select(root.get(Parameter_.KEY)).distinct(true);
cq.select(root.get(Parameter_.NAME)).distinct(true);

if (criteria.getProductName() != null && !criteria.getProductName().isEmpty()) {
cq.where(cb.equal(root.get(Parameter_.PRODUCT_NAME), criteria.getProductName()));
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

This file was deleted.

Loading

0 comments on commit 6f4e7e3

Please sign in to comment.