Search in sources :

Example 1 with NoConnectionException

use of de.symeda.sormas.app.rest.NoConnectionException 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 2 with NoConnectionException

use of de.symeda.sormas.app.rest.NoConnectionException in project SORMAS-Project by hzi-braunschweig.

the class RegionBackendTest method testHandlePulledList.

@Test
public void testHandlePulledList() {
    long startRegionCount = DatabaseHelper.getRegionDao().countOf();
    List<RegionDto> regions = new ArrayList<>();
    RegionDto region1 = RegionDto.build();
    region1.setCreationDate(new Date());
    region1.setChangeDate(new Date());
    region1.setName("TestA");
    regions.add(region1);
    RegionDto region2 = RegionDto.build();
    region2.setCreationDate(new Date());
    region2.setChangeDate(new Date());
    region2.setName("TestB");
    regions.add(region2);
    // this should cause a roll-back
    region2.setUuid(null);
    boolean hadException = false;
    try {
        new RegionDtoHelper().handlePulledList(DatabaseHelper.getRegionDao(), regions);
    } catch (DaoException | NoConnectionException | ServerConnectionException | ServerCommunicationException e) {
        hadException = true;
    }
    assertTrue(hadException);
    long regionCount = DatabaseHelper.getRegionDao().countOf();
    assertEquals(startRegionCount, regionCount);
    // now it should work
    region2.setUuid(DataHelper.createUuid());
    hadException = false;
    try {
        new RegionDtoHelper().handlePulledList(DatabaseHelper.getRegionDao(), regions);
    } catch (DaoException | NoConnectionException | ServerConnectionException | ServerCommunicationException e) {
        hadException = true;
    }
    assertFalse(hadException);
    regionCount = DatabaseHelper.getRegionDao().countOf();
    assertEquals(startRegionCount + 2, regionCount);
}
Also used : ServerCommunicationException(de.symeda.sormas.app.rest.ServerCommunicationException) RegionDtoHelper(de.symeda.sormas.app.backend.region.RegionDtoHelper) NoConnectionException(de.symeda.sormas.app.rest.NoConnectionException) ServerConnectionException(de.symeda.sormas.app.rest.ServerConnectionException) ArrayList(java.util.ArrayList) RegionDto(de.symeda.sormas.api.infrastructure.region.RegionDto) DaoException(de.symeda.sormas.app.backend.common.DaoException) Date(java.util.Date) Test(org.junit.Test)

Aggregations

NoConnectionException (de.symeda.sormas.app.rest.NoConnectionException)2 ServerCommunicationException (de.symeda.sormas.app.rest.ServerCommunicationException)2 ServerConnectionException (de.symeda.sormas.app.rest.ServerConnectionException)2 ArrayList (java.util.ArrayList)2 PushResult (de.symeda.sormas.api.PushResult)1 RegionDto (de.symeda.sormas.api.infrastructure.region.RegionDto)1 DaoException (de.symeda.sormas.app.backend.common.DaoException)1 RegionDtoHelper (de.symeda.sormas.app.backend.region.RegionDtoHelper)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 Date (java.util.Date)1 List (java.util.List)1 Test (org.junit.Test)1