<commit_before>/*
 * 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.webapi.controller.event;

import static org.hisp.dhis.dxf2.webmessage.WebMessageUtils.conflict;
import static org.hisp.dhis.dxf2.webmessage.WebMessageUtils.error;
import static org.hisp.dhis.dxf2.webmessage.WebMessageUtils.importSummaries;
import static org.hisp.dhis.dxf2.webmessage.WebMessageUtils.importSummary;
import static org.hisp.dhis.dxf2.webmessage.WebMessageUtils.jobConfigurationReport;
import static org.hisp.dhis.dxf2.webmessage.WebMessageUtils.notFound;
import static org.hisp.dhis.dxf2.webmessage.WebMessageUtils.ok;
import static org.hisp.dhis.scheduling.JobType.EVENT_IMPORT;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.http.MediaType.APPLICATION_XML_VALUE;
import static org.springframework.http.MediaType.TEXT_XML_VALUE;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;

import org.apache.commons.lang3.StringUtils;
import org.hisp.dhis.category.CategoryOptionCombo;
import org.hisp.dhis.common.AssignedUserSelectionMode;
import org.hisp.dhis.common.AsyncTaskExecutor;
import org.hisp.dhis.common.DhisApiVersion;
import org.hisp.dhis.common.Grid;
import org.hisp.dhis.common.IdSchemes;
import org.hisp.dhis.common.OrganisationUnitSelectionMode;
import org.hisp.dhis.common.PagerUtils;
import org.hisp.dhis.common.SlimPager;
import org.hisp.dhis.common.cache.CacheStrategy;
import org.hisp.dhis.commons.util.StreamUtils;
import org.hisp.dhis.commons.util.TextUtils;
import org.hisp.dhis.dataelement.DataElement;
import org.hisp.dhis.dataelement.DataElementService;
import org.hisp.dhis.dxf2.common.ImportOptions;
import org.hisp.dhis.dxf2.events.event.DataValue;
import org.hisp.dhis.dxf2.events.event.Event;
import org.hisp.dhis.dxf2.events.event.EventSearchParams;
import org.hisp.dhis.dxf2.events.event.EventService;
import org.hisp.dhis.dxf2.events.event.Events;
import org.hisp.dhis.dxf2.events.event.ImportEventsTask;
import org.hisp.dhis.dxf2.events.event.csv.CsvEventService;
import org.hisp.dhis.dxf2.events.report.EventRowService;
import org.hisp.dhis.dxf2.events.report.EventRows;
import org.hisp.dhis.dxf2.importsummary.ImportStatus;
import org.hisp.dhis.dxf2.importsummary.ImportSummaries;
import org.hisp.dhis.dxf2.importsummary.ImportSummary;
import org.hisp.dhis.dxf2.util.InputUtils;
import org.hisp.dhis.dxf2.webmessage.WebMessage;
import org.hisp.dhis.dxf2.webmessage.WebMessageException;
import org.hisp.dhis.dxf2.webmessage.responses.FileResourceWebMessageResponse;
import org.hisp.dhis.event.EventStatus;
import org.hisp.dhis.fieldfilter.FieldFilterParams;
import org.hisp.dhis.fieldfilter.FieldFilterService;
import org.hisp.dhis.fileresource.FileResource;
import org.hisp.dhis.fileresource.FileResourceDomain;
import org.hisp.dhis.fileresource.FileResourceService;
import org.hisp.dhis.fileresource.FileResourceStorageStatus;
import org.hisp.dhis.fileresource.ImageFileDimension;
import org.hisp.dhis.importexport.ImportStrategy;
import org.hisp.dhis.node.NodeUtils;
import org.hisp.dhis.node.Preset;
import org.hisp.dhis.node.types.RootNode;
import org.hisp.dhis.program.Program;
import org.hisp.dhis.program.ProgramStageInstanceService;
import org.hisp.dhis.program.ProgramStatus;
import org.hisp.dhis.query.QueryUtils;
import org.hisp.dhis.render.RenderService;
import org.hisp.dhis.scheduling.JobConfiguration;
import org.hisp.dhis.schema.Schema;
import org.hisp.dhis.schema.SchemaService;
import org.hisp.dhis.system.grid.GridUtils;
import org.hisp.dhis.user.CurrentUserService;
import org.hisp.dhis.webapi.controller.event.mapper.OrderParam;
import org.hisp.dhis.webapi.controller.event.webrequest.EventCriteria;
import org.hisp.dhis.webapi.controller.event.webrequest.OrderCriteria;
import org.hisp.dhis.webapi.mvc.annotation.ApiVersion;
import org.hisp.dhis.webapi.service.ContextService;
import org.hisp.dhis.webapi.utils.ContextUtils;
import org.hisp.dhis.webapi.utils.FileResourceUtils;
import org.hisp.dhis.webapi.webdomain.WebOptions;
import org.locationtech.jts.io.ParseException;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.base.Joiner;
import com.google.common.base.MoreObjects;
import com.google.common.collect.Lists;

/**
 * @author <NAME> <<EMAIL>>
 */
