diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/enrollment/DefaultEnrollmentService.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/enrollment/DefaultEnrollmentService.java index d9bd8ecab83..73a8ec2ed3e 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/enrollment/DefaultEnrollmentService.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/enrollment/DefaultEnrollmentService.java @@ -228,6 +228,7 @@ public List getEnrollments(@Nonnull Set uids) throws ForbiddenE .toList(); } + @Nonnull @Override public List getEnrollments(@Nonnull EnrollmentOperationParams params) throws ForbiddenException, BadRequestException { @@ -240,6 +241,7 @@ public List getEnrollments(@Nonnull EnrollmentOperationParams params queryParams.getOrganisationUnitMode()); } + @Nonnull @Override public Page getEnrollments( @Nonnull EnrollmentOperationParams params, PageParams pageParams) diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/HibernateTrackedEntityStore.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/HibernateTrackedEntityStore.java index bd0c6579599..5fb09065d40 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/HibernateTrackedEntityStore.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/HibernateTrackedEntityStore.java @@ -182,13 +182,13 @@ public Page getTrackedEntityIds( } private Page getPage( - PageParams pageParams, List teUids, LongSupplier enrollmentCount) { + PageParams pageParams, List ids, LongSupplier enrollmentCount) { if (pageParams.isPageTotal()) { return Page.withTotals( - teUids, pageParams.getPage(), pageParams.getPageSize(), enrollmentCount.getAsLong()); + ids, pageParams.getPage(), pageParams.getPageSize(), enrollmentCount.getAsLong()); } - return Page.withoutTotals(teUids, pageParams.getPage(), pageParams.getPageSize()); + return Page.withoutTotals(ids, pageParams.getPage(), pageParams.getPageSize()); } @Override diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/TrackedEntityIdentifiers.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/TrackedEntityIdentifiers.java index a3a6146ae33..008da7ee7b7 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/TrackedEntityIdentifiers.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/TrackedEntityIdentifiers.java @@ -28,7 +28,7 @@ package org.hisp.dhis.tracker.export.trackedentity; /** - * Temporary solution: tuple of primary key and uid needed by the aggregate store. + * Temporary solution: pair of primary key and uid needed by the aggregate store. * * @deprecated do not use this class! This is a temporary solution that will be removed. */ diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/AsyncUtils.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/AsyncUtils.java index fb3eefbdb91..155b8ace1d2 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/AsyncUtils.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/AsyncUtils.java @@ -58,15 +58,4 @@ static CompletableFuture> conditionalAsyncFetch( ? supplyAsync(supplier, executor) : supplyAsync(ArrayListMultimap::create, executor)); } - - /** - * Executes the Supplier asynchronously using the thread pool from the provided {@see Executor} - * - * @param supplier The Supplier to execute - * @return A CompletableFuture with the result of the Supplier - */ - static CompletableFuture> asyncFetch( - Supplier> supplier, Executor executor) { - return supplyAsync(supplier, executor); - } } diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/Context.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/Context.java index 5acba4b0535..22f186b8d2a 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/Context.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/Context.java @@ -32,6 +32,7 @@ import lombok.Value; import org.hisp.dhis.tracker.export.trackedentity.TrackedEntityParams; import org.hisp.dhis.tracker.export.trackedentity.TrackedEntityQueryParams; +import org.hisp.dhis.user.UserDetails; /** * @author Luciano Fiandesio @@ -48,6 +49,8 @@ class Context { /** The current user uid */ String userUid; + UserDetails userDetails; + /** A list of group ID to which the user belongs */ List userGroups; diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/EnrollmentAggregate.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/EnrollmentAggregate.java index d241ecac630..a14047b58c4 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/EnrollmentAggregate.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/EnrollmentAggregate.java @@ -27,26 +27,20 @@ */ package org.hisp.dhis.tracker.export.trackedentity.aggregates; -import static java.util.concurrent.CompletableFuture.allOf; -import static org.hisp.dhis.tracker.export.trackedentity.aggregates.AsyncUtils.asyncFetch; -import static org.hisp.dhis.tracker.export.trackedentity.aggregates.AsyncUtils.conditionalAsyncFetch; -import static org.hisp.dhis.tracker.export.trackedentity.aggregates.ThreadPoolManager.getPool; - +import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.Multimap; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.LinkedHashSet; import java.util.List; -import java.util.concurrent.CompletableFuture; -import javax.annotation.Nonnull; import lombok.RequiredArgsConstructor; -import org.hisp.dhis.note.Note; +import org.hisp.dhis.common.UID; +import org.hisp.dhis.feedback.BadRequestException; +import org.hisp.dhis.feedback.ForbiddenException; +import org.hisp.dhis.feedback.NotFoundException; +import org.hisp.dhis.organisationunit.OrganisationUnit; import org.hisp.dhis.program.Enrollment; -import org.hisp.dhis.program.Event; -import org.hisp.dhis.relationship.RelationshipItem; -import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue; +import org.hisp.dhis.tracker.export.enrollment.EnrollmentOperationParams; import org.hisp.dhis.tracker.export.enrollment.EnrollmentService; -import org.springframework.beans.factory.annotation.Qualifier; +import org.hisp.dhis.tracker.export.trackedentity.TrackedEntityIdentifiers; +import org.hisp.dhis.user.AuthenticationService; import org.springframework.stereotype.Component; /** @@ -55,16 +49,9 @@ @Component("org.hisp.dhis.tracker.trackedentity.aggregates.EnrollmentAggregate") @RequiredArgsConstructor class EnrollmentAggregate { + private final AuthenticationService authenticationService; private final EnrollmentService enrollmentService; - @Qualifier("org.hisp.dhis.tracker.trackedentity.aggregates.EnrollmentStore") - @Nonnull - private final EnrollmentStore enrollmentStore; - - @Qualifier("org.hisp.dhis.tracker.trackedentity.aggregates.EventAggregate") - @Nonnull - private final EventAggregate eventAggregate; - /** * Key: te uid , value Enrollment * @@ -72,66 +59,44 @@ class EnrollmentAggregate { * @return a MultiMap where key is a {@see TrackedEntity} uid and the key a List of {@see * Enrollment} objects */ - Multimap findByTrackedEntityIds(List ids, Context ctx) { - Multimap enrollments = - enrollmentStore.getEnrollmentsByTrackedEntityIds(ids, ctx); - - if (enrollments.isEmpty()) { - return enrollments; + Multimap findByTrackedEntityIds( + List ids, Context ctx) { + Multimap result = ArrayListMultimap.create(); + + try { + authenticationService.obtainAuthentication(ctx.getUserUid()); + ids.forEach( + id -> { + EnrollmentOperationParams params = + EnrollmentOperationParams.builder() + .trackedEntity(UID.of(id.uid())) + .trackedEntityType(ctx.getQueryParams().getTrackedEntityType()) + .enrollmentParams(ctx.getParams().getEnrollmentParams()) + .orgUnitMode(ctx.getQueryParams().getOrgUnitMode()) + .orgUnits(ctx.getQueryParams().getOrgUnits().toArray(new OrganisationUnit[0])) + .program(ctx.getQueryParams().getProgram()) + .programStartDate(ctx.getQueryParams().getProgramEnrollmentStartDate()) + .programEndDate(ctx.getQueryParams().getProgramEnrollmentEndDate()) + .enrollmentStatus(ctx.getQueryParams().getEnrollmentStatus()) + .followUp(ctx.getQueryParams().getFollowUp()) + .build(); + try { + result.putAll(id.uid(), enrollmentService.getEnrollments(params)); + } catch (BadRequestException e) { + throw new IllegalArgumentException( + "this must be a bug in how the EnrollmentOperationParams are built"); + } catch (ForbiddenException e) { + // ForbiddenExceptions are caused when mapping the EnrollmentOperationParams. These + // params should already have been validated as they are coming from the + // TrackedEntityQueryParams. Other reasons the user does not have access to data will + // not be shown as such items are simply not returned in collections. + } + }); + } catch (NotFoundException e) { + throw new RuntimeException(e); + } finally { + authenticationService.clearAuthentication(); } - - List enrollmentIds = enrollments.values().stream().map(Enrollment::getId).toList(); - - final CompletableFuture> eventAsync = - conditionalAsyncFetch( - ctx.getParams().getEnrollmentParams().isIncludeEvents(), - () -> eventAggregate.findByEnrollmentIds(enrollmentIds, ctx), - getPool()); - - final CompletableFuture> relationshipAsync = - conditionalAsyncFetch( - ctx.getParams().getEnrollmentParams().isIncludeRelationships(), - () -> enrollmentStore.getRelationships(enrollmentIds, ctx), - getPool()); - - final CompletableFuture> notesAsync = - asyncFetch(() -> enrollmentStore.getNotes(enrollmentIds), getPool()); - - final CompletableFuture> attributesAsync = - conditionalAsyncFetch( - ctx.getParams().getTeEnrollmentParams().isIncludeAttributes(), - () -> enrollmentStore.getAttributes(enrollmentIds, ctx), - getPool()); - - return allOf(eventAsync, notesAsync, relationshipAsync, attributesAsync) - .thenApplyAsync( - fn -> { - Multimap events = eventAsync.join(); - Multimap notes = notesAsync.join(); - Multimap relationships = relationshipAsync.join(); - Multimap attributes = attributesAsync.join(); - - for (Enrollment enrollment : enrollments.values()) { - if (ctx.getParams().getTeEnrollmentParams().isIncludeEvents()) { - enrollment.setEvents(new HashSet<>(events.get(enrollment.getUid()))); - } - if (ctx.getParams().getTeEnrollmentParams().isIncludeRelationships()) { - enrollment.setRelationshipItems( - new HashSet<>(relationships.get(enrollment.getUid()))); - } - if (ctx.getParams().getTeEnrollmentParams().isIncludeAttributes()) { - enrollment - .getTrackedEntity() - .setTrackedEntityAttributeValues( - new LinkedHashSet<>(attributes.get(enrollment.getUid()))); - } - - enrollment.setNotes(new ArrayList<>(notes.get(enrollment.getUid()))); - } - - return enrollments; - }, - getPool()) - .join(); + return result; } } diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/EnrollmentStore.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/EnrollmentStore.java deleted file mode 100644 index c4171c88cb3..00000000000 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/EnrollmentStore.java +++ /dev/null @@ -1,117 +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.export.trackedentity.aggregates; - -import com.google.common.collect.ArrayListMultimap; -import com.google.common.collect.Lists; -import com.google.common.collect.Multimap; -import java.util.List; -import org.hisp.dhis.note.Note; -import org.hisp.dhis.program.Enrollment; -import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue; -import org.hisp.dhis.tracker.export.trackedentity.aggregates.mapper.EnrollmentRowCallbackHandler; -import org.hisp.dhis.tracker.export.trackedentity.aggregates.mapper.NoteRowCallbackHandler; -import org.hisp.dhis.tracker.export.trackedentity.aggregates.mapper.ProgramAttributeRowCallbackHandler; -import org.hisp.dhis.tracker.export.trackedentity.aggregates.query.EnrollmentQuery; -import org.hisp.dhis.tracker.export.trackedentity.aggregates.query.ProgramAttributeQuery; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.stereotype.Repository; - -/** - * @author Luciano Fiandesio - */ -@Repository("org.hisp.dhis.tracker.trackedentity.aggregates.EnrollmentStore") -class EnrollmentStore extends AbstractStore { - private static final String GET_ENROLLMENT_SQL_BY_TE = EnrollmentQuery.getQuery(); - - private static final String GET_ATTRIBUTES = ProgramAttributeQuery.getQuery(); - - private static final String GET_NOTES_SQL = - "select en.uid as key, n.uid, n.notetext, " - + "n.creator, n.created " - + "from note n join enrollment_notes enn " - + "on n.noteid = enn.noteid " - + "join enrollment en on enn.enrollmentid = en.enrollmentid " - + "where enn.enrollmentid in (:ids)"; - - private static final String FILTER_OUT_DELETED_ENROLLMENTS = "en.deleted=false"; - - EnrollmentStore(@Qualifier("readOnlyJdbcTemplate") JdbcTemplate jdbcTemplate) { - super(jdbcTemplate); - } - - Multimap getEnrollmentsByTrackedEntityIds(List ids, Context ctx) { - List> teIds = Lists.partition(ids, PARITITION_SIZE); - - Multimap enrollmentMultimap = ArrayListMultimap.create(); - - teIds.forEach( - partition -> - enrollmentMultimap.putAll(getEnrollmentsByTrackedEntityIdsPartitioned(partition, ctx))); - - return enrollmentMultimap; - } - - private Multimap getEnrollmentsByTrackedEntityIdsPartitioned( - List ids, Context ctx) { - EnrollmentRowCallbackHandler handler = new EnrollmentRowCallbackHandler(); - - jdbcTemplate.query( - getQuery( - GET_ENROLLMENT_SQL_BY_TE, - ctx, - " en.programid IN (:programIds)", - FILTER_OUT_DELETED_ENROLLMENTS), - createIdsParam(ids).addValue("programIds", ctx.getPrograms()), - handler); - - return handler.getItems(); - } - - Multimap getNotes(List ids) { - return fetch(GET_NOTES_SQL, new NoteRowCallbackHandler(), ids); - } - - Multimap getAttributes(List ids, Context ctx) { - ProgramAttributeRowCallbackHandler handler = new ProgramAttributeRowCallbackHandler(); - - jdbcTemplate.query( - getQuery( - GET_ATTRIBUTES, ctx, " pa.programid IN (:programIds)", FILTER_OUT_DELETED_ENROLLMENTS), - createIdsParam(ids).addValue("programIds", ctx.getPrograms()), - handler); - - return handler.getItems(); - } - - @Override - String getRelationshipEntityColumn() { - return "enrollmentid"; - } -} diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/EventAggregate.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/EventAggregate.java deleted file mode 100644 index 29fcb93fc89..00000000000 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/EventAggregate.java +++ /dev/null @@ -1,126 +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.export.trackedentity.aggregates; - -import static java.util.concurrent.CompletableFuture.allOf; -import static java.util.concurrent.CompletableFuture.supplyAsync; -import static org.hisp.dhis.tracker.export.trackedentity.aggregates.AsyncUtils.asyncFetch; -import static org.hisp.dhis.tracker.export.trackedentity.aggregates.AsyncUtils.conditionalAsyncFetch; -import static org.hisp.dhis.tracker.export.trackedentity.aggregates.ThreadPoolManager.getPool; - -import com.google.common.collect.Multimap; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.concurrent.CompletableFuture; -import javax.annotation.Nonnull; -import lombok.RequiredArgsConstructor; -import org.hisp.dhis.eventdatavalue.EventDataValue; -import org.hisp.dhis.note.Note; -import org.hisp.dhis.program.Event; -import org.hisp.dhis.relationship.RelationshipItem; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.stereotype.Component; - -/** - * @author Luciano Fiandesio - */ -@Component("org.hisp.dhis.tracker.trackedentity.aggregates.EventAggregate") -@RequiredArgsConstructor -class EventAggregate { - @Qualifier("org.hisp.dhis.tracker.trackedentity.aggregates.EventStore") - @Nonnull - private final EventStore eventStore; - - /** - * Key: enrollment uid -> Value: Event - * - * @param ids a List of {@see Enrollment} Primary Keys - * @param ctx the {@see Context} - * @return a Map where the key is a Enrollment Primary Key, and the value is a List of {@see - * Event} - */ - Multimap findByEnrollmentIds(List ids, Context ctx) { - // Fetch all the Events that are linked to the given Enrollment IDs - - Multimap events = this.eventStore.getEventsByEnrollmentIds(ids, ctx); - - if (events.isEmpty()) { - return events; - } - - List eventIds = events.values().stream().map(Event::getId).toList(); - - /* - * Async fetch Relationships for the given Event ids (only if - * isIncludeRelationships = true) - */ - final CompletableFuture> relationshipAsync = - conditionalAsyncFetch( - ctx.getParams().getEventParams().isIncludeRelationships(), - () -> eventStore.getRelationships(eventIds, ctx), - getPool()); - - /* - * Async fetch Notes for the given Event ids - */ - final CompletableFuture> notesAsync = - asyncFetch(() -> eventStore.getNotes(eventIds), getPool()); - - /* - * Async fetch DataValues for the given Event ids - */ - final CompletableFuture>> dataValuesAsync = - supplyAsync(() -> eventStore.getDataValues(eventIds), getPool()); - - return allOf(dataValuesAsync, notesAsync, relationshipAsync) - .thenApplyAsync( - fn -> { - Map> dataValues = dataValuesAsync.join(); - Multimap notes = notesAsync.join(); - Multimap relationships = relationshipAsync.join(); - - for (Event event : events.values()) { - if (ctx.getParams().getEventParams().isIncludeRelationships()) { - event.setRelationshipItems(new HashSet<>(relationships.get(event.getUid()))); - } - - List dataValuesForEvent = dataValues.get(event.getUid()); - if (dataValuesForEvent != null && !dataValuesForEvent.isEmpty()) { - event.setEventDataValues(new HashSet<>(dataValues.get(event.getUid()))); - } - event.setNotes(new ArrayList<>(notes.get(event.getUid()))); - } - - return events; - }, - getPool()) - .join(); - } -} diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/EventStore.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/EventStore.java deleted file mode 100644 index 925c3aa1406..00000000000 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/EventStore.java +++ /dev/null @@ -1,168 +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.export.trackedentity.aggregates; - -import com.google.common.collect.ArrayListMultimap; -import com.google.common.collect.Lists; -import com.google.common.collect.Multimap; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import org.hisp.dhis.eventdatavalue.EventDataValue; -import org.hisp.dhis.note.Note; -import org.hisp.dhis.program.Event; -import org.hisp.dhis.query.JpaQueryUtils; -import org.hisp.dhis.security.acl.AclService; -import org.hisp.dhis.tracker.export.trackedentity.aggregates.mapper.EventDataValueRowCallbackHandler; -import org.hisp.dhis.tracker.export.trackedentity.aggregates.mapper.EventRowCallbackHandler; -import org.hisp.dhis.tracker.export.trackedentity.aggregates.mapper.NoteRowCallbackHandler; -import org.hisp.dhis.tracker.export.trackedentity.aggregates.query.EventQuery; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.stereotype.Repository; - -/** - * @author Luciano Fiandesio - */ -@Repository("org.hisp.dhis.tracker.trackedentity.aggregates.EventStore") -class EventStore extends AbstractStore { - private static final String GET_EVENTS_SQL = EventQuery.getQuery(); - - private static final String GET_DATAVALUES_SQL = - "select ev.uid as key, " - + "ev.eventdatavalues " - + "from event ev " - + "where ev.eventid in (:ids)"; - - private static final String GET_NOTES_SQL = - "select ev.uid as key, n.uid, n.notetext, " - + "n.creator, n.created " - + "from note n " - + "join event_notes evn " - + "on n.noteid = evn.noteid " - + "join event ev on evn.eventid = ev.eventid " - + "where evn.eventid in (:ids)"; - - private static final String ACL_FILTER_SQL = - "CASE WHEN p.type = 'WITH_REGISTRATION' THEN " - + "p.trackedentitytypeid in (:trackedEntityTypeIds) else true END " - + "AND ev.programstageid in (:programStageIds) AND en.programid IN (:programIds)"; - - private static final String ACL_FILTER_SQL_NO_PROGRAM_STAGE = - "CASE WHEN p.type = 'WITH_REGISTRATION' THEN " - + "p.trackedentitytypeid in (:trackedEntityTypeIds) else true END " - + "AND en.programid IN (:programIds)"; - - private static final String FILTER_OUT_DELETED_EVENTS = "ev.deleted=false"; - - EventStore(JdbcTemplate jdbcTemplate) { - super(jdbcTemplate); - } - - String getRelationshipEntityColumn() { - return "eventid"; - } - - public Multimap getEventsByEnrollmentIds(List enrollmentsId, Context ctx) { - List> enrollmentIdsPartitions = Lists.partition(enrollmentsId, PARITITION_SIZE); - - Multimap eventMultimap = ArrayListMultimap.create(); - - enrollmentIdsPartitions.forEach( - partition -> eventMultimap.putAll(getEventsByEnrollmentIdsPartitioned(partition, ctx))); - - return eventMultimap; - } - - private String getAttributeOptionComboClause(Context ctx) { - return " and ev.attributeoptioncomboid not in (" - + "select distinct(cocco.categoryoptioncomboid) " - + "from categoryoptioncombos_categoryoptions as cocco " - + - // Get inaccessible category options - "where cocco.categoryoptionid not in ( " - + "select co.categoryoptionid " - + "from categoryoption co " - + " where " - + JpaQueryUtils.generateSQlQueryForSharingCheck( - "co.sharing", ctx.getUserUid(), ctx.getUserGroups(), AclService.LIKE_READ_DATA) - + ") )"; - } - - private Multimap getEventsByEnrollmentIdsPartitioned( - List enrollmentsId, Context ctx) { - EventRowCallbackHandler handler = new EventRowCallbackHandler(); - - List programStages = ctx.getProgramStages(); - - String aocSql = ctx.isSuperUser() ? "" : getAttributeOptionComboClause(ctx); - - if (programStages.isEmpty()) { - jdbcTemplate.query( - getQuery( - GET_EVENTS_SQL, - ctx, - ACL_FILTER_SQL_NO_PROGRAM_STAGE + aocSql, - FILTER_OUT_DELETED_EVENTS), - createIdsParam(enrollmentsId) - .addValue("trackedEntityTypeIds", ctx.getTrackedEntityTypes()) - .addValue("programIds", ctx.getPrograms()), - handler); - } else { - jdbcTemplate.query( - getQuery(GET_EVENTS_SQL, ctx, ACL_FILTER_SQL + aocSql, FILTER_OUT_DELETED_EVENTS), - createIdsParam(enrollmentsId) - .addValue("trackedEntityTypeIds", ctx.getTrackedEntityTypes()) - .addValue("programStageIds", programStages) - .addValue("programIds", ctx.getPrograms()), - handler); - } - - return handler.getItems(); - } - - Map> getDataValues(List eventIds) { - Map> dataValueListMultimap = new HashMap<>(); - - Lists.partition(eventIds, PARITITION_SIZE) - .forEach(partition -> dataValueListMultimap.putAll(getDataValuesPartitioned(partition))); - - return dataValueListMultimap; - } - - private Map> getDataValuesPartitioned(List eventIds) { - EventDataValueRowCallbackHandler handler = new EventDataValueRowCallbackHandler(); - - jdbcTemplate.query(GET_DATAVALUES_SQL, createIdsParam(eventIds), handler); - - return handler.getItems(); - } - - Multimap getNotes(List eventIds) { - return fetch(GET_NOTES_SQL, new NoteRowCallbackHandler(), eventIds); - } -} diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/TrackedEntityAggregate.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/TrackedEntityAggregate.java index ef741e610ff..564d1f4ddae 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/TrackedEntityAggregate.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/TrackedEntityAggregate.java @@ -67,6 +67,7 @@ import org.hisp.dhis.tracker.export.trackedentity.TrackedEntityQueryParams; import org.hisp.dhis.user.CurrentUserUtil; import org.hisp.dhis.user.User; +import org.hisp.dhis.user.UserDetails; import org.hisp.dhis.user.UserService; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; @@ -126,6 +127,7 @@ public List find( } List ids = identifiers.stream().map(TrackedEntityIdentifiers::id).toList(); + UserDetails currentUserDetails = CurrentUserUtil.getCurrentUserDetails(); User currentUser = userService.getUserByUsername(CurrentUserUtil.getCurrentUsername()); final Optional user = Optional.ofNullable(currentUser); @@ -156,6 +158,7 @@ public List find( .toBuilder() .userId(u.getId()) .userUid(u.getUid()) + .userDetails(currentUserDetails) .userGroups( userGroupUIDCache.get(u.getUid()).orElse(Collections.emptyList())) .superUser(u.isSuper())) @@ -187,7 +190,7 @@ public List find( final CompletableFuture> enrollmentsAsync = conditionalAsyncFetch( ctx.getParams().isIncludeEnrollments(), - () -> enrollmentAggregate.findByTrackedEntityIds(ids, ctx), + () -> enrollmentAggregate.findByTrackedEntityIds(identifiers, ctx), getPool()); /* diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/TrackedEntityStore.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/TrackedEntityStore.java index 07f2a442b8a..12445faa877 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/TrackedEntityStore.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/TrackedEntityStore.java @@ -163,12 +163,12 @@ Multimap getOwnedTrackedEntities( teds.forEach( partition -> ownedTeisMultiMap.putAll( - getOwnedTeisPartitioned(partition, ctx, skipUserScopeValidation))); + getOwnedTrackedEntitiesPartitioned(partition, ctx, skipUserScopeValidation))); return ownedTeisMultiMap; } - private Multimap getOwnedTeisPartitioned( + private Multimap getOwnedTrackedEntitiesPartitioned( List ids, Context ctx, boolean skipUserScopeValidation) { OwnedTeMapper handler = new OwnedTeMapper(); diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/mapper/EnrollmentRowCallbackHandler.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/mapper/EnrollmentRowCallbackHandler.java deleted file mode 100644 index 205b7725fe1..00000000000 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/mapper/EnrollmentRowCallbackHandler.java +++ /dev/null @@ -1,109 +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.export.trackedentity.aggregates.mapper; - -import static org.hisp.dhis.tracker.export.trackedentity.aggregates.mapper.JsonbToObjectHelper.setUserInfoSnapshot; - -import java.sql.ResultSet; -import java.sql.SQLException; -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.trackedentity.TrackedEntity; -import org.hisp.dhis.trackedentity.TrackedEntityType; -import org.hisp.dhis.tracker.export.trackedentity.aggregates.query.EnrollmentQuery; -import org.hisp.dhis.tracker.export.trackedentity.aggregates.query.EnrollmentQuery.COLUMNS; - -/** - * @author Luciano Fiandesio - */ -public class EnrollmentRowCallbackHandler extends AbstractMapper { - @Override - Enrollment getItem(ResultSet rs) throws SQLException { - return getEnrollment(rs); - } - - @Override - String getKeyColumn() { - return "te_uid"; - } - - private Enrollment getEnrollment(ResultSet rs) throws SQLException { - Enrollment enrollment = new Enrollment(); - enrollment.setId(rs.getLong(EnrollmentQuery.getColumnName(COLUMNS.ID))); - enrollment.setUid(rs.getString(EnrollmentQuery.getColumnName(COLUMNS.UID))); - - MapperGeoUtils.resolveGeometry(rs.getBytes(EnrollmentQuery.getColumnName(COLUMNS.GEOMETRY))) - .ifPresent(enrollment::setGeometry); - - TrackedEntity trackedEntity = new TrackedEntity(); - trackedEntity.setUid(rs.getString(EnrollmentQuery.getColumnName(COLUMNS.TE_UID))); - TrackedEntityType trackedEntityType = new TrackedEntityType(); - trackedEntityType.setUid(rs.getString(EnrollmentQuery.getColumnName(COLUMNS.TE_TYPE_UID))); - trackedEntity.setTrackedEntityType(trackedEntityType); - enrollment.setTrackedEntity(trackedEntity); - - OrganisationUnit orgUnit = new OrganisationUnit(); - orgUnit.setUid(rs.getString(EnrollmentQuery.getColumnName(COLUMNS.ORGUNIT_UID))); - orgUnit.setName(rs.getString(EnrollmentQuery.getColumnName(COLUMNS.ORGUNIT_NAME))); - enrollment.setOrganisationUnit(orgUnit); - - enrollment.setCreated(rs.getTimestamp(EnrollmentQuery.getColumnName(COLUMNS.CREATED))); - enrollment.setCreatedAtClient( - rs.getTimestamp(EnrollmentQuery.getColumnName(COLUMNS.CREATEDCLIENT))); - setUserInfoSnapshot( - rs, EnrollmentQuery.getColumnName(COLUMNS.CREATED_BY), enrollment::setCreatedByUserInfo); - enrollment.setLastUpdated(rs.getTimestamp(EnrollmentQuery.getColumnName(COLUMNS.UPDATED))); - enrollment.setLastUpdatedAtClient( - rs.getTimestamp(EnrollmentQuery.getColumnName(COLUMNS.UPDATEDCLIENT))); - setUserInfoSnapshot( - rs, - EnrollmentQuery.getColumnName(COLUMNS.LAST_UPDATED_BY), - enrollment::setLastUpdatedByUserInfo); - - Program program = new Program(); - program.setUid(rs.getString(EnrollmentQuery.getColumnName(COLUMNS.PROGRAM_UID))); - enrollment.setProgram(program); - - final boolean followUp = rs.getBoolean(EnrollmentQuery.getColumnName(COLUMNS.FOLLOWUP)); - enrollment.setFollowup(rs.wasNull() ? null : followUp); - enrollment.setStatus( - EnrollmentStatus.valueOf(rs.getString(EnrollmentQuery.getColumnName(COLUMNS.STATUS)))); - enrollment.setEnrollmentDate( - rs.getTimestamp(EnrollmentQuery.getColumnName(COLUMNS.ENROLLMENTDATE))); - enrollment.setOccurredDate( - rs.getTimestamp(EnrollmentQuery.getColumnName(COLUMNS.OCCURREDDATE))); - enrollment.setCompletedDate(rs.getTimestamp(EnrollmentQuery.getColumnName(COLUMNS.COMPLETED))); - enrollment.setCompletedBy(rs.getString(EnrollmentQuery.getColumnName(COLUMNS.COMPLETEDBY))); - enrollment.setStoredBy(rs.getString(EnrollmentQuery.getColumnName(COLUMNS.STOREDBY))); - enrollment.setDeleted(rs.getBoolean(EnrollmentQuery.getColumnName(COLUMNS.DELETED))); - - return enrollment; - } -} diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/mapper/EventDataValueRowCallbackHandler.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/mapper/EventDataValueRowCallbackHandler.java deleted file mode 100644 index c5801f9b3c9..00000000000 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/mapper/EventDataValueRowCallbackHandler.java +++ /dev/null @@ -1,104 +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.export.trackedentity.aggregates.mapper; - -import com.google.gson.Gson; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import org.hisp.dhis.eventdatavalue.EventDataValue; -import org.hisp.dhis.program.UserInfoSnapshot; -import org.hisp.dhis.util.DateUtils; -import org.postgresql.util.PGobject; -import org.springframework.jdbc.core.RowCallbackHandler; - -/** - * @author Luciano Fiandesio - */ -public class EventDataValueRowCallbackHandler implements RowCallbackHandler { - - private final Map> dataValues; - - private static final Gson gson = new Gson(); - - public EventDataValueRowCallbackHandler() { - this.dataValues = new HashMap<>(); - } - - @Override - public void processRow(ResultSet rs) throws SQLException { - dataValues.put(rs.getString("key"), getDataValue(rs)); - } - - @SuppressWarnings("unchecked") - private List getDataValue(ResultSet rs) throws SQLException { - // TODO not sure this is the most efficient way to handle JSONB -> java - List result = new ArrayList<>(); - - PGobject values = (PGobject) rs.getObject("eventdatavalues"); - Map eventDataValuesJson = gson.fromJson(values.getValue(), Map.class); - - for (Map.Entry entry : eventDataValuesJson.entrySet()) { - Map jsonValues = (Map) entry.getValue(); - - EventDataValue value = new EventDataValue(entry.getKey(), (String) jsonValues.get("value")); - value.setCreated(DateUtils.parseDate((String) jsonValues.get("created"))); - value.setLastUpdated(DateUtils.parseDate((String) jsonValues.get("lastUpdated"))); - value.setStoredBy((String) jsonValues.get("storedBy")); - value.setProvidedElsewhere((Boolean) jsonValues.get("providedElsewhere")); - value.setCreatedByUserInfo( - buildUserInfoSnapshot((Map) jsonValues.get("createdByUserInfo"))); - value.setLastUpdatedByUserInfo( - buildUserInfoSnapshot((Map) jsonValues.get("lastUpdatedByUserInfo"))); - - result.add(value); - } - - return result; - } - - private UserInfoSnapshot buildUserInfoSnapshot(Map createdByUserInfo) { - if (createdByUserInfo == null) { - return null; - } - - UserInfoSnapshot userInfoSnapshot = new UserInfoSnapshot(); - userInfoSnapshot.setUid((String) createdByUserInfo.get("uid")); - userInfoSnapshot.setUsername((String) createdByUserInfo.get("username")); - userInfoSnapshot.setFirstName((String) createdByUserInfo.get("firstName")); - userInfoSnapshot.setSurname((String) createdByUserInfo.get("surname")); - return userInfoSnapshot; - } - - public Map> getItems() { - return this.dataValues; - } -} diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/mapper/EventRowCallbackHandler.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/mapper/EventRowCallbackHandler.java deleted file mode 100644 index 586577c3ffa..00000000000 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/mapper/EventRowCallbackHandler.java +++ /dev/null @@ -1,152 +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.export.trackedentity.aggregates.mapper; - -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.Set; -import java.util.stream.Collectors; -import org.apache.commons.lang3.StringUtils; -import org.hisp.dhis.category.CategoryOption; -import org.hisp.dhis.category.CategoryOptionCombo; -import org.hisp.dhis.commons.util.TextUtils; -import org.hisp.dhis.event.EventStatus; -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.trackedentity.TrackedEntity; -import org.hisp.dhis.tracker.export.trackedentity.aggregates.query.EventQuery; -import org.hisp.dhis.tracker.export.trackedentity.aggregates.query.EventQuery.COLUMNS; -import org.hisp.dhis.user.User; - -/** - * @author Luciano Fiandesio - */ -public class EventRowCallbackHandler extends AbstractMapper { - - @Override - Event getItem(ResultSet rs) throws SQLException { - return getEvent(rs); - } - - @Override - String getKeyColumn() { - return "enruid"; - } - - private Event getEvent(ResultSet rs) throws SQLException { - Event event = new Event(); - event.setUid(rs.getString(EventQuery.getColumnName(COLUMNS.UID))); - event.setId(rs.getLong(EventQuery.getColumnName(COLUMNS.ID))); - - event.setStatus(EventStatus.valueOf(rs.getString(EventQuery.getColumnName(COLUMNS.STATUS)))); - event.setOccurredDate(rs.getTimestamp(EventQuery.getColumnName(COLUMNS.OCCURRED_DATE))); - event.setScheduledDate(rs.getTimestamp(EventQuery.getColumnName(COLUMNS.SCHEDULED_DATE))); - event.setStoredBy(rs.getString(EventQuery.getColumnName(COLUMNS.STOREDBY))); - event.setCompletedBy(rs.getString(EventQuery.getColumnName(COLUMNS.COMPLETEDBY))); - event.setCompletedDate(rs.getTimestamp(EventQuery.getColumnName(COLUMNS.COMPLETEDDATE))); - event.setCreated(rs.getTimestamp(EventQuery.getColumnName(COLUMNS.CREATED))); - event.setCreatedAtClient(rs.getTimestamp(EventQuery.getColumnName(COLUMNS.CREATEDCLIENT))); - JsonbToObjectHelper.setUserInfoSnapshot( - rs, EventQuery.getColumnName(COLUMNS.CREATED_BY), event::setCreatedByUserInfo); - event.setLastUpdated(rs.getTimestamp(EventQuery.getColumnName(COLUMNS.UPDATED))); - event.setLastUpdatedAtClient(rs.getTimestamp(EventQuery.getColumnName(COLUMNS.UPDATEDCLIENT))); - JsonbToObjectHelper.setUserInfoSnapshot( - rs, EventQuery.getColumnName(COLUMNS.LAST_UPDATED_BY), event::setLastUpdatedByUserInfo); - MapperGeoUtils.resolveGeometry(rs.getBytes(EventQuery.getColumnName(COLUMNS.GEOMETRY))) - .ifPresent(event::setGeometry); - event.setDeleted(rs.getBoolean(EventQuery.getColumnName(COLUMNS.DELETED))); - - OrganisationUnit orgUnit = new OrganisationUnit(); - orgUnit.setUid(rs.getString(EventQuery.getColumnName(COLUMNS.ORGUNIT_UID))); - orgUnit.setName(rs.getString(EventQuery.getColumnName(COLUMNS.ORGUNIT_NAME))); - event.setOrganisationUnit(orgUnit); - - Enrollment enrollment = new Enrollment(); - enrollment.setUid(rs.getString(EventQuery.getColumnName(COLUMNS.ENROLLMENT_UID))); - Program program = new Program(); - program.setUid(rs.getString(EventQuery.getColumnName(COLUMNS.PROGRAM_UID))); - enrollment.setProgram(program); - final boolean followUp = rs.getBoolean(EventQuery.getColumnName(COLUMNS.ENROLLMENT_FOLLOWUP)); - enrollment.setFollowup(rs.wasNull() ? null : followUp); - enrollment.setStatus( - EnrollmentStatus.valueOf( - rs.getString(EventQuery.getColumnName(COLUMNS.ENROLLMENT_STATUS)))); - TrackedEntity trackedEntity = new TrackedEntity(); - trackedEntity.setUid(rs.getString(EventQuery.getColumnName(COLUMNS.TE_UID))); - enrollment.setTrackedEntity(trackedEntity); - event.setEnrollment(enrollment); - - ProgramStage programStage = new ProgramStage(); - programStage.setUid(rs.getString(EventQuery.getColumnName(COLUMNS.PROGRAM_STAGE_UID))); - event.setProgramStage(programStage); - - CategoryOptionCombo categoryOptionCombo = new CategoryOptionCombo(); - categoryOptionCombo.setUid(rs.getString(EventQuery.getColumnName(COLUMNS.COC_UID))); - Set categoryOptions = - TextUtils.splitToSet( - rs.getString(EventQuery.getColumnName(COLUMNS.CAT_OPTIONS)), TextUtils.SEMICOLON) - .stream() - .map( - o -> { - CategoryOption co = new CategoryOption(); - co.setUid(o); - return co; - }) - .collect(Collectors.toSet()); - categoryOptionCombo.setCategoryOptions(categoryOptions); - event.setAttributeOptionCombo(categoryOptionCombo); - - String assignedUserUid = rs.getString(EventQuery.getColumnName(COLUMNS.ASSIGNED_USER)); - String assignedUserUsername = - rs.getString(EventQuery.getColumnName(COLUMNS.ASSIGNED_USER_USERNAME)); - String assignedUserFirstName = - rs.getString(EventQuery.getColumnName(COLUMNS.ASSIGNED_USER_FIRST_NAME)); - String assignedUserSurname = - rs.getString(EventQuery.getColumnName(COLUMNS.ASSIGNED_USER_SURNAME)); - if (StringUtils.isNotEmpty(assignedUserUid) - || StringUtils.isNotEmpty(assignedUserUsername) - || StringUtils.isNotEmpty(assignedUserFirstName) - || StringUtils.isNotEmpty(assignedUserSurname)) { - User assignedUser = new User(); - assignedUser.setUid(assignedUserUid); - assignedUser.setUsername(assignedUserUsername); - assignedUser.setFirstName(assignedUserFirstName); - assignedUser.setSurname(assignedUserSurname); - if (assignedUser.getFirstName() != null && assignedUser.getSurname() != null) { - assignedUser.setName(assignedUser.getFirstName() + " " + assignedUser.getSurname()); - } - event.setAssignedUser(assignedUser); - } - - return event; - } -} diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/mapper/NoteRowCallbackHandler.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/mapper/NoteRowCallbackHandler.java deleted file mode 100644 index 0526b710594..00000000000 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/mapper/NoteRowCallbackHandler.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.export.trackedentity.aggregates.mapper; - -import java.sql.ResultSet; -import java.sql.SQLException; -import org.hisp.dhis.note.Note; - -/** - * @author Luciano Fiandesio - */ -public class NoteRowCallbackHandler extends AbstractMapper { - @Override - Note getItem(ResultSet rs) throws SQLException { - return getNote(rs); - } - - @Override - String getKeyColumn() { - return "key"; - } - - 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; - } -} diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/mapper/ProgramAttributeRowCallbackHandler.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/mapper/ProgramAttributeRowCallbackHandler.java deleted file mode 100644 index 62b02c49e4c..00000000000 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/mapper/ProgramAttributeRowCallbackHandler.java +++ /dev/null @@ -1,47 +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.export.trackedentity.aggregates.mapper; - -import java.sql.ResultSet; -import java.sql.SQLException; -import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue; -import org.hisp.dhis.tracker.export.trackedentity.aggregates.query.ProgramAttributeQuery; -import org.hisp.dhis.tracker.export.trackedentity.aggregates.query.ProgramAttributeQuery.COLUMNS; - -public class ProgramAttributeRowCallbackHandler extends AbstractMapper - implements AttributeMapper { - @Override - TrackedEntityAttributeValue getItem(ResultSet rs) throws SQLException { - return getAttribute(rs); - } - - @Override - String getKeyColumn() { - return ProgramAttributeQuery.getColumnName(COLUMNS.PI_UID); - } -} diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/query/EnrollmentQuery.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/query/EnrollmentQuery.java deleted file mode 100644 index 9384db933f9..00000000000 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/query/EnrollmentQuery.java +++ /dev/null @@ -1,109 +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.export.trackedentity.aggregates.query; - -import com.google.common.collect.ImmutableMap; -import java.util.Map; - -/** - * @author Luciano Fiandesio - */ -public class EnrollmentQuery { - public enum COLUMNS { - TE_UID, - ID, - UID, - CREATED, - CREATEDCLIENT, - CREATED_BY, - UPDATED, - UPDATEDCLIENT, - LAST_UPDATED_BY, - STATUS, - GEOMETRY, - ENROLLMENTDATE, - OCCURREDDATE, - FOLLOWUP, - COMPLETED, - COMPLETEDBY, - STOREDBY, - DELETED, - PROGRAM_UID, - PROGRAM_FEATURE_TYPE, - TE_TYPE_UID, - ORGUNIT_UID, - ORGUNIT_NAME - } - - public static final Map columnMap = - ImmutableMap.builder() - .put(COLUMNS.TE_UID, new TableColumn("te", "uid", "te_uid")) - .put(COLUMNS.GEOMETRY, new Function("ST_AsBinary", "en", "geometry", "geometry")) - .put(COLUMNS.ID, new TableColumn("en", "enrollmentid")) - .put(COLUMNS.UID, new TableColumn("en", "uid")) - .put(COLUMNS.CREATED, new TableColumn("en", "created")) - .put(COLUMNS.CREATEDCLIENT, new TableColumn("en", "createdatclient")) - .put(COLUMNS.CREATED_BY, new TableColumn("en", "createdbyuserinfo")) - .put(COLUMNS.UPDATED, new TableColumn("en", "lastupdated")) - .put(COLUMNS.UPDATEDCLIENT, new TableColumn("en", "lastupdatedatclient")) - .put(COLUMNS.LAST_UPDATED_BY, new TableColumn("en", "lastupdatedbyuserinfo")) - .put(COLUMNS.STATUS, new TableColumn("en", "status")) - .put(COLUMNS.ENROLLMENTDATE, new TableColumn("en", "enrollmentdate")) - .put(COLUMNS.OCCURREDDATE, new TableColumn("en", "occurreddate")) - .put(COLUMNS.FOLLOWUP, new TableColumn("en", "followup")) - .put(COLUMNS.COMPLETED, new TableColumn("en", "completeddate")) - .put(COLUMNS.COMPLETEDBY, new TableColumn("en", "completedby")) - .put(COLUMNS.STOREDBY, new TableColumn("en", "storedby")) - .put(COLUMNS.DELETED, new TableColumn("en", "deleted")) - .put(COLUMNS.PROGRAM_UID, new TableColumn("p", "uid", "program_uid")) - .put( - COLUMNS.PROGRAM_FEATURE_TYPE, - new TableColumn("p", "featuretype", "program_feature_type")) - .put(COLUMNS.TE_TYPE_UID, new TableColumn("tet", "uid", "type_uid")) - .put(COLUMNS.ORGUNIT_UID, new TableColumn("o", "uid", "ou_uid")) - .put(COLUMNS.ORGUNIT_NAME, new TableColumn("o", "name", "ou_name")) - .build(); - - public static String getQuery() { - return getSelect() - + "from enrollment en " - + "join program p on en.programid = p.programid " - + "join trackedentity te on en.trackedentityid = te.trackedentityid " - + "join trackedentitytype tet on te.trackedentitytypeid = tet.trackedentitytypeid " - + "join organisationunit o on en.organisationunitid = o.organisationunitid " - + "where en.trackedentityid in (:ids) "; - } - - private static String getSelect() { - return QueryUtils.getSelect(columnMap.values()); - } - - public static String getColumnName(COLUMNS columns) { - return columnMap.get(columns).getResultsetValue(); - } -} diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/query/EventQuery.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/query/EventQuery.java deleted file mode 100644 index b750f77aec8..00000000000 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/query/EventQuery.java +++ /dev/null @@ -1,127 +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.export.trackedentity.aggregates.query; - -import static java.util.stream.Collectors.collectingAndThen; -import static java.util.stream.Collectors.toList; - -import com.google.common.collect.ImmutableList; -import java.util.Arrays; -import java.util.Collection; -import lombok.Getter; -import lombok.RequiredArgsConstructor; - -/** - * @author Luciano Fiandesio - */ -public class EventQuery { - @RequiredArgsConstructor - public enum COLUMNS { - ID(new TableColumn("ev", "eventid")), - UID(new TableColumn("ev", "uid")), - STATUS(new TableColumn("ev", "status")), - OCCURRED_DATE(new TableColumn("ev", "occurreddate")), - SCHEDULED_DATE(new TableColumn("ev", "scheduleddate")), - STOREDBY(new TableColumn("ev", "storedby")), - COMPLETEDBY(new TableColumn("ev", "completedby")), - COMPLETEDDATE(new TableColumn("ev", "completeddate")), - CREATED_BY(new TableColumn("ev", "createdbyuserinfo")), - CREATED(new TableColumn("ev", "created")), - CREATEDCLIENT(new TableColumn("ev", "createdatclient")), - UPDATED(new TableColumn("ev", "lastupdated")), - UPDATEDCLIENT(new TableColumn("ev", "lastupdatedatclient")), - LAST_UPDATED_BY(new TableColumn("ev", "lastupdatedbyuserinfo")), - DELETED(new TableColumn("ev", "deleted")), - GEOMETRY(new Function("ST_AsBinary", "ev", "geometry", "geometry")), - TE_UID(new TableColumn("te", "uid", "te_uid")), - ENROLLMENT_UID(new TableColumn("en", "uid", "enruid")), - ENROLLMENT_FOLLOWUP(new TableColumn("en", "followup", "enrfollowup")), - ENROLLMENT_STATUS(new TableColumn("en", "status", "enrstatus")), - PROGRAM_UID(new TableColumn("p", "uid", "prguid")), - PROGRAM_STAGE_UID(new TableColumn("ps", "uid", "prgstguid")), - ORGUNIT_UID(new TableColumn("o", "uid", "ou_uid")), - ORGUNIT_NAME(new TableColumn("o", "name", "ou_name")), - COC_UID(new TableColumn("coc", "uid", "cocuid")), - CAT_OPTIONS( - new Subselect( - "( " - + "SELECT string_agg(opt.uid::text, ';') " - + "FROM categoryoption opt " - + "join categoryoptioncombos_categoryoptions ccc " - + "on opt.categoryoptionid = ccc.categoryoptionid " - + "WHERE coc.categoryoptioncomboid = ccc.categoryoptioncomboid )", - "catoptions")), - ASSIGNED_USER(new TableColumn("ui", "uid", "userid")), - ASSIGNED_USER_FIRST_NAME(new TableColumn("ui", "firstname")), - ASSIGNED_USER_SURNAME(new TableColumn("ui", "surname")), - ASSIGNED_USER_USERNAME(new TableColumn("ui", "username")); - - @Getter private final QueryElement queryElement; - - public String getColumnName() { - if (queryElement instanceof TableColumn tableColumn) { - return tableColumn.getColumn(); - } - if (queryElement instanceof Function function) { - return function.getColumn(); - } - throw new IllegalArgumentException( - "getColumnName can only be invoked on TableColumn or Function"); - } - } - - private static final Collection QUERY_ELEMENTS; - - static { - QUERY_ELEMENTS = - Arrays.stream(COLUMNS.values()) - .map(COLUMNS::getQueryElement) - .collect(collectingAndThen(toList(), ImmutableList::copyOf)); - } - - public static String getQuery() { - return getSelect() - + "from event ev " - + "join enrollment en on ev.enrollmentid = en.enrollmentid " - + "join trackedentity te on en.trackedentityid = te.trackedentityid " - + "join program p on en.programid = p.programid " - + "join programstage ps on ev.programstageid = ps.programstageid " - + "join organisationunit o on ev.organisationunitid = o.organisationunitid " - + "join categoryoptioncombo coc on ev.attributeoptioncomboid = coc.categoryoptioncomboid " - + "left join userinfo ui on ev.assigneduserid = ui.userinfoid " - + "where en.enrollmentid in (:ids)"; - } - - private static String getSelect() { - return QueryUtils.getSelect(QUERY_ELEMENTS); - } - - public static String getColumnName(COLUMNS columns) { - return columns.getQueryElement().getResultsetValue(); - } -} diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/query/ProgramAttributeQuery.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/query/ProgramAttributeQuery.java deleted file mode 100644 index a9809135447..00000000000 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/query/ProgramAttributeQuery.java +++ /dev/null @@ -1,80 +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.export.trackedentity.aggregates.query; - -import com.google.common.collect.ImmutableMap; -import java.util.Map; - -public class ProgramAttributeQuery { - public enum COLUMNS { - PI_UID, - PI_ID, - CREATED, - UPDATED, - VALUE, - STOREDBY, - ATTR_UID, - ATTR_NAME, - ATTR_VALUE_TYPE, - ATTR_CODE, - ATTR_SKIP_SYNC - } - - public static final Map columnMap = - ImmutableMap.builder() - .put(COLUMNS.PI_UID, new TableColumn("en", "uid", "pi_uid")) - .put(COLUMNS.PI_ID, new TableColumn("en", "enrollmentid", "id")) - .put(COLUMNS.CREATED, new TableColumn("teav", "created")) - .put(COLUMNS.UPDATED, new TableColumn("teav", "lastupdated")) - .put(COLUMNS.STOREDBY, new TableColumn("teav", "storedby")) - .put(COLUMNS.VALUE, new TableColumn("teav", "value")) - .put(COLUMNS.ATTR_UID, new TableColumn("t", "uid", "att_uid")) - .put(COLUMNS.ATTR_VALUE_TYPE, new TableColumn("t", "valuetype", "att_val_type")) - .put(COLUMNS.ATTR_CODE, new TableColumn("t", "code", "att_code")) - .put(COLUMNS.ATTR_NAME, new TableColumn("t", "name", "att_name")) - .put(COLUMNS.ATTR_SKIP_SYNC, new TableColumn("t", "skipsynchronization", "att_skip_sync")) - .build(); - - public static String getQuery() { - return getSelect() - + "from trackedentityattributevalue teav " - + "join program_attributes pa on teav.trackedentityattributeid = pa.trackedentityattributeid " - + "join trackedentityattribute t on t.trackedentityattributeid = pa.trackedentityattributeid " - + "join trackedentity te on te.trackedentityid = teav.trackedentityid " - + "join enrollment en on en.programid = pa.programid and en.trackedentityid = te.trackedentityid " - + "where en.enrollmentid IN (:ids)"; - } - - private static String getSelect() { - return QueryUtils.getSelect(columnMap.values()); - } - - public static String getColumnName(COLUMNS columns) { - return columnMap.get(columns).getResultsetValue(); - } -} diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/query/Subselect.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/query/Subselect.java deleted file mode 100644 index 23869e72b52..00000000000 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/export/trackedentity/aggregates/query/Subselect.java +++ /dev/null @@ -1,52 +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.export.trackedentity.aggregates.query; - -import lombok.AllArgsConstructor; -import lombok.Getter; - -/** - * @author Luciano Fiandesio - */ -@Getter -@AllArgsConstructor -class Subselect implements QueryElement { - private String query; - - private String alias; - - @Override - public String useInSelect() { - return query + " as " + alias; - } - - @Override - public String getResultsetValue() { - return alias; - } -}