Search in sources :

Example 86 with ImportSummary

use of org.hisp.dhis.dxf2.common.ImportSummary 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 87 with ImportSummary

use of org.hisp.dhis.dxf2.common.ImportSummary in project dhis2-core by dhis2.

the class EventController method deleteEvent.

// -------------------------------------------------------------------------
// DELETE
// -------------------------------------------------------------------------
@RequestMapping(value = "/{uid}", method = RequestMethod.DELETE)
@PreAuthorize("hasRole('ALL') or hasRole('F_TRACKED_ENTITY_DATAVALUE_DELETE')")
public void deleteEvent(HttpServletResponse response, HttpServletRequest request, @PathVariable("uid") String uid) throws WebMessageException {
    if (!programStageInstanceService.programStageInstanceExists(uid)) {
        throw new WebMessageException(WebMessageUtils.notFound("Event not found for ID " + uid));
    }
    response.setStatus(HttpServletResponse.SC_OK);
    try {
        ImportSummary importSummary = eventService.deleteEvent(uid);
        webMessageService.send(WebMessageUtils.importSummary(importSummary), response, request);
    } catch (Exception ex) {
        webMessageService.send(WebMessageUtils.conflict("Unable to delete event " + uid, ex.getMessage()), response, request);
    }
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) IOException(java.io.IOException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 88 with ImportSummary

use of org.hisp.dhis.dxf2.common.ImportSummary in project dhis2-core by dhis2.

the class SystemController method getTaskSummaryJson.

@RequestMapping(value = "/taskSummaries/{category}", method = RequestMethod.GET, produces = { "*/*", "application/json" })
public void getTaskSummaryJson(HttpServletResponse response, @PathVariable("category") String category) throws IOException {
    if (category != null) {
        TaskCategory taskCategory = TaskCategory.valueOf(category.toUpperCase());
        TaskId taskId = new TaskId(taskCategory, currentUserService.getCurrentUser());
        Object summary = notifier.getTaskSummary(taskId);
        if (//TODO improve this
        summary != null && summary.getClass().isAssignableFrom(ImportSummary.class)) {
            ImportSummary importSummary = (ImportSummary) summary;
            renderService.toJson(response.getOutputStream(), importSummary);
            return;
        } else {
            renderService.toJson(response.getOutputStream(), summary);
            return;
        }
    }
    renderService.toJson(response.getOutputStream(), new ImportSummary());
}
Also used : TaskId(org.hisp.dhis.scheduling.TaskId) TaskCategory(org.hisp.dhis.scheduling.TaskCategory) ImportSummary(org.hisp.dhis.dxf2.common.ImportSummary) StyleObject(org.hisp.dhis.setting.StyleObject) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 89 with ImportSummary

use of org.hisp.dhis.dxf2.common.ImportSummary in project dhis2-core by dhis2.

the class AdxController method postXMLDataValueSet.

@RequestMapping(method = RequestMethod.POST, consumes = "application/xml")
@PreAuthorize("hasRole('ALL') or hasRole('F_DATAVALUE_ADD')")
public void postXMLDataValueSet(ImportOptions importOptions, HttpServletResponse response, InputStream in, Model model) throws IOException {
    ImportSummary importSummary = adxService.saveDataValueSet(in, importOptions, null);
    importSummary.setImportOptions(importOptions);
    log.debug("Data values set saved");
    response.setContentType(CONTENT_TYPE_XML);
    DefaultRenderService.getXmlMapper().writeValue(response.getOutputStream(), importSummary);
}
Also used : ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 90 with ImportSummary

use of org.hisp.dhis.dxf2.common.ImportSummary in project dhis2-core by dhis2.

the class DataValueSetServiceIntegrationTest method testImportDeleteValuesXml.

// -------------------------------------------------------------------------
// Tests
// -------------------------------------------------------------------------
/**
     * Import 3 data values, then delete 3 data values.
     */
@Test
public void testImportDeleteValuesXml() throws Exception {
    assertEquals(0, dataValueService.getAllDataValues().size());
    in = new ClassPathResource("datavalueset/dataValueSetA.xml").getInputStream();
    ImportSummary summary = dataValueSetService.saveDataValueSet(in);
    assertEquals(3, summary.getImportCount().getImported());
    assertEquals(0, summary.getImportCount().getUpdated());
    assertEquals(0, summary.getImportCount().getDeleted());
    assertEquals(summary.getConflicts().toString(), 0, summary.getConflicts().size());
    assertEquals(ImportStatus.SUCCESS, summary.getStatus());
    assertEquals(3, dataValueService.getAllDataValues().size());
    // Delete values
    in = new ClassPathResource("datavalueset/dataValueSetADeleted.xml").getInputStream();
    summary = dataValueSetService.saveDataValueSet(in);
    assertEquals(0, summary.getImportCount().getImported());
    assertEquals(0, summary.getImportCount().getUpdated());
    assertEquals(3, summary.getImportCount().getDeleted());
    assertEquals(summary.getConflicts().toString(), 0, summary.getConflicts().size());
    assertEquals(ImportStatus.SUCCESS, summary.getStatus());
    assertEquals(0, dataValueService.getAllDataValues().size());
}
Also used : ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test) DhisTest(org.hisp.dhis.DhisTest)

Aggregations

ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)102 Test (org.junit.Test)58 DhisSpringTest (org.hisp.dhis.DhisSpringTest)49 ClassPathResource (org.springframework.core.io.ClassPathResource)39 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)29 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)25 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)22 DataValue (org.hisp.dhis.datavalue.DataValue)18 Event (org.hisp.dhis.dxf2.events.event.Event)17 InputStream (java.io.InputStream)14 Date (java.util.Date)11 ImportConflict (org.hisp.dhis.dxf2.importsummary.ImportConflict)11 ImportSummaries (org.hisp.dhis.dxf2.importsummary.ImportSummaries)11 IOException (java.io.IOException)10 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)10 DhisTest (org.hisp.dhis.DhisTest)9 DhisApiVersion (org.hisp.dhis.common.DhisApiVersion)7 DataElement (org.hisp.dhis.dataelement.DataElement)7 ImportStatus (org.hisp.dhis.dxf2.importsummary.ImportStatus)7 ApiVersion (org.hisp.dhis.webapi.mvc.annotation.ApiVersion)7