use of org.hisp.dhis.dxf2.importsummary.ImportStatus.ERROR in project dhis2-core by dhis2.
the class DataValueSetController method postAdxDataValueSet.
@RequestMapping(method = RequestMethod.POST, consumes = CONTENT_TYPE_XML_ADX)
@PreAuthorize("hasRole('ALL') or hasRole('F_DATAVALUE_ADD')")
public void postAdxDataValueSet(ImportOptions importOptions, HttpServletRequest request, HttpServletResponse response) throws IOException {
if (importOptions.isAsync()) {
startAsyncImport(importOptions, ImportDataValueTask.FORMAT_ADX, request, response);
} else {
try {
ImportSummary summary = adxDataService.saveDataValueSet(request.getInputStream(), importOptions, null);
summary.setImportOptions(importOptions);
response.setContentType(CONTENT_TYPE_XML);
renderService.toXml(response.getOutputStream(), summary);
} catch (Exception ex) {
log.error("ADX Import error: ", ex);
throw ex;
}
}
}
use of org.hisp.dhis.dxf2.importsummary.ImportStatus.ERROR in project dhis2-core by dhis2.
the class DefaultSynchronizationManager method executeDataPush.
/**
* Executes a push of data values to the given remote instance.
*
* @param instance the remote system instance.
* @return an ImportSummary.
*/
private ImportSummary executeDataPush(SystemInstance instance) throws WebMessageParseException {
// ---------------------------------------------------------------------
// Set time for last success to start of process to make data saved
// subsequently part of next synch process without being ignored
// ---------------------------------------------------------------------
final Date startTime = new Date();
final Date lastSuccessTime = getLastDataSynchSuccessFallback();
final int lastUpdatedCount = dataValueService.getDataValueCountLastUpdatedAfter(lastSuccessTime, true);
log.info("Values: " + lastUpdatedCount + " since last synch success: " + lastSuccessTime);
if (lastUpdatedCount == 0) {
log.debug("Skipping synch, no new or updated data values");
return null;
}
log.info("Values: " + lastUpdatedCount + " since last synch success: " + lastSuccessTime);
log.info("Remote server POST URL: " + instance.getUrl());
final RequestCallback requestCallback = request -> {
request.getHeaders().setContentType(MediaType.APPLICATION_JSON);
request.getHeaders().add(HEADER_AUTHORIZATION, CodecUtils.getBasicAuthString(instance.getUsername(), instance.getPassword()));
dataValueSetService.writeDataValueSetJson(lastSuccessTime, request.getBody(), new IdSchemes());
};
ResponseExtractor<ImportSummary> responseExtractor = new ImportSummaryResponseExtractor();
ImportSummary summary = null;
try {
summary = restTemplate.execute(instance.getUrl(), HttpMethod.POST, requestCallback, responseExtractor);
} catch (HttpClientErrorException ex) {
String responseBody = ex.getResponseBodyAsString();
summary = WebMessageParseUtils.fromWebMessageResponse(responseBody, ImportSummary.class);
} catch (HttpServerErrorException ex) {
String responseBody = ex.getResponseBodyAsString();
log.error("Internal error happened during event data push: " + responseBody, ex);
throw ex;
} catch (ResourceAccessException ex) {
log.error("Exception during event data push: " + ex.getMessage(), ex);
throw ex;
}
log.info("Synch summary: " + summary);
if (summary != null && ImportStatus.SUCCESS.equals(summary.getStatus())) {
setLastDataSynchSuccess(startTime);
log.info("Synch successful, setting last success time: " + startTime);
} else {
log.warn("Sync failed: " + summary);
}
return summary;
}
use of org.hisp.dhis.dxf2.importsummary.ImportStatus.ERROR in project dhis2-core by dhis2.
the class DefaultSynchronizationManager method executeEventPush.
@Override
public ImportSummaries executeEventPush() throws WebMessageParseException {
AvailabilityStatus availability = isRemoteServerAvailable();
if (!availability.isAvailable()) {
log.info("Aborting synch, server not available");
return null;
}
// ---------------------------------------------------------------------
// Set time for last success to start of process to make data saved
// subsequently part of next synch process without being ignored
// ---------------------------------------------------------------------
final Date startTime = new Date();
final Date lastSuccessTime = getLastEventSynchSuccessFallback();
int lastUpdatedEventsCount = eventService.getAnonymousEventValuesCountLastUpdatedAfter(lastSuccessTime);
log.info("Events: " + lastUpdatedEventsCount + " since last synch success: " + lastSuccessTime);
if (lastUpdatedEventsCount == 0) {
log.info("Skipping synch, no new or updated data values for events");
return null;
}
String url = systemSettingManager.getSystemSetting(SettingKey.REMOTE_INSTANCE_URL) + "/api/events";
log.info("Remote server events POST URL: " + url);
final String username = (String) systemSettingManager.getSystemSetting(SettingKey.REMOTE_INSTANCE_USERNAME);
final String password = (String) systemSettingManager.getSystemSetting(SettingKey.REMOTE_INSTANCE_PASSWORD);
final RequestCallback requestCallback = new RequestCallback() {
@Override
public void doWithRequest(ClientHttpRequest request) throws IOException {
request.getHeaders().setContentType(MediaType.APPLICATION_JSON);
request.getHeaders().add(HEADER_AUTHORIZATION, CodecUtils.getBasicAuthString(username, password));
Events result = eventService.getAnonymousEventValuesLastUpdatedAfter(lastSuccessTime);
renderService.toJson(request.getBody(), result);
}
};
ResponseExtractor<ImportSummaries> responseExtractor = new ImportSummariesResponseExtractor();
ImportSummaries summaries = null;
try {
summaries = restTemplate.execute(url, HttpMethod.POST, requestCallback, responseExtractor);
} catch (HttpClientErrorException ex) {
String responseBody = ex.getResponseBodyAsString();
summaries = WebMessageParseUtils.fromWebMessageResponse(responseBody, ImportSummaries.class);
} catch (HttpServerErrorException ex) {
String responseBody = ex.getResponseBodyAsString();
log.error("Internal error happened during event data push: " + responseBody, ex);
throw ex;
} catch (ResourceAccessException ex) {
log.error("Exception during event data push: " + ex.getMessage(), ex);
throw ex;
}
log.info("Event synch summary: " + summaries);
boolean isError = false;
if (summaries != null) {
for (ImportSummary summary : summaries.getImportSummaries()) {
if (ImportStatus.ERROR.equals(summary.getStatus()) || ImportStatus.WARNING.equals(summary.getStatus())) {
isError = true;
log.debug("Sync failed: " + summaries);
break;
}
}
}
if (!isError) {
setLastEventSynchSuccess(startTime);
log.info("Synch successful, setting last success time: " + startTime);
}
return summaries;
}
use of org.hisp.dhis.dxf2.importsummary.ImportStatus.ERROR in project dhis2-core by dhis2.
the class AbstractEnrollmentService method validateRequest.
private ImportSummary validateRequest(Program program, org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance, Enrollment enrollment, OrganisationUnit organisationUnit, ImportOptions importOptions) {
ImportSummary importSummary = new ImportSummary(enrollment.getEnrollment());
String error = validateProgramForEnrollment(program, enrollment, organisationUnit, importOptions);
if (!StringUtils.isEmpty(error)) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.setDescription(error);
importSummary.incrementIgnored();
return importSummary;
}
ProgramInstanceQueryParams params = new ProgramInstanceQueryParams();
params.setOrganisationUnitMode(OrganisationUnitSelectionMode.ALL);
params.setSkipPaging(true);
params.setProgram(program);
params.setTrackedEntityInstanceUid(entityInstance.getUid());
// endpoint is used for bulk import and sync purposes as well
if (enrollment.getStatus() != EnrollmentStatus.CANCELLED) {
List<Enrollment> enrollments = getEnrollments(programInstanceService.getProgramInstances(params));
Set<Enrollment> activeEnrollments = enrollments.stream().filter(e -> e.getStatus() == EnrollmentStatus.ACTIVE).collect(Collectors.toSet());
// needed
if (!activeEnrollments.isEmpty() && enrollment.getStatus() == EnrollmentStatus.ACTIVE) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.setDescription("TrackedEntityInstance " + entityInstance.getUid() + " already has an active enrollment in program " + program.getUid());
importSummary.incrementIgnored();
return importSummary;
}
// imported enrollment has a state other than CANCELLED
if (program.getOnlyEnrollOnce()) {
Set<Enrollment> activeOrCompletedEnrollments = enrollments.stream().filter(e -> e.getStatus() == EnrollmentStatus.ACTIVE || e.getStatus() == EnrollmentStatus.COMPLETED).collect(Collectors.toSet());
if (!activeOrCompletedEnrollments.isEmpty()) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.setDescription("TrackedEntityInstance " + entityInstance.getUid() + " already has an active or completed enrollment in program " + program.getUid() + ", and this program only allows enrolling one time");
importSummary.incrementIgnored();
return importSummary;
}
}
}
checkAttributes(entityInstance, enrollment, importOptions, importSummary);
if (importSummary.hasConflicts()) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.incrementIgnored();
}
return importSummary;
}
use of org.hisp.dhis.dxf2.importsummary.ImportStatus.ERROR in project dhis2-core by dhis2.
the class EventManager method updateEvents.
public ImportSummaries updateEvents(final List<Event> events, final WorkContext workContext) {
final ImportSummaries importSummaries = new ImportSummaries();
// pre-process events
executorsByPhase.get(EventProcessorPhase.UPDATE_PRE).execute(workContext, events);
if (ImportStrategyUtils.isUpdate(workContext.getImportOptions().getImportStrategy())) {
importSummaries.addImportSummaries(run(workContext, events, checkersRunOnUpdate));
}
// collect the UIDs of events that did not pass validation
final List<String> eventValidationFailedUids = importSummaries.getImportSummaries().stream().filter(i -> i.isStatus(ERROR)).map(ImportSummary::getReference).collect(toList());
if (eventValidationFailedUids.size() == events.size()) {
return importSummaries;
}
if (!workContext.getImportOptions().isDryRun()) {
try {
eventPersistenceService.update(workContext, eventValidationFailedUids.isEmpty() ? events : events.stream().filter(e -> !eventValidationFailedUids.contains(e.getEvent())).collect(toList()));
} catch (Exception e) {
handleFailure(workContext, importSummaries, events, IMPORT_ERROR_STRING, UPDATE);
}
final List<String> eventPersistenceFailedUids = importSummaries.getImportSummaries().stream().filter(i -> i.isStatus(ERROR)).map(ImportSummary::getReference).collect(toList());
// Post processing only the events that passed validation and were
// persisted
// correctly.
List<Event> savedEvents = events.stream().filter(e -> !eventPersistenceFailedUids.contains(e.getEvent())).collect(toList());
executorsByPhase.get(EventProcessorPhase.UPDATE_POST).execute(workContext, savedEvents);
incrementSummaryTotals(events, importSummaries, UPDATE);
}
return importSummaries;
}
Aggregations