Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Set event attribute option combo column to not null [DHIS2-14885] #17849

Merged
merged 27 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ public Event(
this.organisationUnit = organisationUnit;
}

public Event(
Enrollment enrollment,
ProgramStage programStage,
OrganisationUnit organisationUnit,
CategoryOptionCombo attributeOptionCombo) {
this(enrollment, programStage, organisationUnit);
this.attributeOptionCombo = attributeOptionCombo;
}

@Override
public void setAutoFields() {
super.setAutoFields();
Expand All @@ -128,6 +137,11 @@ public void setAutoFields() {
lastUpdatedAtClient = lastUpdated;
}

@JsonIgnore
public boolean hasAttributeOptionCombo() {
return attributeOptionCombo != null;
}

// -------------------------------------------------------------------------
// Getters and setters
// -------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public interface EventService {
long getEventCount(int days);

/**
* Creates an event.
* Creates and saves an event.
*
* @param enrollment the Enrollment.
* @param programStage the ProgramStage.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.hisp.dhis.category.CategoryOptionCombo;
import org.hisp.dhis.category.CategoryService;
import org.hisp.dhis.changelog.ChangeLogType;
import org.hisp.dhis.common.IllegalQueryException;
import org.hisp.dhis.dataelement.DataElement;
Expand All @@ -61,6 +63,8 @@
public class DefaultEventService implements EventService {
private final EventStore eventStore;

private final CategoryService categoryService;

private final TrackedEntityDataValueChangeLogService dataValueAuditService;

private final FileResourceService fileResourceService;
Expand All @@ -75,6 +79,12 @@ public class DefaultEventService implements EventService {
@Transactional
public long addEvent(Event event) {
event.setAutoFields();

if (!event.hasAttributeOptionCombo()) {
CategoryOptionCombo aoc = categoryService.getDefaultCategoryOptionCombo();
event.setAttributeOptionCombo(aoc);
}

eventStore.save(event);
return event.getId();
}
Expand Down Expand Up @@ -145,6 +155,7 @@ public Event createEvent(
event.setOrganisationUnit(organisationUnit);
event.setScheduledDate(dueDate);
event.setStatus(EventStatus.SCHEDULE);
event.setAttributeOptionCombo(categoryService.getDefaultCategoryOptionCombo());

if (programStage.getOpenAfterEnrollment()
|| enrollment.getProgram().isWithoutRegistration()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
column="programstageid" not-null="true" foreign-key="fk_programstageinstance_programstageid" />

<many-to-one name="attributeOptionCombo" class="org.hisp.dhis.category.CategoryOptionCombo"
column="attributeoptioncomboid" foreign-key="fk_programstageinstance_attributeoptioncomboid" />
column="attributeoptioncomboid" not-null="true" foreign-key="fk_programstageinstance_attributeoptioncomboid" />

<property name="deleted" column="deleted" />

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

-- Retrieves the default cat opt combo by first looking for the default UID, then the default name for older databases
with default_cat_opt_combo as (
select coalesce ((
select "categoryoptioncomboid" from "categoryoptioncombo" where "uid" = 'HllvX50cXC0'), (
select "categoryoptioncomboid" from "categoryoptioncombo" where "name" = 'default')) as id
)
-- Sets the attributeoptioncomboid column to default for null values
update "event"
set "attributeoptioncomboid" = (select id from default_cat_opt_combo)
where "attributeoptioncomboid" is null;

-- Sets the attributeoptioncomboid column to not null
alter table "event" alter column "attributeoptioncomboid" set not null;
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,12 @@ public static CategoryCombo createCategoryCombo(String name, String uid, Categor
return categoryCombo;
}

public static CategoryOptionCombo createCategoryOptionCombo() {
CategoryOptionCombo coc = new CategoryOptionCombo();
coc.setAutoFields();
return coc;
}

/**
* @param categoryComboUniqueIdentifier A unique character to identify the category combo.
* @param categoryOptionUniqueIdentifiers Unique characters to identify the category options.
Expand Down Expand Up @@ -1651,7 +1657,6 @@ public static Event createEvent(
event.setProgramStage(programStage);
event.setEnrollment(enrollment);
event.setOrganisationUnit(organisationUnit);

return event;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
import org.hisp.dhis.category.CategoryCombo;
import org.hisp.dhis.category.CategoryOption;
import org.hisp.dhis.category.CategoryOptionCombo;
import org.hisp.dhis.category.CategoryService;
import org.hisp.dhis.common.BaseDimensionalObject;
import org.hisp.dhis.common.DataDimensionType;
import org.hisp.dhis.common.DimensionType;
Expand Down Expand Up @@ -166,6 +167,8 @@ class EventAnalyticsServiceTest extends SingleSetupIntegrationTestBase {

@Autowired private UserService _userService;

@Autowired private CategoryService categoryService;

private OrganisationUnit ouA;

private OrganisationUnit ouB;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
import org.hisp.dhis.audit.AuditScope;
import org.hisp.dhis.audit.AuditService;
import org.hisp.dhis.audit.AuditType;
import org.hisp.dhis.category.CategoryOptionCombo;
import org.hisp.dhis.category.CategoryService;
import org.hisp.dhis.changelog.ChangeLogType;
import org.hisp.dhis.common.DeliveryChannel;
import org.hisp.dhis.commons.util.RelationshipUtils;
Expand Down Expand Up @@ -112,10 +114,14 @@ class MaintenanceServiceTest extends IntegrationTestBase {

@Autowired private AuditService auditService;

@Autowired private CategoryService categoryService;

private Date incidenDate;

private Date enrollmentDate;

private CategoryOptionCombo coA;

private Program program;

private ProgramStage stageA;
Expand Down Expand Up @@ -144,6 +150,7 @@ class MaintenanceServiceTest extends IntegrationTestBase {

@Override
public void setUpTest() {
coA = categoryService.getDefaultCategoryOptionCombo();
organisationUnit = createOrganisationUnit('A');
organisationUnitService.addOrganisationUnit(organisationUnit);
program = createProgram('A', new HashSet<>(), organisationUnit);
Expand Down Expand Up @@ -190,11 +197,13 @@ public void setUpTest() {
event.setOrganisationUnit(organisationUnit);
event.setEnrollment(enrollment);
event.setOccurredDate(new Date());
event.setAttributeOptionCombo(coA);
eventWithTeAssociation = new Event(enrollmentWithTeAssociation, stageA);
eventWithTeAssociation.setUid("PSUID-C");
eventWithTeAssociation.setOrganisationUnit(organisationUnit);
eventWithTeAssociation.setEnrollment(enrollmentWithTeAssociation);
eventWithTeAssociation.setOccurredDate(new Date());
eventWithTeAssociation.setAttributeOptionCombo(coA);
eventService.addEvent(eventWithTeAssociation);
relationshipType = createPersonToPersonRelationshipType('A', program, trackedEntityType, false);
relationshipTypeService.addRelationshipType(relationshipType);
Expand Down Expand Up @@ -311,6 +320,7 @@ void testDeleteSoftDeletedEnrollmentLinkedToATrackedEntityDataValueAudit() {
Event eventA = new Event(enrollment, program.getProgramStageByStage(1));
eventA.setScheduledDate(enrollmentDate);
eventA.setUid("UID-A");
eventA.setAttributeOptionCombo(coA);
eventService.addEvent(eventA);
TrackedEntityDataValueChangeLog trackedEntityDataValueChangeLog =
new TrackedEntityDataValueChangeLog(
Expand Down Expand Up @@ -338,6 +348,7 @@ void testDeleteSoftDeletedEventLinkedToARelationshipItem() {
Event eventA = new Event(enrollment, program.getProgramStageByStage(1));
eventA.setScheduledDate(enrollmentDate);
eventA.setUid("UID-A");
eventA.setAttributeOptionCombo(coA);
long idA = eventService.addEvent(eventA);
Relationship r = new Relationship();
RelationshipItem rItem1 = new RelationshipItem();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.hisp.dhis.category.CategoryOptionCombo;
import org.hisp.dhis.category.CategoryService;
import org.hisp.dhis.common.CodeGenerator;
import org.hisp.dhis.note.Note;
import org.hisp.dhis.note.NoteService;
Expand Down Expand Up @@ -70,12 +72,16 @@ class EnrollmentServiceTest extends TransactionalIntegrationTest {

@Autowired protected UserService _userService;

@Autowired NoteService noteService;
@Autowired private NoteService noteService;

@Autowired private CategoryService categoryService;

private Date incidentDate;

private Date enrollmentDate;

private CategoryOptionCombo coA;

private Program programA;

private Program programB;
Expand Down Expand Up @@ -104,6 +110,8 @@ class EnrollmentServiceTest extends TransactionalIntegrationTest {
public void setUpTest() {
userService = _userService;

coA = categoryService.getDefaultCategoryOptionCombo();

organisationUnitA = createOrganisationUnit('A');
organisationUnitService.addOrganisationUnit(organisationUnitA);
organisationUnitB = createOrganisationUnit('B');
Expand Down Expand Up @@ -146,9 +154,6 @@ public void setUpTest() {
enrollmentA = new Enrollment(enrollmentDate, incidentDate, trackedEntityA, programA);
enrollmentA.setUid("UID-A");
enrollmentA.setOrganisationUnit(organisationUnitA);
eventA = new Event(enrollmentA, stageA);
eventA.setUid("UID-PSI-A");
eventA.setOrganisationUnit(organisationUnitA);
enrollmentB = new Enrollment(enrollmentDate, incidentDate, trackedEntityA, programB);
enrollmentB.setUid("UID-B");
enrollmentB.setStatus(EnrollmentStatus.CANCELLED);
Expand All @@ -160,6 +165,10 @@ public void setUpTest() {
enrollmentD = new Enrollment(enrollmentDate, incidentDate, trackedEntityB, programA);
enrollmentD.setUid("UID-D");
enrollmentD.setOrganisationUnit(organisationUnitB);
eventA = new Event(enrollmentA, stageA);
eventA.setUid("UID-PSI-A");
eventA.setOrganisationUnit(organisationUnitA);
eventA.setAttributeOptionCombo(coA);

injectSecurityContextUser(user);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class EventServiceTest extends TransactionalIntegrationTest {

@Autowired private TrackedEntityAttributeValueService attributeValueService;

@Autowired NoteService noteService;
@Autowired private NoteService noteService;

private OrganisationUnit organisationUnitA;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.hisp.dhis.category.CategoryOptionCombo;
import org.hisp.dhis.category.CategoryService;
import org.hisp.dhis.common.IdentifiableObjectManager;
import org.hisp.dhis.common.IdentifiableObjectStore;
import org.hisp.dhis.dataelement.DataElement;
Expand Down Expand Up @@ -78,10 +80,14 @@ class EventStoreTest extends TransactionalIntegrationTest {

@Autowired private IdentifiableObjectManager idObjectManager;

@Autowired private CategoryService categoryService;

@Autowired
@Qualifier("org.hisp.dhis.program.notification.ProgramNotificationStore")
private IdentifiableObjectStore<ProgramNotificationTemplate> programNotificationStore;

private CategoryOptionCombo coA;

private OrganisationUnit organisationUnitA;

private OrganisationUnit organisationUnitB;
Expand Down Expand Up @@ -132,6 +138,7 @@ class EventStoreTest extends TransactionalIntegrationTest {

@Override
public void setUpTest() {
coA = categoryService.getDefaultCategoryOptionCombo();
organisationUnitA = createOrganisationUnit('A');
organisationUnitB = createOrganisationUnit('B');
idObjectManager.save(organisationUnitA);
Expand Down Expand Up @@ -194,18 +201,23 @@ public void setUpTest() {
eventA = new Event(enrollmentA, stageA);
eventA.setScheduledDate(enrollmentDate);
eventA.setUid("UID-A");
eventA.setAttributeOptionCombo(coA);
eventB = new Event(enrollmentA, stageB);
eventB.setScheduledDate(enrollmentDate);
eventB.setUid("UID-B");
eventB.setAttributeOptionCombo(coA);
eventC = new Event(enrollmentB, stageC);
eventC.setScheduledDate(enrollmentDate);
eventC.setUid("UID-C");
eventC.setAttributeOptionCombo(coA);
eventD1 = new Event(enrollmentB, stageD);
eventD1.setScheduledDate(enrollmentDate);
eventD1.setUid("UID-D1");
eventD1.setAttributeOptionCombo(coA);
eventD2 = new Event(enrollmentB, stageD);
eventD2.setScheduledDate(enrollmentDate);
eventD2.setUid("UID-D2");
eventD2.setAttributeOptionCombo(coA);
}

@Test
Expand Down Expand Up @@ -306,12 +318,15 @@ void testGetWithScheduledNotifications() {
// Events
Event eventA = new Event(enrollmentA, stageA);
eventA.setScheduledDate(tomorrow);
eventA.setAttributeOptionCombo(coA);
eventStore.save(eventA);
Event eventB = new Event(enrollmentB, stageB);
eventB.setScheduledDate(today);
eventB.setAttributeOptionCombo(coA);
eventStore.save(eventB);
Event eventC = new Event(enrollmentB, stageC);
eventC.setScheduledDate(yesterday);
eventC.setAttributeOptionCombo(coA);
eventStore.save(eventC);
// Queries
List<Event> results;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,13 @@ public void setUpTest() {
// ---------------------------------------------------------------------
// TrackedEntityDataValue
// ---------------------------------------------------------------------
Event stageInstanceA =
Event eventA =
eventService.createEvent(enrollment, psA, enrollmentDate, incidentDate, organisationUnit);
Event stageInstanceB =
Event eventB =
eventService.createEvent(enrollment, psB, enrollmentDate, incidentDate, organisationUnit);
Set<Event> events = new HashSet<>();
events.add(stageInstanceA);
events.add(stageInstanceB);
events.add(eventA);
events.add(eventB);
enrollment.setEvents(events);
enrollment.setProgram(programA);
// ---------------------------------------------------------------------
Expand Down
Loading
Loading