Search in sources :

Example 26 with EventSearchParams

use of org.hisp.dhis.dxf2.events.event.EventSearchParams in project dhis2-core by dhis2.

the class EventExporterTest method testExportEventsWhenFilteringByEnrollment.

@Test
void testExportEventsWhenFilteringByEnrollment() {
    EventSearchParams params = new EventSearchParams();
    params.setOrgUnit(orgUnit);
    params.setProgramInstances(Sets.newHashSet("TvctPPhpD8z"));
    Events events = eventService.getEvents(params);
    assertNotNull(events);
    assertEquals(1, events.getEvents().size());
}
Also used : Events(org.hisp.dhis.dxf2.events.event.Events) EventSearchParams(org.hisp.dhis.dxf2.events.event.EventSearchParams) Test(org.junit.jupiter.api.Test)

Example 27 with EventSearchParams

use of org.hisp.dhis.dxf2.events.event.EventSearchParams in project dhis2-core by dhis2.

the class EventController method getCsvEvents.

@GetMapping(produces = { "application/csv", "application/csv+gzip", "text/csv" })
public void getCsvEvents(EventCriteria eventCriteria, @RequestParam(required = false, defaultValue = "false") boolean skipHeader, HttpServletResponse response, HttpServletRequest request) throws IOException, WebMessageException {
    EventSearchParams params = requestToSearchParamsMapper.map(eventCriteria);
    Events events = eventService.getEvents(params);
    OutputStream outputStream = response.getOutputStream();
    response.setContentType("application/csv");
    if (ContextUtils.isAcceptCsvGzip(request)) {
        response.addHeader(ContextUtils.HEADER_CONTENT_TRANSFER_ENCODING, "binary");
        outputStream = new GZIPOutputStream(outputStream);
        response.setContentType("application/csv+gzip");
    }
    if (!StringUtils.isEmpty(eventCriteria.getAttachment())) {
        response.addHeader("Content-Disposition", "attachment; filename=" + eventCriteria.getAttachment());
    }
    csvEventService.writeEvents(outputStream, events.getEvents(), !skipHeader);
}
Also used : Events(org.hisp.dhis.dxf2.events.event.Events) GZIPOutputStream(java.util.zip.GZIPOutputStream) EventSearchParams(org.hisp.dhis.dxf2.events.event.EventSearchParams) GZIPOutputStream(java.util.zip.GZIPOutputStream) OutputStream(java.io.OutputStream) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 28 with EventSearchParams

use of org.hisp.dhis.dxf2.events.event.EventSearchParams in project dhis2-core by dhis2.

the class EventController method queryEventsCsv.

