Search in sources :

Example 21 with ImportSummaries

use of org.hisp.dhis.dxf2.importsummary.ImportSummaries in project dhis2-core by dhis2.

the class EnrollmentController method postEnrollmentJson.

// -------------------------------------------------------------------------
// CREATE
// -------------------------------------------------------------------------
@RequestMapping(value = "", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasRole('ALL') or hasRole('F_PROGRAM_ENROLLMENT')")
public void postEnrollmentJson(@RequestParam(defaultValue = "CREATE") ImportStrategy strategy, ImportOptions importOptions, HttpServletRequest request, HttpServletResponse response) throws IOException {
    importOptions.setStrategy(strategy);
    InputStream inputStream = StreamUtils.wrapAndCheckCompressionFormat(request.getInputStream());
    ImportSummaries importSummaries = enrollmentService.addEnrollmentsJson(inputStream, importOptions);
    importSummaries.setImportOptions(importOptions);
    response.setContentType(MediaType.APPLICATION_JSON_VALUE);
    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 (!importSummary.getStatus().equals(ImportStatus.ERROR)) {
            response.setHeader("Location", getResourcePath(request, importSummary));
        }
    }
    response.setStatus(HttpServletResponse.SC_CREATED);
    webMessageService.send(WebMessageUtils.importSummaries(importSummaries), response, request);
}
Also used : DhisApiVersion(org.hisp.dhis.common.DhisApiVersion) ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) PathVariable(org.springframework.web.bind.annotation.PathVariable) RequestParam(org.springframework.web.bind.annotation.RequestParam) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Date(java.util.Date) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Controller(org.springframework.stereotype.Controller) StreamUtils(org.hisp.dhis.commons.util.StreamUtils) ApiVersion(org.hisp.dhis.webapi.mvc.annotation.ApiVersion) WebMessageService(org.hisp.dhis.webapi.service.WebMessageService) ArrayList(java.util.ArrayList) NodeUtils(org.hisp.dhis.node.NodeUtils) Model(org.springframework.ui.Model) HttpServletRequest(javax.servlet.http.HttpServletRequest) Lists(com.google.common.collect.Lists) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Map(java.util.Map) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ImportStatus(org.hisp.dhis.dxf2.importsummary.ImportStatus) WebMessageUtils(org.hisp.dhis.dxf2.webmessage.WebMessageUtils) EnrollmentService(org.hisp.dhis.dxf2.events.enrollment.EnrollmentService) ContextUtils(org.hisp.dhis.webapi.utils.ContextUtils) ProgramInstanceQueryParams(org.hisp.dhis.program.ProgramInstanceQueryParams) NotFoundException(org.hisp.dhis.webapi.controller.exception.NotFoundException) ContextService(org.hisp.dhis.webapi.service.ContextService) OrganisationUnitSelectionMode(org.hisp.dhis.common.OrganisationUnitSelectionMode) FieldFilterService(org.hisp.dhis.fieldfilter.FieldFilterService) MediaType(org.springframework.http.MediaType) HttpServletResponse(javax.servlet.http.HttpServletResponse) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Set(java.util.Set) IOException(java.io.IOException) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) Collectors(java.util.stream.Collectors) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) ProgramStatus(org.hisp.dhis.program.ProgramStatus) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) ProgramInstanceService(org.hisp.dhis.program.ProgramInstanceService) Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment) RootNode(org.hisp.dhis.node.types.RootNode) TextUtils(org.hisp.dhis.commons.util.TextUtils) InputStream(java.io.InputStream) InputStream(java.io.InputStream) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 22 with ImportSummaries

use of org.hisp.dhis.dxf2.importsummary.ImportSummaries in project dhis2-core by dhis2.

the class JacksonEventService method addEvents.

