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

feat: support /tracker/event?dataElementIdScheme DHIS2-14968 #19153

Merged
merged 6 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
import javax.annotation.Nonnull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hisp.dhis.common.IdScheme;
import org.hisp.dhis.common.IdentifiableObjectManager;
import org.hisp.dhis.common.IdentifiableProperty;
import org.hisp.dhis.common.OrganisationUnitSelectionMode;
import org.hisp.dhis.common.UID;
import org.hisp.dhis.dataelement.DataElement;
Expand All @@ -50,6 +53,9 @@
import org.hisp.dhis.program.Event;
import org.hisp.dhis.relationship.Relationship;
import org.hisp.dhis.relationship.RelationshipItem;
import org.hisp.dhis.tracker.TrackerIdScheme;
import org.hisp.dhis.tracker.TrackerIdSchemeParam;
import org.hisp.dhis.tracker.TrackerIdSchemeParams;
import org.hisp.dhis.tracker.acl.TrackerAccessManager;
import org.hisp.dhis.tracker.export.FileResourceStream;
import org.hisp.dhis.tracker.export.Page;
Expand All @@ -67,7 +73,9 @@
@RequiredArgsConstructor
class DefaultEventService implements EventService {

private final EventStore eventStore;
private final JdbcEventStore eventStore;

private final IdentifiableObjectManager manager;

private final TrackerAccessManager trackerAccessManager;

Expand Down Expand Up @@ -148,17 +156,24 @@ private FileResource getFileResourceMetadata(UID eventUid, UID dataElementUid)

@Override
public Event getEvent(@Nonnull UID event) throws ForbiddenException, NotFoundException {
return getEvent(event, EventParams.FALSE, getCurrentUserDetails());
return getEvent(
event, TrackerIdSchemeParams.builder().build(), EventParams.FALSE, getCurrentUserDetails());
}

@Override
public Event getEvent(@Nonnull UID event, @Nonnull EventParams eventParams)
public Event getEvent(
@Nonnull UID event,
@Nonnull TrackerIdSchemeParams idSchemeParams,
@Nonnull EventParams eventParams)
throws ForbiddenException, NotFoundException {
return getEvent(event, eventParams, getCurrentUserDetails());
return getEvent(event, idSchemeParams, eventParams, getCurrentUserDetails());
}

private Event getEvent(
@Nonnull UID eventUid, @Nonnull EventParams eventParams, @Nonnull UserDetails user)
@Nonnull UID eventUid,
@Nonnull TrackerIdSchemeParams idSchemeParams,
@Nonnull EventParams eventParams,
@Nonnull UserDetails user)
throws NotFoundException, ForbiddenException {
Page<Event> events;
try {
Expand All @@ -167,6 +182,7 @@ private Event getEvent(
.orgUnitMode(OrganisationUnitSelectionMode.ACCESSIBLE)
.events(Set.of(eventUid))
.eventParams(eventParams)
.idSchemeParams(idSchemeParams)
.build();
events = getEvents(operationParams, new PageParams(1, 1, false));
} catch (BadRequestException e) {
Expand All @@ -179,64 +195,35 @@ private Event getEvent(
}
Event event = events.getItems().get(0);

return getEvent(event, eventParams, user);
}

private Event getEvent(
@Nonnull Event event, @Nonnull EventParams eventParams, @Nonnull UserDetails user) {
Event result = new Event();
result.setId(event.getId());
result.setUid(event.getUid());

result.setStatus(event.getStatus());
result.setOccurredDate(event.getOccurredDate());
result.setScheduledDate(event.getScheduledDate());
result.setStoredBy(event.getStoredBy());
result.setCompletedBy(event.getCompletedBy());
result.setCompletedDate(event.getCompletedDate());
result.setCreated(event.getCreated());
result.setCreatedByUserInfo(event.getCreatedByUserInfo());
result.setLastUpdatedByUserInfo(event.getLastUpdatedByUserInfo());
result.setCreatedAtClient(event.getCreatedAtClient());
result.setLastUpdated(event.getLastUpdated());
result.setLastUpdatedAtClient(event.getLastUpdatedAtClient());
result.setGeometry(event.getGeometry());
result.setDeleted(event.isDeleted());
result.setAssignedUser(event.getAssignedUser());

result.setEnrollment(event.getEnrollment());
result.setProgramStage(event.getProgramStage());

result.setOrganisationUnit(event.getOrganisationUnit());
result.setProgramStage(event.getProgramStage());

result.setAttributeOptionCombo(event.getAttributeOptionCombo());

Set<EventDataValue> dataValues = new HashSet<>(event.getEventDataValues().size());
for (EventDataValue dataValue : event.getEventDataValues()) {
if (dataElementService.getDataElement(dataValue.getDataElement())
!= null) // check permissions
{
EventDataValue value = new EventDataValue();
value.setCreated(dataValue.getCreated());
value.setCreatedByUserInfo(dataValue.getCreatedByUserInfo());
value.setLastUpdated(dataValue.getLastUpdated());
value.setLastUpdatedByUserInfo(dataValue.getLastUpdatedByUserInfo());
value.setDataElement(dataValue.getDataElement());
value.setValue(dataValue.getValue());
value.setProvidedElsewhere(dataValue.getProvidedElsewhere());
value.setStoredBy(dataValue.getStoredBy());
DataElement dataElement = null;
TrackerIdSchemeParam dataElementIdScheme = idSchemeParams.getDataElementIdScheme();
if (TrackerIdScheme.UID == dataElementIdScheme.getIdScheme()) {
dataElement = dataElementService.getDataElement(dataValue.getDataElement());
} else if (TrackerIdScheme.CODE == dataElementIdScheme.getIdScheme()) {
dataElement = manager.getByCode(DataElement.class, dataValue.getDataElement());
} else if (TrackerIdScheme.NAME == dataElementIdScheme.getIdScheme()) {
dataElement = manager.getByName(DataElement.class, dataValue.getDataElement());
} else if (TrackerIdScheme.ATTRIBUTE == dataElementIdScheme.getIdScheme()) {
dataElement =
manager.getObject(
DataElement.class,
new IdScheme(IdentifiableProperty.ATTRIBUTE, dataElementIdScheme.getAttributeUid()),
dataValue.getDataElement());
}

result.getEventDataValues().add(value);
if (dataElement != null) // check permissions
{
dataValues.add(dataValue);
} else {
log.info("Cannot find data element with UID {}", dataValue.getDataElement());
}
}

result.getNotes().addAll(event.getNotes());
event.setEventDataValues(dataValues);

if (eventParams.isIncludeRelationships()) {
Set<RelationshipItem> relationshipItems = new HashSet<>();

for (RelationshipItem relationshipItem : event.getRelationshipItems()) {
Relationship daoRelationship = relationshipItem.getRelationship();
if (trackerAccessManager.canRead(user, daoRelationship).isEmpty()
Expand All @@ -245,10 +232,10 @@ private Event getEvent(
}
}

result.setRelationshipItems(relationshipItems);
event.setRelationshipItems(relationshipItems);
}

return result;
return event;
}

@Override
Expand All @@ -271,7 +258,7 @@ public RelationshipItem getEventInRelationshipItem(
@Nonnull UID uid, @Nonnull EventParams eventParams) {
Event event;
try {
event = getEvent(uid, eventParams);
event = getEvent(uid, TrackerIdSchemeParams.builder().build(), eventParams);
} catch (NotFoundException | ForbiddenException e) {
// events are not shown in relationships if the user has no access to them
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.hisp.dhis.program.ProgramStage;
import org.hisp.dhis.program.ProgramType;
import org.hisp.dhis.trackedentity.TrackedEntity;
import org.hisp.dhis.tracker.TrackerIdSchemeParams;
import org.hisp.dhis.tracker.export.Order;

@Getter
Expand Down Expand Up @@ -146,6 +147,9 @@ public class EventOperationParams {

private EventParams eventParams;

@Builder.Default
private TrackerIdSchemeParams idSchemeParams = TrackerIdSchemeParams.builder().build();

public static class EventOperationParamsBuilder {

private final List<Order> order = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ public EventQueryParams map(
.setEvents(operationParams.getEvents())
.setEnrollments(operationParams.getEnrollments())
.setIncludeDeleted(operationParams.isIncludeDeleted())
.setIncludeRelationships(operationParams.getEventParams().isIncludeRelationships());
.setIncludeRelationships(operationParams.getEventParams().isIncludeRelationships())
.setIdSchemeParams(operationParams.getIdSchemeParams());
}

private ProgramStage validateProgramStage(String programStageUid, UserDetails user)
Expand Down Expand Up @@ -261,15 +262,17 @@ private void mapOrderParam(EventQueryParams params, List<Order> orders)
throw new BadRequestException(
"Cannot order by '"
+ uid.getValue()
+ "' as its neither a data element nor a tracked entity attribute. Events can be ordered by event fields, data elements and tracked entity attributes.");
+ "' as its neither a data element nor a tracked entity attribute. Events can be"
+ " ordered by event fields, data elements and tracked entity attributes.");
}

params.orderBy(tea, order.getDirection());
} else {
throw new IllegalArgumentException(
"Cannot order by '"
+ order.getField()
+ "'. Events can be ordered by event fields, data elements and tracked entity attributes.");
+ "'. Events can be ordered by event fields, data elements and tracked entity"
+ " attributes.");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.hisp.dhis.program.ProgramType;
import org.hisp.dhis.trackedentity.TrackedEntity;
import org.hisp.dhis.trackedentity.TrackedEntityAttribute;
import org.hisp.dhis.tracker.TrackerIdSchemeParams;
import org.hisp.dhis.tracker.export.Order;

/**
Expand Down Expand Up @@ -149,6 +150,8 @@ class EventQueryParams {

@Getter private AssignedUserQueryParam assignedUserQueryParam = AssignedUserQueryParam.ALL;

@Getter private TrackerIdSchemeParams idSchemeParams = TrackerIdSchemeParams.builder().build();

public EventQueryParams() {}

public boolean hasProgram() {
Expand Down Expand Up @@ -551,4 +554,9 @@ public boolean isPathOrganisationUnitMode() {
&& (OrganisationUnitSelectionMode.DESCENDANTS.equals(orgUnitMode)
|| OrganisationUnitSelectionMode.CHILDREN.equals(orgUnitMode));
}

public EventQueryParams setIdSchemeParams(TrackerIdSchemeParams idSchemeParams) {
this.idSchemeParams = idSchemeParams;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import org.hisp.dhis.fileresource.ImageFileDimension;
import org.hisp.dhis.program.Event;
import org.hisp.dhis.relationship.RelationshipItem;
import org.hisp.dhis.tracker.TrackerIdSchemeParam;
import org.hisp.dhis.tracker.TrackerIdSchemeParams;
import org.hisp.dhis.tracker.export.FileResourceStream;
import org.hisp.dhis.tracker.export.Page;
import org.hisp.dhis.tracker.export.PageParams;
Expand All @@ -61,28 +63,33 @@ FileResourceStream getFileResourceImage(UID event, UID dataElement, ImageFileDim

/**
* Get event matching given {@code UID} under the privileges of the currently authenticated user.
* Use {@link #getEvent(UID, EventParams)} instead to also get the events relationships.
* Metadata identifiers will use the {@code idScheme} {@link TrackerIdSchemeParam#UID}. Use {@link
* #getEvent(UID, TrackerIdSchemeParams, EventParams)} instead to also get the events
* relationships and specify different {@code idSchemes}.
*/
Event getEvent(UID uid) throws NotFoundException, ForbiddenException;

/**
* Get event matching given {@code UID} and params under the privileges of the currently
* authenticated user.
* authenticated user. Metadata identifiers will use the {@code idScheme} defined by {@link
* TrackerIdSchemeParams}.
*/
Event getEvent(UID uid, EventParams eventParams) throws NotFoundException, ForbiddenException;
Event getEvent(UID uid, @Nonnull TrackerIdSchemeParams idSchemeParams, EventParams eventParams)
throws NotFoundException, ForbiddenException;

/**
* Get all events matching given params under the privileges of the currently authenticated user.
*/
@Nonnull
List<Event> getEvents(EventOperationParams params) throws BadRequestException, ForbiddenException;
List<Event> getEvents(@Nonnull EventOperationParams params)
throws BadRequestException, ForbiddenException;

/**
* Get a page of events matching given params under the privileges of the currently authenticated
* user.
*/
@Nonnull
Page<Event> getEvents(EventOperationParams params, PageParams pageParams)
Page<Event> getEvents(@Nonnull EventOperationParams params, @Nonnull PageParams pageParams)
throws BadRequestException, ForbiddenException;

RelationshipItem getEventInRelationshipItem(UID uid, EventParams eventParams)
Expand Down

This file was deleted.

Loading
Loading