@GetMapping(value = "/query", produces = ContextUtils.CONTENT_TYPE_CSV)
public void queryEventsCsv(@RequestParam(required = false) String program, @RequestParam(required = false) String programStage, @RequestParam(required = false) ProgramStatus programStatus, @RequestParam(required = false) Boolean followUp, @RequestParam(required = false) String trackedEntityInstance, @RequestParam(required = false) String orgUnit, @RequestParam(required = false) OrganisationUnitSelectionMode ouMode, @RequestParam(required = false) AssignedUserSelectionMode assignedUserMode, @RequestParam(required = false) String assignedUser, @RequestParam(required = false) Date startDate, @RequestParam(required = false) Date endDate, @RequestParam(required = false) Date dueDateStart, @RequestParam(required = false) Date dueDateEnd, @RequestParam(required = false) Date lastUpdated, @RequestParam(required = false) Date lastUpdatedStartDate, @RequestParam(required = false) Date lastUpdatedEndDate, @RequestParam(required = false) EventStatus status, @RequestParam(required = false) String attributeCc, @RequestParam(required = false) String attributeCos, @RequestParam(required = false) boolean skipMeta, @RequestParam(required = false) Integer page, @RequestParam(required = false) Integer pageSize, @RequestParam(required = false) boolean totalPages, @RequestParam(required = false) Boolean skipPaging, @RequestParam(required = false) Boolean paging, @RequestParam(required = false) List<OrderCriteria> order, @RequestParam(required = false) String attachment, @RequestParam(required = false, defaultValue = "false") boolean includeDeleted, @RequestParam(required = false) String event, @RequestParam(required = false) Set<String> filter, @RequestParam(required = false) Set<String> dataElement, @RequestParam(required = false, defaultValue = "false") boolean includeAllDataElements, @RequestParam Map<String, String> parameters, IdSchemes idSchemes, Model model, HttpServletResponse response, HttpServletRequest request) throws Exception {
    List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
    if (fields.isEmpty()) {
        fields.addAll(Preset.ALL.getFields());
    }
    CategoryOptionCombo attributeOptionCombo = inputUtils.getAttributeOptionCombo(attributeCc, attributeCos, false);
    if (attributeOptionCombo == null) {
        throw new WebMessageException(conflict("Illegal attribute option combo identifier: " + attributeCc + " " + attributeCos));
    }
    Set<String> eventIds = TextUtils.splitToArray(event, TextUtils.SEMICOLON);
    Set<String> assignedUserIds = TextUtils.splitToArray(assignedUser, TextUtils.SEMICOLON);
    lastUpdatedStartDate = lastUpdatedStartDate != null ? lastUpdatedStartDate : lastUpdated;
    skipPaging = PagerUtils.isSkipPaging(skipPaging, paging);
    EventSearchParams params = requestToSearchParamsMapper.map(program, programStage, programStatus, followUp, orgUnit, ouMode, trackedEntityInstance, startDate, endDate, dueDateStart, dueDateEnd, lastUpdatedStartDate, lastUpdatedEndDate, null, status, attributeOptionCombo, idSchemes, page, pageSize, totalPages, skipPaging, null, getGridOrderParams(order), false, eventIds, false, assignedUserMode, assignedUserIds, filter, dataElement, includeAllDataElements, includeDeleted);
    contextUtils.configureResponse(response, ContextUtils.CONTENT_TYPE_CSV, CacheStrategy.NO_CACHE);
    Grid grid = eventService.getEventsGrid(params);
    GridUtils.toCsv(grid, response.getWriter());
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) EventSearchParams(org.hisp.dhis.dxf2.events.event.EventSearchParams) Grid(org.hisp.dhis.common.Grid) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 29 with EventSearchParams

use of org.hisp.dhis.dxf2.events.event.EventSearchParams in project dhis2-core by dhis2.

the class EventController method queryEventsXls.

@GetMapping(value = "/query", produces = ContextUtils.CONTENT_TYPE_EXCEL)
public void queryEventsXls(@RequestParam(required = false) String program, @RequestParam(required = false) String programStage, @RequestParam(required = false) ProgramStatus programStatus, @RequestParam(required = false) Boolean followUp, @RequestParam(required = false) String trackedEntityInstance, @RequestParam(required = false) String orgUnit, @RequestParam(required = false) OrganisationUnitSelectionMode ouMode, @RequestParam(required = false) AssignedUserSelectionMode assignedUserMode, @RequestParam(required = false) String assignedUser, @RequestParam(required = false) Date startDate, @RequestParam(required = false) Date endDate, @RequestParam(required = false) Date dueDateStart, @RequestParam(required = false) Date dueDateEnd, @RequestParam(required = false) Date lastUpdated, @RequestParam(required = false) Date lastUpdatedStartDate, @RequestParam(required = false) Date lastUpdatedEndDate, @RequestParam(required = false) EventStatus status, @RequestParam(required = false) String attributeCc, @RequestParam(required = false) String attributeCos, @RequestParam(required = false) boolean skipMeta, @RequestParam(required = false) Integer page, @RequestParam(required = false) Integer pageSize, @RequestParam(required = false) boolean totalPages, @RequestParam(required = false) Boolean skipPaging, @RequestParam(required = false) Boolean paging, @RequestParam(required = false) List<OrderCriteria> order, @RequestParam(required = false) String attachment, @RequestParam(required = false, defaultValue = "false") boolean includeDeleted, @RequestParam(required = false) String event, @RequestParam(required = false) Set<String> filter, @RequestParam(required = false) Set<String> dataElement, @RequestParam(required = false, defaultValue = "false") boolean includeAllDataElements, @RequestParam Map<String, String> parameters, IdSchemes idSchemes, Model model, HttpServletResponse response, HttpServletRequest request) throws Exception {
    List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
    if (fields.isEmpty()) {
        fields.addAll(Preset.ALL.getFields());
    }
    CategoryOptionCombo attributeOptionCombo = inputUtils.getAttributeOptionCombo(attributeCc, attributeCos, false);
    if (attributeOptionCombo == null) {
        throw new WebMessageException(conflict("Illegal attribute option combo identifier: " + attributeCc + " " + attributeCos));
    }
    Set<String> eventIds = TextUtils.splitToArray(event, TextUtils.SEMICOLON);
    Set<String> assignedUserIds = TextUtils.splitToArray(assignedUser, TextUtils.SEMICOLON);
    lastUpdatedStartDate = lastUpdatedStartDate != null ? lastUpdatedStartDate : lastUpdated;
    skipPaging = PagerUtils.isSkipPaging(skipPaging, paging);
    EventSearchParams params = requestToSearchParamsMapper.map(program, programStage, programStatus, followUp, orgUnit, ouMode, trackedEntityInstance, startDate, endDate, dueDateStart, dueDateEnd, lastUpdatedStartDate, lastUpdatedEndDate, null, status, attributeOptionCombo, idSchemes, page, pageSize, totalPages, skipPaging, null, getGridOrderParams(order), false, eventIds, false, assignedUserMode, assignedUserIds, filter, dataElement, includeAllDataElements, includeDeleted);
    contextUtils.configureResponse(response, ContextUtils.CONTENT_TYPE_EXCEL, CacheStrategy.NO_CACHE);
    Grid grid = eventService.getEventsGrid(params);
    GridUtils.toXls(grid, response.getOutputStream());
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) EventSearchParams(org.hisp.dhis.dxf2.events.event.EventSearchParams) Grid(org.hisp.dhis.common.Grid) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 30 with EventSearchParams

