use of org.hisp.dhis.dxf2.importsummary.ImportSummaries 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.ImportSummaries in project dhis2-core by dhis2.
the class MetadataSyncPreProcessor method handleEventDataPush.
public ImportSummaries handleEventDataPush(MetadataRetryContext context) {
log.debug("Entering event data push");
ImportSummaries importSummary = null;
AvailabilityStatus remoteServerAvailable = synchronizationManager.isRemoteServerAvailable();
if (!remoteServerAvailable.isAvailable()) {
String message = remoteServerAvailable.getMessage();
log.error(message);
context.updateRetryContext(MetadataSyncTask.EVENT_PUSH_SUMMARY, remoteServerAvailable.getMessage(), null, null);
throw new MetadataSyncServiceException(message);
}
try {
importSummary = synchronizationManager.executeEventPush();
handleEventImportSummary(importSummary, context);
} catch (Exception ex) {
log.error("Exception happened while trying to do event data push " + ex.getMessage(), ex);
if (ex instanceof MetadataSyncServiceException) {
throw (MetadataSyncServiceException) ex;
}
context.updateRetryContext(MetadataSyncTask.EVENT_PUSH_SUMMARY, ex.getMessage(), null, null);
throw new MetadataSyncServiceException(ex.getMessage(), ex);
}
log.debug("Exiting event data push");
return importSummary;
}
use of org.hisp.dhis.dxf2.importsummary.ImportSummaries in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method deleteTrackedEntityInstances.
@Override
public ImportSummaries deleteTrackedEntityInstances(List<String> uids) {
ImportSummaries importSummaries = new ImportSummaries();
int counter = 0;
for (String uid : uids) {
importSummaries.addImportSummary(deleteTrackedEntityInstance(uid));
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 AbstractTrackedEntityInstanceService method addTrackedEntityInstances.
// -------------------------------------------------------------------------
// CREATE
// -------------------------------------------------------------------------
@Override
public ImportSummaries addTrackedEntityInstances(List<TrackedEntityInstance> trackedEntityInstances, ImportOptions importOptions) {
if (importOptions == null) {
importOptions = new ImportOptions();
}
ImportSummaries importSummaries = new ImportSummaries();
int counter = 0;
for (TrackedEntityInstance trackedEntityInstance : trackedEntityInstances) {
importSummaries.addImportSummary(addTrackedEntityInstance(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 JacksonTrackedEntityInstanceService method addTrackedEntityInstanceList.
private ImportSummaries addTrackedEntityInstanceList(List<TrackedEntityInstance> trackedEntityInstances, ImportOptions importOptions) {
ImportSummaries importSummaries = new ImportSummaries();
List<TrackedEntityInstance> create = new ArrayList<>();
List<TrackedEntityInstance> update = new ArrayList<>();
List<String> delete = new ArrayList<>();
if (importOptions.getImportStrategy().isCreate()) {
create.addAll(trackedEntityInstances);
} else if (importOptions.getImportStrategy().isCreateAndUpdate()) {
for (TrackedEntityInstance trackedEntityInstance : trackedEntityInstances) {
if (StringUtils.isEmpty(trackedEntityInstance.getTrackedEntityInstance())) {
create.add(trackedEntityInstance);
} else {
if (!teiService.trackedEntityInstanceExists(trackedEntityInstance.getTrackedEntityInstance())) {
create.add(trackedEntityInstance);
} else {
update.add(trackedEntityInstance);
}
}
}
} else if (importOptions.getImportStrategy().isUpdate()) {
update.addAll(trackedEntityInstances);
} else if (importOptions.getImportStrategy().isDelete()) {
delete.addAll(trackedEntityInstances.stream().map(TrackedEntityInstance::getTrackedEntityInstance).collect(Collectors.toList()));
}
importSummaries.addImportSummaries(addTrackedEntityInstances(create, importOptions));
importSummaries.addImportSummaries(updateTrackedEntityInstances(update, importOptions));
importSummaries.addImportSummaries(deleteTrackedEntityInstances(delete));
return importSummaries;
}
Aggregations