use of org.hisp.dhis.dxf2.common.ImportSummaryResponseExtractor 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;
}
Aggregations