Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

chore: use EnrollmentService in TE service to get enrollements/events DHIS2-18541 #19723

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@
<one-to-many class="org.hisp.dhis.relationship.RelationshipItem" />
</set>

<list name="notes" table="enrollment_notes" cascade="all-delete-orphan">
<!-- eagerly fetch notes to avoid lazy init exceptions in code outside a hibernate session we do not
exclude notes like we do other collections if the fields parameter does not contain them like in
fields=!notes. reconsider this if we do implement that optimization -->
<list name="notes" table="enrollment_notes" cascade="all-delete-orphan" lazy="false" fetch="join">
<key column="enrollmentid" foreign-key="fk_programinstancecomments_programinstanceid" />
<list-index column="sort_order" base="1" />
<many-to-many class="org.hisp.dhis.note.Note" column="noteid"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@

<property name="eventDataValues" type="jsbEventDataValues" />

<list name="notes" table="event_notes" cascade="all-delete-orphan">
<!-- eagerly fetch notes to avoid lazy init exceptions in code outside a hibernate session we do not
exclude notes like we do other collections if the fields parameter does not contain them like in
fields=!notes. reconsider this if we do implement that optimization -->
<list name="notes" table="event_notes" cascade="all-delete-orphan" lazy="false" fetch="join">
<key column="eventid" foreign-key="fk_programstageinstancecomments_programstageinstanceid" />
<list-index column="sort_order" base="1" />
<many-to-many class="org.hisp.dhis.note.Note" column="noteid"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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.export;

import java.util.Set;
import org.hisp.dhis.program.Enrollment;
import org.hisp.dhis.program.Event;
import org.hisp.dhis.program.Program;
import org.hisp.dhis.trackedentity.TrackedEntity;
import org.hisp.dhis.tracker.imports.preheat.mappers.AttributeValuesMapper;
import org.hisp.dhis.tracker.imports.preheat.mappers.OrganisationUnitMapper;
import org.mapstruct.BeanMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;

