diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/TrackerObjectsMapper.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/TrackerObjectsMapper.java new file mode 100644 index 000000000000..44e4ca8dbadd --- /dev/null +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/TrackerObjectsMapper.java @@ -0,0 +1,340 @@ +/* + * Copyright (c) 2004-2024, University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of the HISP project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.hisp.dhis.tracker.imports.bundle; + +import static org.apache.commons.collections4.CollectionUtils.isNotEmpty; +import static org.hisp.dhis.program.EnrollmentStatus.COMPLETED; +import static org.hisp.dhis.relationship.RelationshipEntity.PROGRAM_INSTANCE; +import static org.hisp.dhis.relationship.RelationshipEntity.PROGRAM_STAGE_INSTANCE; +import static org.hisp.dhis.relationship.RelationshipEntity.TRACKED_ENTITY_INSTANCE; + +import java.util.Date; +import java.util.Optional; +import java.util.stream.Collectors; +import javax.annotation.Nonnull; +import org.hisp.dhis.category.CategoryOptionCombo; +import org.hisp.dhis.dataelement.DataElement; +import org.hisp.dhis.event.EventStatus; +import org.hisp.dhis.eventdatavalue.EventDataValue; +import org.hisp.dhis.note.Note; +import org.hisp.dhis.organisationunit.OrganisationUnit; +import org.hisp.dhis.program.Enrollment; +import org.hisp.dhis.program.EnrollmentStatus; +import org.hisp.dhis.program.Event; +import org.hisp.dhis.program.Program; +import org.hisp.dhis.program.ProgramStage; +import org.hisp.dhis.program.UserInfoSnapshot; +import org.hisp.dhis.relationship.Relationship; +import org.hisp.dhis.relationship.RelationshipItem; +import org.hisp.dhis.relationship.RelationshipKey; +import org.hisp.dhis.relationship.RelationshipType; +import org.hisp.dhis.trackedentity.TrackedEntity; +import org.hisp.dhis.trackedentity.TrackedEntityType; +import org.hisp.dhis.tracker.imports.domain.DataValue; +import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; +import org.hisp.dhis.tracker.imports.util.RelationshipKeySupport; +import org.hisp.dhis.user.User; +import org.hisp.dhis.user.UserDetails; +import org.hisp.dhis.util.DateUtils; + +public class TrackerObjectsMapper { + private TrackerObjectsMapper() { + throw new IllegalStateException("Utility class"); + } + + public static @Nonnull TrackedEntity map( + @Nonnull TrackerPreheat preheat, + @Nonnull org.hisp.dhis.tracker.imports.domain.TrackedEntity trackedEntity, + @Nonnull UserDetails user) { + TrackedEntity dbTrackedEntity = preheat.getTrackedEntity(trackedEntity.getTrackedEntity()); + OrganisationUnit organisationUnit = preheat.getOrganisationUnit(trackedEntity.getOrgUnit()); + TrackedEntityType trackedEntityType = + preheat.getTrackedEntityType(trackedEntity.getTrackedEntityType()); + + Date now = new Date(); + + if (dbTrackedEntity == null) { + dbTrackedEntity = new TrackedEntity(); + dbTrackedEntity.setUid(trackedEntity.getTrackedEntity()); + dbTrackedEntity.setCreated(now); + dbTrackedEntity.setCreatedByUserInfo(UserInfoSnapshot.from(user)); + dbTrackedEntity.setStoredBy(trackedEntity.getStoredBy()); + } + + dbTrackedEntity.setLastUpdatedByUserInfo(UserInfoSnapshot.from(user)); + dbTrackedEntity.setLastUpdated(now); + dbTrackedEntity.setPotentialDuplicate(trackedEntity.isPotentialDuplicate()); + dbTrackedEntity.setCreatedAtClient(DateUtils.fromInstant(trackedEntity.getCreatedAtClient())); + dbTrackedEntity.setLastUpdatedAtClient( + DateUtils.fromInstant(trackedEntity.getUpdatedAtClient())); + dbTrackedEntity.setOrganisationUnit(organisationUnit); + dbTrackedEntity.setTrackedEntityType(trackedEntityType); + dbTrackedEntity.setInactive(trackedEntity.isInactive()); + dbTrackedEntity.setGeometry(trackedEntity.getGeometry()); + + return dbTrackedEntity; + } + + public static @Nonnull Enrollment map( + @Nonnull TrackerPreheat preheat, + @Nonnull org.hisp.dhis.tracker.imports.domain.Enrollment enrollment, + @Nonnull UserDetails user) { + Enrollment dbEnrollment = preheat.getEnrollment(enrollment.getEnrollment()); + OrganisationUnit organisationUnit = preheat.getOrganisationUnit(enrollment.getOrgUnit()); + + Program program = preheat.getProgram(enrollment.getProgram()); + + TrackedEntity trackedEntity = preheat.getTrackedEntity(enrollment.getTrackedEntity()); + + Date now = new Date(); + + if (dbEnrollment == null) { + dbEnrollment = new Enrollment(); + dbEnrollment.setUid(enrollment.getEnrollment()); + dbEnrollment.setCreated(now); + dbEnrollment.setStoredBy(enrollment.getStoredBy()); + dbEnrollment.setCreatedByUserInfo(UserInfoSnapshot.from(user)); + } + + dbEnrollment.setLastUpdated(now); + dbEnrollment.setLastUpdatedByUserInfo(UserInfoSnapshot.from(user)); + dbEnrollment.setCreatedAtClient(DateUtils.fromInstant(enrollment.getCreatedAtClient())); + dbEnrollment.setLastUpdatedAtClient(DateUtils.fromInstant(enrollment.getUpdatedAtClient())); + + Date enrollmentDate = DateUtils.fromInstant(enrollment.getEnrolledAt()); + Date occurredDate = DateUtils.fromInstant(enrollment.getOccurredAt()); + + dbEnrollment.setEnrollmentDate(enrollmentDate); + dbEnrollment.setOccurredDate(occurredDate != null ? occurredDate : enrollmentDate); + dbEnrollment.setOrganisationUnit(organisationUnit); + dbEnrollment.setProgram(program); + dbEnrollment.setTrackedEntity(trackedEntity); + dbEnrollment.setFollowup(enrollment.isFollowUp()); + dbEnrollment.setGeometry(enrollment.getGeometry()); + + EnrollmentStatus previousStatus = dbEnrollment.getStatus(); + dbEnrollment.setStatus(enrollment.getStatus()); + + if (previousStatus != dbEnrollment.getStatus()) { + switch (dbEnrollment.getStatus()) { + case ACTIVE -> { + dbEnrollment.setCompletedDate(null); + dbEnrollment.setCompletedBy(null); + } + case COMPLETED -> { + dbEnrollment.setCompletedDate(now); + dbEnrollment.setCompletedBy(user.getUsername()); + } + case CANCELLED -> { + dbEnrollment.setCompletedDate(now); + dbEnrollment.setCompletedBy(null); + } + } + } + + if (isNotEmpty(enrollment.getNotes())) { + dbEnrollment + .getNotes() + .addAll( + enrollment.getNotes().stream() + .map(note -> map(preheat, note, user)) + .collect(Collectors.toSet())); + } + return dbEnrollment; + } + + public static @Nonnull Event map( + @Nonnull TrackerPreheat preheat, + @Nonnull org.hisp.dhis.tracker.imports.domain.Event event, + @Nonnull UserDetails user) { + Event dbEvent = preheat.getEvent(event.getEvent()); + ProgramStage programStage = preheat.getProgramStage(event.getProgramStage()); + Program program = preheat.getProgram(event.getProgram()); + OrganisationUnit organisationUnit = preheat.getOrganisationUnit(event.getOrgUnit()); + + Date now = new Date(); + + if (dbEvent == null) { + dbEvent = new Event(); + dbEvent.setUid(event.getEvent()); + dbEvent.setCreated(now); + dbEvent.setStoredBy(event.getStoredBy()); + dbEvent.setCreatedByUserInfo(UserInfoSnapshot.from(user)); + } + dbEvent.setCreatedAtClient(DateUtils.fromInstant(event.getCreatedAtClient())); + dbEvent.setLastUpdatedByUserInfo(UserInfoSnapshot.from(user)); + dbEvent.setLastUpdated(now); + dbEvent.setLastUpdatedAtClient(DateUtils.fromInstant(event.getUpdatedAtClient())); + dbEvent.setEnrollment(getEnrollment(preheat, event.getEnrollment(), program)); + dbEvent.setProgramStage(programStage); + dbEvent.setOrganisationUnit(organisationUnit); + dbEvent.setOccurredDate(DateUtils.fromInstant(event.getOccurredAt())); + dbEvent.setScheduledDate(DateUtils.fromInstant(event.getScheduledAt())); + + if (event.getAttributeOptionCombo().isNotBlank()) { + dbEvent.setAttributeOptionCombo( + preheat.getCategoryOptionCombo(event.getAttributeOptionCombo())); + } else { + dbEvent.setAttributeOptionCombo(preheat.getDefault(CategoryOptionCombo.class)); + } + + dbEvent.setGeometry(event.getGeometry()); + + EventStatus currentStatus = event.getStatus(); + EventStatus previousStatus = dbEvent.getStatus(); + + if (currentStatus != previousStatus && currentStatus == EventStatus.COMPLETED) { + dbEvent.setCompletedDate(now); + dbEvent.setCompletedBy(user.getUsername()); + } + + if (currentStatus != EventStatus.COMPLETED) { + dbEvent.setCompletedDate(null); + dbEvent.setCompletedBy(null); + } + + dbEvent.setStatus(currentStatus); + + if (Boolean.TRUE.equals(programStage.isEnableUserAssignment()) + && event.getAssignedUser() != null + && !event.getAssignedUser().isEmpty()) { + Optional assignedUser = + preheat.getUserByUsername(event.getAssignedUser().getUsername()); + assignedUser.ifPresent(dbEvent::setAssignedUser); + } + + if (program.isRegistration() + && dbEvent.getScheduledDate() == null + && dbEvent.getOccurredDate() != null) { + dbEvent.setScheduledDate(dbEvent.getOccurredDate()); + } + + // TODO(DHIS2-18223): Remove data value mapping and fix changelog logic + for (DataValue dataValue : event.getDataValues()) { + DataElement dataElement = preheat.getDataElement(dataValue.getDataElement()); + + EventDataValue eventDataValue = new EventDataValue(); + eventDataValue.setDataElement(dataElement.getUid()); + eventDataValue.setCreated(DateUtils.fromInstant(dataValue.getCreatedAt())); + eventDataValue.setCreatedByUserInfo(UserInfoSnapshot.from(user)); + eventDataValue.setValue(dataValue.getValue()); + eventDataValue.setLastUpdated(now); + eventDataValue.setProvidedElsewhere(dataValue.isProvidedElsewhere()); + eventDataValue.setLastUpdatedByUserInfo(UserInfoSnapshot.from(user)); + + dbEvent.getEventDataValues().add(eventDataValue); + } + + if (isNotEmpty(event.getNotes())) { + dbEvent + .getNotes() + .addAll( + event.getNotes().stream() + .map(note -> map(preheat, note, user)) + .collect(Collectors.toSet())); + } + + return dbEvent; + } + + public static @Nonnull Relationship map( + @Nonnull TrackerPreheat preheat, + @Nonnull org.hisp.dhis.tracker.imports.domain.Relationship relationship, + @Nonnull UserDetails user) { + RelationshipType relationshipType = + preheat.getRelationshipType(relationship.getRelationshipType()); + RelationshipItem fromItem = new org.hisp.dhis.relationship.RelationshipItem(); + RelationshipItem toItem = new org.hisp.dhis.relationship.RelationshipItem(); + + Date now = new Date(); + Relationship dbRelationship = new org.hisp.dhis.relationship.Relationship(); + dbRelationship.setUid(relationship.getRelationship()); + dbRelationship.setCreated(now); + dbRelationship.setLastUpdated(now); + dbRelationship.setLastUpdatedBy(preheat.getUserByUid(user.getUid()).orElse(null)); + dbRelationship.setRelationshipType(relationshipType); + dbRelationship.setCreatedAtClient(DateUtils.fromInstant(relationship.getCreatedAtClient())); + + // FROM + fromItem.setRelationship(dbRelationship); + + switch (relationshipType.getFromConstraint().getRelationshipEntity()) { + case TRACKED_ENTITY_INSTANCE -> + fromItem.setTrackedEntity( + preheat.getTrackedEntity(relationship.getFrom().getTrackedEntity())); + case PROGRAM_INSTANCE -> + fromItem.setEnrollment(preheat.getEnrollment(relationship.getFrom().getEnrollment())); + case PROGRAM_STAGE_INSTANCE -> + fromItem.setEvent(preheat.getEvent(relationship.getFrom().getEvent())); + } + + // TO + toItem.setRelationship(dbRelationship); + + switch (relationshipType.getToConstraint().getRelationshipEntity()) { + case TRACKED_ENTITY_INSTANCE -> + toItem.setTrackedEntity( + preheat.getTrackedEntity(relationship.getTo().getTrackedEntity())); + case PROGRAM_INSTANCE -> + toItem.setEnrollment(preheat.getEnrollment(relationship.getTo().getEnrollment())); + case PROGRAM_STAGE_INSTANCE -> + toItem.setEvent(preheat.getEvent(relationship.getTo().getEvent())); + } + + dbRelationship.setFrom(fromItem); + dbRelationship.setTo(toItem); + RelationshipKey relationshipKey = + RelationshipKeySupport.getRelationshipKey(relationship, relationshipType); + dbRelationship.setKey(relationshipKey.asString()); + dbRelationship.setInvertedKey(relationshipKey.inverseKey().asString()); + + return dbRelationship; + } + + private static @Nonnull Note map( + @Nonnull TrackerPreheat preheat, + @Nonnull org.hisp.dhis.tracker.imports.domain.Note note, + @Nonnull UserDetails user) { + Note dbNote = new Note(); + dbNote.setAutoFields(); + dbNote.setUid(note.getNote()); + dbNote.setNoteText(note.getValue()); + + dbNote.setLastUpdatedBy(preheat.getUserByUid(user.getUid()).orElse(null)); + dbNote.setCreator(note.getStoredBy()); + return dbNote; + } + + private static Enrollment getEnrollment( + TrackerPreheat preheat, String enrollment, Program program) { + return switch (program.getProgramType()) { + case WITH_REGISTRATION -> preheat.getEnrollment(enrollment); + case WITHOUT_REGISTRATION -> preheat.getEnrollmentsWithoutRegistration(program.getUid()); + }; + } +} diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/persister/AbstractTrackerPersister.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/persister/AbstractTrackerPersister.java index d6b8b9875e13..2d7839f6b63b 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/persister/AbstractTrackerPersister.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/persister/AbstractTrackerPersister.java @@ -57,6 +57,7 @@ import org.hisp.dhis.tracker.imports.AtomicMode; import org.hisp.dhis.tracker.imports.FlushMode; import org.hisp.dhis.tracker.imports.TrackerIdSchemeParams; +import org.hisp.dhis.tracker.imports.TrackerImportStrategy; import org.hisp.dhis.tracker.imports.bundle.TrackerBundle; import org.hisp.dhis.tracker.imports.domain.Attribute; import org.hisp.dhis.tracker.imports.domain.MetadataIdentifier; @@ -127,14 +128,17 @@ public TrackerTypeReport persist(EntityManager entityManager, TrackerBundle bund // // Save or update the entity // - if (isNew(bundle.getPreheat(), trackerDto)) { + if (bundle.getStrategy(trackerDto) == TrackerImportStrategy.CREATE) { entityManager.persist(convertedDto); typeReport.getStats().incCreated(); typeReport.addEntity(objectReport); updateAttributes( entityManager, bundle.getPreheat(), trackerDto, convertedDto, bundle.getUser()); } else { - if (isUpdatable()) { + if (trackerDto.getTrackerType() == TrackerType.RELATIONSHIP) { + typeReport.getStats().incIgnored(); + // Relationships are not updated. A warning was already added to the report + } else { updateAttributes( entityManager, bundle.getPreheat(), trackerDto, convertedDto, bundle.getUser()); entityManager.merge(convertedDto); @@ -142,8 +146,6 @@ public TrackerTypeReport persist(EntityManager entityManager, TrackerBundle bund typeReport.addEntity(objectReport); Optional.ofNullable(getUpdatedTrackedEntity(convertedDto)) .ifPresent(updatedTrackedEntities::add); - } else { - typeReport.getStats().incIgnored(); } } @@ -224,22 +226,6 @@ protected abstract void updateAttributes( /** Updates the {@link TrackerPreheat} object with the entity that has been persisted */ protected abstract void updatePreheat(TrackerPreheat preheat, V convertedDto); - /** - * informs this persister wether specific entity type should be updated defaults to true, is known - * to be false for Relationships - */ - protected boolean isUpdatable() { - return true; - } - - /** Determines if the given trackerDto belongs to an existing entity */ - protected boolean isNew(TrackerPreheat preheat, T trackerDto) { - return isNew(preheat, trackerDto.getUid()); - } - - /** Determines if the given uid belongs to an existing entity */ - protected abstract boolean isNew(TrackerPreheat preheat, String uid); - /** TODO add comment */ protected abstract TrackerNotificationDataBundle handleNotifications( TrackerBundle bundle, V entity, List triggers); diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/persister/EnrollmentPersister.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/persister/EnrollmentPersister.java index f0421b334628..b1450eef203e 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/persister/EnrollmentPersister.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/persister/EnrollmentPersister.java @@ -39,7 +39,7 @@ import org.hisp.dhis.tracker.acl.TrackedEntityProgramOwnerService; import org.hisp.dhis.tracker.export.trackedentity.TrackedEntityChangeLogService; import org.hisp.dhis.tracker.imports.bundle.TrackerBundle; -import org.hisp.dhis.tracker.imports.converter.TrackerConverterService; +import org.hisp.dhis.tracker.imports.bundle.TrackerObjectsMapper; import org.hisp.dhis.tracker.imports.job.NotificationTrigger; import org.hisp.dhis.tracker.imports.job.TrackerNotificationDataBundle; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; @@ -52,20 +52,14 @@ @Component public class EnrollmentPersister extends AbstractTrackerPersister { - private final TrackerConverterService - enrollmentConverter; - private final TrackedEntityProgramOwnerService trackedEntityProgramOwnerService; public EnrollmentPersister( ReservedValueService reservedValueService, - TrackerConverterService - enrollmentConverter, TrackedEntityProgramOwnerService trackedEntityProgramOwnerService, TrackedEntityChangeLogService trackedEntityChangeLogService) { super(reservedValueService, trackedEntityChangeLogService); - this.enrollmentConverter = enrollmentConverter; this.trackedEntityProgramOwnerService = trackedEntityProgramOwnerService; } @@ -103,11 +97,6 @@ protected void updatePreheat(TrackerPreheat preheat, Enrollment enrollment) { enrollment.getOrganisationUnit()); } - @Override - protected boolean isNew(TrackerPreheat preheat, String uid) { - return preheat.getEnrollment(uid) == null; - } - @Override protected TrackerNotificationDataBundle handleNotifications( TrackerBundle bundle, Enrollment enrollment, List triggers) { @@ -153,7 +142,7 @@ protected List determineNotificationTriggers( @Override protected Enrollment convert( TrackerBundle bundle, org.hisp.dhis.tracker.imports.domain.Enrollment enrollment) { - return enrollmentConverter.from(bundle.getPreheat(), enrollment, bundle.getUser()); + return TrackerObjectsMapper.map(bundle.getPreheat(), enrollment, bundle.getUser()); } @Override @@ -163,16 +152,15 @@ protected TrackerType getType() { @Override protected void persistOwnership(TrackerPreheat preheat, Enrollment entity) { - if (isNew(preheat, entity.getUid())) { - if (preheat.getProgramOwner().get(entity.getTrackedEntity().getUid()) == null - || preheat - .getProgramOwner() - .get(entity.getTrackedEntity().getUid()) - .get(entity.getProgram().getUid()) - == null) { - trackedEntityProgramOwnerService.createTrackedEntityProgramOwner( - entity.getTrackedEntity(), entity.getProgram(), entity.getOrganisationUnit()); - } + if (preheat.getEnrollment(entity.getUid()) == null + && (preheat.getProgramOwner().get(entity.getTrackedEntity().getUid()) == null + || preheat + .getProgramOwner() + .get(entity.getTrackedEntity().getUid()) + .get(entity.getProgram().getUid()) + == null)) { + trackedEntityProgramOwnerService.createTrackedEntityProgramOwner( + entity.getTrackedEntity(), entity.getProgram(), entity.getOrganisationUnit()); } } diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/persister/EventPersister.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/persister/EventPersister.java index ef72605bf325..14a6196a5e93 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/persister/EventPersister.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/persister/EventPersister.java @@ -56,7 +56,7 @@ import org.hisp.dhis.tracker.export.event.TrackedEntityDataValueChangeLog; import org.hisp.dhis.tracker.export.trackedentity.TrackedEntityChangeLogService; import org.hisp.dhis.tracker.imports.bundle.TrackerBundle; -import org.hisp.dhis.tracker.imports.converter.TrackerConverterService; +import org.hisp.dhis.tracker.imports.bundle.TrackerObjectsMapper; import org.hisp.dhis.tracker.imports.domain.DataValue; import org.hisp.dhis.tracker.imports.job.NotificationTrigger; import org.hisp.dhis.tracker.imports.job.TrackerNotificationDataBundle; @@ -71,18 +71,13 @@ @Component public class EventPersister extends AbstractTrackerPersister { - private final TrackerConverterService - eventConverter; - private final EventChangeLogService eventChangeLogService; public EventPersister( ReservedValueService reservedValueService, - TrackerConverterService eventConverter, TrackedEntityChangeLogService trackedEntityChangeLogService, EventChangeLogService eventChangeLogService) { super(reservedValueService, trackedEntityChangeLogService); - this.eventConverter = eventConverter; this.eventChangeLogService = eventChangeLogService; } @@ -91,11 +86,6 @@ protected void updatePreheat(TrackerPreheat preheat, Event event) { preheat.putEvents(Collections.singletonList(event)); } - @Override - protected boolean isNew(TrackerPreheat preheat, String uid) { - return preheat.getEvent(uid) == null; - } - @Override protected TrackerNotificationDataBundle handleNotifications( TrackerBundle bundle, Event event, List triggers) { @@ -136,7 +126,7 @@ protected List determineNotificationTriggers( @Override protected Event convert(TrackerBundle bundle, org.hisp.dhis.tracker.imports.domain.Event event) { - return eventConverter.from(bundle.getPreheat(), event, bundle.getUser()); + return TrackerObjectsMapper.map(bundle.getPreheat(), event, bundle.getUser()); } @Override diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/persister/RelationshipPersister.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/persister/RelationshipPersister.java index 22c0821bf982..0450eb789049 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/persister/RelationshipPersister.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/persister/RelationshipPersister.java @@ -32,8 +32,9 @@ import org.hisp.dhis.reservedvalue.ReservedValueService; import org.hisp.dhis.tracker.TrackerType; import org.hisp.dhis.tracker.export.trackedentity.TrackedEntityChangeLogService; +import org.hisp.dhis.tracker.imports.TrackerImportStrategy; import org.hisp.dhis.tracker.imports.bundle.TrackerBundle; -import org.hisp.dhis.tracker.imports.converter.TrackerConverterService; +import org.hisp.dhis.tracker.imports.bundle.TrackerObjectsMapper; import org.hisp.dhis.tracker.imports.domain.Relationship; import org.hisp.dhis.tracker.imports.job.NotificationTrigger; import org.hisp.dhis.tracker.imports.job.TrackerNotificationDataBundle; @@ -47,23 +48,22 @@ @Component public class RelationshipPersister extends AbstractTrackerPersister { - private final TrackerConverterService - relationshipConverter; public RelationshipPersister( ReservedValueService reservedValueService, - TrackerConverterService - relationshipConverter, TrackedEntityChangeLogService trackedEntityChangeLogService) { super(reservedValueService, trackedEntityChangeLogService); - this.relationshipConverter = relationshipConverter; } @Override protected org.hisp.dhis.relationship.Relationship convert( TrackerBundle bundle, Relationship trackerDto) { - return relationshipConverter.from(bundle.getPreheat(), trackerDto, bundle.getUser()); + if (bundle.getStrategy(trackerDto) == TrackerImportStrategy.UPDATE) { + return null; + } + + return TrackerObjectsMapper.map(bundle.getPreheat(), trackerDto, bundle.getUser()); } @Override @@ -92,28 +92,6 @@ protected void updatePreheat( // NOTHING TO DO } - @Override - protected boolean isUpdatable() { - // We don't want to update relationships. Only CREATE/DELETE is - // supported - // so this method will inform AbstractTrackerPersister to not proceed - // with merge. - return false; - } - - @Override - protected boolean isNew(TrackerPreheat preheat, Relationship trackerDto) { - return preheat.getRelationship(trackerDto) == null; - } - - @Override - protected boolean isNew(TrackerPreheat preheat, String uid) { - // Normally this method is never invoked, since for Relationships - // isNew( TrackerPreheat, Relationship ) is invoked instead - throw new UnsupportedOperationException( - "use isNew(TrackerPreheat preheat, Relationship trackerDto) instead"); - } - @Override protected TrackerNotificationDataBundle handleNotifications( TrackerBundle bundle, diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/persister/TrackedEntityPersister.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/persister/TrackedEntityPersister.java index 71ed7d32035e..13a35a7887ed 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/persister/TrackedEntityPersister.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/persister/TrackedEntityPersister.java @@ -30,13 +30,12 @@ import jakarta.persistence.EntityManager; import java.util.Collections; import java.util.List; -import javax.annotation.Nonnull; import org.hisp.dhis.reservedvalue.ReservedValueService; import org.hisp.dhis.trackedentity.TrackedEntity; import org.hisp.dhis.tracker.TrackerType; import org.hisp.dhis.tracker.export.trackedentity.TrackedEntityChangeLogService; import org.hisp.dhis.tracker.imports.bundle.TrackerBundle; -import org.hisp.dhis.tracker.imports.converter.TrackerConverterService; +import org.hisp.dhis.tracker.imports.bundle.TrackerObjectsMapper; import org.hisp.dhis.tracker.imports.job.NotificationTrigger; import org.hisp.dhis.tracker.imports.job.TrackerNotificationDataBundle; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; @@ -50,18 +49,11 @@ public class TrackedEntityPersister extends AbstractTrackerPersister< org.hisp.dhis.tracker.imports.domain.TrackedEntity, TrackedEntity> { - @Nonnull - private final TrackerConverterService< - org.hisp.dhis.tracker.imports.domain.TrackedEntity, TrackedEntity> - teConverter; public TrackedEntityPersister( ReservedValueService reservedValueService, - TrackerConverterService - teConverter, TrackedEntityChangeLogService trackedEntityChangeLogService) { super(reservedValueService, trackedEntityChangeLogService); - this.teConverter = teConverter; } @Override @@ -93,7 +85,7 @@ protected void updatePreheat(TrackerPreheat preheat, TrackedEntity dto) { @Override protected TrackedEntity convert( TrackerBundle bundle, org.hisp.dhis.tracker.imports.domain.TrackedEntity trackerDto) { - return teConverter.from(bundle.getPreheat(), trackerDto, bundle.getUser()); + return TrackerObjectsMapper.map(bundle.getPreheat(), trackerDto, bundle.getUser()); } @Override @@ -101,11 +93,6 @@ protected TrackerType getType() { return TrackerType.TRACKED_ENTITY; } - @Override - protected boolean isNew(TrackerPreheat preheat, String uid) { - return preheat.getTrackedEntity(uid) == null; - } - @Override protected TrackerNotificationDataBundle handleNotifications( TrackerBundle bundle, TrackedEntity entity, List triggers) { diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/converter/EnrollmentTrackerConverterService.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/converter/EnrollmentTrackerConverterService.java deleted file mode 100644 index 8c212d9902e5..000000000000 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/converter/EnrollmentTrackerConverterService.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright (c) 2004-2022, University of Oslo - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * Neither the name of the HISP project nor the names of its contributors may - * be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package org.hisp.dhis.tracker.imports.converter; - -import static org.apache.commons.collections4.CollectionUtils.isNotEmpty; - -import java.util.Date; -import java.util.stream.Collectors; -import lombok.RequiredArgsConstructor; -import org.apache.commons.lang3.StringUtils; -import org.hisp.dhis.organisationunit.OrganisationUnit; -import org.hisp.dhis.program.Enrollment; -import org.hisp.dhis.program.EnrollmentStatus; -import org.hisp.dhis.program.Program; -import org.hisp.dhis.program.UserInfoSnapshot; -import org.hisp.dhis.trackedentity.TrackedEntity; -import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; -import org.hisp.dhis.user.UserDetails; -import org.hisp.dhis.util.DateUtils; -import org.hisp.dhis.util.ObjectUtils; -import org.springframework.stereotype.Service; - -/** - * @author Morten Olav Hansen - */ -@RequiredArgsConstructor -@Service -public class EnrollmentTrackerConverterService - implements TrackerConverterService< - org.hisp.dhis.tracker.imports.domain.Enrollment, Enrollment> { - private final NotesConverterService notesConverterService; - - @Override - public Enrollment from( - TrackerPreheat preheat, - org.hisp.dhis.tracker.imports.domain.Enrollment enrollment, - UserDetails user) { - Enrollment dbEnrollment = preheat.getEnrollment(enrollment.getEnrollment()); - OrganisationUnit organisationUnit = preheat.getOrganisationUnit(enrollment.getOrgUnit()); - - Program program = preheat.getProgram(enrollment.getProgram()); - - TrackedEntity trackedEntity = preheat.getTrackedEntity(enrollment.getTrackedEntity()); - - Date now = new Date(); - - if (isNewEntity(dbEnrollment)) { - dbEnrollment = new Enrollment(); - dbEnrollment.setUid( - !StringUtils.isEmpty(enrollment.getEnrollment()) - ? enrollment.getEnrollment() - : enrollment.getUid()); - dbEnrollment.setCreated(now); - dbEnrollment.setStoredBy(enrollment.getStoredBy()); - dbEnrollment.setCreatedByUserInfo(UserInfoSnapshot.from(user)); - } - - dbEnrollment.setLastUpdated(now); - dbEnrollment.setLastUpdatedByUserInfo(UserInfoSnapshot.from(user)); - dbEnrollment.setDeleted(false); - dbEnrollment.setCreatedAtClient(DateUtils.fromInstant(enrollment.getCreatedAtClient())); - dbEnrollment.setLastUpdatedAtClient(DateUtils.fromInstant(enrollment.getUpdatedAtClient())); - - Date enrollmentDate = DateUtils.fromInstant(enrollment.getEnrolledAt()); - Date occurredDate = DateUtils.fromInstant(enrollment.getOccurredAt()); - - dbEnrollment.setEnrollmentDate(enrollmentDate); - dbEnrollment.setOccurredDate(occurredDate != null ? occurredDate : enrollmentDate); - dbEnrollment.setOrganisationUnit(organisationUnit); - dbEnrollment.setProgram(program); - dbEnrollment.setTrackedEntity(trackedEntity); - dbEnrollment.setFollowup(enrollment.isFollowUp()); - dbEnrollment.setGeometry(enrollment.getGeometry()); - - EnrollmentStatus previousStatus = dbEnrollment.getStatus(); - dbEnrollment.setStatus( - ObjectUtils.firstNonNull(enrollment.getStatus(), EnrollmentStatus.ACTIVE)); - - if (previousStatus != dbEnrollment.getStatus()) { - if (dbEnrollment.isCompleted()) { - dbEnrollment.setCompletedDate(now); - dbEnrollment.setCompletedBy(user.getUsername()); - } else if (EnrollmentStatus.CANCELLED == dbEnrollment.getStatus()) { - dbEnrollment.setCompletedDate(now); - } - } - - if (isNotEmpty(enrollment.getNotes())) { - dbEnrollment - .getNotes() - .addAll( - enrollment.getNotes().stream() - .map(note -> notesConverterService.from(preheat, note, user)) - .collect(Collectors.toSet())); - } - return dbEnrollment; - } -} diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/converter/EventTrackerConverterService.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/converter/EventTrackerConverterService.java deleted file mode 100644 index 04ed66d24280..000000000000 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/converter/EventTrackerConverterService.java +++ /dev/null @@ -1,170 +0,0 @@ -/* - * Copyright (c) 2004-2022, University of Oslo - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * Neither the name of the HISP project nor the names of its contributors may - * be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package org.hisp.dhis.tracker.imports.converter; - -import static org.apache.commons.collections4.CollectionUtils.isNotEmpty; - -import java.util.Date; -import java.util.Optional; -import java.util.stream.Collectors; -import lombok.RequiredArgsConstructor; -import org.apache.commons.lang3.StringUtils; -import org.hisp.dhis.category.CategoryOptionCombo; -import org.hisp.dhis.dataelement.DataElement; -import org.hisp.dhis.event.EventStatus; -import org.hisp.dhis.eventdatavalue.EventDataValue; -import org.hisp.dhis.organisationunit.OrganisationUnit; -import org.hisp.dhis.program.Enrollment; -import org.hisp.dhis.program.Event; -import org.hisp.dhis.program.Program; -import org.hisp.dhis.program.ProgramStage; -import org.hisp.dhis.program.ProgramType; -import org.hisp.dhis.program.UserInfoSnapshot; -import org.hisp.dhis.tracker.imports.domain.DataValue; -import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; -import org.hisp.dhis.user.UserDetails; -import org.hisp.dhis.util.DateUtils; -import org.springframework.stereotype.Service; - -/** - * @author Morten Olav Hansen - */ -@RequiredArgsConstructor -@Service -public class EventTrackerConverterService - implements TrackerConverterService { - private final NotesConverterService notesConverterService; - - @Override - public Event from( - TrackerPreheat preheat, org.hisp.dhis.tracker.imports.domain.Event event, UserDetails user) { - Event result = preheat.getEvent(event.getEvent()); - ProgramStage programStage = preheat.getProgramStage(event.getProgramStage()); - Program program = preheat.getProgram(event.getProgram()); - OrganisationUnit organisationUnit = preheat.getOrganisationUnit(event.getOrgUnit()); - - Date now = new Date(); - - if (isNewEntity(result)) { - result = new Event(); - result.setUid(!StringUtils.isEmpty(event.getEvent()) ? event.getEvent() : event.getUid()); - result.setCreated(now); - result.setStoredBy(event.getStoredBy()); - result.setCreatedByUserInfo(UserInfoSnapshot.from(user)); - } - result.setCreatedAtClient(DateUtils.fromInstant(event.getCreatedAtClient())); - result.setLastUpdatedByUserInfo(UserInfoSnapshot.from(user)); - result.setLastUpdated(now); - result.setDeleted(false); - result.setLastUpdatedAtClient(DateUtils.fromInstant(event.getUpdatedAtClient())); - result.setEnrollment(getEnrollment(preheat, event.getEnrollment(), program)); - result.setProgramStage(programStage); - result.setOrganisationUnit(organisationUnit); - result.setOccurredDate(DateUtils.fromInstant(event.getOccurredAt())); - result.setScheduledDate(DateUtils.fromInstant(event.getScheduledAt())); - - if (event.getAttributeOptionCombo().isNotBlank()) { - result.setAttributeOptionCombo( - preheat.getCategoryOptionCombo(event.getAttributeOptionCombo())); - } else { - result.setAttributeOptionCombo(preheat.getDefault(CategoryOptionCombo.class)); - } - - result.setGeometry(event.getGeometry()); - - EventStatus currentStatus = event.getStatus(); - EventStatus previousStatus = result.getStatus(); - - if (currentStatus != previousStatus && currentStatus == EventStatus.COMPLETED) { - result.setCompletedDate(now); - result.setCompletedBy(user.getUsername()); - } - - if (currentStatus != EventStatus.COMPLETED) { - result.setCompletedDate(null); - result.setCompletedBy(null); - } - - result.setStatus(currentStatus); - - if (Boolean.TRUE.equals(programStage.isEnableUserAssignment()) - && event.getAssignedUser() != null - && !event.getAssignedUser().isEmpty()) { - Optional assignedUser = - preheat.getUserByUsername(event.getAssignedUser().getUsername()); - assignedUser.ifPresent(result::setAssignedUser); - } - - if (program.isRegistration() - && result.getScheduledDate() == null - && result.getOccurredDate() != null) { - result.setScheduledDate(result.getOccurredDate()); - } - - for (DataValue dataValue : event.getDataValues()) { - EventDataValue eventDataValue = new EventDataValue(); - eventDataValue.setValue(dataValue.getValue()); - eventDataValue.setCreated(DateUtils.fromInstant(dataValue.getCreatedAt())); - eventDataValue.setLastUpdated(now); - eventDataValue.setProvidedElsewhere(dataValue.isProvidedElsewhere()); - // ensure dataElement is referred to by UID as multiple - // dataElementIdSchemes are supported - DataElement dataElement = preheat.getDataElement(dataValue.getDataElement()); - eventDataValue.setDataElement(dataElement.getUid()); - eventDataValue.setLastUpdatedByUserInfo(UserInfoSnapshot.from(user)); - eventDataValue.setCreatedByUserInfo(UserInfoSnapshot.from(user)); - - result.getEventDataValues().add(eventDataValue); - } - - if (isNotEmpty(event.getNotes())) { - result - .getNotes() - .addAll( - event.getNotes().stream() - .map(note -> notesConverterService.from(preheat, note, user)) - .collect(Collectors.toSet())); - } - - return result; - } - - private Enrollment getEnrollment(TrackerPreheat preheat, String enrollment, Program program) { - if (ProgramType.WITH_REGISTRATION == program.getProgramType()) { - return preheat.getEnrollment(enrollment); - } - - if (ProgramType.WITHOUT_REGISTRATION == program.getProgramType()) { - return preheat.getEnrollmentsWithoutRegistration(program.getUid()); - } - - // no valid enrollment given and program not single event, just return - // null - return null; - } -} diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/converter/NotesConverterService.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/converter/NotesConverterService.java deleted file mode 100644 index 5fafdcc5b206..000000000000 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/converter/NotesConverterService.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (c) 2004-2022, University of Oslo - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * Neither the name of the HISP project nor the names of its contributors may - * be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package org.hisp.dhis.tracker.imports.converter; - -import lombok.RequiredArgsConstructor; -import org.hisp.dhis.note.Note; -import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; -import org.hisp.dhis.user.UserDetails; -import org.springframework.stereotype.Service; - -/** - * @author Luciano Fiandesio - */ -@Service -@RequiredArgsConstructor -public class NotesConverterService - implements TrackerConverterService { - - @Override - public Note from( - TrackerPreheat preheat, org.hisp.dhis.tracker.imports.domain.Note note, UserDetails user) { - org.hisp.dhis.note.Note trackerNote = new org.hisp.dhis.note.Note(); - trackerNote.setUid(note.getNote()); - trackerNote.setAutoFields(); - trackerNote.setNoteText(note.getValue()); - - trackerNote.setLastUpdatedBy(preheat.getUserByUid(user.getUid()).orElse(null)); - trackerNote.setCreator(note.getStoredBy()); - return trackerNote; - } -} diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/converter/RelationshipTrackerConverterService.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/converter/RelationshipTrackerConverterService.java deleted file mode 100644 index 0860304e1688..000000000000 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/converter/RelationshipTrackerConverterService.java +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Copyright (c) 2004-2022, University of Oslo - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * Neither the name of the HISP project nor the names of its contributors may - * be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package org.hisp.dhis.tracker.imports.converter; - -import static org.hisp.dhis.relationship.RelationshipEntity.PROGRAM_INSTANCE; -import static org.hisp.dhis.relationship.RelationshipEntity.PROGRAM_STAGE_INSTANCE; -import static org.hisp.dhis.relationship.RelationshipEntity.TRACKED_ENTITY_INSTANCE; - -import java.util.Date; -import org.hisp.dhis.relationship.RelationshipKey; -import org.hisp.dhis.tracker.imports.domain.Relationship; -import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; -import org.hisp.dhis.tracker.imports.util.RelationshipKeySupport; -import org.hisp.dhis.user.UserDetails; -import org.hisp.dhis.util.DateUtils; -import org.springframework.stereotype.Service; - -/** - * @author Enrico Colasante - */ -@Service -public class RelationshipTrackerConverterService - implements TrackerConverterService { - - @Override - public org.hisp.dhis.relationship.Relationship from( - TrackerPreheat preheat, Relationship fromRelationship, UserDetails user) { - org.hisp.dhis.relationship.Relationship toRelationship = - preheat.getRelationship(fromRelationship.getRelationship()); - org.hisp.dhis.relationship.RelationshipType relationshipType = - preheat.getRelationshipType(fromRelationship.getRelationshipType()); - org.hisp.dhis.relationship.RelationshipItem fromItem = - new org.hisp.dhis.relationship.RelationshipItem(); - org.hisp.dhis.relationship.RelationshipItem toItem = - new org.hisp.dhis.relationship.RelationshipItem(); - - Date now = new Date(); - if (toRelationship == null) { - - toRelationship = new org.hisp.dhis.relationship.Relationship(); - toRelationship.setUid(fromRelationship.getRelationship()); - toRelationship.setCreated(now); - } - toRelationship.setLastUpdated(now); - - toRelationship.setRelationshipType(relationshipType); - toRelationship.setCreatedAtClient(DateUtils.fromInstant(fromRelationship.getCreatedAtClient())); - - if (fromRelationship.getRelationship() != null) { - toRelationship.setUid(fromRelationship.getRelationship()); - } - - // FROM - fromItem.setRelationship(toRelationship); - - if (relationshipType - .getFromConstraint() - .getRelationshipEntity() - .equals(TRACKED_ENTITY_INSTANCE)) { - fromItem.setTrackedEntity( - preheat.getTrackedEntity(fromRelationship.getFrom().getTrackedEntity())); - } else if (relationshipType - .getFromConstraint() - .getRelationshipEntity() - .equals(PROGRAM_INSTANCE)) { - fromItem.setEnrollment(preheat.getEnrollment(fromRelationship.getFrom().getEnrollment())); - } else if (relationshipType - .getFromConstraint() - .getRelationshipEntity() - .equals(PROGRAM_STAGE_INSTANCE)) { - fromItem.setEvent(preheat.getEvent(fromRelationship.getFrom().getEvent())); - } - - // TO - toItem.setRelationship(toRelationship); - - if (relationshipType - .getToConstraint() - .getRelationshipEntity() - .equals(TRACKED_ENTITY_INSTANCE)) { - toItem.setTrackedEntity( - preheat.getTrackedEntity(fromRelationship.getTo().getTrackedEntity())); - } else if (relationshipType - .getToConstraint() - .getRelationshipEntity() - .equals(PROGRAM_INSTANCE)) { - toItem.setEnrollment(preheat.getEnrollment(fromRelationship.getTo().getEnrollment())); - } else if (relationshipType - .getToConstraint() - .getRelationshipEntity() - .equals(PROGRAM_STAGE_INSTANCE)) { - toItem.setEvent(preheat.getEvent(fromRelationship.getTo().getEvent())); - } - - toRelationship.setFrom(fromItem); - toRelationship.setTo(toItem); - RelationshipKey relationshipKey = - RelationshipKeySupport.getRelationshipKey(fromRelationship, relationshipType); - toRelationship.setKey(relationshipKey.asString()); - toRelationship.setInvertedKey(relationshipKey.inverseKey().asString()); - - return toRelationship; - } -} diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/converter/TrackedEntityTrackerConverterService.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/converter/TrackedEntityTrackerConverterService.java deleted file mode 100644 index ef2ac37c8ada..000000000000 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/converter/TrackedEntityTrackerConverterService.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright (c) 2004-2022, University of Oslo - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * Neither the name of the HISP project nor the names of its contributors may - * be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package org.hisp.dhis.tracker.imports.converter; - -import java.util.Date; -import org.hisp.dhis.organisationunit.OrganisationUnit; -import org.hisp.dhis.program.UserInfoSnapshot; -import org.hisp.dhis.trackedentity.TrackedEntity; -import org.hisp.dhis.trackedentity.TrackedEntityType; -import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; -import org.hisp.dhis.user.UserDetails; -import org.hisp.dhis.util.DateUtils; -import org.springframework.stereotype.Service; - -/** - * @author Morten Olav Hansen - */ -@Service -public class TrackedEntityTrackerConverterService - implements TrackerConverterService< - org.hisp.dhis.tracker.imports.domain.TrackedEntity, TrackedEntity> { - - @Override - public TrackedEntity from( - TrackerPreheat preheat, - org.hisp.dhis.tracker.imports.domain.TrackedEntity trackedEntity, - UserDetails user) { - TrackedEntity dbTrackedEntity = preheat.getTrackedEntity(trackedEntity.getTrackedEntity()); - OrganisationUnit organisationUnit = preheat.getOrganisationUnit(trackedEntity.getOrgUnit()); - TrackedEntityType trackedEntityType = - preheat.getTrackedEntityType(trackedEntity.getTrackedEntityType()); - - Date now = new Date(); - - if (isNewEntity(dbTrackedEntity)) { - dbTrackedEntity = new TrackedEntity(); - dbTrackedEntity.setUid(trackedEntity.getTrackedEntity()); - dbTrackedEntity.setCreated(now); - dbTrackedEntity.setCreatedByUserInfo(UserInfoSnapshot.from(user)); - } - - dbTrackedEntity.setLastUpdatedByUserInfo(UserInfoSnapshot.from(user)); - dbTrackedEntity.setStoredBy(trackedEntity.getStoredBy()); - dbTrackedEntity.setLastUpdated(now); - dbTrackedEntity.setDeleted(false); - dbTrackedEntity.setPotentialDuplicate(trackedEntity.isPotentialDuplicate()); - dbTrackedEntity.setCreatedAtClient(DateUtils.fromInstant(trackedEntity.getCreatedAtClient())); - dbTrackedEntity.setLastUpdatedAtClient( - DateUtils.fromInstant(trackedEntity.getUpdatedAtClient())); - dbTrackedEntity.setOrganisationUnit(organisationUnit); - dbTrackedEntity.setTrackedEntityType(trackedEntityType); - dbTrackedEntity.setInactive(trackedEntity.isInactive()); - dbTrackedEntity.setGeometry(trackedEntity.getGeometry()); - - return dbTrackedEntity; - } -} diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/converter/TrackerConverterService.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/converter/TrackerConverterService.java deleted file mode 100644 index fb27c2538bf7..000000000000 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/converter/TrackerConverterService.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2004-2022, University of Oslo - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * Neither the name of the HISP project nor the names of its contributors may - * be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package org.hisp.dhis.tracker.imports.converter; - -import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; -import org.hisp.dhis.user.UserDetails; - -/** - * @author Morten Olav Hansen - */ -public interface TrackerConverterService { - T from(TrackerPreheat preheat, F object, UserDetails user); - - default boolean isNewEntity(T entity) { - return entity == null; - } -} diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Enrollment.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Enrollment.java index ab3eeb75a88b..05f6ba440b0f 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Enrollment.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Enrollment.java @@ -47,14 +47,15 @@ @Builder @NoArgsConstructor @AllArgsConstructor +// TODO(DHIS2-18222) Remove unused fields public class Enrollment implements TrackerDto, Serializable { @JsonProperty private String enrollment; - @JsonProperty private Instant createdAt; + @JsonProperty private Instant createdAt; // remove @JsonProperty private Instant createdAtClient; - @JsonProperty private Instant updatedAt; + @JsonProperty private Instant updatedAt; // remove @JsonProperty private Instant updatedAtClient; @@ -62,7 +63,7 @@ public class Enrollment implements TrackerDto, Serializable { @JsonProperty private MetadataIdentifier program; - @JsonProperty private EnrollmentStatus status; + @JsonProperty private EnrollmentStatus status = EnrollmentStatus.ACTIVE; @JsonProperty private MetadataIdentifier orgUnit; @@ -72,23 +73,24 @@ public class Enrollment implements TrackerDto, Serializable { @JsonProperty private boolean followUp; - @JsonProperty private String completedBy; + @JsonProperty private String completedBy; // remove - @JsonProperty private Instant completedAt; + @JsonProperty private Instant completedAt; // remove - @JsonProperty private boolean deleted; + @JsonProperty private boolean deleted; // remove @JsonProperty private String storedBy; - @JsonProperty private User createdBy; + @JsonProperty private User createdBy; // remove - @JsonProperty private User updatedBy; + @JsonProperty private User updatedBy; // remove @JsonProperty private Geometry geometry; - @JsonProperty @Builder.Default private List events = new ArrayList<>(); + @JsonProperty @Builder.Default private List events = new ArrayList<>(); // remove - @JsonProperty @Builder.Default private List relationships = new ArrayList<>(); + @JsonProperty @Builder.Default + private List relationships = new ArrayList<>(); // remove @JsonProperty @Builder.Default private List attributes = new ArrayList<>(); diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Event.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Event.java index 5298abab4e03..569a23b8c388 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Event.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Event.java @@ -50,6 +50,7 @@ @Builder @NoArgsConstructor @AllArgsConstructor +// TODO(DHIS2-18222) Remove unused fields public class Event implements TrackerDto, Serializable { @JsonProperty private String event; @@ -63,7 +64,8 @@ public class Event implements TrackerDto, Serializable { @JsonProperty private MetadataIdentifier orgUnit; - @JsonProperty @Builder.Default private List relationships = new ArrayList<>(); + @JsonProperty @Builder.Default + private List relationships = new ArrayList<>(); // remove @JsonProperty private Instant occurredAt; @@ -71,13 +73,13 @@ public class Event implements TrackerDto, Serializable { @JsonProperty private String storedBy; - @JsonProperty private boolean deleted; + @JsonProperty private boolean deleted; // remove - @JsonProperty private Instant createdAt; + @JsonProperty private Instant createdAt; // remove @JsonProperty private Instant createdAtClient; - @JsonProperty private Instant updatedAt; + @JsonProperty private Instant updatedAt; // remove @JsonProperty private Instant updatedAtClient; @@ -94,9 +96,9 @@ public class Event implements TrackerDto, Serializable { @JsonProperty private User assignedUser; - @JsonProperty private User createdBy; + @JsonProperty private User createdBy; // remove - @JsonProperty private User updatedBy; + @JsonProperty private User updatedBy; // remove @JsonProperty @Builder.Default private Set dataValues = new HashSet<>(); diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Relationship.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Relationship.java index 83beb87b27a5..d46903a91fe0 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Relationship.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Relationship.java @@ -43,23 +43,22 @@ @Builder @NoArgsConstructor @AllArgsConstructor +// TODO(DHIS2-18222) Remove unused fields public class Relationship implements TrackerDto, Serializable { @JsonProperty private String relationship; - @JsonProperty private String relationshipName; + @JsonProperty private String relationshipName; // remove @JsonProperty private MetadataIdentifier relationshipType; - @JsonProperty private Instant createdAt; + @JsonProperty private Instant createdAt; // remove @JsonProperty private Instant createdAtClient; - @JsonProperty private Instant updatedAt; + @JsonProperty private Instant updatedAt; // remove @JsonProperty private boolean bidirectional; - @JsonProperty private boolean deleted; - @JsonProperty private RelationshipItem from; @JsonProperty private RelationshipItem to; diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/TrackedEntity.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/TrackedEntity.java index 29eaf07e3f9d..2f18fc01759c 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/TrackedEntity.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/TrackedEntity.java @@ -46,16 +46,17 @@ @Builder @NoArgsConstructor @AllArgsConstructor +// TODO(DHIS2-18222) Remove unused fields public class TrackedEntity implements TrackerDto, Serializable { @JsonProperty private String trackedEntity; @JsonProperty private MetadataIdentifier trackedEntityType; - @JsonProperty private Instant createdAt; + @JsonProperty private Instant createdAt; // remove @JsonProperty private Instant createdAtClient; - @JsonProperty private Instant updatedAt; + @JsonProperty private Instant updatedAt; // remove @JsonProperty private Instant updatedAtClient; @@ -63,7 +64,7 @@ public class TrackedEntity implements TrackerDto, Serializable { @JsonProperty private boolean inactive; - @JsonProperty private boolean deleted; + @JsonProperty private boolean deleted; // remove @JsonProperty private boolean potentialDuplicate; @@ -71,15 +72,16 @@ public class TrackedEntity implements TrackerDto, Serializable { @JsonProperty private String storedBy; - @JsonProperty private User createdBy; + @JsonProperty private User createdBy; // remove - @JsonProperty private User updatedBy; + @JsonProperty private User updatedBy; // remove - @JsonProperty @Builder.Default private List relationships = new ArrayList<>(); + @JsonProperty @Builder.Default + private List relationships = new ArrayList<>(); // remove @JsonProperty @Builder.Default private List attributes = new ArrayList<>(); - @JsonProperty @Builder.Default private List enrollments = new ArrayList<>(); + @JsonProperty @Builder.Default private List enrollments = new ArrayList<>(); // remove @Override public String getUid() { diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/validation/validator/relationship/SecurityOwnershipValidator.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/validation/validator/relationship/SecurityOwnershipValidator.java index 74b9d9a1abd2..60abf301941f 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/validation/validator/relationship/SecurityOwnershipValidator.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/validation/validator/relationship/SecurityOwnershipValidator.java @@ -31,7 +31,7 @@ import org.hisp.dhis.tracker.acl.TrackerAccessManager; import org.hisp.dhis.tracker.imports.TrackerImportStrategy; import org.hisp.dhis.tracker.imports.bundle.TrackerBundle; -import org.hisp.dhis.tracker.imports.converter.RelationshipTrackerConverterService; +import org.hisp.dhis.tracker.imports.bundle.TrackerObjectsMapper; import org.hisp.dhis.tracker.imports.domain.Relationship; import org.hisp.dhis.tracker.imports.validation.Reporter; import org.hisp.dhis.tracker.imports.validation.ValidationCode; @@ -44,7 +44,6 @@ @RequiredArgsConstructor class SecurityOwnershipValidator implements Validator { private final TrackerAccessManager trackerAccessManager; - private final RelationshipTrackerConverterService relationshipTrackerConverterService; @Override public void validate(Reporter reporter, TrackerBundle bundle, Relationship relationship) { @@ -61,9 +60,7 @@ public void validate(Reporter reporter, TrackerBundle bundle, Relationship relat if (strategy.isCreate() && (!trackerAccessManager .canWrite( - user, - relationshipTrackerConverterService.from( - bundle.getPreheat(), relationship, bundle.getUser())) + user, TrackerObjectsMapper.map(bundle.getPreheat(), relationship, bundle.getUser())) .isEmpty())) { reporter.addError(relationship, ValidationCode.E4020, user, relationship.getRelationship()); } diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerObjectsMapperTest.java b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerObjectsMapperTest.java new file mode 100644 index 000000000000..90f164f7327f --- /dev/null +++ b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerObjectsMapperTest.java @@ -0,0 +1,699 @@ +/* + * Copyright (c) 2004-2022, University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of the HISP project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.hisp.dhis.tracker.imports.bundle; + +import static org.hisp.dhis.program.EnrollmentStatus.ACTIVE; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + +import java.util.Date; +import java.util.List; +import java.util.Set; +import org.hisp.dhis.category.CategoryOptionCombo; +import org.hisp.dhis.common.CodeGenerator; +import org.hisp.dhis.event.EventStatus; +import org.hisp.dhis.note.Note; +import org.hisp.dhis.organisationunit.OrganisationUnit; +import org.hisp.dhis.program.Enrollment; +import org.hisp.dhis.program.EnrollmentStatus; +import org.hisp.dhis.program.Event; +import org.hisp.dhis.program.Program; +import org.hisp.dhis.program.ProgramStage; +import org.hisp.dhis.program.UserInfoSnapshot; +import org.hisp.dhis.relationship.Relationship; +import org.hisp.dhis.relationship.RelationshipKey; +import org.hisp.dhis.relationship.RelationshipType; +import org.hisp.dhis.test.TestBase; +import org.hisp.dhis.trackedentity.TrackedEntity; +import org.hisp.dhis.trackedentity.TrackedEntityType; +import org.hisp.dhis.tracker.imports.TrackerIdSchemeParam; +import org.hisp.dhis.tracker.imports.domain.MetadataIdentifier; +import org.hisp.dhis.tracker.imports.domain.RelationshipItem; +import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; +import org.hisp.dhis.tracker.imports.util.RelationshipKeySupport; +import org.hisp.dhis.user.User; +import org.hisp.dhis.user.UserDetails; +import org.hisp.dhis.util.DateUtils; +import org.hisp.dhis.util.ObjectUtils; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class TrackerObjectsMapperTest extends TestBase { + private static final Date NOW = new Date(); + + private static final Date YESTERDAY = DateUtils.addDays(NOW, -1); + + private static final String TE_UID = CodeGenerator.generateUid(); + + private static final String ENROLLMENT_UID = CodeGenerator.generateUid(); + + private static final String EVENT_UID = CodeGenerator.generateUid(); + + private static final String RELATIONSHIP_UID = CodeGenerator.generateUid(); + + private static final String NOTE_UID = CodeGenerator.generateUid(); + + private static final String PROGRAM_STAGE_UID = CodeGenerator.generateUid(); + + private static final String ORGANISATION_UNIT_UID = CodeGenerator.generateUid(); + + private static final String PROGRAM_UID = CodeGenerator.generateUid(); + + private static final String COC_UID = CodeGenerator.generateUid(); + + private static final String TE_TO_ENROLLMENT_RELATIONSHIP_TYPE = CodeGenerator.generateUid(); + + private static final String TE_TO_EVENT_RELATIONSHIP_TYPE = CodeGenerator.generateUid(); + + private static final String TE_TO_TE_RELATIONSHIP_TYPE = CodeGenerator.generateUid(); + + private static final Date today = new Date(); + + public TrackerPreheat preheat; + + private final Program program = createProgram('A'); + + private ProgramStage programStage; + + private OrganisationUnit organisationUnit; + + private TrackedEntityType trackerEntityType; + + private CategoryOptionCombo defaultCategoryOptionCombo; + + private UserDetails updatingUser; + + private UserDetails creatingUser; + + @BeforeEach + void setUpTest() { + User user = makeUser("C"); + creatingUser = UserDetails.fromUser(user); + User userB = makeUser("U"); + updatingUser = UserDetails.fromUser(userB); + + trackerEntityType = createTrackedEntityType('A'); + programStage = createProgramStage('A', 1); + programStage.setUid(PROGRAM_STAGE_UID); + programStage.setProgram(program); + programStage.setEnableUserAssignment(true); + organisationUnit = createOrganisationUnit('A'); + organisationUnit.setUid(ORGANISATION_UNIT_UID); + program.setUid(PROGRAM_UID); + + defaultCategoryOptionCombo = createCategoryOptionCombo('C'); + CategoryOptionCombo categoryOptionCombo = createCategoryOptionCombo('C'); + categoryOptionCombo.setUid(COC_UID); + + RelationshipType teToEnrollmentRelationshipType = + createTeToEnrollmentRelationshipType('A', program, trackerEntityType, true); + teToEnrollmentRelationshipType.setUid(TE_TO_ENROLLMENT_RELATIONSHIP_TYPE); + RelationshipType teToEventRelationshipType = + createTeToEventRelationshipType('B', program, trackerEntityType, false); + teToEventRelationshipType.setUid(TE_TO_EVENT_RELATIONSHIP_TYPE); + RelationshipType teToTeRelationshipType = + createPersonToPersonRelationshipType('C', program, trackerEntityType, true); + teToTeRelationshipType.setUid(TE_TO_TE_RELATIONSHIP_TYPE); + + preheat = new TrackerPreheat(); + preheat.put(TrackerIdSchemeParam.UID, programStage); + preheat.put(TrackerIdSchemeParam.UID, program); + preheat.put(TrackerIdSchemeParam.UID, organisationUnit); + preheat.put(TrackerIdSchemeParam.UID, trackerEntityType); + preheat.addUsers(Set.of(user, userB)); + preheat.putDefault(CategoryOptionCombo.class, defaultCategoryOptionCombo); + preheat.put(TrackerIdSchemeParam.UID, categoryOptionCombo); + preheat.put(TrackerIdSchemeParam.UID, teToEnrollmentRelationshipType); + preheat.put(TrackerIdSchemeParam.UID, teToEventRelationshipType); + preheat.put(TrackerIdSchemeParam.UID, teToTeRelationshipType); + } + + @Test + void shouldMapTrackedEntityWhenItIsACreation() { + org.hisp.dhis.tracker.imports.domain.TrackedEntity trackedEntity = + org.hisp.dhis.tracker.imports.domain.TrackedEntity.builder() + .trackedEntity(TE_UID) + .orgUnit(MetadataIdentifier.ofUid(organisationUnit.getUid())) + .trackedEntityType(MetadataIdentifier.ofUid(trackerEntityType)) + .createdAtClient(NOW.toInstant()) + .updatedAtClient(NOW.toInstant()) + .storedBy(creatingUser.getUsername()) + .inactive(true) + .build(); + + TrackedEntity actual = TrackerObjectsMapper.map(preheat, trackedEntity, creatingUser); + + assertMappedTrackedEntity(trackedEntity, actual, creatingUser, creatingUser); + } + + @Test + void shouldMapTrackedEntityWhenItIsAnUpdate() { + preheat.putTrackedEntities(List.of(trackedEntity())); + org.hisp.dhis.tracker.imports.domain.TrackedEntity trackedEntity = + org.hisp.dhis.tracker.imports.domain.TrackedEntity.builder() + .trackedEntity(TE_UID) + .orgUnit(MetadataIdentifier.ofUid(organisationUnit.getUid())) + .trackedEntityType(MetadataIdentifier.ofUid(trackerEntityType)) + .createdAtClient(NOW.toInstant()) + .updatedAtClient(NOW.toInstant()) + .storedBy(creatingUser.getUsername()) + .inactive(true) + .build(); + + TrackedEntity actual = TrackerObjectsMapper.map(preheat, trackedEntity, updatingUser); + + assertMappedTrackedEntity(trackedEntity, actual, creatingUser, updatingUser); + } + + @Test + void shouldMapEnrollmentWhenItIsACreation() { + preheat.putTrackedEntities(List.of(trackedEntity())); + org.hisp.dhis.tracker.imports.domain.Enrollment enrollment = + org.hisp.dhis.tracker.imports.domain.Enrollment.builder() + .enrollment(ENROLLMENT_UID) + .trackedEntity(TE_UID) + .orgUnit(MetadataIdentifier.ofUid(organisationUnit)) + .program(MetadataIdentifier.ofUid(program)) + .createdAtClient(NOW.toInstant()) + .updatedAtClient(NOW.toInstant()) + .enrolledAt(NOW.toInstant()) + .occurredAt(YESTERDAY.toInstant()) + .status(ACTIVE) + .storedBy(creatingUser.getUsername()) + .notes(notes(creatingUser)) + .build(); + + Enrollment actual = TrackerObjectsMapper.map(preheat, enrollment, creatingUser); + + assertMappedEnrollment(enrollment, actual, creatingUser, creatingUser); + assertEquals(YESTERDAY, actual.getOccurredDate()); + assertNull(actual.getCompletedBy()); + assertNull(actual.getCompletedDate()); + } + + @Test + void shouldMapEnrollmentWhenItIsACreationAndOccurredDateIsNull() { + preheat.putTrackedEntities(List.of(trackedEntity())); + org.hisp.dhis.tracker.imports.domain.Enrollment enrollment = + org.hisp.dhis.tracker.imports.domain.Enrollment.builder() + .enrollment(ENROLLMENT_UID) + .trackedEntity(TE_UID) + .orgUnit(MetadataIdentifier.ofUid(organisationUnit)) + .program(MetadataIdentifier.ofUid(program)) + .createdAtClient(NOW.toInstant()) + .updatedAtClient(NOW.toInstant()) + .enrolledAt(NOW.toInstant()) + .status(ACTIVE) + .storedBy(creatingUser.getUsername()) + .notes(notes(creatingUser)) + .build(); + + Enrollment actual = TrackerObjectsMapper.map(preheat, enrollment, creatingUser); + + assertMappedEnrollment(enrollment, actual, creatingUser, creatingUser); + assertEquals(actual.getEnrollmentDate(), actual.getOccurredDate()); + assertNull(actual.getCompletedBy()); + assertNull(actual.getCompletedDate()); + } + + @Test + void shouldMapEnrollmentWhenItIsAnUpdateAndEnrollmentGetCompleted() { + Enrollment savedEnrollment = enrollment(ACTIVE); + preheat.putTrackedEntities(List.of(savedEnrollment.getTrackedEntity())); + preheat.putEnrollments(List.of(savedEnrollment)); + org.hisp.dhis.tracker.imports.domain.Enrollment enrollment = + org.hisp.dhis.tracker.imports.domain.Enrollment.builder() + .enrollment(ENROLLMENT_UID) + .trackedEntity(TE_UID) + .orgUnit(MetadataIdentifier.ofUid(organisationUnit)) + .program(MetadataIdentifier.ofUid(program)) + .createdAtClient(NOW.toInstant()) + .updatedAtClient(NOW.toInstant()) + .enrolledAt(NOW.toInstant()) + .status(EnrollmentStatus.COMPLETED) + .storedBy(creatingUser.getUsername()) + .notes(notes(creatingUser)) + .build(); + + Enrollment actual = TrackerObjectsMapper.map(preheat, enrollment, updatingUser); + + assertMappedEnrollment(enrollment, actual, creatingUser, updatingUser); + assertEquals(actual.getEnrollmentDate(), actual.getOccurredDate()); + assertEquals(updatingUser.getUsername(), actual.getCompletedBy()); + assertNotNull(actual.getCompletedDate()); + } + + @Test + void shouldMapEnrollmentWhenItIsAnUpdateAndEnrollmentGetCancelled() { + Enrollment savedEnrollment = enrollment(ACTIVE); + preheat.putTrackedEntities(List.of(savedEnrollment.getTrackedEntity())); + preheat.putEnrollments(List.of(savedEnrollment)); + org.hisp.dhis.tracker.imports.domain.Enrollment enrollment = + org.hisp.dhis.tracker.imports.domain.Enrollment.builder() + .enrollment(ENROLLMENT_UID) + .trackedEntity(TE_UID) + .orgUnit(MetadataIdentifier.ofUid(organisationUnit)) + .program(MetadataIdentifier.ofUid(program)) + .createdAtClient(NOW.toInstant()) + .updatedAtClient(NOW.toInstant()) + .enrolledAt(NOW.toInstant()) + .status(EnrollmentStatus.CANCELLED) + .storedBy(creatingUser.getUsername()) + .notes(notes(creatingUser)) + .build(); + + Enrollment actual = TrackerObjectsMapper.map(preheat, enrollment, updatingUser); + + assertMappedEnrollment(enrollment, actual, creatingUser, updatingUser); + assertEquals(actual.getEnrollmentDate(), actual.getOccurredDate()); + assertNull(actual.getCompletedBy()); + assertNotNull(actual.getCompletedDate()); + } + + @Test + void shouldMapEnrollmentWhenItIsAnUpdateAndEnrollmentGetUncompleted() { + Enrollment savedEnrollment = enrollment(EnrollmentStatus.COMPLETED); + preheat.putTrackedEntities(List.of(savedEnrollment.getTrackedEntity())); + preheat.putEnrollments(List.of(savedEnrollment)); + org.hisp.dhis.tracker.imports.domain.Enrollment enrollment = + org.hisp.dhis.tracker.imports.domain.Enrollment.builder() + .enrollment(ENROLLMENT_UID) + .trackedEntity(TE_UID) + .orgUnit(MetadataIdentifier.ofUid(organisationUnit)) + .program(MetadataIdentifier.ofUid(program)) + .createdAtClient(NOW.toInstant()) + .updatedAtClient(NOW.toInstant()) + .enrolledAt(NOW.toInstant()) + .status(ACTIVE) + .storedBy(creatingUser.getUsername()) + .notes(notes(creatingUser)) + .build(); + + Enrollment actual = TrackerObjectsMapper.map(preheat, enrollment, updatingUser); + + assertMappedEnrollment(enrollment, actual, creatingUser, updatingUser); + assertEquals(actual.getEnrollmentDate(), actual.getOccurredDate()); + assertNull(actual.getCompletedBy()); + assertNull(actual.getCompletedDate()); + } + + @Test + void shouldMapEventWhenItIsACreation() { + preheat.putEnrollments(List.of(event(EventStatus.ACTIVE).getEnrollment())); + + org.hisp.dhis.tracker.imports.domain.Event event = + org.hisp.dhis.tracker.imports.domain.Event.builder() + .event(EVENT_UID) + .enrollment(ENROLLMENT_UID) + .status(EventStatus.COMPLETED) + .programStage(MetadataIdentifier.ofUid(PROGRAM_STAGE_UID)) + .program(MetadataIdentifier.ofUid(PROGRAM_UID)) + .orgUnit(MetadataIdentifier.ofUid(ORGANISATION_UNIT_UID)) + .attributeOptionCombo(MetadataIdentifier.EMPTY_UID) + .storedBy(creatingUser.getUsername()) + .build(); + + Event result = TrackerObjectsMapper.map(preheat, event, creatingUser); + + assertMappedEvent(event, result, creatingUser, creatingUser); + assertEquals(defaultCategoryOptionCombo.getUid(), result.getAttributeOptionCombo().getUid()); + } + + @Test + void shouldMapEventWithNullCompletedDataWhenStatusIsActive() { + Event dbEvent = event(EventStatus.COMPLETED); + preheat.putEnrollments(List.of(dbEvent.getEnrollment())); + preheat.putEvents(List.of(dbEvent)); + + org.hisp.dhis.tracker.imports.domain.Event event = + org.hisp.dhis.tracker.imports.domain.Event.builder() + .event(EVENT_UID) + .enrollment(ENROLLMENT_UID) + .programStage(MetadataIdentifier.ofUid(PROGRAM_STAGE_UID)) + .program(MetadataIdentifier.ofUid(PROGRAM_UID)) + .orgUnit(MetadataIdentifier.ofUid(ORGANISATION_UNIT_UID)) + .attributeOptionCombo(MetadataIdentifier.EMPTY_UID) + .notes(notes(creatingUser)) + .build(); + + Event result = TrackerObjectsMapper.map(preheat, event, updatingUser); + + assertMappedEvent(event, result, creatingUser, updatingUser); + assertEquals(defaultCategoryOptionCombo.getUid(), result.getAttributeOptionCombo().getUid()); + assertNull(result.getCompletedBy()); + assertNull(result.getCompletedDate()); + } + + @Test + void shouldMapEventWhenStatusIsCompleted() { + Event dbEvent = event(EventStatus.ACTIVE); + preheat.putEnrollments(List.of(dbEvent.getEnrollment())); + preheat.putEvents(List.of(dbEvent)); + + org.hisp.dhis.tracker.imports.domain.Event event = + org.hisp.dhis.tracker.imports.domain.Event.builder() + .event(EVENT_UID) + .enrollment(ENROLLMENT_UID) + .status(EventStatus.COMPLETED) + .programStage(MetadataIdentifier.ofUid(PROGRAM_STAGE_UID)) + .program(MetadataIdentifier.ofUid(PROGRAM_UID)) + .orgUnit(MetadataIdentifier.ofUid(ORGANISATION_UNIT_UID)) + .attributeOptionCombo(MetadataIdentifier.EMPTY_UID) + .notes(notes(creatingUser)) + .build(); + + Event result = TrackerObjectsMapper.map(preheat, event, updatingUser); + + assertMappedEvent(event, result, creatingUser, updatingUser); + assertEquals(defaultCategoryOptionCombo.getUid(), result.getAttributeOptionCombo().getUid()); + assertEquals(updatingUser.getUsername(), result.getCompletedBy()); + assertNotNull(result.getCompletedDate()); + } + + @Test + void shouldMapEventWhenAssignedUserIsPresent() { + Event dbEvent = event(EventStatus.ACTIVE); + preheat.putEnrollments(List.of(dbEvent.getEnrollment())); + preheat.putEvents(List.of(dbEvent)); + + org.hisp.dhis.tracker.imports.domain.User user = + org.hisp.dhis.tracker.imports.domain.User.builder() + .uid(creatingUser.getUid()) + .username(creatingUser.getUsername()) + .build(); + + org.hisp.dhis.tracker.imports.domain.Event event = + org.hisp.dhis.tracker.imports.domain.Event.builder() + .event(EVENT_UID) + .enrollment(ENROLLMENT_UID) + .programStage(MetadataIdentifier.ofUid(PROGRAM_STAGE_UID)) + .program(MetadataIdentifier.ofUid(PROGRAM_UID)) + .orgUnit(MetadataIdentifier.ofUid(ORGANISATION_UNIT_UID)) + .attributeOptionCombo(MetadataIdentifier.EMPTY_UID) + .notes(notes(creatingUser)) + .assignedUser(user) + .build(); + + Event result = TrackerObjectsMapper.map(preheat, event, updatingUser); + + assertMappedEvent(event, result, creatingUser, updatingUser); + assertEquals(defaultCategoryOptionCombo.getUid(), result.getAttributeOptionCombo().getUid()); + assertEquals(event.getAssignedUser().getUid(), result.getAssignedUser().getUid()); + } + + @Test + void shouldMapEventWhenCategoryOptionComboIsPresent() { + Event dbEvent = event(EventStatus.ACTIVE); + preheat.putEnrollments(List.of(dbEvent.getEnrollment())); + preheat.putEvents(List.of(dbEvent)); + + org.hisp.dhis.tracker.imports.domain.User user = + org.hisp.dhis.tracker.imports.domain.User.builder() + .uid(creatingUser.getUid()) + .username(creatingUser.getUsername()) + .build(); + + org.hisp.dhis.tracker.imports.domain.Event event = + org.hisp.dhis.tracker.imports.domain.Event.builder() + .event(EVENT_UID) + .enrollment(ENROLLMENT_UID) + .programStage(MetadataIdentifier.ofUid(PROGRAM_STAGE_UID)) + .program(MetadataIdentifier.ofUid(PROGRAM_UID)) + .orgUnit(MetadataIdentifier.ofUid(ORGANISATION_UNIT_UID)) + .attributeOptionCombo(MetadataIdentifier.EMPTY_UID) + .notes(notes(creatingUser)) + .assignedUser(user) + .attributeOptionCombo(MetadataIdentifier.ofUid(COC_UID)) + .build(); + + Event result = TrackerObjectsMapper.map(preheat, event, updatingUser); + + assertMappedEvent(event, result, creatingUser, updatingUser); + assertEquals( + event.getAttributeOptionCombo().getIdentifier(), result.getAttributeOptionCombo().getUid()); + } + + @Test + void testMapRelationshipFromTEToEnrollment() { + preheat.putTrackedEntities(List.of(trackedEntity())); + preheat.putEnrollments(List.of(enrollment(ACTIVE))); + org.hisp.dhis.tracker.imports.domain.Relationship relationship = + org.hisp.dhis.tracker.imports.domain.Relationship.builder() + .relationship(RELATIONSHIP_UID) + .relationshipType(MetadataIdentifier.ofUid(TE_TO_ENROLLMENT_RELATIONSHIP_TYPE)) + .from(RelationshipItem.builder().trackedEntity(TE_UID).build()) + .to(RelationshipItem.builder().enrollment(ENROLLMENT_UID).build()) + .build(); + + org.hisp.dhis.relationship.Relationship dbRelationship = + TrackerObjectsMapper.map(preheat, relationship, creatingUser); + + assertMappedRelationship(relationship, dbRelationship, creatingUser); + } + + @Test + void testMapRelationshipFromTEToEvent() { + preheat.putTrackedEntities(List.of(trackedEntity())); + preheat.putEvents(List.of(event(EventStatus.ACTIVE))); + org.hisp.dhis.tracker.imports.domain.Relationship relationship = + org.hisp.dhis.tracker.imports.domain.Relationship.builder() + .relationship(RELATIONSHIP_UID) + .relationshipType(MetadataIdentifier.ofUid(TE_TO_EVENT_RELATIONSHIP_TYPE)) + .from(RelationshipItem.builder().trackedEntity(TE_UID).build()) + .to(RelationshipItem.builder().event(EVENT_UID).build()) + .build(); + + org.hisp.dhis.relationship.Relationship dbRelationship = + TrackerObjectsMapper.map(preheat, relationship, creatingUser); + + assertMappedRelationship(relationship, dbRelationship, creatingUser); + } + + @Test + void testMapRelationshipFromTEToTE() { + TrackedEntity toTrackedEntity = trackedEntity(); + String toTrackedEntityUid = CodeGenerator.generateUid(); + toTrackedEntity.setUid(toTrackedEntityUid); + preheat.putTrackedEntities(List.of(toTrackedEntity, trackedEntity())); + org.hisp.dhis.tracker.imports.domain.Relationship relationship = + org.hisp.dhis.tracker.imports.domain.Relationship.builder() + .relationship(RELATIONSHIP_UID) + .relationshipType(MetadataIdentifier.ofUid(TE_TO_TE_RELATIONSHIP_TYPE)) + .from(RelationshipItem.builder().trackedEntity(TE_UID).build()) + .to(RelationshipItem.builder().trackedEntity(toTrackedEntityUid).build()) + .build(); + + org.hisp.dhis.relationship.Relationship dbRelationship = + TrackerObjectsMapper.map(preheat, relationship, creatingUser); + + assertMappedRelationship(relationship, dbRelationship, creatingUser); + } + + private void assertMappedTrackedEntity( + org.hisp.dhis.tracker.imports.domain.TrackedEntity trackedEntity, + TrackedEntity actual, + UserDetails createdBy, + UserDetails updatedBy) { + assertEquals(trackedEntity.getUid(), actual.getUid()); + assertEquals(trackedEntity.getOrgUnit().getIdentifier(), actual.getOrganisationUnit().getUid()); + assertEquals( + trackedEntity.getTrackedEntityType().getIdentifier(), + actual.getTrackedEntityType().getUid()); + assertEquals(UserInfoSnapshot.from(createdBy), actual.getCreatedByUserInfo()); + assertEquals(UserInfoSnapshot.from(updatedBy), actual.getLastUpdatedByUserInfo()); + assertEquals( + DateUtils.fromInstant(trackedEntity.getCreatedAtClient()), actual.getCreatedAtClient()); + assertEquals( + DateUtils.fromInstant(trackedEntity.getUpdatedAtClient()), actual.getLastUpdatedAtClient()); + assertEquals(createdBy.getUsername(), actual.getStoredBy()); + assertEquals(trackedEntity.isPotentialDuplicate(), actual.isPotentialDuplicate()); + assertEquals(trackedEntity.isInactive(), actual.isInactive()); + assertEquals(trackedEntity.getGeometry(), actual.getGeometry()); + } + + private void assertMappedEnrollment( + org.hisp.dhis.tracker.imports.domain.Enrollment enrollment, + Enrollment actual, + UserDetails createdBy, + UserDetails updatedBy) { + assertEquals(enrollment.getUid(), actual.getUid()); + assertEquals(enrollment.getTrackedEntity(), actual.getTrackedEntity().getUid()); + assertEquals(enrollment.getOrgUnit().getIdentifier(), actual.getOrganisationUnit().getUid()); + assertEquals(enrollment.getProgram().getIdentifier(), actual.getProgram().getUid()); + assertEquals(UserInfoSnapshot.from(createdBy), actual.getCreatedByUserInfo()); + assertEquals(UserInfoSnapshot.from(updatedBy), actual.getLastUpdatedByUserInfo()); + assertEquals( + DateUtils.fromInstant(enrollment.getCreatedAtClient()), actual.getCreatedAtClient()); + assertEquals( + DateUtils.fromInstant(enrollment.getUpdatedAtClient()), actual.getLastUpdatedAtClient()); + assertEquals(DateUtils.fromInstant(enrollment.getEnrolledAt()), actual.getEnrollmentDate()); + assertEquals(createdBy.getUsername(), actual.getStoredBy()); + assertEquals(enrollment.getStatus(), actual.getStatus()); + assertEquals(enrollment.getGeometry(), actual.getGeometry()); + assertEquals(enrollment.isFollowUp(), actual.getFollowup()); + assertNotes(enrollment.getNotes(), actual.getNotes(), updatedBy); + } + + private void assertMappedEvent( + org.hisp.dhis.tracker.imports.domain.Event event, + Event actual, + UserDetails createdBy, + UserDetails updatedBy) { + assertEquals(event.getUid(), actual.getUid()); + assertEquals(event.getEnrollment(), actual.getEnrollment().getUid()); + assertEquals(event.getOrgUnit().getIdentifier(), actual.getOrganisationUnit().getUid()); + assertEquals(event.getProgramStage().getIdentifier(), actual.getProgramStage().getUid()); + assertEquals(UserInfoSnapshot.from(createdBy), actual.getCreatedByUserInfo()); + assertEquals(UserInfoSnapshot.from(updatedBy), actual.getLastUpdatedByUserInfo()); + assertEquals(DateUtils.fromInstant(event.getCreatedAtClient()), actual.getCreatedAtClient()); + assertEquals( + DateUtils.fromInstant(event.getUpdatedAtClient()), actual.getLastUpdatedAtClient()); + assertEquals(DateUtils.fromInstant(event.getScheduledAt()), actual.getScheduledDate()); + assertEquals(DateUtils.fromInstant(event.getOccurredAt()), actual.getOccurredDate()); + assertEquals(createdBy.getUsername(), actual.getStoredBy()); + assertEquals(event.getStatus(), actual.getStatus()); + assertEquals(event.getGeometry(), actual.getGeometry()); + assertNotes(event.getNotes(), actual.getNotes(), updatedBy); + } + + private void assertMappedRelationship( + org.hisp.dhis.tracker.imports.domain.Relationship relationship, + Relationship actual, + UserDetails createdBy) { + assertEquals(relationship.getUid(), actual.getUid()); + assertEquals(createdBy.getUid(), actual.getLastUpdatedBy().getUid()); + assertEquals( + DateUtils.fromInstant(relationship.getCreatedAtClient()), actual.getCreatedAtClient()); + assertEquals( + relationship.getRelationshipType().getIdentifier(), actual.getRelationshipType().getUid()); + assertEquals( + relationship.getFrom().getTrackedEntity(), + ObjectUtils.applyIfNotNull(actual.getFrom().getTrackedEntity(), TrackedEntity::getUid)); + assertEquals( + relationship.getFrom().getEnrollment(), + ObjectUtils.applyIfNotNull(actual.getFrom().getEnrollment(), Enrollment::getUid)); + assertEquals( + relationship.getFrom().getEvent(), + ObjectUtils.applyIfNotNull(actual.getFrom().getEvent(), Event::getUid)); + assertEquals( + relationship.getTo().getTrackedEntity(), + ObjectUtils.applyIfNotNull(actual.getTo().getTrackedEntity(), TrackedEntity::getUid)); + assertEquals( + relationship.getTo().getEnrollment(), + ObjectUtils.applyIfNotNull(actual.getTo().getEnrollment(), Enrollment::getUid)); + assertEquals( + relationship.getTo().getEvent(), + ObjectUtils.applyIfNotNull(actual.getTo().getEvent(), Event::getUid)); + RelationshipKey relationshipKey = + RelationshipKeySupport.getRelationshipKey(relationship, actual.getRelationshipType()); + assertEquals(relationshipKey.asString(), actual.getKey()); + assertEquals(relationshipKey.inverseKey().asString(), actual.getInvertedKey()); + } + + private void assertNotes( + List notes, + List dbNotes, + UserDetails updatedBy) { + for (org.hisp.dhis.tracker.imports.domain.Note note : notes) { + Note dbNote = + dbNotes.stream().filter(n -> n.getUid().equals(note.getNote())).findFirst().orElse(null); + assertNotNull(dbNote); + assertEquals(note.getValue(), dbNote.getNoteText()); + assertEquals(note.getStoredBy(), dbNote.getCreator()); + assertEquals(updatedBy.getUid(), dbNote.getLastUpdatedBy().getUid()); + } + } + + private TrackedEntity trackedEntity() { + TrackedEntity dbTrackedEntity = new TrackedEntity(); + dbTrackedEntity.setUid(TE_UID); + dbTrackedEntity.setCreated(NOW); + dbTrackedEntity.setCreatedByUserInfo(UserInfoSnapshot.from(creatingUser)); + + dbTrackedEntity.setLastUpdatedByUserInfo(UserInfoSnapshot.from(updatingUser)); + dbTrackedEntity.setStoredBy(creatingUser.getUsername()); + dbTrackedEntity.setLastUpdated(NOW); + dbTrackedEntity.setDeleted(false); + dbTrackedEntity.setPotentialDuplicate(false); + dbTrackedEntity.setCreatedAtClient(NOW); + dbTrackedEntity.setLastUpdatedAtClient(NOW); + dbTrackedEntity.setOrganisationUnit(organisationUnit); + dbTrackedEntity.setTrackedEntityType(trackerEntityType); + dbTrackedEntity.setInactive(false); + return dbTrackedEntity; + } + + private Enrollment enrollment(EnrollmentStatus status) { + Enrollment dbEnrollment = new Enrollment(); + dbEnrollment.setUid(ENROLLMENT_UID); + dbEnrollment.setCreated(NOW); + dbEnrollment.setCreatedByUserInfo(UserInfoSnapshot.from(creatingUser)); + dbEnrollment.setLastUpdatedByUserInfo(UserInfoSnapshot.from(updatingUser)); + dbEnrollment.setTrackedEntity(trackedEntity()); + dbEnrollment.setOrganisationUnit(organisationUnit); + dbEnrollment.setProgram(program); + dbEnrollment.setCreatedAtClient(NOW); + dbEnrollment.setLastUpdatedAtClient(NOW); + dbEnrollment.setEnrollmentDate(NOW); + dbEnrollment.setOccurredDate(YESTERDAY); + dbEnrollment.setStatus(status); + dbEnrollment.setStoredBy(creatingUser.getUsername()); + return dbEnrollment; + } + + private Event event(EventStatus status) { + Event dbEvent = new Event(); + dbEvent.setUid(EVENT_UID); + dbEvent.setCreated(NOW); + dbEvent.setCreatedByUserInfo(UserInfoSnapshot.from(creatingUser)); + dbEvent.setLastUpdatedByUserInfo(UserInfoSnapshot.from(updatingUser)); + dbEvent.setEnrollment(enrollment(ACTIVE)); + dbEvent.setOrganisationUnit(organisationUnit); + dbEvent.setProgramStage(programStage); + dbEvent.setCreatedAtClient(NOW); + dbEvent.setLastUpdatedAtClient(NOW); + dbEvent.setScheduledDate(NOW); + dbEvent.setOccurredDate(YESTERDAY); + dbEvent.setStatus(status); + dbEvent.setStoredBy(creatingUser.getUsername()); + dbEvent.setAttributeOptionCombo(defaultCategoryOptionCombo); + dbEvent.setOccurredDate(today); + dbEvent.setStoredBy(creatingUser.getUsername()); + return dbEvent; + } + + private List notes(UserDetails user) { + return List.of( + org.hisp.dhis.tracker.imports.domain.Note.builder() + .note(NOTE_UID) + .value("This is a note") + .storedBy(user.getUsername()) + .build()); + } +} diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/converter/EventTrackerConverterServiceTest.java b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/converter/EventTrackerConverterServiceTest.java deleted file mode 100644 index e2c2ddb425cb..000000000000 --- a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/converter/EventTrackerConverterServiceTest.java +++ /dev/null @@ -1,299 +0,0 @@ -/* - * Copyright (c) 2004-2022, University of Oslo - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * Neither the name of the HISP project nor the names of its contributors may - * be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package org.hisp.dhis.tracker.imports.converter; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.mockito.Mockito.when; - -import com.google.common.collect.Sets; -import java.time.Instant; -import java.util.Date; -import java.util.Set; -import org.hisp.dhis.common.CodeGenerator; -import org.hisp.dhis.dataelement.DataElement; -import org.hisp.dhis.event.EventStatus; -import org.hisp.dhis.eventdatavalue.EventDataValue; -import org.hisp.dhis.organisationunit.OrganisationUnit; -import org.hisp.dhis.program.Enrollment; -import org.hisp.dhis.program.Event; -import org.hisp.dhis.program.Program; -import org.hisp.dhis.program.ProgramStage; -import org.hisp.dhis.program.ProgramType; -import org.hisp.dhis.program.UserInfoSnapshot; -import org.hisp.dhis.test.TestBase; -import org.hisp.dhis.trackedentity.TrackedEntity; -import org.hisp.dhis.tracker.imports.domain.DataValue; -import org.hisp.dhis.tracker.imports.domain.MetadataIdentifier; -import org.hisp.dhis.tracker.imports.domain.User; -import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; -import org.hisp.dhis.user.UserDetails; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -/** - * @author Morten Olav Hansen - */ -@ExtendWith(MockitoExtension.class) -class EventTrackerConverterServiceTest extends TestBase { - - private static final String ENROLLMENT_UID = "enrollmentUid"; - - private static final String PROGRAM_STAGE_UID = "ProgramStageUid"; - - private static final String ORGANISATION_UNIT_UID = "OrganisationUnitUid"; - - private static final String PROGRAM_UID = "ProgramUid"; - - private static final String USERNAME = "usernameu"; - - private static final Date today = new Date(); - - private final NotesConverterService notesConverterService = new NotesConverterService(); - - private TrackerConverterService converter; - - @Mock public TrackerPreheat preheat; - - private final Program program = createProgram('A'); - - private ProgramStage programStage; - - private OrganisationUnit organisationUnit; - - private Event dbEvent; - - private org.hisp.dhis.user.User user; - - private UserDetails currentUser; - - @BeforeEach - void setUpTest() { - converter = new EventTrackerConverterService(notesConverterService); - user = makeUser("U"); - currentUser = UserDetails.fromUser(user); - - programStage = createProgramStage('A', 1); - programStage.setUid(PROGRAM_STAGE_UID); - programStage.setProgram(program); - organisationUnit = createOrganisationUnit('A'); - organisationUnit.setUid(ORGANISATION_UNIT_UID); - program.setUid(PROGRAM_UID); - program.setProgramType(ProgramType.WITHOUT_REGISTRATION); - TrackedEntity te = createTrackedEntity(organisationUnit); - Enrollment enrollment = createEnrollment(program, te, organisationUnit); - enrollment.setUid(ENROLLMENT_UID); - dbEvent = new Event(); - dbEvent.setAutoFields(); - dbEvent.setAttributeOptionCombo(createCategoryOptionCombo('C')); - dbEvent.setCreated(today); - dbEvent.setOccurredDate(today); - dbEvent.setEnrollment(enrollment); - dbEvent.setOrganisationUnit(organisationUnit); - dbEvent.setProgramStage(programStage); - dbEvent.setEventDataValues(Sets.newHashSet()); - dbEvent.setScheduledDate(null); - dbEvent.setCompletedDate(null); - dbEvent.setStoredBy(user.getUsername()); - dbEvent.setLastUpdatedByUserInfo(UserInfoSnapshot.from(user)); - dbEvent.setCreatedByUserInfo(UserInfoSnapshot.from(user)); - } - - @Test - void shouldConvertFromEventWithNullCompletedDataWhenStatusIsActive() { - setUpMocks(); - - DataElement dataElement = new DataElement(); - dataElement.setUid(CodeGenerator.generateUid()); - when(preheat.getDataElement(MetadataIdentifier.ofUid(dataElement.getUid()))) - .thenReturn(dataElement); - - org.hisp.dhis.tracker.imports.domain.Event event = - event(dataValue(MetadataIdentifier.ofUid(dataElement.getUid()), "value")); - - Event result = converter.from(preheat, event, currentUser); - - assertNotNull(result); - assertNotNull(result.getProgramStage()); - assertNotNull(result.getProgramStage().getProgram()); - assertNotNull(result.getOrganisationUnit()); - assertEquals(PROGRAM_UID, result.getProgramStage().getProgram().getUid()); - assertEquals(PROGRAM_STAGE_UID, result.getProgramStage().getUid()); - assertEquals(ORGANISATION_UNIT_UID, result.getOrganisationUnit().getUid()); - assertNull(result.getCompletedBy()); - assertNull(result.getCompletedDate()); - Set eventDataValues = result.getEventDataValues(); - eventDataValues.forEach( - e -> { - assertEquals(USERNAME, e.getCreatedByUserInfo().getUsername()); - assertEquals(USERNAME, e.getLastUpdatedByUserInfo().getUsername()); - }); - } - - @Test - void shouldConvertFromEventWithCompletedDataWhenStatusIsCompleted() { - setUpMocks(); - - DataElement dataElement = new DataElement(); - dataElement.setUid(CodeGenerator.generateUid()); - when(preheat.getDataElement(MetadataIdentifier.ofUid(dataElement.getUid()))) - .thenReturn(dataElement); - - org.hisp.dhis.tracker.imports.domain.Event event = - event(dataValue(MetadataIdentifier.ofUid(dataElement.getUid()), "value")); - event.setStatus(EventStatus.COMPLETED); - - Event result = converter.from(preheat, event, currentUser); - - assertNotNull(result); - assertNotNull(result.getProgramStage()); - assertNotNull(result.getProgramStage().getProgram()); - assertNotNull(result.getOrganisationUnit()); - assertEquals(PROGRAM_UID, result.getProgramStage().getProgram().getUid()); - assertEquals(PROGRAM_STAGE_UID, result.getProgramStage().getUid()); - assertEquals(ORGANISATION_UNIT_UID, result.getOrganisationUnit().getUid()); - assertNotNull(result.getCompletedDate()); - assertEquals(USERNAME, result.getCompletedBy()); - Set eventDataValues = result.getEventDataValues(); - eventDataValues.forEach( - e -> { - assertEquals(USERNAME, e.getCreatedByUserInfo().getUsername()); - assertEquals(USERNAME, e.getLastUpdatedByUserInfo().getUsername()); - }); - } - - @Test - void shouldConvertFromExistingEventWithNullCompletedDataWhenStatusIsActive() { - setUpMocks(); - - DataElement dataElement = new DataElement(); - dataElement.setUid(CodeGenerator.generateUid()); - String eventUid = CodeGenerator.generateUid(); - when(preheat.getDataElement(MetadataIdentifier.ofUid(dataElement.getUid()))) - .thenReturn(dataElement); - dbEvent.setStatus(EventStatus.COMPLETED); - when(preheat.getEvent(eventUid)).thenReturn(dbEvent); - - org.hisp.dhis.tracker.imports.domain.Event event = - event(eventUid, dataValue(MetadataIdentifier.ofUid(dataElement.getUid()), "value")); - - Event result = converter.from(preheat, event, currentUser); - - assertNotNull(result); - assertNotNull(result.getProgramStage()); - assertNotNull(result.getProgramStage().getProgram()); - assertNotNull(result.getOrganisationUnit()); - assertEquals(PROGRAM_UID, result.getProgramStage().getProgram().getUid()); - assertEquals(PROGRAM_STAGE_UID, result.getProgramStage().getUid()); - assertEquals(ORGANISATION_UNIT_UID, result.getOrganisationUnit().getUid()); - assertNull(result.getCompletedBy()); - assertNull(result.getCompletedDate()); - Set eventDataValues = result.getEventDataValues(); - eventDataValues.forEach( - e -> { - assertEquals(USERNAME, e.getCreatedByUserInfo().getUsername()); - assertEquals(USERNAME, e.getLastUpdatedByUserInfo().getUsername()); - }); - } - - @Test - void shouldConvertFromExistingEventWithCompletedDataWhenStatusIsCompleted() { - setUpMocks(); - - DataElement dataElement = new DataElement(); - dataElement.setUid(CodeGenerator.generateUid()); - String eventUid = CodeGenerator.generateUid(); - when(preheat.getDataElement(MetadataIdentifier.ofUid(dataElement.getUid()))) - .thenReturn(dataElement); - dbEvent.setStatus(EventStatus.ACTIVE); - when(preheat.getEvent(eventUid)).thenReturn(dbEvent); - - org.hisp.dhis.tracker.imports.domain.Event event = - event(eventUid, dataValue(MetadataIdentifier.ofUid(dataElement.getUid()), "value")); - event.setStatus(EventStatus.COMPLETED); - - Event result = converter.from(preheat, event, currentUser); - - assertNotNull(result); - assertNotNull(result.getProgramStage()); - assertNotNull(result.getProgramStage().getProgram()); - assertNotNull(result.getOrganisationUnit()); - assertEquals(PROGRAM_UID, result.getProgramStage().getProgram().getUid()); - assertEquals(PROGRAM_STAGE_UID, result.getProgramStage().getUid()); - assertEquals(ORGANISATION_UNIT_UID, result.getOrganisationUnit().getUid()); - assertNotNull(result.getCompletedDate()); - assertEquals(USERNAME, result.getCompletedBy()); - Set eventDataValues = result.getEventDataValues(); - eventDataValues.forEach( - e -> { - assertEquals(USERNAME, e.getCreatedByUserInfo().getUsername()); - assertEquals(USERNAME, e.getLastUpdatedByUserInfo().getUsername()); - }); - } - - private void setUpMocks() { - when(preheat.getProgramStage(MetadataIdentifier.ofUid(programStage))).thenReturn(programStage); - when(preheat.getProgram(MetadataIdentifier.ofUid(program))).thenReturn(program); - when(preheat.getOrganisationUnit(MetadataIdentifier.ofUid(organisationUnit))) - .thenReturn(organisationUnit); - } - - private org.hisp.dhis.tracker.imports.domain.Event event(DataValue dataValue) { - return event(null, dataValue); - } - - private org.hisp.dhis.tracker.imports.domain.Event event(String uid, DataValue dataValue) { - return org.hisp.dhis.tracker.imports.domain.Event.builder() - .event(uid) - .programStage(MetadataIdentifier.ofUid(PROGRAM_STAGE_UID)) - .program(MetadataIdentifier.ofUid(PROGRAM_UID)) - .orgUnit(MetadataIdentifier.ofUid(ORGANISATION_UNIT_UID)) - .attributeOptionCombo(MetadataIdentifier.EMPTY_UID) - .dataValues(Sets.newHashSet(dataValue)) - .build(); - } - - private DataValue dataValue(MetadataIdentifier dataElement, String value) { - User trackerUser = User.builder().username(USERNAME).build(); - - return DataValue.builder() - .dataElement(dataElement) - .value(value) - .providedElsewhere(true) - .createdBy(trackerUser) - .updatedBy(trackerUser) - .createdAt(Instant.now()) - .storedBy(USERNAME) - .updatedAt(Instant.now()) - .build(); - } -} diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/converter/NotesConverterServiceTest.java b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/converter/NotesConverterServiceTest.java deleted file mode 100644 index a04e95b1b535..000000000000 --- a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/converter/NotesConverterServiceTest.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright (c) 2004-2022, University of Oslo - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * Neither the name of the HISP project nor the names of its contributors may - * be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package org.hisp.dhis.tracker.imports.converter; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.MatcherAssert.assertThat; - -import java.util.Date; -import java.util.Set; -import org.hisp.dhis.common.CodeGenerator; -import org.hisp.dhis.note.Note; -import org.hisp.dhis.test.TestBase; -import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; -import org.hisp.dhis.user.User; -import org.hisp.dhis.user.UserDetails; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -/** - * @author Luciano Fiandesio - */ -class NotesConverterServiceTest extends TestBase { - - private static final String CURRENT_USER = "usernamea"; - - private NotesConverterService notesConverterService; - - private TrackerPreheat preheat; - - private UserDetails currentUser; - - private User user; - - @BeforeEach - void setUp() { - this.notesConverterService = new NotesConverterService(); - user = makeUser("A"); - currentUser = UserDetails.fromUser(user); - - this.preheat = new TrackerPreheat(); - this.preheat.addUsers(Set.of(user)); - } - - @Test - void verifyConvertTrackerNoteToNote() { - org.hisp.dhis.tracker.imports.domain.Note trackerNote = trackerNote(); - final Note note = notesConverterService.from(preheat, trackerNote, currentUser); - assertNoteValues(note, trackerNote); - } - - @Test - void verifyConvertTrackerNoteToNoteWithNoStoredByDefined() { - org.hisp.dhis.tracker.imports.domain.Note trackerNote = trackerNote(); - trackerNote.setStoredBy(null); - final Note note = notesConverterService.from(preheat, trackerNote, currentUser); - assertNoteValues(note, trackerNote); - } - - private void assertNoteValues(Note note, org.hisp.dhis.tracker.imports.domain.Note trackerNote) { - assertThat(note, is(notNullValue())); - assertThat(note.getUid(), is(trackerNote.getNote())); - assertThat(note.getNoteText(), is(trackerNote.getValue())); - assertThat(note.getCreator(), is(trackerNote.getStoredBy())); - assertThat(note.getLastUpdatedBy().getUsername(), is(CURRENT_USER)); - } - - private org.hisp.dhis.tracker.imports.domain.Note trackerNote() { - String uid = CodeGenerator.generateUid(); - org.hisp.dhis.tracker.imports.domain.User trackerUser = - org.hisp.dhis.tracker.imports.domain.User.builder() - .uid(CodeGenerator.generateUid()) - .username(CURRENT_USER) - .build(); - return org.hisp.dhis.tracker.imports.domain.Note.builder() - .value("Note text for note: " + uid) - .note(uid) - .storedAt(new Date().toInstant()) - .storedBy(CURRENT_USER) - .createdBy(trackerUser) - .build(); - } -} diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/converter/RelationshipTrackerConverterServiceTest.java b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/converter/RelationshipTrackerConverterServiceTest.java deleted file mode 100644 index 50b853595897..000000000000 --- a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/converter/RelationshipTrackerConverterServiceTest.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Copyright (c) 2004-2022, University of Oslo - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * Neither the name of the HISP project nor the names of its contributors may - * be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package org.hisp.dhis.tracker.imports.converter; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.mockito.Mockito.when; - -import org.hisp.dhis.common.CodeGenerator; -import org.hisp.dhis.organisationunit.OrganisationUnit; -import org.hisp.dhis.program.Enrollment; -import org.hisp.dhis.program.Program; -import org.hisp.dhis.relationship.RelationshipType; -import org.hisp.dhis.test.TestBase; -import org.hisp.dhis.trackedentity.TrackedEntity; -import org.hisp.dhis.trackedentity.TrackedEntityType; -import org.hisp.dhis.tracker.imports.domain.MetadataIdentifier; -import org.hisp.dhis.tracker.imports.domain.Relationship; -import org.hisp.dhis.tracker.imports.domain.RelationshipItem; -import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; -import org.hisp.dhis.user.SystemUser; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -/** - * @author Enrico Colasante - */ -@ExtendWith(MockitoExtension.class) -class RelationshipTrackerConverterServiceTest extends TestBase { - - private static final String TE_TO_ENROLLMENT_RELATIONSHIP_TYPE = "xLmPUYJX8Ks"; - - private static final String TE = CodeGenerator.generateUid(); - - private static final String ENROLLMENT = "ENROLLMENT_UID"; - - private static final String RELATIONSHIP_A = "RELATIONSHIP_A_UID"; - - private RelationshipType teToEnrollment; - - private TrackedEntity trackedEntity; - - private Enrollment enrollment; - - private TrackerConverterService - relationshipConverterService; - - @Mock public TrackerPreheat preheat; - - @BeforeEach - protected void setupTest() { - OrganisationUnit organisationUnit = createOrganisationUnit('A'); - Program program = createProgram('A'); - TrackedEntityType teType = createTrackedEntityType('A'); - - teToEnrollment = createTeToEnrollmentRelationshipType('A', program, teType, false); - teToEnrollment.setUid(TE_TO_ENROLLMENT_RELATIONSHIP_TYPE); - - trackedEntity = createTrackedEntity(organisationUnit); - trackedEntity.setTrackedEntityType(teType); - trackedEntity.setUid(TE); - enrollment = createEnrollment(program, trackedEntity, organisationUnit); - enrollment.setUid(ENROLLMENT); - - relationshipConverterService = new RelationshipTrackerConverterService(); - } - - @Test - void testConverterFromRelationship() { - when(preheat.getRelationship(RELATIONSHIP_A)).thenReturn(relationshipAFromDB()); - when(preheat.getRelationshipType(MetadataIdentifier.ofUid(TE_TO_ENROLLMENT_RELATIONSHIP_TYPE))) - .thenReturn(teToEnrollment); - when(preheat.getTrackedEntity(TE)).thenReturn(trackedEntity); - when(preheat.getEnrollment(ENROLLMENT)).thenReturn(enrollment); - - org.hisp.dhis.relationship.Relationship from = - relationshipConverterService.from(preheat, relationshipA(), new SystemUser()); - - assertNotNull(from); - assertEquals(TE, from.getFrom().getTrackedEntity().getUid()); - assertEquals(ENROLLMENT, from.getTo().getEnrollment().getUid()); - } - - private Relationship relationshipA() { - return Relationship.builder() - .relationship(RELATIONSHIP_A) - .relationshipType(MetadataIdentifier.ofUid(TE_TO_ENROLLMENT_RELATIONSHIP_TYPE)) - .from(RelationshipItem.builder().trackedEntity(TE).build()) - .to(RelationshipItem.builder().enrollment(ENROLLMENT).build()) - .build(); - } - - private org.hisp.dhis.relationship.Relationship relationshipAFromDB() { - return createTeToEnrollmentRelationship(trackedEntity, enrollment, teToEnrollment); - } -} diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/validation/validator/relationship/SecurityOwnershipValidatorTest.java b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/validation/validator/relationship/SecurityOwnershipValidatorTest.java index ca1fe7e8822e..e29a91340675 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/validation/validator/relationship/SecurityOwnershipValidatorTest.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/validation/validator/relationship/SecurityOwnershipValidatorTest.java @@ -34,15 +34,18 @@ import static org.hisp.dhis.tracker.imports.validation.validator.AssertValidations.assertHasError; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.lenient; import static org.mockito.Mockito.when; import java.util.List; -import org.hisp.dhis.security.acl.AclService; +import org.hisp.dhis.test.TestBase; import org.hisp.dhis.tracker.acl.TrackerAccessManager; import org.hisp.dhis.tracker.imports.TrackerIdSchemeParams; import org.hisp.dhis.tracker.imports.bundle.TrackerBundle; -import org.hisp.dhis.tracker.imports.converter.RelationshipTrackerConverterService; +import org.hisp.dhis.tracker.imports.bundle.TrackerObjectsMapper; +import org.hisp.dhis.tracker.imports.domain.MetadataIdentifier; import org.hisp.dhis.tracker.imports.domain.Relationship; +import org.hisp.dhis.tracker.imports.domain.RelationshipItem; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.hisp.dhis.tracker.imports.validation.Reporter; import org.hisp.dhis.user.SystemUser; @@ -53,7 +56,7 @@ import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) -class SecurityOwnershipValidatorTest { +class SecurityOwnershipValidatorTest extends TestBase { private SecurityOwnershipValidator validator; @@ -61,12 +64,8 @@ class SecurityOwnershipValidatorTest { @Mock private TrackerPreheat preheat; - @Mock private AclService aclService; - @Mock private TrackerAccessManager trackerAccessManager; - @Mock private RelationshipTrackerConverterService converterService; - private org.hisp.dhis.relationship.Relationship convertedRelationship; private Reporter reporter; @@ -75,16 +74,27 @@ class SecurityOwnershipValidatorTest { @BeforeEach public void setUp() { - validator = new SecurityOwnershipValidator(trackerAccessManager, converterService); + validator = new SecurityOwnershipValidator(trackerAccessManager); + MetadataIdentifier relationshipTypeUid = MetadataIdentifier.ofUid("relationshipType"); when(bundle.getPreheat()).thenReturn(preheat); + lenient() + .when(preheat.getRelationshipType(relationshipTypeUid)) + .thenReturn( + createPersonToPersonRelationshipType( + 'A', createProgram('A'), createTrackedEntityType('A'), false)); TrackerIdSchemeParams idSchemes = TrackerIdSchemeParams.builder().build(); reporter = new Reporter(idSchemes); - relationship = new Relationship(); - relationship.setRelationship("relationshipUid"); - - convertedRelationship = new org.hisp.dhis.relationship.Relationship(); + relationship = + Relationship.builder() + .relationship("relationshipUid") + .relationshipType(relationshipTypeUid) + .from(RelationshipItem.builder().build()) + .to(RelationshipItem.builder().build()) + .build(); + + convertedRelationship = TrackerObjectsMapper.map(preheat, relationship, new SystemUser()); } @Test @@ -92,7 +102,6 @@ void shouldCreateWhenUserHasWriteAccessToRelationship() { SystemUser user = new SystemUser(); when(bundle.getStrategy(relationship)).thenReturn(CREATE); when(bundle.getUser()).thenReturn(user); - when(converterService.from(preheat, relationship, user)).thenReturn(convertedRelationship); when(trackerAccessManager.canWrite(any(), eq(convertedRelationship))).thenReturn(List.of()); validator.validate(reporter, bundle, relationship); @@ -105,7 +114,6 @@ void shouldFailToCreateWhenUserHasNoWriteAccessToRelationship() { SystemUser user = new SystemUser(); when(bundle.getStrategy(relationship)).thenReturn(CREATE); when(bundle.getUser()).thenReturn(user); - when(converterService.from(preheat, relationship, user)).thenReturn(convertedRelationship); when(trackerAccessManager.canWrite(any(), eq(convertedRelationship))) .thenReturn(List.of("error")); diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerBundleServiceTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerBundleServiceTest.java deleted file mode 100644 index 3bfe4bcce816..000000000000 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerBundleServiceTest.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (c) 2004-2022, University of Oslo - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * Neither the name of the HISP project nor the names of its contributors may - * be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package org.hisp.dhis.tracker.imports.bundle; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; - -import java.io.IOException; -import java.util.List; -import org.hisp.dhis.common.IdentifiableObjectManager; -import org.hisp.dhis.organisationunit.OrganisationUnit; -import org.hisp.dhis.program.Program; -import org.hisp.dhis.trackedentity.TrackedEntity; -import org.hisp.dhis.tracker.TrackerTest; -import org.hisp.dhis.tracker.imports.TrackerImportParams; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; -import org.hisp.dhis.user.SystemUser; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; - -/** - * @author Morten Olav Hansen - */ -class TrackerBundleServiceTest extends TrackerTest { - - @Autowired private TrackerBundleService trackerBundleService; - - @Autowired private IdentifiableObjectManager manager; - - @BeforeAll - void setUp() throws IOException { - setUpMetadata("tracker/tracker_basic_metadata.json"); - } - - @Test - void testVerifyMetadata() { - Program program = manager.get(Program.class, "E8o1E9tAppy"); - OrganisationUnit organisationUnit = manager.get(OrganisationUnit.class, "QfUVllTs6cS"); - assertNotNull(program); - assertNotNull(organisationUnit); - assertFalse(program.getProgramStages().isEmpty()); - } - - @Test - void testTrackedEntityImport() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/trackedentity_basic_data.json"); - assertEquals(13, trackerObjects.getTrackedEntities().size()); - TrackerBundle trackerBundle = - trackerBundleService.create(new TrackerImportParams(), trackerObjects, new SystemUser()); - trackerBundleService.commit(trackerBundle); - List trackedEntities = manager.getAll(TrackedEntity.class); - assertEquals(13, trackedEntities.size()); - } -} diff --git a/dhis-2/dhis-test-integration/src/test/resources/tracker/trackedentity_basic_data.json b/dhis-2/dhis-test-integration/src/test/resources/tracker/trackedentity_basic_data.json deleted file mode 100644 index 47caff5f0aa0..000000000000 --- a/dhis-2/dhis-test-integration/src/test/resources/tracker/trackedentity_basic_data.json +++ /dev/null @@ -1,481 +0,0 @@ -{ - "importMode": "COMMIT", - "idSchemes": { - "dataElementIdScheme": { - "idScheme": "UID" - }, - "orgUnitIdScheme": { - "idScheme": "UID" - }, - "programIdScheme": { - "idScheme": "UID" - }, - "programStageIdScheme": { - "idScheme": "UID" - }, - "idScheme": { - "idScheme": "UID" - }, - "categoryOptionComboIdScheme": { - "idScheme": "UID" - }, - "categoryOptionIdScheme": { - "idScheme": "UID" - } - }, - "importStrategy": "CREATE", - "atomicMode": "ALL", - "flushMode": "AUTO", - "validationMode": "FULL", - "skipPatternValidation": false, - "skipSideEffects": false, - "skipRuleEngine": false, - "trackedEntities": [ - { - "trackedEntity": "NCc1rCEOKaY", - "trackedEntityType": { - "idScheme": "UID", - "identifier": "bPJ0FMtcnEh" - }, - "orgUnit": { - "idScheme": "UID", - "identifier": "QfUVllTs6cS" - }, - "inactive": false, - "deleted": false, - "potentialDuplicate": false, - "relationships": [], - "attributes": [ - { - "attribute": { - "idScheme": "UID", - "identifier": "fmBIpOStKkF" - }, - "storedBy": "admin", - "value": "PersonA" - }, - { - "attribute": { - "idScheme": "UID", - "identifier": "sTJvSLN7Kcb" - }, - "storedBy": "admin", - "value": "AddressA" - } - ], - "enrollments": [] - }, - { - "trackedEntity": "PB9VMezGkwA", - "trackedEntityType": { - "idScheme": "UID", - "identifier": "bPJ0FMtcnEh" - }, - "orgUnit": { - "idScheme": "UID", - "identifier": "QfUVllTs6cS" - }, - "inactive": false, - "deleted": false, - "potentialDuplicate": false, - "relationships": [], - "attributes": [ - { - "attribute": { - "idScheme": "UID", - "identifier": "fmBIpOStKkF" - }, - "storedBy": "admin", - "value": "PersonA" - }, - { - "attribute": { - "idScheme": "UID", - "identifier": "sTJvSLN7Kcb" - }, - "storedBy": "admin", - "value": "AddressA" - } - ], - "enrollments": [] - }, - { - "trackedEntity": "N0wuhEwexUW", - "trackedEntityType": { - "idScheme": "UID", - "identifier": "bPJ0FMtcnEh" - }, - "orgUnit": { - "idScheme": "UID", - "identifier": "QfUVllTs6cS" - }, - "inactive": false, - "deleted": false, - "potentialDuplicate": false, - "relationships": [], - "attributes": [ - { - "attribute": { - "idScheme": "UID", - "identifier": "fmBIpOStKkF" - }, - "storedBy": "admin", - "value": "PersonA" - }, - { - "attribute": { - "idScheme": "UID", - "identifier": "sTJvSLN7Kcb" - }, - "storedBy": "admin", - "value": "AddressA" - } - ], - "enrollments": [] - }, - { - "trackedEntity": "g3eF1fJXuSz", - "trackedEntityType": { - "idScheme": "UID", - "identifier": "bPJ0FMtcnEh" - }, - "orgUnit": { - "idScheme": "UID", - "identifier": "QfUVllTs6cS" - }, - "inactive": false, - "deleted": false, - "potentialDuplicate": false, - "relationships": [], - "attributes": [ - { - "attribute": { - "idScheme": "UID", - "identifier": "fmBIpOStKkF" - }, - "storedBy": "admin", - "value": "PersonA" - }, - { - "attribute": { - "idScheme": "UID", - "identifier": "sTJvSLN7Kcb" - }, - "storedBy": "admin", - "value": "AddressA" - } - ], - "enrollments": [] - }, - { - "trackedEntity": "s9D7VHiYLwj", - "trackedEntityType": { - "idScheme": "UID", - "identifier": "bPJ0FMtcnEh" - }, - "orgUnit": { - "idScheme": "UID", - "identifier": "QfUVllTs6cS" - }, - "inactive": false, - "deleted": false, - "potentialDuplicate": false, - "relationships": [], - "attributes": [ - { - "attribute": { - "idScheme": "UID", - "identifier": "fmBIpOStKkF" - }, - "storedBy": "admin", - "value": "PersonA" - }, - { - "attribute": { - "idScheme": "UID", - "identifier": "sTJvSLN7Kcb" - }, - "storedBy": "admin", - "value": "AddressA" - } - ], - "enrollments": [] - }, - { - "trackedEntity": "C5nACFyIFQo", - "trackedEntityType": { - "idScheme": "UID", - "identifier": "bPJ0FMtcnEh" - }, - "orgUnit": { - "idScheme": "UID", - "identifier": "QfUVllTs6cS" - }, - "inactive": false, - "deleted": false, - "potentialDuplicate": false, - "relationships": [], - "attributes": [ - { - "attribute": { - "idScheme": "UID", - "identifier": "fmBIpOStKkF" - }, - "storedBy": "admin", - "value": "PersonA" - }, - { - "attribute": { - "idScheme": "UID", - "identifier": "sTJvSLN7Kcb" - }, - "storedBy": "admin", - "value": "AddressA" - } - ], - "enrollments": [] - }, - { - "trackedEntity": "Jx8Ul6pnRfM", - "trackedEntityType": { - "idScheme": "UID", - "identifier": "bPJ0FMtcnEh" - }, - "orgUnit": { - "idScheme": "UID", - "identifier": "QfUVllTs6cS" - }, - "inactive": false, - "deleted": false, - "potentialDuplicate": false, - "relationships": [], - "attributes": [ - { - "attribute": { - "idScheme": "UID", - "identifier": "fmBIpOStKkF" - }, - "storedBy": "admin", - "value": "PersonA" - }, - { - "attribute": { - "idScheme": "UID", - "identifier": "sTJvSLN7Kcb" - }, - "storedBy": "admin", - "value": "AddressA" - } - ], - "enrollments": [] - }, - { - "trackedEntity": "Z324qoN3WrF", - "trackedEntityType": { - "idScheme": "UID", - "identifier": "bPJ0FMtcnEh" - }, - "orgUnit": { - "idScheme": "UID", - "identifier": "QfUVllTs6cS" - }, - "inactive": false, - "deleted": false, - "potentialDuplicate": false, - "relationships": [], - "attributes": [ - { - "attribute": { - "idScheme": "UID", - "identifier": "fmBIpOStKkF" - }, - "storedBy": "admin", - "value": "PersonA" - }, - { - "attribute": { - "idScheme": "UID", - "identifier": "sTJvSLN7Kcb" - }, - "storedBy": "admin", - "value": "AddressA" - } - ], - "enrollments": [] - }, - { - "trackedEntity": "TGeGZIIt4MO", - "trackedEntityType": { - "idScheme": "UID", - "identifier": "bPJ0FMtcnEh" - }, - "orgUnit": { - "idScheme": "UID", - "identifier": "QfUVllTs6cS" - }, - "inactive": false, - "deleted": false, - "potentialDuplicate": false, - "relationships": [], - "attributes": [ - { - "attribute": { - "idScheme": "UID", - "identifier": "fmBIpOStKkF" - }, - "storedBy": "admin", - "value": "PersonA" - }, - { - "attribute": { - "idScheme": "UID", - "identifier": "sTJvSLN7Kcb" - }, - "storedBy": "admin", - "value": "AddressA" - } - ], - "enrollments": [] - }, - { - "trackedEntity": "KSLmzEjaV6A", - "trackedEntityType": { - "idScheme": "UID", - "identifier": "bPJ0FMtcnEh" - }, - "orgUnit": { - "idScheme": "UID", - "identifier": "QfUVllTs6cS" - }, - "inactive": false, - "deleted": false, - "potentialDuplicate": false, - "relationships": [], - "attributes": [ - { - "attribute": { - "idScheme": "UID", - "identifier": "fmBIpOStKkF" - }, - "storedBy": "admin", - "value": "PersonA" - }, - { - "attribute": { - "idScheme": "UID", - "identifier": "sTJvSLN7Kcb" - }, - "storedBy": "admin", - "value": "AddressA" - } - ], - "enrollments": [] - }, - { - "trackedEntity": "IybbQIQt6rn", - "trackedEntityType": { - "idScheme": "UID", - "identifier": "bPJ0FMtcnEh" - }, - "orgUnit": { - "idScheme": "UID", - "identifier": "QfUVllTs6cS" - }, - "inactive": false, - "deleted": false, - "potentialDuplicate": false, - "relationships": [], - "attributes": [ - { - "attribute": { - "idScheme": "UID", - "identifier": "fmBIpOStKkF" - }, - "storedBy": "admin", - "value": "PersonA" - }, - { - "attribute": { - "idScheme": "UID", - "identifier": "sTJvSLN7Kcb" - }, - "storedBy": "admin", - "value": "AddressA" - } - ], - "enrollments": [] - }, - { - "trackedEntity": "daMwzsKN3oJ", - "trackedEntityType": { - "idScheme": "UID", - "identifier": "bPJ0FMtcnEh" - }, - "orgUnit": { - "idScheme": "UID", - "identifier": "QfUVllTs6cS" - }, - "inactive": false, - "deleted": false, - "potentialDuplicate": false, - "relationships": [], - "attributes": [ - { - "attribute": { - "idScheme": "UID", - "identifier": "fmBIpOStKkF" - }, - "storedBy": "admin", - "value": "PersonA" - }, - { - "attribute": { - "idScheme": "UID", - "identifier": "sTJvSLN7Kcb" - }, - "storedBy": "admin", - "value": "AddressA" - } - ], - "enrollments": [] - }, - { - "trackedEntity": "pcSaZP9lFqu", - "trackedEntityType": { - "idScheme": "UID", - "identifier": "bPJ0FMtcnEh" - }, - "orgUnit": { - "idScheme": "UID", - "identifier": "QfUVllTs6cS" - }, - "inactive": false, - "deleted": false, - "potentialDuplicate": false, - "relationships": [], - "attributes": [ - { - "attribute": { - "idScheme": "UID", - "identifier": "fmBIpOStKkF" - }, - "storedBy": "admin", - "value": "PersonA" - }, - { - "attribute": { - "idScheme": "UID", - "identifier": "sTJvSLN7Kcb" - }, - "storedBy": "admin", - "value": "AddressA" - } - ], - "enrollments": [] - } - ], - "enrollments": [], - "events": [], - "relationships": [], - "username": "system-process" -} \ No newline at end of file diff --git a/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/view/Enrollment.java b/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/view/Enrollment.java index 828a440df1e0..ea6d8b850807 100644 --- a/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/view/Enrollment.java +++ b/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/view/Enrollment.java @@ -69,7 +69,7 @@ public class Enrollment { @JsonProperty private String program; - @JsonProperty private EnrollmentStatus status; + @JsonProperty private EnrollmentStatus status = EnrollmentStatus.ACTIVE; @JsonProperty private String orgUnit;