use of org.hisp.dhis.dxf2.common.ImportSummary 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);
}
use of org.hisp.dhis.dxf2.common.ImportSummary in project dhis2-core by dhis2.
the class TrackedEntityInstanceController method updateTrackedEntityInstanceJson.
@RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasRole('ALL') or hasRole('F_TRACKED_ENTITY_INSTANCE_ADD')")
public void updateTrackedEntityInstanceJson(@PathVariable String id, ImportOptions importOptions, HttpServletRequest request, HttpServletResponse response) throws IOException {
InputStream inputStream = StreamUtils.wrapAndCheckCompressionFormat(request.getInputStream());
ImportSummary importSummary = trackedEntityInstanceService.updateTrackedEntityInstanceJson(id, inputStream, importOptions);
importSummary.setImportOptions(importOptions);
webMessageService.send(WebMessageUtils.importSummary(importSummary), response, request);
}
use of org.hisp.dhis.dxf2.common.ImportSummary in project dhis2-core by dhis2.
the class TrackedEntityInstanceController method postTrackedEntityInstanceXml.
@RequestMapping(value = "", method = RequestMethod.POST, consumes = MediaType.APPLICATION_XML_VALUE)
@PreAuthorize("hasRole('ALL') or hasRole('F_TRACKED_ENTITY_INSTANCE_ADD')")
public void postTrackedEntityInstanceXml(@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.addTrackedEntityInstanceXml(inputStream, importOptions);
response.setContentType(MediaType.APPLICATION_XML_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);
}
use of org.hisp.dhis.dxf2.common.ImportSummary in project dhis2-core by dhis2.
the class MetadataSyncPreProcessorTest method testhandleAggregateDataPushShouldNotThrowExceptionWhenDataPushIsSuccessful.
@Test
public void testhandleAggregateDataPushShouldNotThrowExceptionWhenDataPushIsSuccessful() throws Exception {
MetadataRetryContext mockRetryContext = mock(MetadataRetryContext.class);
ImportSummary expectedSummary = new ImportSummary();
expectedSummary.setStatus(ImportStatus.SUCCESS);
AvailabilityStatus availabilityStatus = new AvailabilityStatus(true, "test_message", null);
when(synchronizationManager.isRemoteServerAvailable()).thenReturn(availabilityStatus);
when(metadataSyncPreProcessor.handleAggregateDataPush(mockRetryContext)).thenReturn(expectedSummary);
ImportSummary actualSummary = metadataSyncPreProcessor.handleAggregateDataPush(mockRetryContext);
assertEquals(expectedSummary.getStatus(), actualSummary.getStatus());
}
use of org.hisp.dhis.dxf2.common.ImportSummary in project dhis2-core by dhis2.
the class MetadataSyncPreProcessorTest method testHandleEventDataPushShouldThrowExceptionWhenEventDataPushIsUnsuccessful.
@Test(expected = MetadataSyncServiceException.class)
public void testHandleEventDataPushShouldThrowExceptionWhenEventDataPushIsUnsuccessful() throws Exception {
MetadataRetryContext mockRetryContext = mock(MetadataRetryContext.class);
ImportSummaries expectedSummary = new ImportSummaries();
ImportSummary summary = new ImportSummary();
summary.setStatus(ImportStatus.ERROR);
expectedSummary.addImportSummary(summary);
AvailabilityStatus availabilityStatus = new AvailabilityStatus(true, "test_message", null);
when(synchronizationManager.isRemoteServerAvailable()).thenReturn(availabilityStatus);
when(metadataSyncPreProcessor.handleEventDataPush(mockRetryContext)).thenReturn(expectedSummary);
ImportSummaries actualSummary = metadataSyncPreProcessor.handleEventDataPush(mockRetryContext);
assertEquals(expectedSummary.getImportSummaries().get(0).getStatus(), actualSummary.getImportSummaries().get(0).getStatus());
}
Aggregations