Skip to content

Commit

Permalink
fix: add predicate to EndDateFrom case if endDate is null (#59)
Browse files Browse the repository at this point in the history
* fix: add predicate to EndDateFrom case if endDate is null

* fix: add indices to announcement

* fix: add indices in hibernate

* fix: rename uppercase indices

* fix: rename uppercase indices
  • Loading branch information
jsteenke authored Jul 25, 2024
1 parent 28afbad commit 917c369
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public PageResult<Announcement> loadAnnouncementByCriteria(AnnouncementSearchCri
predicates.add(cb.equal(root.get(Announcement_.WORKSPACE_NAME), criteria.getWorkspaceName()));
}
if (criteria.getStartDateFrom() != null) {

predicates.add(cb.greaterThanOrEqualTo(root.get(Announcement_.START_DATE),
criteria.getStartDateFrom().toLocalDateTime()));
}
Expand All @@ -64,8 +65,15 @@ public PageResult<Announcement> loadAnnouncementByCriteria(AnnouncementSearchCri
criteria.getStartDateTo().toLocalDateTime()));
}
if (criteria.getEndDateFrom() != null) {
predicates.add(cb.greaterThanOrEqualTo(root.get(Announcement_.END_DATE),
criteria.getEndDateFrom().toLocalDateTime()));

Predicate endDatePredicate = cb.greaterThanOrEqualTo(root.get(Announcement_.END_DATE),
criteria.getEndDateFrom().toLocalDateTime());
// Also include cases where END_DATE is null
Predicate endDateIsNullPredicate = cb.isNull(root.get(Announcement_.END_DATE));

// Combine the predicates using OR
predicates.add(cb.or(endDatePredicate, endDateIsNullPredicate));

}
if (criteria.getEndDateTo() != null) {
predicates.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

import java.time.LocalDateTime;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.Table;
import jakarta.persistence.*;

import org.hibernate.annotations.TenantId;
import org.tkit.quarkus.jpa.models.TraceableEntity;
Expand All @@ -16,7 +12,11 @@
@Getter
@Setter
@Entity
@Table(name = "announcement")
@Table(name = "ANNOUNCEMENT", indexes = {
@Index(name = "START_STATUS_IDX", columnList = "STARTDATE, STATUS, TENANT_ID"),
@Index(name = "PRODUCT_IDX", columnList = "PRODUCT_NAME, TENANT_ID"),
@Index(name = "WORKSPACE_IDX", columnList = "WORKSPACENAME, TENANT_ID")
})
@SuppressWarnings("java:S2160")
public class Announcement extends TraceableEntity {

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/db/changeLog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@

<include relativeToChangelogFile="true" file="v1/2023-12-11-create-tables.xml"/>
<include relativeToChangelogFile="true" file="v1/2024-07-08-product-names.xml"/>
<include relativeToChangelogFile="true" file="v1/2024-07-24-indices-added.xml"/>

</databaseChangeLog>
19 changes: 19 additions & 0 deletions src/main/resources/db/v1/2024-07-24-indices-added.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd"
objectQuotingStrategy="QUOTE_ONLY_RESERVED_WORDS">
<changeSet author="dev (generated)" id="1720423004675-1">
<sql>
DROP INDEX IF EXISTS start_status_idx;
CREATE INDEX IF NOT EXISTS start_status_idx ON announcement (startdate, status, tenant_id);

DROP INDEX IF EXISTS product_idx;
CREATE INDEX IF NOT EXISTS product_idx ON announcement (product_name, tenant_id);

DROP INDEX IF EXISTS workspace_idx;
CREATE INDEX IF NOT EXISTS workspace_idx ON announcement (workspaceName, tenant_id);
</sql>
</changeSet>
</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,34 @@ void getAnnouncementsByCriteriaAllTest() {
.as(AnnouncementPageResultDTO.class);

Assertions.assertThat(data).isNotNull();
Assertions.assertThat(data.getTotalElements()).isEqualTo(5);
Assertions.assertThat(data.getStream()).isNotNull().hasSize(5);
Assertions.assertThat(data.getTotalElements()).isEqualTo(6);
Assertions.assertThat(data.getStream()).isNotNull().hasSize(6);
}

@Test
void getActiveAnnouncementsByCriteriaAllTest() {
AnnouncementSearchCriteriaDTO criteria = new AnnouncementSearchCriteriaDTO();

criteria.status(AnnouncementStatusDTO.ACTIVE);
criteria.workspaceName("workspace6");
criteria.setStartDateTo(OffsetDateTime.parse("2023-03-10T12:15:50-04:00"));
criteria.setEndDateFrom(OffsetDateTime.parse("2023-03-10T12:15:50-04:00"));

var data = given()
.auth().oauth2(getKeycloakClientToken("testClient"))
.contentType(APPLICATION_JSON)
.body(criteria)
.post("search")
.then()
.statusCode(OK.getStatusCode())
.contentType(APPLICATION_JSON)
.extract()
.as(AnnouncementPageResultDTO.class);

Assertions.assertThat(data).isNotNull();
Assertions.assertThat(data.getTotalElements()).isEqualTo(1);
Assertions.assertThat(data.getStream()).isNotNull().hasSize(1);
Assertions.assertThat(data.getStream().get(0).getWorkspaceName()).isEqualTo("workspace6");
}

@Test
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/data/test-internal.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
<ANNOUNCEMENT guid="a5" optlock="0" enddate="2022-02-05 06:01:00" startdate="2009-12-30 14:55:00" status="ACTIVE"
product_name="product2" content="some-content" priority="NORMAL" title="title5" type="INFO" tenant_id="default" workspaceName="workspace2"/>

<ANNOUNCEMENT guid="a6" optlock="0" startdate="2009-12-30 14:55:00" status="ACTIVE"
product_name="product2" content="some-content" priority="NORMAL" title="title6" type="INFO" tenant_id="default" workspaceName="workspace6"/>

<ANNOUNCEMENT guid="a3-100" optlock="0" enddate="2022-02-05 06:01:00" startdate="2009-12-30 14:55:00" status="ACTIVE"
product_name="product2" content="some-content" priority="NORMAL" title="title2" type="EVENT" tenant_id="tenant-100" workspaceName="workspace2"/>
<ANNOUNCEMENT guid="a4-100" optlock="0" enddate="2022-02-05 06:01:00" startdate="2009-12-30 14:55:00" status="INACTIVE"
Expand Down

0 comments on commit 917c369

Please sign in to comment.