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);
}
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;
}
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;
}
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. ");
}
}
}
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);
}
Aggregations