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 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,13 @@ public Event(Enrollment enrollment, ProgramStage programStage) {
}

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
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 +62,8 @@
public class DefaultEventService implements EventService {
private final EventStore eventStore;

private final CategoryService categoryService;

private final TrackedEntityDataValueChangeLogService dataValueAuditService;

private final FileResourceService fileResourceService;
Expand Down Expand Up @@ -145,6 +148,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
Expand Up @@ -38,6 +38,7 @@

import java.util.List;
import org.hisp.dhis.DhisConvenienceTest;
import org.hisp.dhis.category.CategoryOptionCombo;
import org.hisp.dhis.common.SortDirection;
import org.hisp.dhis.feedback.ForbiddenException;
import org.hisp.dhis.feedback.NotFoundException;
Expand Down Expand Up @@ -83,6 +84,8 @@ class RelationshipOperationParamsMapperTest extends DhisConvenienceTest {

@InjectMocks private RelationshipOperationParamsMapper mapper;

private CategoryOptionCombo coc;

private TrackedEntity trackedEntity;

private Enrollment enrollment;
Expand All @@ -93,6 +96,8 @@ class RelationshipOperationParamsMapperTest extends DhisConvenienceTest {

@BeforeEach
public void setUp() {
coc = new CategoryOptionCombo();
coc.setAutoFields();
OrganisationUnit organisationUnit = createOrganisationUnit('A');
Program program = createProgram('A');
ProgramStage programStage = createProgramStage('A', program);
Expand All @@ -101,7 +106,7 @@ public void setUp() {
trackedEntity.setUid(TE_UID);
enrollment = createEnrollment(program, trackedEntity, organisationUnit);
enrollment.setUid(EN_UID);
event = createEvent(programStage, enrollment, organisationUnit);
event = createEvent(programStage, enrollment, organisationUnit, coc);
event.setUid(EV_UID);

User u = new User();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import java.util.List;
import org.hisp.dhis.DhisConvenienceTest;
import org.hisp.dhis.category.CategoryOptionCombo;
import org.hisp.dhis.common.CodeGenerator;
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.program.Enrollment;
Expand Down Expand Up @@ -72,6 +73,8 @@ class RelationshipTrackerConverterServiceTest extends DhisConvenienceTest {

private static final String RELATIONSHIP_B = "RELATIONSHIP_B_UID";

private CategoryOptionCombo coc;

private RelationshipType teToEnrollment;

private RelationshipType teToEvent;
Expand All @@ -89,6 +92,8 @@ class RelationshipTrackerConverterServiceTest extends DhisConvenienceTest {

@BeforeEach
protected void setupTest() {
coc = new CategoryOptionCombo();
coc.setAutoFields();
OrganisationUnit organisationUnit = createOrganisationUnit('A');
Program program = createProgram('A');
TrackedEntityType teType = createTrackedEntityType('A');
Expand All @@ -104,7 +109,7 @@ protected void setupTest() {
trackedEntity.setUid(TE);
enrollment = createEnrollment(program, trackedEntity, organisationUnit);
enrollment.setUid(ENROLLMENT);
event = createEvent(createProgramStage('A', program), enrollment, organisationUnit);
event = createEvent(createProgramStage('A', program), enrollment, organisationUnit, coc);
event.setUid(EVENT);

relationshipConverterService = new RelationshipTrackerConverterService();
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 @@ -1644,23 +1644,27 @@ public static Enrollment createEnrollment(
}

public static Event createEvent(
ProgramStage programStage, Enrollment enrollment, OrganisationUnit organisationUnit) {
ProgramStage programStage,
Enrollment enrollment,
OrganisationUnit organisationUnit,
CategoryOptionCombo attributeOptionCombo) {
Event event = new Event();
event.setAutoFields();

event.setProgramStage(programStage);
event.setEnrollment(enrollment);
event.setOrganisationUnit(organisationUnit);

event.setAttributeOptionCombo(attributeOptionCombo);
return event;
}

public static Event createEvent(
Enrollment enrollment,
ProgramStage programStage,
OrganisationUnit organisationUnit,
CategoryOptionCombo attributeOptionCombo,
Set<EventDataValue> dataValues) {
Event event = createEvent(programStage, enrollment, organisationUnit);
Event event = createEvent(programStage, enrollment, organisationUnit, attributeOptionCombo);
event.setOccurredDate(new Date());
event.setStatus(EventStatus.ACTIVE);
event.setEventDataValues(dataValues);
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,10 @@ class EventAnalyticsServiceTest extends SingleSetupIntegrationTestBase {

@Autowired private UserService _userService;

@Autowired private CategoryService categoryService;

private CategoryOptionCombo coc;

private OrganisationUnit ouA;

private OrganisationUnit ouB;
Expand Down Expand Up @@ -259,6 +264,8 @@ public void beforeEach() {
public void setUpTest() throws IOException, InterruptedException, ConflictException {
userService = _userService;

coc = categoryService.getDefaultCategoryOptionCombo();

// Organisation Units
//
// A -> B -> D,E,F,G
Expand Down Expand Up @@ -460,7 +467,7 @@ public void setUpTest() throws IOException, InterruptedException, ConflictExcept
addProgramOwnershipHistory(programA, teiA, ouG, feb15Noon, mar15);
trackedEntityProgramOwnerService.createOrUpdateTrackedEntityProgramOwner(teiA, programA, ouH);

Event eventA1 = createEvent(psA, piA, ouI);
Event eventA1 = createEvent(psA, piA, ouI, coc);
eventA1.setScheduledDate(jan15);
eventA1.setOccurredDate(jan15);
eventA1.setUid("event0000A1");
Expand All @@ -469,7 +476,7 @@ public void setUpTest() throws IOException, InterruptedException, ConflictExcept
new EventDataValue(deA.getUid(), "1"), new EventDataValue(deU.getUid(), ouL.getUid())));
eventA1.setAttributeOptionCombo(cocDefault);

Event eventA2 = createEvent(psA, piA, ouJ);
Event eventA2 = createEvent(psA, piA, ouJ, coc);
eventA2.setScheduledDate(feb15);
eventA2.setOccurredDate(feb15);
eventA2.setUid("event0000A2");
Expand All @@ -478,7 +485,7 @@ public void setUpTest() throws IOException, InterruptedException, ConflictExcept
new EventDataValue(deA.getUid(), "2"), new EventDataValue(deU.getUid(), ouM.getUid())));
eventA2.setAttributeOptionCombo(cocDefault);

Event eventA3 = createEvent(psA, piA, ouK);
Event eventA3 = createEvent(psA, piA, ouK, coc);
eventA3.setScheduledDate(mar15);
eventA3.setOccurredDate(mar15);
eventA3.setUid("event0000A3");
Expand All @@ -487,71 +494,71 @@ public void setUpTest() throws IOException, InterruptedException, ConflictExcept
new EventDataValue(deA.getUid(), "4"), new EventDataValue(deU.getUid(), ouN.getUid())));
eventA3.setAttributeOptionCombo(cocDefault);

Event eventB1 = createEvent(psB, piB, ouI);
Event eventB1 = createEvent(psB, piB, ouI, coc);
eventB1.setScheduledDate(jan1);
eventB1.setOccurredDate(jan1);
eventB1.setUid("event0000B1");
eventB1.setEventDataValues(
Set.of(new EventDataValue(deA.getUid(), "10"), new EventDataValue(deB.getUid(), "A")));
eventB1.setAttributeOptionCombo(cocDefault);

Event eventB2 = createEvent(psB, piB, ouI);
Event eventB2 = createEvent(psB, piB, ouI, coc);
eventB2.setScheduledDate(jan20);
eventB2.setOccurredDate(jan20);
eventB2.setUid("event0000B2");
eventB2.setEventDataValues(
Set.of(new EventDataValue(deA.getUid(), "20"), new EventDataValue(deB.getUid(), "B")));
eventB2.setAttributeOptionCombo(cocDefault);

Event eventB3 = createEvent(psB, piB, ouJ);
Event eventB3 = createEvent(psB, piB, ouJ, coc);
eventB3.setScheduledDate(jan1);
eventB3.setOccurredDate(jan1);
eventB3.setUid("event0000B3");
eventB3.setEventDataValues(
Set.of(new EventDataValue(deA.getUid(), "30"), new EventDataValue(deB.getUid(), "C")));
eventB3.setAttributeOptionCombo(cocDefault);

Event eventB4 = createEvent(psB, piB, ouJ);
Event eventB4 = createEvent(psB, piB, ouJ, coc);
eventB4.setScheduledDate(jan20);
eventB4.setOccurredDate(jan20);
eventB4.setUid("event0000B4");
eventB4.setEventDataValues(
Set.of(new EventDataValue(deA.getUid(), "40"), new EventDataValue(deB.getUid(), "D")));
eventB4.setAttributeOptionCombo(cocDefault);

Event eventB5 = createEvent(psB, piB, ouI);
Event eventB5 = createEvent(psB, piB, ouI, coc);
eventB5.setScheduledDate(feb15);
eventB5.setOccurredDate(feb15);
eventB5.setUid("event0000B5");
eventB5.setEventDataValues(
Set.of(new EventDataValue(deA.getUid(), "50"), new EventDataValue(deB.getUid(), "E")));
eventB5.setAttributeOptionCombo(cocDefault);

Event eventB6 = createEvent(psB, piB, ouI);
Event eventB6 = createEvent(psB, piB, ouI, coc);
eventB6.setScheduledDate(feb15Noon);
eventB6.setOccurredDate(feb15Noon);
eventB6.setUid("event0000B6");
eventB6.setEventDataValues(
Set.of(new EventDataValue(deA.getUid(), "60"), new EventDataValue(deB.getUid(), "F")));
eventB6.setAttributeOptionCombo(cocDefault);

Event eventB7 = createEvent(psB, piB, ouJ);
Event eventB7 = createEvent(psB, piB, ouJ, coc);
eventB7.setScheduledDate(feb15);
eventB7.setOccurredDate(feb15);
eventB7.setUid("event0000B7");
eventB7.setEventDataValues(
Set.of(new EventDataValue(deA.getUid(), "70"), new EventDataValue(deB.getUid(), "G")));
eventB7.setAttributeOptionCombo(cocDefault);

Event eventB8 = createEvent(psB, piB, ouJ);
Event eventB8 = createEvent(psB, piB, ouJ, coc);
eventB8.setScheduledDate(feb15Noon);
eventB8.setOccurredDate(feb15Noon);
eventB8.setUid("event0000B8");
eventB8.setEventDataValues(
Set.of(new EventDataValue(deA.getUid(), "80"), new EventDataValue(deB.getUid(), "H")));
eventB8.setAttributeOptionCombo(cocDefault);

Event eventM1 = createEvent(psB, piB, ouI);
Event eventM1 = createEvent(psB, piB, ouI, coc);
eventM1.setScheduledDate(jan15);
eventM1.setOccurredDate(jan15);
eventM1.setUid("event0000M1");
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
Loading
Loading