Search in sources :

Example 1 with ImportEventTask

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

the class EventController method postJsonEvent.

@RequestMapping(method = RequestMethod.POST, consumes = "application/json")
@PreAuthorize("hasRole('ALL') or hasRole('F_TRACKED_ENTITY_DATAVALUE_ADD')")
public void postJsonEvent(@RequestParam(defaultValue = "CREATE") ImportStrategy strategy, HttpServletResponse response, HttpServletRequest request, ImportOptions importOptions) throws Exception {
    importOptions.setImportStrategy(strategy);
    InputStream inputStream = StreamUtils.wrapAndCheckCompressionFormat(request.getInputStream());
    importOptions.setIdSchemes(getIdSchemesFromParameters(importOptions.getIdSchemes(), contextService.getParameterValuesMap()));
    if (!importOptions.isAsync()) {
        ImportSummaries importSummaries = eventService.addEventsJson(inputStream, importOptions);
        importSummaries.setImportOptions(importOptions);
        importSummaries.getImportSummaries().stream().filter(importSummary -> !importOptions.isDryRun() && !importSummary.getStatus().equals(ImportStatus.ERROR) && !importOptions.getImportStrategy().isDelete()).forEach(importSummary -> importSummary.setHref(ContextUtils.getRootPath(request) + RESOURCE_PATH + "/" + importSummary.getReference()));
        if (importSummaries.getImportSummaries().size() == 1) {
            ImportSummary importSummary = importSummaries.getImportSummaries().get(0);
            importSummary.setImportOptions(importOptions);
            if (!importOptions.isDryRun()) {
                if (!importSummary.getStatus().equals(ImportStatus.ERROR)) {
                    response.setHeader("Location", ContextUtils.getRootPath(request) + RESOURCE_PATH + "/" + importSummary.getReference());
                }
            }
        }
        webMessageService.send(WebMessageUtils.importSummaries(importSummaries), response, request);
    } else {
        TaskId taskId = new TaskId(TaskCategory.EVENT_IMPORT, currentUserService.getCurrentUser());
        List<Event> events = eventService.getEventsJson(inputStream);
        scheduler.executeTask(new ImportEventTask(events, eventService, importOptions, taskId));
        response.setHeader("Location", ContextUtils.getRootPath(request) + "/system/tasks/" + TaskCategory.EVENT_IMPORT);
        response.setStatus(HttpServletResponse.SC_NO_CONTENT);
    }
}
Also used : ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) PathVariable(org.springframework.web.bind.annotation.PathVariable) DataValue(org.hisp.dhis.dxf2.events.event.DataValue) EventRowService(org.hisp.dhis.dxf2.events.report.EventRowService) Order(org.hisp.dhis.query.Order) RequestParam(org.springframework.web.bind.annotation.RequestParam) Arrays(java.util.Arrays) EventService(org.hisp.dhis.dxf2.events.event.EventService) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Date(java.util.Date) RenderService(org.hisp.dhis.render.RenderService) Autowired(org.springframework.beans.factory.annotation.Autowired) WebMessageService(org.hisp.dhis.webapi.service.WebMessageService) StringUtils(org.apache.commons.lang3.StringUtils) FileResourceStorageStatus(org.hisp.dhis.fileresource.FileResourceStorageStatus) ProgramStageInstanceService(org.hisp.dhis.program.ProgramStageInstanceService) NodeUtils(org.hisp.dhis.node.NodeUtils) Model(org.springframework.ui.Model) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) FileResourceService(org.hisp.dhis.fileresource.FileResourceService) Map(java.util.Map) Preset(org.hisp.dhis.node.Preset) URI(java.net.URI) InputUtils(org.hisp.dhis.dxf2.utils.InputUtils) Scheduler(org.hisp.dhis.system.scheduling.Scheduler) ImportEventTask(org.hisp.dhis.dxf2.events.event.ImportEventTask) ImportEventsTask(org.hisp.dhis.dxf2.events.event.ImportEventsTask) ContextService(org.hisp.dhis.webapi.service.ContextService) CsvEventService(org.hisp.dhis.dxf2.events.event.csv.CsvEventService) OrganisationUnitSelectionMode(org.hisp.dhis.common.OrganisationUnitSelectionMode) HttpHeaders(org.springframework.http.HttpHeaders) FieldFilterService(org.hisp.dhis.fieldfilter.FieldFilterService) CacheStrategy(org.hisp.dhis.common.cache.CacheStrategy) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Set(java.util.Set) EventStatus(org.hisp.dhis.event.EventStatus) SchemaService(org.hisp.dhis.schema.SchemaService) Sets(com.google.common.collect.Sets) Event(org.hisp.dhis.dxf2.events.event.Event) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) EventRows(org.hisp.dhis.dxf2.events.report.EventRows) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Events(org.hisp.dhis.dxf2.events.event.Events) Schema(org.hisp.dhis.schema.Schema) GZIPOutputStream(java.util.zip.GZIPOutputStream) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) RootNode(org.hisp.dhis.node.types.RootNode) TaskId(org.hisp.dhis.scheduling.TaskId) DhisApiVersion(org.hisp.dhis.common.DhisApiVersion) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) DataElementService(org.hisp.dhis.dataelement.DataElementService) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) HashMap(java.util.HashMap) Controller(org.springframework.stereotype.Controller) StreamUtils(org.hisp.dhis.commons.util.StreamUtils) ApiVersion(org.hisp.dhis.webapi.mvc.annotation.ApiVersion) Program(org.hisp.dhis.program.Program) DataElement(org.hisp.dhis.dataelement.DataElement) EventSearchParams(org.hisp.dhis.dxf2.events.event.EventSearchParams) HttpServletRequest(javax.servlet.http.HttpServletRequest) Lists(com.google.common.collect.Lists) ByteSource(com.google.common.io.ByteSource) ImportStatus(org.hisp.dhis.dxf2.importsummary.ImportStatus) WebMessageUtils(org.hisp.dhis.dxf2.webmessage.WebMessageUtils) OutputStream(java.io.OutputStream) TrackedEntityInstanceService(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstanceService) ContextUtils(org.hisp.dhis.webapi.utils.ContextUtils) IdSchemes(org.hisp.dhis.common.IdSchemes) FileResource(org.hisp.dhis.fileresource.FileResource) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Grid(org.hisp.dhis.common.Grid) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) ProgramStatus(org.hisp.dhis.program.ProgramStatus) OrderParams(org.hisp.dhis.dxf2.common.OrderParams) FileResourceWebMessageResponse(org.hisp.dhis.dxf2.webmessage.responses.FileResourceWebMessageResponse) CurrentUserService(org.hisp.dhis.user.CurrentUserService) TaskCategory(org.hisp.dhis.scheduling.TaskCategory) FileResourceDomain(org.hisp.dhis.fileresource.FileResourceDomain) TextUtils(org.hisp.dhis.commons.util.TextUtils) InputStream(java.io.InputStream) TaskId(org.hisp.dhis.scheduling.TaskId) ImportEventTask(org.hisp.dhis.dxf2.events.event.ImportEventTask) InputStream(java.io.InputStream) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Event(org.hisp.dhis.dxf2.events.event.Event) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with ImportEventTask

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

