Search in sources :

Example 1 with PushResult

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;
}
Also used : HashMap(java.util.HashMap) PushResult(de.symeda.sormas.api.PushResult) EnumMap(java.util.EnumMap) HashMap(java.util.HashMap) Map(java.util.Map) EnumMap(java.util.EnumMap) ValidationException(de.symeda.sormas.api.utils.ValidationException) OutdatedEntityException(de.symeda.sormas.api.utils.OutdatedEntityException)

Example 2 with PushResult

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;
}
Also used : ArrayList(java.util.ArrayList) PushResult(de.symeda.sormas.api.PushResult) ValidationException(de.symeda.sormas.api.utils.ValidationException) OutdatedEntityException(de.symeda.sormas.api.utils.OutdatedEntityException)

Example 3 with PushResult

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;
}
Also used : ValidationException(de.symeda.sormas.api.utils.ValidationException) OutdatedEntityException(de.symeda.sormas.api.utils.OutdatedEntityException) PushResult(de.symeda.sormas.api.PushResult)

Example 4 with PushResult

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;
}
Also used : ServerCommunicationException(de.symeda.sormas.app.rest.ServerCommunicationException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) PushResult(de.symeda.sormas.api.PushResult) ServerCommunicationException(de.symeda.sormas.app.rest.ServerCommunicationException) IOException(java.io.IOException) NoConnectionException(de.symeda.sormas.app.rest.NoConnectionException) ServerConnectionException(de.symeda.sormas.app.rest.ServerConnectionException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with PushResult

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();
}
Also used : LbdsSyncDao(de.symeda.sormas.app.backend.lbds.LbdsSyncDao) PersonDto(de.symeda.sormas.api.person.PersonDto) TypeToken(com.google.gson.reflect.TypeToken) Gson(com.google.gson.Gson) HttpResult(org.hzi.sormas.lbds.core.http.HttpResult) PushResult(de.symeda.sormas.api.PushResult) HttpMethod(org.hzi.sormas.lbds.core.http.HttpMethod)

Aggregations

PushResult (de.symeda.sormas.api.PushResult)6 OutdatedEntityException (de.symeda.sormas.api.utils.OutdatedEntityException)3 ValidationException (de.symeda.sormas.api.utils.ValidationException)3 Gson (com.google.gson.Gson)2 TypeToken (com.google.gson.reflect.TypeToken)2 LbdsSyncDao (de.symeda.sormas.app.backend.lbds.LbdsSyncDao)2 ArrayList (java.util.ArrayList)2 HttpMethod (org.hzi.sormas.lbds.core.http.HttpMethod)2 HttpResult (org.hzi.sormas.lbds.core.http.HttpResult)2 CaseDataDto (de.symeda.sormas.api.caze.CaseDataDto)1 PersonDto (de.symeda.sormas.api.person.PersonDto)1 NoConnectionException (de.symeda.sormas.app.rest.NoConnectionException)1 ServerCommunicationException (de.symeda.sormas.app.rest.ServerCommunicationException)1 ServerConnectionException (de.symeda.sormas.app.rest.ServerConnectionException)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 EnumMap (java.util.EnumMap)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1