private ImportSummaries addEvents(List<Event> events, TaskId taskId, ImportOptions importOptions) {
    ImportSummaries importSummaries = new ImportSummaries();
    notifier.clear(taskId).notify(taskId, "Importing events");
    Clock clock = new Clock(log).startClock();
    List<Event> create = new ArrayList<>();
    List<Event> update = new ArrayList<>();
    List<String> delete = new ArrayList<>();
    if (importOptions.getImportStrategy().isCreate()) {
        create.addAll(events);
    } else if (importOptions.getImportStrategy().isCreateAndUpdate()) {
        for (Event event : events) {
            if (StringUtils.isEmpty(event.getEvent())) {
                create.add(event);
            } else {
                ProgramStageInstance programStageInstance = manager.getObject(ProgramStageInstance.class, importOptions.getIdSchemes().getProgramStageInstanceIdScheme(), event.getEvent());
                if (programStageInstance == null) {
                    create.add(event);
                } else {
                    update.add(event);
                }
            }
        }
    } else if (importOptions.getImportStrategy().isUpdate()) {
        update.addAll(events);
    } else if (importOptions.getImportStrategy().isDelete()) {
        delete.addAll(events.stream().map(Event::getEvent).collect(Collectors.toList()));
    }
    importSummaries.addImportSummaries(addEvents(create, importOptions));
    importSummaries.addImportSummaries(updateEvents(update, false));
    importSummaries.addImportSummaries(deleteEvents(delete));
    if (taskId != null) {
        notifier.notify(taskId, NotificationLevel.INFO, "Import done. Completed in " + clock.time() + ".", true).addTaskSummary(taskId, importSummaries);
    } else {
        clock.logTime("Import done");
    }
    return importSummaries;
}
Also used : ArrayList(java.util.ArrayList) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) Clock(org.hisp.dhis.system.util.Clock) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 23 with ImportSummaries

use of org.hisp.dhis.dxf2.importsummary.ImportSummaries in project dhis2-core by dhis2.

the class AbstractTrackedEntityInstanceService method updateTrackedEntityInstances.

// -------------------------------------------------------------------------
// UPDATE
// -------------------------------------------------------------------------
@Override
public ImportSummaries updateTrackedEntityInstances(List<TrackedEntityInstance> trackedEntityInstances, ImportOptions importOptions) {
    if (importOptions == null) {
        importOptions = new ImportOptions();
    }
    ImportSummaries importSummaries = new ImportSummaries();
    int counter = 0;
    for (TrackedEntityInstance trackedEntityInstance : trackedEntityInstances) {
        importSummaries.addImportSummary(updateTrackedEntityInstance(trackedEntityInstance, importOptions));
        if (counter % FLUSH_FREQUENCY == 0) {
            clearSession();
        }
        counter++;
    }
    return importSummaries;
}
Also used : ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions)

Example 24 with ImportSummaries

use of org.hisp.dhis.dxf2.importsummary.ImportSummaries in project dhis2-core by dhis2.

the class MetadataSyncPreProcessor method handleEventImportSummary.

private void handleEventImportSummary(ImportSummaries importSummary, MetadataRetryContext context) {
    if (importSummary != null) {
        boolean isImportError = false;
        StringBuilder summaryDescription = new StringBuilder();
        for (ImportSummary summary : importSummary.getImportSummaries()) {
            if (ImportStatus.ERROR.equals(summary.getStatus())) {
                isImportError = true;
                summaryDescription.append(summary.getDescription());
                summaryDescription.append("\n");
            }
        }
        if (isImportError) {
            log.error("Import Summary description: " + summaryDescription.toString());
            context.updateRetryContext(MetadataSyncTask.EVENT_PUSH_SUMMARY, summaryDescription.toString(), null, null);
            throw new MetadataSyncServiceException("The Event Data Push was not successful. ");
        }
    }
}
Also used : MetadataSyncServiceException(org.hisp.dhis.dxf2.metadata.sync.exception.MetadataSyncServiceException) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary)

Example 25 with ImportSummaries

use of org.hisp.dhis.dxf2.importsummary.ImportSummaries in project dhis2-core by dhis2.

the class TrackedEntityInstanceController method postTrackedEntityInstanceJson.