the class ImportEventAction method execute.

// -------------------------------------------------------------------------
// Action Implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
    TaskId taskId = new TaskId(TaskCategory.EVENT_IMPORT, currentUserService.getCurrentUser());
    notifier.clear(taskId);
    InputStream in = new FileInputStream(upload);
    in = StreamUtils.wrapAndCheckCompressionFormat(in);
    ImportOptions importOptions = new ImportOptions().setDryRun(dryRun).setOrgUnitIdScheme(orgUnitIdScheme.toString()).setEventIdScheme(eventIdScheme.toString()).setFilename(uploadFileName);
    if (FORMAT_CSV.equals(payloadFormat)) {
        Events events = csvEventService.readEvents(in, skipFirst);
        scheduler.executeTask(new ImportEventsTask(events.getEvents(), eventService, importOptions, taskId));
    } else {
        List<Event> events;
        if (FORMAT_JSON.equals(payloadFormat)) {
            events = eventService.getEventsJson(in);
        } else {
            events = eventService.getEventsXml(in);
        }
        scheduler.executeTask(new ImportEventTask(events, eventService, importOptions, taskId));
    }
    return SUCCESS;
}
Also used : ImportEventsTask(org.hisp.dhis.dxf2.events.event.ImportEventsTask) TaskId(org.hisp.dhis.scheduling.TaskId) ImportEventTask(org.hisp.dhis.dxf2.events.event.ImportEventTask) Events(org.hisp.dhis.dxf2.events.event.Events) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Event(org.hisp.dhis.dxf2.events.event.Event) FileInputStream(java.io.FileInputStream) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions)

