Skip to content

Commit

Permalink
add filter by cash flow type
Browse files Browse the repository at this point in the history
  • Loading branch information
vananiev committed Nov 18, 2024
1 parent c856110 commit 2bd4864
Show file tree
Hide file tree
Showing 22 changed files with 106 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import jakarta.persistence.criteria.Root;
import lombok.RequiredArgsConstructor;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spacious_team.broker.pojo.CashFlowType;
import org.springframework.data.jpa.domain.Specification;
import ru.investbook.entity.EventCashFlowEntity;
import ru.investbook.entity.EventCashFlowEntity_;
Expand All @@ -37,16 +38,18 @@

@RequiredArgsConstructor(staticName = "of")
public class EventCashFlowEntitySearchSpecification implements Specification<EventCashFlowEntity> {
private final String portfolio;
private final LocalDate dateFrom;
private final LocalDate dateTo;
private final @Nullable String portfolio;
private final @Nullable LocalDate dateFrom;
private final @Nullable LocalDate dateTo;
private final @Nullable CashFlowType cashFlowType;

@Override
public Predicate toPredicate(Root<EventCashFlowEntity> root, @Nullable CriteriaQuery<?> query, CriteriaBuilder builder) {
return Stream.of(
filterByPortfolio(root, builder, EventCashFlowEntity_.portfolio, portfolio),
filterByDateFrom(root, builder, EventCashFlowEntity_.timestamp, dateFrom),
filterByDateTo(root, builder, EventCashFlowEntity_.timestamp, dateTo))
filterByDateTo(root, builder, EventCashFlowEntity_.timestamp, dateTo),
filterByEquals(root, builder, EventCashFlowEntity_.cashFlowType, cashFlowType))
.filter(Objects::nonNull)
.reduce(builder::and)
.orElseGet(builder::conjunction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

@RequiredArgsConstructor(staticName = "of")
public class ForeignExchangeRateSearchSpecification implements Specification<ForeignExchangeRateEntity> {
private final String currency;
private final @Nullable String currency;
private final @Nullable LocalDate date;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@

@RequiredArgsConstructor(staticName = "of")
public class PortfolioCashSearchSpecification implements Specification<PortfolioCashEntity> {
private final String portfolio;
private final LocalDate dateFrom;
private final LocalDate dateTo;
private final String currency;
private final @Nullable String portfolio;
private final @Nullable LocalDate dateFrom;
private final @Nullable LocalDate dateTo;
private final @Nullable String currency;

@Override
public Predicate toPredicate(Root<PortfolioCashEntity> root, @Nullable CriteriaQuery<?> query, CriteriaBuilder builder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@

@RequiredArgsConstructor(staticName = "of")
public class PortfolioPropertySearchSpecification implements Specification<PortfolioPropertyEntity> {
private final String portfolio;
private final LocalDate date;
private final @Nullable String portfolio;
private final @Nullable LocalDate date;
private final @Nullable PortfolioPropertyType property;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@

@RequiredArgsConstructor(staticName = "of")
public class SecurityDescriptionSearchSpecification implements Specification<SecurityDescriptionEntity> {
private final String security;
private final String securitySector;
private final @Nullable String security;
private final @Nullable String securitySector;

@Override
public Predicate toPredicate(Root<SecurityDescriptionEntity> root, @Nullable CriteriaQuery<?> query, CriteriaBuilder builder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@

@RequiredArgsConstructor(staticName = "of")
public class SecurityEventCashFlowEntitySearchSpecification implements Specification<SecurityEventCashFlowEntity> {
private final String portfolio;
private final String security;
private final LocalDate dateFrom;
private final LocalDate dateTo;
private final @Nullable String portfolio;
private final @Nullable String security;
private final @Nullable LocalDate dateFrom;
private final @Nullable LocalDate dateTo;
private final @Nullable CashFlowType cashFlowType;

@Override
public Predicate toPredicate(Root<SecurityEventCashFlowEntity> root,
Expand All @@ -54,6 +55,7 @@ public Predicate toPredicate(Root<SecurityEventCashFlowEntity> root,
filterByDateFrom(root, builder, SecurityEventCashFlowEntity_.timestamp, dateFrom),
filterByDateTo(root, builder, SecurityEventCashFlowEntity_.timestamp, dateTo),
filterBySecurity(root, builder, SecurityEventCashFlowEntity_.security, security),
filterByEquals(root, builder, SecurityEventCashFlowEntity_.cashFlowType, cashFlowType),
filterByCacheFlowType(root, builder))
.filter(Objects::nonNull)
.reduce(builder::and)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@

@RequiredArgsConstructor(staticName = "of")
public class SecurityQuoteSearchSpecification implements Specification<SecurityQuoteEntity> {
private final String security;
private final String currency;
private final LocalDate date;
private final @Nullable String security;
private final @Nullable String currency;
private final @Nullable LocalDate date;

@Override
public Predicate toPredicate(Root<SecurityQuoteEntity> root, @Nullable CriteriaQuery<?> query, CriteriaBuilder builder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import jakarta.persistence.metamodel.SingularAttribute;
import lombok.NoArgsConstructor;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spacious_team.broker.pojo.CashFlowType;
import ru.investbook.entity.CashFlowTypeEntity;
import ru.investbook.entity.PortfolioEntity;
import ru.investbook.entity.PortfolioEntity_;
import ru.investbook.entity.SecurityEntity;
Expand All @@ -37,6 +39,7 @@
import java.time.LocalTime;
import java.time.ZoneId;

import static java.util.Objects.nonNull;
import static lombok.AccessLevel.PRIVATE;
import static org.springframework.util.StringUtils.hasText;

Expand All @@ -46,7 +49,7 @@ class SpecificationHelper {
static <T> @Nullable Predicate filterBySecurity(Root<T> root,
CriteriaBuilder builder,
SingularAttribute<T, SecurityEntity> attribute,
String security) {
@Nullable String security) {
if (hasText(security)) {
Path<SecurityEntity> securityPath = root.get(attribute);
return builder.or(
Expand All @@ -60,7 +63,7 @@ class SpecificationHelper {
static <T> @Nullable Predicate filterBySecurityId(Root<T> root,
CriteriaBuilder builder,
SingularAttribute<T, Integer> attribute,
String security,
@Nullable String security,
CriteriaQuery<?> query) {
if (hasText(security)) {
// Do sub-query because SecurityEntity is not related to SecurityDescriptionEntity in Java model, so
Expand Down Expand Up @@ -131,7 +134,7 @@ class SpecificationHelper {
static <T> Predicate filterByPortfolio(Root<T> root,
CriteriaBuilder builder,
SingularAttribute<T, PortfolioEntity> attribute,
String portfolio) {
@Nullable String portfolio) {
if (hasText(portfolio)) {
Path<String> path = root.get(attribute)
.get(PortfolioEntity_.ID);
Expand All @@ -145,7 +148,7 @@ static <T> Predicate filterByPortfolio(Root<T> root,
static <T> Predicate filterByPortfolioName(Root<T> root,
CriteriaBuilder builder,
SingularAttribute<T, String> attribute,
String portfolio,
@Nullable String portfolio,
CriteriaQuery<?> query) {
Path<String> transactionPortfolioPath = root.get(attribute);
if (hasText(portfolio)) {
Expand All @@ -166,6 +169,19 @@ static <T> Predicate filterByPortfolioName(Root<T> root,
.value(enabledPortfolioIds);
}

static <X> @Nullable Predicate filterByEquals(Root<X> root,
CriteriaBuilder builder,
SingularAttribute<X, CashFlowTypeEntity> attribute,
@Nullable CashFlowType value) {
if (nonNull(value)) {
Path<CashFlowTypeEntity> path = root.get(attribute);
CashFlowTypeEntity entity = new CashFlowTypeEntity();
entity.setId(value.getId());
return builder.equal(path, entity);
}
return null;
}

static <X, T> @Nullable Predicate filterByEquals(Root<X> root,
CriteriaBuilder builder,
SingularAttribute<X, T> attribute,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@

@RequiredArgsConstructor(staticName = "of")
public class TransactionSearchSpecification implements Specification<TransactionEntity> {
private final String portfolio;
private final String security;
private final LocalDate dateFrom;
private final LocalDate dateTo;
private final @Nullable String portfolio;
private final @Nullable String security;
private final @Nullable LocalDate dateFrom;
private final @Nullable LocalDate dateTo;

@Override
public Predicate toPredicate(Root<TransactionEntity> root, @Nullable CriteriaQuery<?> query, CriteriaBuilder builder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spacious_team.broker.pojo.CashFlowType;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDate;
Expand All @@ -29,9 +31,10 @@
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class EventCashFlowFormFilterModel extends AbstractFormFilterModel {
private String portfolio;
private @Nullable String portfolio;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate dateFrom;
private @Nullable LocalDate dateFrom;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate dateTo;
private @Nullable LocalDate dateTo;
private @Nullable CashFlowType cashFlowType;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDate;
Expand All @@ -29,7 +30,7 @@
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class ForeignExchangeRateFormFilterModel extends AbstractFormFilterModel {
private String currency;
private @Nullable String currency;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate date;
private @Nullable LocalDate date;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDate;
Expand All @@ -29,10 +30,10 @@
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class PortfolioCashFormFilterModel extends AbstractFormFilterModel {
private String portfolio;
private @Nullable String portfolio;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate dateFrom;
private @Nullable LocalDate dateFrom;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate dateTo;
private String currency;
private @Nullable LocalDate dateTo;
private @Nullable String currency;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spacious_team.broker.pojo.PortfolioPropertyType;
import org.springframework.format.annotation.DateTimeFormat;

Expand All @@ -30,8 +31,8 @@
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class PortfolioPropertyFormFilterModel extends AbstractFormFilterModel {
private String portfolio;
private @Nullable String portfolio;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate date;
private PortfolioPropertyType property;
private @Nullable LocalDate date;
private @Nullable PortfolioPropertyType property;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.checkerframework.checker.nullness.qual.Nullable;

@Data
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class SecurityDescriptionFormFilterModel extends AbstractFormFilterModel {
private String security;
private String securitySector;
private @Nullable String security;
private @Nullable String securitySector;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spacious_team.broker.pojo.CashFlowType;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDate;
Expand All @@ -29,10 +31,11 @@
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class SecurityEventCashFlowFormFilterModel extends AbstractFormFilterModel {
private String portfolio;
private String security;
private @Nullable String portfolio;
private @Nullable String security;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate dateFrom;
private @Nullable LocalDate dateFrom;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate dateTo;
private @Nullable LocalDate dateTo;
private @Nullable CashFlowType cashFlowType;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDate;
Expand All @@ -29,8 +30,8 @@
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class SecurityQuoteFormFilterModel extends AbstractFormFilterModel {
private String security;
private String currency;
private @Nullable String security;
private @Nullable String currency;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate date;
private @Nullable LocalDate date;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDate;
Expand All @@ -29,10 +30,10 @@
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class TransactionFormFilterModel extends AbstractFormFilterModel {
private String portfolio;
private String security;
private @Nullable String portfolio;
private @Nullable String security;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate dateFrom;
private @Nullable LocalDate dateFrom;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate dateTo;
private @Nullable LocalDate dateTo;
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public Optional<EventCashFlowModel> getById(Integer id) {
@Transactional(readOnly = true)
public Page<EventCashFlowModel> getPage(EventCashFlowFormFilterModel filter) {
EventCashFlowEntitySearchSpecification spec = EventCashFlowEntitySearchSpecification.of(
filter.getPortfolio(), filter.getDateFrom(), filter.getDateTo());
filter.getPortfolio(), filter.getDateFrom(), filter.getDateTo(), filter.getCashFlowType());

Sort sort = Sort.by(Order.asc("portfolio.id"), Order.desc(EventCashFlowEntity_.TIMESTAMP));
PageRequest page = PageRequest.of(filter.getPage(), filter.getPageSize(), sort);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public Optional<SecurityEventCashFlowModel> getById(Integer id) {
@Transactional(readOnly = true)
public Page<SecurityEventCashFlowModel> getPage(SecurityEventCashFlowFormFilterModel filter) {
SecurityEventCashFlowEntitySearchSpecification spec =
SecurityEventCashFlowEntitySearchSpecification.of(
filter.getPortfolio(), filter.getSecurity(), filter.getDateFrom(), filter.getDateTo());
SecurityEventCashFlowEntitySearchSpecification.of(filter.getPortfolio(), filter.getSecurity(),
filter.getDateFrom(), filter.getDateTo(), filter.getCashFlowType());

Sort sort = Sort.by(asc("portfolio.id"), desc(SecurityEventCashFlowEntity_.TIMESTAMP), asc("security.id"));
PageRequest page = PageRequest.of(filter.getPage(), filter.getPageSize(), sort);
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/templates/events/table.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ <h1>События по счетам</h1>
<form action="#" th:action="@{/events/search}" th:object="${filter}" method="post" class="search-filter">
<div th:replace="~{fragments/search :: portfolio (selected='*{portfolio}', portfolios=${portfolios})}"></div>
<div th:replace="~{fragments/search :: date-range (from='*{dateFrom}', to='*{dateTo}')}"></div>
<div th:replace="~{fragments/search :: cash-flow-event (type='*{cashFlowType}')}"></div>
<div th:replace="~{fragments/search :: page-size ('*{pageSize}')}"></div>
</form>

Expand Down
Loading

0 comments on commit 2bd4864

Please sign in to comment.