use of de.symeda.sormas.api.PushResult in project SORMAS-Project by hzi-braunschweig.
the class EntityDtoResource method savePushedDetailedDto.
protected <T> Map<String, Map<PushResult, String>> savePushedDetailedDto(List<T> dtos, Function<T, T> saveEntityDto) {
Map<String, Map<PushResult, String>> results = new HashMap<>(dtos.size());
for (T dto : dtos) {
PushResult result;
try {
dto = transactionWrapper.execute(saveEntityDto, dto);
result = PushResult.OK;
Map<PushResult, String> map = new EnumMap<>(PushResult.class);
map.put(result, StringUtils.EMPTY);
results.put(dto.toString(), map);
} catch (Exception e) {
String errorMessage = createErrorMessage(dto);
errorMessage += e.getMessage();
result = getPushResultError(e, errorMessage);
Map<PushResult, String> map = new EnumMap<>(PushResult.class);
map.put(result, errorMessage + (isNull(e.getMessage()) ? " - " + e.getCause() : ""));
results.put(dto.toString(), map);
}
}
return results;
}
use of de.symeda.sormas.api.PushResult in project SORMAS-Project by hzi-braunschweig.
the class EntityDtoResource method savePushedDto.
protected <T> List<PushResult> savePushedDto(List<T> dtos, Function<T, T> saveEntityDto) {
List<PushResult> results = new ArrayList<>(dtos.size());
for (T dto : dtos) {
PushResult result;
try {
dto = transactionWrapper.execute(saveEntityDto, dto);
result = PushResult.OK;
} catch (Exception e) {
String errorMessage = createErrorMessage(dto);
errorMessage += e.getMessage();
result = getPushResultError(e, errorMessage);
}
results.add(result);
}
return results;
}
use of de.symeda.sormas.api.PushResult in project SORMAS-Project by hzi-braunschweig.
the class EntityDtoResource method getPushResultError.
private PushResult getPushResultError(Exception e, String errorMessage) {
PushResult result;
if (e instanceof OutdatedEntityException || ExceptionUtils.getRootCause(e) instanceof OutdatedEntityException) {
logger.warn(errorMessage, e);
result = PushResult.TOO_OLD;
} else if (e instanceof ValidationException || ExceptionUtils.getRootCause(e) instanceof ValidationException) {
logger.error(errorMessage, e);
result = PushResult.VALIDATION_EXCEPTION;
} else if (e instanceof javax.ejb.EJBTransactionRolledbackException || ExceptionUtils.getRootCause(e) instanceof javax.ejb.EJBTransactionRolledbackException) {
logger.error(errorMessage, e);
result = PushResult.TRANSACTION_ROLLED_BACK_EXCEPTION;
} else {
logger.error(errorMessage, e);
result = PushResult.ERROR;
}
return result;
}
use of de.symeda.sormas.api.PushResult in project SORMAS-Project by hzi-braunschweig.
the class AdoDtoHelper method pushEntities.
/**
* @return true: another pull is needed, because data has been changed on the server
*/
public boolean pushEntities(boolean onlyNewEntities) throws DaoException, ServerConnectionException, ServerCommunicationException, NoConnectionException {
final AbstractAdoDao<ADO> dao = DatabaseHelper.getAdoDao(getAdoClass());
final List<ADO> modifiedAdos = onlyNewEntities ? dao.queryForNew() : dao.queryForModified();
List<DTO> modifiedDtos = new ArrayList<>(modifiedAdos.size());
for (ADO ado : modifiedAdos) {
DTO dto = adoToDto(ado);
modifiedDtos.add(dto);
}
if (modifiedDtos.isEmpty()) {
return false;
}
Call<List<PushResult>> call = pushAll(modifiedDtos);
Response<List<PushResult>> response;
try {
response = call.execute();
} catch (IOException e) {
throw new ServerCommunicationException(e);
}
if (!response.isSuccessful()) {
RetroProvider.throwException(response);
}
final List<PushResult> pushResults = response.body();
if (pushResults.size() != modifiedDtos.size()) {
throw new ServerCommunicationException("Server responded with wrong count of received entities: " + pushResults.size() + " - expected: " + modifiedDtos.size());
}
pushedTooOldCount = 0;
pushedErrorCount = 0;
dao.callBatchTasks(new Callable<Void>() {
public Void call() throws Exception {
for (int i = 0; i < modifiedAdos.size(); i++) {
ADO ado = modifiedAdos.get(i);
PushResult pushResult = pushResults.get(i);
switch(pushResult) {
case OK:
// data has been pushed, we no longer need the old unmodified version
dao.accept(ado);
break;
case TOO_OLD:
pushedTooOldCount++;
break;
case ERROR:
pushedErrorCount++;
break;
default:
throw new IllegalArgumentException(pushResult.toString());
}
}
return null;
}
});
if (modifiedAdos.size() > 0) {
Log.d(dao.getTableName(), "Pushed: " + modifiedAdos.size() + " Too old: " + pushedTooOldCount + " Erros: " + pushedErrorCount);
}
return true;
}
use of de.symeda.sormas.api.PushResult in project SORMAS-Project by hzi-braunschweig.
the class LbdsRecevierComponent method processLbdsResponsePersons.
private void processLbdsResponsePersons(Context context, HttpContainer httpContainerResponse) {
HttpMethod methodFromResponse = httpContainerResponse.getMethod();
HttpResult resultFromResponse = httpContainerResponse.getResult();
Log.i("SORMAS_LBDS", "Request: " + methodFromResponse);
Log.i("SORMAS_LBDS", "Result Headers: " + resultFromResponse.headers);
Log.i("SORMAS_LBDS", "Result Body: " + resultFromResponse.body);
Gson gson = RetroProvider.initGson();
List<PersonDto> sentPersonDtos = gson.fromJson(methodFromResponse.payload, new TypeToken<List<PersonDto>>() {
}.getType());
List<PushResult> responsePushResults = gson.fromJson(resultFromResponse.body, new TypeToken<List<PushResult>>() {
}.getType());
Log.i("SORMAS_LBDS", "sent " + sentPersonDtos.size() + " PersonDtos and received " + responsePushResults.size() + " PushResults");
if (sentPersonDtos.size() != responsePushResults.size()) {
Log.i("SORMAS_LBDS", "List lengths differ, abort.");
throw new IllegalArgumentException("Number of sent Dtos and received PushResults must be equal");
}
LbdsSyncDao lbdsSyncDao = DatabaseHelper.getLbdsSyncDao();
int successful = 0;
int ignored = 0;
for (int i = 0; i < sentPersonDtos.size(); i++) {
PersonDto personDto = sentPersonDtos.get(i);
String uuid = personDto.getUuid();
PushResult pushResult = responsePushResults.get(i);
if (pushResult == PushResult.OK) {
Log.i("SORMAS_LBDS", "Process PushResult " + pushResult + " for PersonDto " + uuid);
lbdsSyncDao.logLbdsReceive(uuid);
successful++;
} else {
Log.i("SORMAS_LBDS", "Ignore PushResult " + pushResult + " for PersonDto " + uuid);
ignored++;
}
}
Toast.makeText(context, String.format(context.getResources().getString(R.string.lbds_response_persons), successful, ignored), Toast.LENGTH_LONG).show();
}
Aggregations