use of de.symeda.sormas.app.rest.ServerConnectionException 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.app.rest.ServerConnectionException 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);
}
Aggregations