use of org.hisp.dhis.dxf2.events.event.EventSearchParams in project dhis2-core by dhis2.

the class RequestToSearchParamsMapper method map.

public EventSearchParams map(String program, String programStage, ProgramStatus programStatus, Boolean followUp, String orgUnit, OrganisationUnitSelectionMode orgUnitSelectionMode, String trackedEntityInstance, Date startDate, Date endDate, Date dueDateStart, Date dueDateEnd, Date lastUpdatedStartDate, Date lastUpdatedEndDate, String lastUpdatedDuration, EventStatus status, CategoryOptionCombo attributeOptionCombo, IdSchemes idSchemes, Integer page, Integer pageSize, boolean totalPages, boolean skipPaging, List<OrderParam> orders, List<OrderParam> gridOrders, boolean includeAttributes, Set<String> events, Set<String> programInstances, Boolean skipEventId, AssignedUserSelectionMode assignedUserSelectionMode, Set<String> assignedUsers, Set<String> filters, Set<String> dataElements, boolean includeAllDataElements, boolean includeDeleted) {
    User user = currentUserService.getCurrentUser();
    EventSearchParams params = new EventSearchParams();
    Program pr = programService.getProgram(program);
    if (!StringUtils.isEmpty(program) && pr == null) {
        throw new IllegalQueryException("Program is specified but does not exist: " + program);
    }
    ProgramStage ps = programStageService.getProgramStage(programStage);
    if (!StringUtils.isEmpty(programStage) && ps == null) {
        throw new IllegalQueryException("Program stage is specified but does not exist: " + programStage);
    }
    OrganisationUnit ou = organisationUnitService.getOrganisationUnit(orgUnit);
    if (!StringUtils.isEmpty(orgUnit) && ou == null) {
        throw new IllegalQueryException("Org unit is specified but does not exist: " + orgUnit);
    }
    if (pr != null && !user.isSuper() && !aclService.canDataRead(user, pr)) {
        throw new IllegalQueryException("User has no access to program: " + pr.getUid());
    }
    if (ps != null && !user.isSuper() && !aclService.canDataRead(user, ps)) {
        throw new IllegalQueryException("User has no access to program stage: " + ps.getUid());
    }
    TrackedEntityInstance tei = entityInstanceService.getTrackedEntityInstance(trackedEntityInstance);
    if (!StringUtils.isEmpty(trackedEntityInstance) && tei == null) {
        throw new IllegalQueryException("Tracked entity instance is specified but does not exist: " + trackedEntityInstance);
    }
    if (attributeOptionCombo != null && !user.isSuper() && !aclService.canDataRead(user, attributeOptionCombo)) {
        throw new IllegalQueryException("User has no access to attribute category option combo: " + attributeOptionCombo.getUid());
    }
    if (!CollectionUtils.isEmpty(events) && !CollectionUtils.isEmpty(filters)) {
        throw new IllegalQueryException("Event UIDs and filters can not be specified at the same time");
    }
    if (events == null) {
        events = new HashSet<>();
    }
    if (filters != null) {
        if (!StringUtils.isEmpty(programStage) && ps == null) {
            throw new IllegalQueryException("ProgramStage needs to be specified for event filtering to work");
        }
        for (String filter : filters) {
            QueryItem item = getQueryItem(filter);
            params.getFilters().add(item);
        }
    }
    if (dataElements != null) {
        for (String de : dataElements) {
            QueryItem dataElement = getQueryItem(de);
            params.getDataElements().add(dataElement);
        }
    }
    if (assignedUserSelectionMode != null && assignedUsers != null && !assignedUsers.isEmpty() && !assignedUserSelectionMode.equals(AssignedUserSelectionMode.PROVIDED)) {
        throw new IllegalQueryException("Assigned User uid(s) cannot be specified if selectionMode is not PROVIDED");
    }
    if (assignedUsers != null) {
        assignedUsers = assignedUsers.stream().filter(CodeGenerator::isValidUid).collect(Collectors.toSet());
    }
    if (programInstances != null) {
        programInstances = programInstances.stream().filter(CodeGenerator::isValidUid).collect(Collectors.toSet());
    }
    return params.setProgram(pr).setProgramStage(ps).setOrgUnit(ou).setTrackedEntityInstance(tei).setProgramStatus(programStatus).setFollowUp(followUp).setOrgUnitSelectionMode(orgUnitSelectionMode).setAssignedUserSelectionMode(assignedUserSelectionMode).setAssignedUsers(assignedUsers).setStartDate(startDate).setEndDate(endDate).setDueDateStart(dueDateStart).setDueDateEnd(dueDateEnd).setLastUpdatedStartDate(lastUpdatedStartDate).setLastUpdatedEndDate(lastUpdatedEndDate).setLastUpdatedDuration(lastUpdatedDuration).setEventStatus(status).setCategoryOptionCombo(attributeOptionCombo).setIdSchemes(idSchemes).setPage(page).setPageSize(pageSize).setTotalPages(totalPages).setSkipPaging(skipPaging).setSkipEventId(skipEventId).setIncludeAttributes(includeAttributes).setIncludeAllDataElements(includeAllDataElements).setOrders(orders).setGridOrders(gridOrders).setEvents(events).setProgramInstances(programInstances).setIncludeDeleted(includeDeleted);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) QueryItem(org.hisp.dhis.common.QueryItem) User(org.hisp.dhis.user.User) Program(org.hisp.dhis.program.Program) EventSearchParams(org.hisp.dhis.dxf2.events.event.EventSearchParams) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) CodeGenerator(org.hisp.dhis.common.CodeGenerator) ProgramStage(org.hisp.dhis.program.ProgramStage) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance)

Aggregations

EventSearchParams (org.hisp.dhis.dxf2.events.event.EventSearchParams)28 Events (org.hisp.dhis.dxf2.events.event.Events)11 Test (org.junit.jupiter.api.Test)10 GetMapping (org.springframework.web.bind.annotation.GetMapping)10 Event (org.hisp.dhis.dxf2.events.event.Event)8 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)7 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)7 User (org.hisp.dhis.user.User)7 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)7 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)6 Program (org.hisp.dhis.program.Program)5 ProgramStage (org.hisp.dhis.program.ProgramStage)5 HashSet (java.util.HashSet)4 QueryItem (org.hisp.dhis.common.QueryItem)4 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)4 ImmutableList (com.google.common.collect.ImmutableList)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 CategoryOptionCombo (org.hisp.dhis.category.CategoryOptionCombo)3