// TODO(DHIS2-18883) move this into the relationship service/store
// double-check that we only map whats needed!
@Mapper(uses = {EventMapper.class, OrganisationUnitMapper.class, AttributeValuesMapper.class})
public interface EnrollmentMapper {
@BeanMapping(ignoreByDefault = true)
@Mapping(target = "uid")
@Mapping(target = "code")
@Mapping(target = "user")
@Mapping(target = "completedBy")
@Mapping(target = "completedDate")
@Mapping(target = "program")
@Mapping(target = "trackedEntity", qualifiedByName = "mapTrackedEntityUidOnly")
@Mapping(target = "organisationUnit")
@Mapping(target = "created")
@Mapping(target = "occurredDate")
@Mapping(target = "enrollmentDate")
@Mapping(target = "events")
@Mapping(target = "notes")
@Mapping(target = "deleted")
@Mapping(target = "createdByUserInfo")
@Mapping(target = "lastUpdatedByUserInfo")
@Mapping(target = "status")
Enrollment map(Enrollment enrollment);
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed

Set<Event> map(Set<Event> events);

// relationshipItem.enrollment.trackedEntity is only exported as UID
@Named("mapTrackedEntityUidOnly")
@BeanMapping(ignoreByDefault = true)
@Mapping(target = "uid")
TrackedEntity mapTrackedEntityUidOnly(TrackedEntity trackedEntity);

@BeanMapping(ignoreByDefault = true)
@Mapping(target = "uid")
@Mapping(target = "code")
@Mapping(target = "name")
@Mapping(target = "attributeValues")
@Mapping(target = "trackedEntityType")
@Mapping(target = "programType")
@Mapping(target = "sharing")
@Mapping(target = "accessLevel")
Program map(Program p);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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.export;

import java.util.Set;
import org.hisp.dhis.category.CategoryOption;
import org.hisp.dhis.category.CategoryOptionCombo;
import org.hisp.dhis.program.Enrollment;
import org.hisp.dhis.program.Event;
import org.hisp.dhis.tracker.imports.preheat.mappers.OrganisationUnitMapper;
import org.hisp.dhis.tracker.imports.preheat.mappers.ProgramStageMapper;
import org.mapstruct.BeanMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;

// TODO(DHIS2-18883) move this into the relationship service/store
// double-check that we only map whats needed!
@Mapper(
uses = {
ProgramStageMapper.class,
OrganisationUnitMapper.class,
})
public interface EventMapper {
@BeanMapping(ignoreByDefault = true)
@Mapping(target = "uid")
@Mapping(target = "code")
@Mapping(target = "user")
@Mapping(target = "enrollment", qualifiedByName = "mapEnrollmentUidOnly")
@Mapping(target = "programStage")
@Mapping(target = "status")
@Mapping(target = "attributeOptionCombo")
@Mapping(target = "organisationUnit")
@Mapping(target = "created")
@Mapping(target = "eventDataValues")
@Mapping(target = "notes")
@Mapping(target = "scheduledDate")
@Mapping(target = "occurredDate")
@Mapping(target = "completedDate")
@Mapping(target = "completedBy")
@Mapping(target = "deleted")
@Mapping(target = "createdByUserInfo")
@Mapping(target = "lastUpdatedByUserInfo")
@Mapping(target = "geometry")
Event map(Event event);
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed

@BeanMapping(ignoreByDefault = true)
@Mapping(target = "uid")
@Mapping(target = "categoryOptions")
CategoryOptionCombo map(CategoryOptionCombo categoryOptionCombo);

Set<CategoryOption> map(Set<CategoryOption> categoryOption);

@BeanMapping(ignoreByDefault = true)
@Mapping(target = "uid")
CategoryOption map(CategoryOption categoryOption);

// relationshipItem.event.enrollment is only exported as UID
@Named("mapEnrollmentUidOnly")
@BeanMapping(ignoreByDefault = true)
@Mapping(target = "uid")
Enrollment map(Enrollment enrollment);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (c) 2004-2023, 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.export;

import org.hisp.dhis.relationship.Relationship;
import org.hisp.dhis.relationship.RelationshipItem;
import org.hisp.dhis.tracker.imports.preheat.mappers.RelationshipTypeMapper;
import org.mapstruct.BeanMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;

// TODO(DHIS2-18883) move this into the relationship service/store
// double-check that we only map whats needed!
@Mapper(
uses = {
RelationshipTypeMapper.class,
TrackedEntityMapper.class,
EnrollmentMapper.class,
EventMapper.class
})
public interface RelationshipItemMapper {
@BeanMapping(ignoreByDefault = true)
@Mapping(target = "uid")
@Mapping(target = "relationshipType")
@Mapping(target = "created")
@Mapping(target = "createdAtClient")
@Mapping(target = "lastUpdated")
@Mapping(
target = "from",
source = "from",
qualifiedByName = "mapRelationshipItemWithoutRelationship")
@Mapping(target = "to", source = "to", qualifiedByName = "mapRelationshipItemWithoutRelationship")
Relationship map(Relationship relationship);

@BeanMapping(ignoreByDefault = true)
@Mapping(target = "relationship")
@Mapping(target = "trackedEntity")
@Mapping(target = "enrollment")
@Mapping(target = "event")
RelationshipItem map(RelationshipItem relationshipItem);

// we need to ignore relationship to break the cycle between relationship and relationshipItem
@Named("mapRelationshipItemWithoutRelationship")
@BeanMapping(ignoreByDefault = true)
@Mapping(target = "trackedEntity")
@Mapping(target = "enrollment")
@Mapping(target = "event")
RelationshipItem mapRelationshipItemWithoutRelationship(RelationshipItem relationshipItem);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,45 @@
* (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.export.trackedentity.aggregates.mapper;
package org.hisp.dhis.tracker.export;

import java.sql.ResultSet;
import java.sql.SQLException;
import org.hisp.dhis.note.Note;
import java.util.Set;
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.program.Enrollment;
import org.hisp.dhis.trackedentity.TrackedEntity;
import org.hisp.dhis.tracker.imports.preheat.mappers.AttributeValuesMapper;
import org.hisp.dhis.tracker.imports.preheat.mappers.TrackedEntityTypeMapper;
import org.mapstruct.BeanMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

/**
* @author Luciano Fiandesio
*/
public class NoteRowCallbackHandler extends AbstractMapper<Note> {
@Override
Note getItem(ResultSet rs) throws SQLException {
return getNote(rs);
}
// TODO(DHIS2-18883) move this into the relationship service/store
// double-check that we only map whats needed!
@Mapper(uses = {EnrollmentMapper.class, TrackedEntityTypeMapper.class, AttributeValuesMapper.class})
public interface TrackedEntityMapper {
@BeanMapping(ignoreByDefault = true)
@Mapping(target = "uid")
@Mapping(target = "code")
@Mapping(target = "user")
@Mapping(target = "organisationUnit")
@Mapping(target = "trackedEntityType")
@Mapping(target = "inactive")
@Mapping(target = "enrollments")
@Mapping(target = "created")
@Mapping(target = "trackedEntityAttributeValues")
@Mapping(target = "deleted")
@Mapping(target = "createdByUserInfo")
@Mapping(target = "lastUpdatedByUserInfo")
TrackedEntity map(TrackedEntity trackedEntity);
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed

@Override
String getKeyColumn() {
return "key";
}
Set<Enrollment> map(Set<Enrollment> enrollments);

private Note getNote(ResultSet rs) throws SQLException {
Note note = new Note();
note.setUid(rs.getString("uid"));
note.setNoteText(rs.getString("notetext"));
note.setCreator(rs.getString("creator"));
note.setCreated(rs.getTimestamp("created"));
return note;
}
@BeanMapping(ignoreByDefault = true)
@Mapping(target = "uid")
@Mapping(target = "code")
@Mapping(target = "name")
@Mapping(target = "attributeValues")
@Mapping(target = "user")
@Mapping(target = "parent")
OrganisationUnit map(OrganisationUnit organisationUnit);
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,21 @@
import org.hisp.dhis.tracker.acl.TrackerOwnershipManager;
import org.hisp.dhis.tracker.export.Page;
import org.hisp.dhis.tracker.export.PageParams;
import org.hisp.dhis.tracker.export.RelationshipItemMapper;
import org.hisp.dhis.tracker.export.event.EventOperationParams;
import org.hisp.dhis.tracker.export.event.EventParams;
import org.hisp.dhis.tracker.export.event.EventService;
import org.hisp.dhis.user.UserDetails;
import org.mapstruct.factory.Mappers;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional(readOnly = true)
@RequiredArgsConstructor
@Service("org.hisp.dhis.tracker.export.enrollment.EnrollmentService")
class DefaultEnrollmentService implements EnrollmentService {
private static final RelationshipItemMapper RELATIONSHIP_ITEM_MAPPER =
Mappers.getMapper(RelationshipItemMapper.class);
private final EnrollmentStore enrollmentStore;

private final EventService eventService;
Expand Down Expand Up @@ -145,7 +149,6 @@ private Enrollment getEnrollment(
@Nonnull EnrollmentParams params,
boolean includeDeleted,
@Nonnull UserDetails user) {

Enrollment result = new Enrollment();
result.setId(enrollment.getId());
result.setUid(enrollment.getUid());
Expand Down Expand Up @@ -190,6 +193,7 @@ private Enrollment getEnrollment(
return result;
}

// TODO(DHIS2-18883) move this into the relationship service/store
private Set<RelationshipItem> getRelationshipItems(
UserDetails user, Enrollment enrollment, boolean includeDeleted) {
Set<RelationshipItem> relationshipItems = new HashSet<>();
Expand All @@ -198,7 +202,7 @@ private Set<RelationshipItem> getRelationshipItems(
org.hisp.dhis.relationship.Relationship daoRelationship = relationshipItem.getRelationship();
if (trackerAccessManager.canRead(user, daoRelationship).isEmpty()
&& (includeDeleted || !daoRelationship.isDeleted())) {
relationshipItems.add(relationshipItem);
relationshipItems.add(RELATIONSHIP_ITEM_MAPPER.map(relationshipItem));
}
}

Expand Down
Loading
Loading