Search in sources :

Example 1 with ServerCommunicationException

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

the class AdoDtoHelper method pullMissing.

public void pullMissing(List<String> uuids) throws ServerCommunicationException, ServerConnectionException, DaoException, NoConnectionException {
    final AbstractAdoDao<ADO> dao = DatabaseHelper.getAdoDao(getAdoClass());
    uuids = dao.filterMissing(uuids);
    if (!uuids.isEmpty()) {
        Response<List<DTO>> response;
        try {
            response = pullByUuids(uuids).execute();
        } catch (IOException e) {
            throw new ServerCommunicationException(e);
        }
        handlePullResponse(false, dao, response);
    }
}
Also used : ServerCommunicationException(de.symeda.sormas.app.rest.ServerCommunicationException) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException)

Example 2 with ServerCommunicationException

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

the class AdoDtoHelper method repullEntities.

public void repullEntities(Context context) throws DaoException, ServerCommunicationException, ServerConnectionException, NoConnectionException {
    try {
        final AbstractAdoDao<ADO> dao = DatabaseHelper.getAdoDao(getAdoClass());
        long approximateJsonSizeInBytes = getApproximateJsonSizeInBytes();
        final int batchSize = approximateJsonSizeInBytes != 0 ? RetroProvider.getNumberOfEntitiesToBePulledInOneBatch(approximateJsonSizeInBytes, context) : Integer.MAX_VALUE;
        int lastBatchSize = batchSize;
        while (lastBatchSize == batchSize) {
            Call<List<DTO>> dtoCall = pullAllSince(lastSyncedEntityDate != null ? lastSyncedEntityDate.getTime() : 0, batchSize, lastSyncedEntityDate != null && lastSyncedEntityUuid != null ? lastSyncedEntityUuid : EntityDto.NO_LAST_SYNCED_UUID);
            if (dtoCall == null) {
                return;
            }
            Response<List<DTO>> response;
            try {
                response = dtoCall.execute();
            } catch (IOException e) {
                throw new ServerCommunicationException(e);
            }
            lastBatchSize = handlePullResponse(false, dao, response);
        }
    } catch (RuntimeException e) {
        Log.e(getClass().getName(), "Exception thrown when trying to pull entities");
        throw new DaoException(e);
    }
}
Also used : ServerCommunicationException(de.symeda.sormas.app.rest.ServerCommunicationException) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException)

Example 3 with ServerCommunicationException

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

the class AdoDtoHelper method pullEntities.

public void pullEntities(final boolean markAsRead, Context context) throws DaoException, ServerCommunicationException, ServerConnectionException, NoConnectionException {
    try {
        final AbstractAdoDao<ADO> dao = DatabaseHelper.getAdoDao(getAdoClass());
        Date maxModifiedDate = dao.getLatestChangeDate();
        long approximateJsonSizeInBytes = getApproximateJsonSizeInBytes();
        final int batchSize = approximateJsonSizeInBytes != 0 ? RetroProvider.getNumberOfEntitiesToBePulledInOneBatch(approximateJsonSizeInBytes, context) : Integer.MAX_VALUE;
        int lastBatchSize = batchSize;
        lastSyncedEntityDate = maxModifiedDate;
        while (lastBatchSize == batchSize) {
            Call<List<DTO>> dtoCall = pullAllSince(lastSyncedEntityDate.getTime(), batchSize, lastSyncedEntityDate != null && lastSyncedEntityUuid != null ? lastSyncedEntityUuid : EntityDto.NO_LAST_SYNCED_UUID);
            if (dtoCall == null) {
                return;
            }
            Response<List<DTO>> response;
            try {
                response = dtoCall.execute();
            } catch (IOException e) {
                throw new ServerCommunicationException(e);
            }
            lastBatchSize = handlePullResponse(markAsRead, dao, response);
        }
    } catch (RuntimeException e) {
        Log.e(getClass().getName(), "Exception thrown when trying to pull entities");
        throw new DaoException(e);
    }
}
Also used : ServerCommunicationException(de.symeda.sormas.app.rest.ServerCommunicationException) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) Date(java.util.Date)

Example 4 with ServerCommunicationException

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

the class FacilityDtoHelper method pullEntities.

/**
 * Pulls the data chunkwise per region
 *
 * @param markAsRead
 * @param context
 * @throws DaoException
 * @throws SQLException
 * @throws IOException
 */
@Override
public void pullEntities(final boolean markAsRead, Context context) throws DaoException, ServerCommunicationException, ServerConnectionException, NoConnectionException {
    try {
        final FacilityDao facilityDao = DatabaseHelper.getFacilityDao();
        List<Region> regions = DatabaseHelper.getRegionDao().queryForAll();
        for (Region region : regions) {
            Date maxModifiedDate = facilityDao.getLatestChangeDateByRegion(region);
            long maxModifiedTime = maxModifiedDate != null ? maxModifiedDate.getTime() : 0;
            databaseWasEmpty = maxModifiedDate == null;
            Call<List<FacilityDto>> dtoCall = pullAllByRegionSince(region, maxModifiedTime);
            if (dtoCall == null) {
                return;
            }
            handlePullResponse(markAsRead, facilityDao, dtoCall.execute());
        }
        {
            // Pull 'Other' health facility which has no region set
            Date maxModifiedDate = facilityDao.getLatestChangeDateByRegion(null);
            long maxModifiedTime = maxModifiedDate != null ? maxModifiedDate.getTime() : 0;
            databaseWasEmpty = maxModifiedDate == null;
            Call<List<FacilityDto>> dtoCall = pullAllWithoutRegionSince(maxModifiedTime);
            if (dtoCall == null) {
                return;
            }
            handlePullResponse(markAsRead, facilityDao, dtoCall.execute());
        }
    } catch (IOException e) {
        throw new ServerCommunicationException(e);
    } finally {
        databaseWasEmpty = false;
    }
}
Also used : ServerCommunicationException(de.symeda.sormas.app.rest.ServerCommunicationException) Call(retrofit2.Call) Region(de.symeda.sormas.app.backend.region.Region) List(java.util.List) FacilityDto(de.symeda.sormas.api.infrastructure.facility.FacilityDto) IOException(java.io.IOException) Date(java.util.Date)

Example 5 with ServerCommunicationException

use of de.symeda.sormas.app.rest.ServerCommunicationException 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)

Aggregations

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