@Controller
@RequestMapping( value = EventController.RESOURCE_PATH )
@ApiVersion( { DhisApiVersion.DEFAULT, DhisApiVersion.ALL } )
@RequiredArgsConstructor
public class EventController
{
    public static final String RESOURCE_PATH = "/events";

    private static final String META_DATA_KEY_DE = "de";

    // --------------------------------------------------------------------------
    // Dependencies
    // --------------------------------------------------------------------------

    private final CurrentUserService currentUserService;

    private final AsyncTaskExecutor taskExecutor;

    private final EventService eventService;

    private final CsvEventService<Event> csvEventService;

    private final EventRowService eventRowService;

    private final DataElementService dataElementService;

    private final InputUtils inputUtils;

    private final RenderService renderService;

    private final ProgramStageInstanceService programStageInstanceService;

    private final FileResourceService fileResourceService;

    private final FieldFilterService fieldFilterService;

    private final ContextService contextService;

    private final SchemaService schemaService;

    private final EventRequestToSearchParamsMapper requestToSearchParamsMapper;

    private final ContextUtils contextUtils;

    private Schema schema;

    protected Schema getSchema()
    {
        if ( schema == null )
        {
            schema = schemaService.getDynamicSchema( Event.class );
        }

        return schema;
    }

    // -------------------------------------------------------------------------
    // Read
    // -------------------------------------------------------------------------

    // -------------------------------------------------------------------------
    // Query Read
    // -------------------------------------------------------------------------

    @GetMapping( value = "/query", produces = { ContextUtils.CONTENT_TYPE_JSON,
        ContextUtils.CONTENT_TYPE_JAVASCRIPT } )
    public @ResponseBody Grid queryEventsJson(
        @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 WebMessageException
    {
        List<String> fields = Lists.newArrayList( contextService.getParameterValues( "fields" ) );

        if ( fields.isEmpty() )
        {
            fields.add( "*" );
            fields.add( "dataValues" );
        }

        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.splitToSet( event, TextUtils.SEMICOLON );

        Set<String> assignedUserIds = TextUtils.splitToSet( 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, getOrderParams( null ), getGridOrderParams( order ), false, eventIds, false,
            assignedUserMode, assignedUserIds, filter, dataElement, includeAllDataElements, includeDeleted );

        contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_JSON, CacheStrategy.NO_CACHE );

        return eventService.getEventsGrid( params );

    }