// -------------------------------------------------------------------------
// CREATE
// -------------------------------------------------------------------------
@RequestMapping(value = "", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasRole('ALL') or hasRole('F_TRACKED_ENTITY_INSTANCE_ADD')")
public void postTrackedEntityInstanceJson(@RequestParam(defaultValue = "CREATE_AND_UPDATE") ImportStrategy strategy, ImportOptions importOptions, HttpServletRequest request, HttpServletResponse response) throws IOException {
    importOptions.setStrategy(strategy);
    InputStream inputStream = StreamUtils.wrapAndCheckCompressionFormat(request.getInputStream());
    ImportSummaries importSummaries = trackedEntityInstanceService.addTrackedEntityInstanceJson(inputStream, importOptions);
    importSummaries.setImportOptions(importOptions);
    response.setContentType(MediaType.APPLICATION_JSON_VALUE);
    importSummaries.getImportSummaries().stream().filter(importSummary -> !importOptions.isDryRun() && !importSummary.getStatus().equals(ImportStatus.ERROR) && !importOptions.getImportStrategy().isDelete()).forEach(importSummary -> importSummary.setHref(ContextUtils.getRootPath(request) + TrackedEntityInstanceSchemaDescriptor.API_ENDPOINT + "/" + importSummary.getReference()));
    if (importSummaries.getImportSummaries().size() == 1) {
        ImportSummary importSummary = importSummaries.getImportSummaries().get(0);
        importSummary.setImportOptions(importOptions);
        if (!importSummary.getStatus().equals(ImportStatus.ERROR)) {
            response.setHeader("Location", getResourcePath(request, importSummary));
        }
    }
    response.setStatus(HttpServletResponse.SC_CREATED);
    webMessageService.send(WebMessageUtils.importSummaries(importSummaries), response, request);
}
Also used : ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) PathVariable(org.springframework.web.bind.annotation.PathVariable) RequestParam(org.springframework.web.bind.annotation.RequestParam) Arrays(java.util.Arrays) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Date(java.util.Date) Autowired(org.springframework.beans.factory.annotation.Autowired) WebMessageService(org.hisp.dhis.webapi.service.WebMessageService) StringUtils(org.apache.commons.lang3.StringUtils) NodeUtils(org.hisp.dhis.node.NodeUtils) Model(org.springframework.ui.Model) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) GridUtils(org.hisp.dhis.system.grid.GridUtils) ContextService(org.hisp.dhis.webapi.service.ContextService) DxfNamespaces(org.hisp.dhis.common.DxfNamespaces) OrganisationUnitSelectionMode(org.hisp.dhis.common.OrganisationUnitSelectionMode) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) FieldFilterService(org.hisp.dhis.fieldfilter.FieldFilterService) MediaType(org.springframework.http.MediaType) CacheStrategy(org.hisp.dhis.common.cache.CacheStrategy) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Set(java.util.Set) EventStatus(org.hisp.dhis.event.EventStatus) TrackedEntityInstanceSchemaDescriptor(org.hisp.dhis.schema.descriptors.TrackedEntityInstanceSchemaDescriptor) Collectors(java.util.stream.Collectors) List(java.util.List) RootNode(org.hisp.dhis.node.types.RootNode) Joiner(com.google.common.base.Joiner) DhisApiVersion(org.hisp.dhis.common.DhisApiVersion) CollectionNode(org.hisp.dhis.node.types.CollectionNode) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Controller(org.springframework.stereotype.Controller) StreamUtils(org.hisp.dhis.commons.util.StreamUtils) ApiVersion(org.hisp.dhis.webapi.mvc.annotation.ApiVersion) HttpServletRequest(javax.servlet.http.HttpServletRequest) Lists(com.google.common.collect.Lists) ObjectUtils(org.apache.commons.lang3.ObjectUtils) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ImportStatus(org.hisp.dhis.dxf2.importsummary.ImportStatus) WebMessageUtils(org.hisp.dhis.dxf2.webmessage.WebMessageUtils) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) TrackedEntityInstanceService(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstanceService) ContextUtils(org.hisp.dhis.webapi.utils.ContextUtils) NotFoundException(org.hisp.dhis.webapi.controller.exception.NotFoundException) Pager(org.hisp.dhis.common.Pager) TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) 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) HttpStatus(org.springframework.http.HttpStatus) TextUtils(org.hisp.dhis.commons.util.TextUtils) InputStream(java.io.InputStream) InputStream(java.io.InputStream) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ImportSummaries (org.hisp.dhis.dxf2.importsummary.ImportSummaries)26 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)10 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)9 InputStream (java.io.InputStream)8 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)7 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)7 Date (java.util.Date)6 Lists (com.google.common.collect.Lists)5 IOException (java.io.IOException)5 List (java.util.List)5 Set (java.util.Set)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 DhisApiVersion (org.hisp.dhis.common.DhisApiVersion)5 OrganisationUnitSelectionMode (org.hisp.dhis.common.OrganisationUnitSelectionMode)5 StreamUtils (org.hisp.dhis.commons.util.StreamUtils)5 TextUtils (org.hisp.dhis.commons.util.TextUtils)5 ArrayList (java.util.ArrayList)4 Arrays (java.util.Arrays)4 StringUtils (org.apache.commons.lang3.StringUtils)4