Example 3 with ImportEventTask

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

the class EventController method postXmlEvent.

// -------------------------------------------------------------------------
// CREATE
// -------------------------------------------------------------------------
@RequestMapping(method = RequestMethod.POST, consumes = "application/xml")
@PreAuthorize("hasRole('ALL') or hasRole('F_TRACKED_ENTITY_DATAVALUE_ADD')")
public void postXmlEvent(@RequestParam(defaultValue = "CREATE") ImportStrategy strategy, HttpServletResponse response, HttpServletRequest request, ImportOptions importOptions) throws Exception {
    importOptions.setImportStrategy(strategy);
    InputStream inputStream = StreamUtils.wrapAndCheckCompressionFormat(request.getInputStream());
    importOptions.setIdSchemes(getIdSchemesFromParameters(importOptions.getIdSchemes(), contextService.getParameterValuesMap()));
    if (!importOptions.isAsync()) {
        ImportSummaries importSummaries = eventService.addEventsXml(inputStream, importOptions);
        importSummaries.setImportOptions(importOptions);
        importSummaries.getImportSummaries().stream().filter(importSummary -> !importOptions.isDryRun() && !importSummary.getStatus().equals(ImportStatus.ERROR) && !importOptions.getImportStrategy().isDelete()).forEach(importSummary -> importSummary.setHref(ContextUtils.getRootPath(request) + RESOURCE_PATH + "/" + importSummary.getReference()));
        if (importSummaries.getImportSummaries().size() == 1) {
            ImportSummary importSummary = importSummaries.getImportSummaries().get(0);
            importSummary.setImportOptions(importOptions);
            if (!importOptions.isDryRun()) {
                if (!importSummary.getStatus().equals(ImportStatus.ERROR)) {
                    response.setHeader("Location", ContextUtils.getRootPath(request) + RESOURCE_PATH + "/" + importSummary.getReference());
                }
            }
        }
        webMessageService.send(WebMessageUtils.importSummaries(importSummaries), response, request);
    } else {
        TaskId taskId = new TaskId(TaskCategory.EVENT_IMPORT, currentUserService.getCurrentUser());
        List<Event> events = eventService.getEventsXml(inputStream);
        scheduler.executeTask(new ImportEventTask(events, eventService, importOptions, taskId));
        response.setHeader("Location", ContextUtils.getRootPath(request) + "/system/tasks/" + TaskCategory.EVENT_IMPORT);
        response.setStatus(HttpServletResponse.SC_NO_CONTENT);
    }
}
Also used : ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) PathVariable(org.springframework.web.bind.annotation.PathVariable) DataValue(org.hisp.dhis.dxf2.events.event.DataValue) EventRowService(org.hisp.dhis.dxf2.events.report.EventRowService) Order(org.hisp.dhis.query.Order) RequestParam(org.springframework.web.bind.annotation.RequestParam) Arrays(java.util.Arrays) EventService(org.hisp.dhis.dxf2.events.event.EventService) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Date(java.util.Date) RenderService(org.hisp.dhis.render.RenderService) Autowired(org.springframework.beans.factory.annotation.Autowired) WebMessageService(org.hisp.dhis.webapi.service.WebMessageService) StringUtils(org.apache.commons.lang3.StringUtils) FileResourceStorageStatus(org.hisp.dhis.fileresource.FileResourceStorageStatus) ProgramStageInstanceService(org.hisp.dhis.program.ProgramStageInstanceService) NodeUtils(org.hisp.dhis.node.NodeUtils) Model(org.springframework.ui.Model) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) FileResourceService(org.hisp.dhis.fileresource.FileResourceService) Map(java.util.Map) Preset(org.hisp.dhis.node.Preset) URI(java.net.URI) InputUtils(org.hisp.dhis.dxf2.utils.InputUtils) Scheduler(org.hisp.dhis.system.scheduling.Scheduler) ImportEventTask(org.hisp.dhis.dxf2.events.event.ImportEventTask) ImportEventsTask(org.hisp.dhis.dxf2.events.event.ImportEventsTask) ContextService(org.hisp.dhis.webapi.service.ContextService) CsvEventService(org.hisp.dhis.dxf2.events.event.csv.CsvEventService) OrganisationUnitSelectionMode(org.hisp.dhis.common.OrganisationUnitSelectionMode) HttpHeaders(org.springframework.http.HttpHeaders) FieldFilterService(org.hisp.dhis.fieldfilter.FieldFilterService) CacheStrategy(org.hisp.dhis.common.cache.CacheStrategy) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Set(java.util.Set) EventStatus(org.hisp.dhis.event.EventStatus) SchemaService(org.hisp.dhis.schema.SchemaService) Sets(com.google.common.collect.Sets) Event(org.hisp.dhis.dxf2.events.event.Event) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) EventRows(org.hisp.dhis.dxf2.events.report.EventRows) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Events(org.hisp.dhis.dxf2.events.event.Events) Schema(org.hisp.dhis.schema.Schema) GZIPOutputStream(java.util.zip.GZIPOutputStream) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) RootNode(org.hisp.dhis.node.types.RootNode) TaskId(org.hisp.dhis.scheduling.TaskId) DhisApiVersion(org.hisp.dhis.common.DhisApiVersion) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) DataElementService(org.hisp.dhis.dataelement.DataElementService) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) HashMap(java.util.HashMap) Controller(org.springframework.stereotype.Controller) StreamUtils(org.hisp.dhis.commons.util.StreamUtils) ApiVersion(org.hisp.dhis.webapi.mvc.annotation.ApiVersion) Program(org.hisp.dhis.program.Program) DataElement(org.hisp.dhis.dataelement.DataElement) EventSearchParams(org.hisp.dhis.dxf2.events.event.EventSearchParams) HttpServletRequest(javax.servlet.http.HttpServletRequest) Lists(com.google.common.collect.Lists) ByteSource(com.google.common.io.ByteSource) ImportStatus(org.hisp.dhis.dxf2.importsummary.ImportStatus) WebMessageUtils(org.hisp.dhis.dxf2.webmessage.WebMessageUtils) OutputStream(java.io.OutputStream) TrackedEntityInstanceService(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstanceService) ContextUtils(org.hisp.dhis.webapi.utils.ContextUtils) IdSchemes(org.hisp.dhis.common.IdSchemes) FileResource(org.hisp.dhis.fileresource.FileResource) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Grid(org.hisp.dhis.common.Grid) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) ProgramStatus(org.hisp.dhis.program.ProgramStatus) OrderParams(org.hisp.dhis.dxf2.common.OrderParams) FileResourceWebMessageResponse(org.hisp.dhis.dxf2.webmessage.responses.FileResourceWebMessageResponse) CurrentUserService(org.hisp.dhis.user.CurrentUserService) TaskCategory(org.hisp.dhis.scheduling.TaskCategory) FileResourceDomain(org.hisp.dhis.fileresource.FileResourceDomain) TextUtils(org.hisp.dhis.commons.util.TextUtils) InputStream(java.io.InputStream) TaskId(org.hisp.dhis.scheduling.TaskId) ImportEventTask(org.hisp.dhis.dxf2.events.event.ImportEventTask) InputStream(java.io.InputStream) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Event(org.hisp.dhis.dxf2.events.event.Event) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

InputStream (java.io.InputStream)3 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)3 Event (org.hisp.dhis.dxf2.events.event.Event)3 Events (org.hisp.dhis.dxf2.events.event.Events)3 ImportEventTask (org.hisp.dhis.dxf2.events.event.ImportEventTask)3 ImportEventsTask (org.hisp.dhis.dxf2.events.event.ImportEventsTask)3 Lists (com.google.common.collect.Lists)2 Sets (com.google.common.collect.Sets)2 ByteSource (com.google.common.io.ByteSource)2 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 URI (java.net.URI)2 Arrays (java.util.Arrays)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 GZIPOutputStream (java.util.zip.GZIPOutputStream)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2