    @GetMapping( value = "/query", produces = ContextUtils.CONTENT_TYPE_XML )
    public void queryEventsXml(
        @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.splitToSet( event, TextUtils.SEMICOLON );

        Set<String> assignedUserIds = TextUtils.splitToSet( 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, getOrderParams( null ), getGridOrderParams( order ), false, eventIds, false,
            assignedUserMode, assignedUserIds, filter, dataElement, includeAllDataElements, includeDeleted );

        contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_XML, CacheStrategy.NO_CACHE );
        Grid grid = eventService.getEventsGrid( params );
        GridUtils.toXml( grid, response.getOutputStream() );
    }

    @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.splitToSet( event, TextUtils.SEMICOLON );

        Set<String> assignedUserIds = TextUtils.splitToSet( 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, getOrderParams( 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() );

    }

    @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.splitToSet( event, TextUtils.SEMICOLON );

        Set<String> assignedUserIds = TextUtils.splitToSet( 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, getOrderParams( 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() );

    }

    // -------------------------------------------------------------------------
    // Object Read
    // -------------------------------------------------------------------------

    @GetMapping
    public @ResponseBody RootNode getEvents(
        EventCriteria eventCriteria, @RequestParam Map<String, String> parameters, Model model,
        HttpServletResponse response,
        HttpServletRequest request )
    {
        WebOptions options = new WebOptions( parameters );<commit_msg>fix: incorrect content-type for events/query.csv (#12155)<commit_after>/*
 * 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.webapi.controller.event;

import static org.hisp.dhis.dxf2.webmessage.WebMessageUtils.conflict;
import static org.hisp.dhis.dxf2.webmessage.WebMessageUtils.error;
import static org.hisp.dhis.dxf2.webmessage.WebMessageUtils.importSummaries;
import static org.hisp.dhis.dxf2.webmessage.WebMessageUtils.importSummary;
import static org.hisp.dhis.dxf2.webmessage.WebMessageUtils.jobConfigurationReport;
import static org.hisp.dhis.dxf2.webmessage.WebMessageUtils.notFound;
import static org.hisp.dhis.dxf2.webmessage.WebMessageUtils.ok;
import static org.hisp.dhis.scheduling.JobType.EVENT_IMPORT;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.http.MediaType.APPLICATION_XML_VALUE;
import static org.springframework.http.MediaType.TEXT_XML_VALUE;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;

import org.apache.commons.lang3.StringUtils;
import org.hisp.dhis.category.CategoryOptionCombo;
import org.hisp.dhis.common.AssignedUserSelectionMode;
import org.hisp.dhis.common.AsyncTaskExecutor;
import org.hisp.dhis.common.DhisApiVersion;
import org.hisp.dhis.common.Grid;
import org.hisp.dhis.common.IdSchemes;
import org.hisp.dhis.common.OrganisationUnitSelectionMode;
import org.hisp.dhis.common.PagerUtils;
import org.hisp.dhis.common.SlimPager;
import org.hisp.dhis.common.cache.CacheStrategy;
import org.hisp.dhis.commons.util.StreamUtils;
import org.hisp.dhis.commons.util.TextUtils;
import org.hisp.dhis.dataelement.DataElement;
import org.hisp.dhis.dataelement.DataElementService;
import org.hisp.dhis.dxf2.common.ImportOptions;
import org.hisp.dhis.dxf2.events.event.DataValue;
import org.hisp.dhis.dxf2.events.event.Event;
import org.hisp.dhis.dxf2.events.event.EventSearchParams;
import org.hisp.dhis.dxf2.events.event.EventService;
import org.hisp.dhis.dxf2.events.event.Events;
import org.hisp.dhis.dxf2.events.event.ImportEventsTask;
import org.hisp.dhis.dxf2.events.event.csv.CsvEventService;
import org.hisp.dhis.dxf2.events.report.EventRowService;
import org.hisp.dhis.dxf2.events.report.EventRows;
import org.hisp.dhis.dxf2.importsummary.ImportStatus;
import org.hisp.dhis.dxf2.importsummary.ImportSummaries;
import org.hisp.dhis.dxf2.importsummary.ImportSummary;
import org.hisp.dhis.dxf2.util.InputUtils;
import org.hisp.dhis.dxf2.webmessage.WebMessage;
import org.hisp.dhis.dxf2.webmessage.WebMessageException;
import org.hisp.dhis.dxf2.webmessage.responses.FileResourceWebMessageResponse;
import org.hisp.dhis.event.EventStatus;
import org.hisp.dhis.fieldfilter.FieldFilterParams;
import org.hisp.dhis.fieldfilter.FieldFilterService;
import org.hisp.dhis.fileresource.FileResource;
import org.hisp.dhis.fileresource.FileResourceDomain;
import org.hisp.dhis.fileresource.FileResourceService;
import org.hisp.dhis.fileresource.FileResourceStorageStatus;
import org.hisp.dhis.fileresource.ImageFileDimension;
import org.hisp.dhis.importexport.ImportStrategy;
import org.hisp.dhis.node.NodeUtils;
import org.hisp.dhis.node.Preset;
import org.hisp.dhis.node.types.RootNode;
import org.hisp.dhis.program.Program;
import org.hisp.dhis.program.ProgramStageInstanceService;
import org.hisp.dhis.program.ProgramStatus;
import org.hisp.dhis.query.QueryUtils;
import org.hisp.dhis.render.RenderService;
import org.hisp.dhis.scheduling.JobConfiguration;
import org.hisp.dhis.schema.Schema;
import org.hisp.dhis.schema.SchemaService;
import org.hisp.dhis.system.grid.GridUtils;
import org.hisp.dhis.user.CurrentUserService;
import org.hisp.dhis.webapi.controller.event.mapper.OrderParam;
import org.hisp.dhis.webapi.controller.event.webrequest.EventCriteria;
import org.hisp.dhis.webapi.controller.event.webrequest.OrderCriteria;
import org.hisp.dhis.webapi.mvc.annotation.ApiVersion;
import org.hisp.dhis.webapi.service.ContextService;
import org.hisp.dhis.webapi.utils.ContextUtils;
import org.hisp.dhis.webapi.utils.FileResourceUtils;
import org.hisp.dhis.webapi.webdomain.WebOptions;
import org.locationtech.jts.io.ParseException;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.base.Joiner;
import com.google.common.base.MoreObjects;
import com.google.common.collect.Lists;

/**
 * @author <NAME> <<EMAIL>>
 */
@Controller
@RequestMapping( value = EventController.RESOURCE_PATH )
@ApiVersion( { DhisApiVersion.DEFAULT, DhisApiVersion.ALL } )
@RequiredArgsConstructor
public class EventController
{
    public static final String RESOURCE_PATH = "/events";

    private static final String META_DATA_KEY_DE = "de";

    // --------------------------------------------------------------------------
    // Dependencies
    // --------------------------------------------------------------------------

    private final CurrentUserService currentUserService;

    private final AsyncTaskExecutor taskExecutor;

    private final EventService eventService;

    private final CsvEventService<Event> csvEventService;

    private final EventRowService eventRowService;

    private final DataElementService dataElementService;

    private final InputUtils inputUtils;

    private final RenderService renderService;

    private final ProgramStageInstanceService programStageInstanceService;

    private final FileResourceService fileResourceService;

    private final FieldFilterService fieldFilterService;

    private final ContextService contextService;

    private final SchemaService schemaService;

    private final EventRequestToSearchParamsMapper requestToSearchParamsMapper;

    private final ContextUtils contextUtils;

    private Schema schema;

    protected Schema getSchema()
    {
        if ( schema == null )
        {
            schema = schemaService.getDynamicSchema( Event.class );
        }

        return schema;
    }

    // -------------------------------------------------------------------------
    // Read
    // -------------------------------------------------------------------------

    // -------------------------------------------------------------------------
    // Query Read
    // -------------------------------------------------------------------------

    @GetMapping( value = "/query", produces = { ContextUtils.CONTENT_TYPE_JSON,
        ContextUtils.CONTENT_TYPE_JAVASCRIPT } )
    public @ResponseBody Grid queryEventsJson(
        @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 WebMessageException
    {
        List<String> fields = Lists.newArrayList( contextService.getParameterValues( "fields" ) );

        if ( fields.isEmpty() )
        {
            fields.add( "*" );
            fields.add( "dataValues" );
        }

        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.splitToSet( event, TextUtils.SEMICOLON );

        Set<String> assignedUserIds = TextUtils.splitToSet( 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, getOrderParams( null ), getGridOrderParams( order ), false, eventIds, false,
            assignedUserMode, assignedUserIds, filter, dataElement, includeAllDataElements, includeDeleted );

        contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_JSON, CacheStrategy.NO_CACHE );

        return eventService.getEventsGrid( params );

    }

    @GetMapping( value = "/query", produces = ContextUtils.CONTENT_TYPE_XML )
    public void queryEventsXml(
        @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.splitToSet( event, TextUtils.SEMICOLON );

        Set<String> assignedUserIds = TextUtils.splitToSet( 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, getOrderParams( null ), getGridOrderParams( order ), false, eventIds, false,
            assignedUserMode, assignedUserIds, filter, dataElement, includeAllDataElements, includeDeleted );

        contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_XML, CacheStrategy.NO_CACHE );
        Grid grid = eventService.getEventsGrid( params );
        GridUtils.toXml( grid, response.getOutputStream() );
    }

    @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.splitToSet( event, TextUtils.SEMICOLON );

        Set<String> assignedUserIds = TextUtils.splitToSet( 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, getOrderParams( 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() );

    }

    @GetMapping( value = "/query", produces = ContextUtils.CONTENT_TYPE_TEXT_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.splitToSet( event, TextUtils.SEMICOLON );

        Set<String> assignedUserIds = TextUtils.splitToSet( 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, getOrderParams( null ), getGridOrderParams( order ), false, eventIds, false,
            assignedUserMode, assignedUserIds, filter, dataElement, includeAllDataElements, includeDeleted );

        contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_TEXT_CSV, CacheStrategy.NO_CACHE );
        Grid grid = eventService.getEventsGrid( params );
        GridUtils.toCsv( grid, response.getWriter() );

    }

    // -------------------------------------------------------------------------
    // Object Read
    // -------------------------------------------------------------------------

    @GetMapping
    public @ResponseBody RootNode getEvents(
        EventCriteria eventCriteria, @RequestParam Map<String, String> parameters, Model model,
        HttpServletResponse response,
        HttpServletRequest request )
    {
        WebOptions options = new WebOptions( parameters );
{'fraction_non_alphanumeric': 0.10551386242563285, 'fraction_numerical': 0.001380409845627406, 'mean_word_length': 3.4017971758664953, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1570716', 'n_tokens_mistral': 13410, 'n_tokens_neox': 13152, 'n_words': 3415}