Search in sources :

Example 1 with NetworkException

use of org.eyeseetea.malariacare.domain.exception.NetworkException in project pictureapp by EyeSeeTea.

the class AuthenticationDhisSDKDataSource method login.

@Override
public void login(final Credentials credentials, final IDataSourceCallback<UserAccount> callback) {
    boolean isNetworkAvailable = isNetworkAvailable();
    if (!isNetworkAvailable) {
        callback.onError(new NetworkException());
    } else {
        Configuration configuration = new Configuration(credentials.getServerURL());
        D2.configure(configuration).flatMap(new Func1<Void, Observable<org.hisp.dhis.client.sdk.models.user.UserAccount>>() {

            @Override
            public Observable<org.hisp.dhis.client.sdk.models.user.UserAccount> call(Void aVoid) {
                return D2.me().signIn(credentials.getUsername(), credentials.getPassword());
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<org.hisp.dhis.client.sdk.models.user.UserAccount>() {

            @Override
            public void call(org.hisp.dhis.client.sdk.models.user.UserAccount dhisUserAccount) {
                UserAccount userAccount = new UserAccount(credentials.getUsername(), credentials.isDemoCredentials());
                callback.onSuccess(userAccount);
            }
        }, new Action1<Throwable>() {

            @Override
            public void call(Throwable throwable) {
                Throwable throwableResult = mapThrowable(throwable);
                callback.onError(throwableResult);
            }
        });
    }
}
Also used : Configuration(org.hisp.dhis.client.sdk.core.common.network.Configuration) Func1(rx.functions.Func1) NetworkException(org.eyeseetea.malariacare.domain.exception.NetworkException) UserAccount(org.eyeseetea.malariacare.domain.entity.UserAccount)

Example 2 with NetworkException

use of org.eyeseetea.malariacare.domain.exception.NetworkException in project pictureapp by EyeSeeTea.

the class PushController method push.

public void push(final IPushControllerCallback callback) {
    if (!ServerAPIController.isNetworkAvailable()) {
        Log.d(TAG, "No network");
        callback.onError(new NetworkException());
    } else {
        Log.d(TAG, "Network connected");
        List<Survey> surveys = Survey.getAllCompletedSurveysNoReceiptReset();
        //Fixme Check if is necessary other conditions
        if (surveys == null || surveys.size() == 0) {
            Log.d("DpBlank", "Sets of Surveys to push");
            callback.onError(new SurveysToPushNotFoundException());
        } else {
            for (Survey srv : surveys) {
                Log.d("DpBlank", "Survey to push " + srv.toString());
                for (Value dv : srv.getValuesFromDB()) {
                    Log.d("DpBlank", "Values to push " + dv.toString());
                }
            }
            mPushDhisSDKDataSource.wipeEvents();
            try {
                convertToSDK(surveys);
            } catch (Exception ex) {
                callback.onError(new ConversionException(ex));
            }
            if (EventExtended.getAllEvents().size() == 0) {
                callback.onError(new ConversionException());
            } else {
                pushData(callback);
            }
        }
    }
}
Also used : ConversionException(org.eyeseetea.malariacare.domain.exception.ConversionException) Survey(org.eyeseetea.malariacare.data.database.model.Survey) SurveysToPushNotFoundException(org.eyeseetea.malariacare.domain.exception.SurveysToPushNotFoundException) Value(org.eyeseetea.malariacare.data.database.model.Value) NetworkException(org.eyeseetea.malariacare.domain.exception.NetworkException) NetworkException(org.eyeseetea.malariacare.domain.exception.NetworkException) ImportSummaryErrorException(org.eyeseetea.malariacare.domain.exception.ImportSummaryErrorException) SurveysToPushNotFoundException(org.eyeseetea.malariacare.domain.exception.SurveysToPushNotFoundException) ConversionException(org.eyeseetea.malariacare.domain.exception.ConversionException)

Example 3 with NetworkException

use of org.eyeseetea.malariacare.domain.exception.NetworkException in project pictureapp by EyeSeeTea.

the class PullDhisSDKDataSource method pullData.

public void pullData(PullFilters pullFilters, List<OrganisationUnit> organisationUnits, IDataSourceCallback<List<Event>> callback) {
    boolean isNetworkAvailable = isNetworkAvailable();
    if (!isNetworkAvailable) {
        callback.onError(new NetworkException());
    } else {
        try {
            List<Event> events = new ArrayList<>();
            for (OrganisationUnit organisationUnit : organisationUnits) {
                Scheduler pullEventsThread = Schedulers.newThread();
                EventFilters eventFilters = new EventFilters();
                eventFilters.setStartDate(pullFilters.getStartDate());
                eventFilters.setEndDate(pullFilters.getEndDate());
                eventFilters.setMaxEvents(pullFilters.getMaxEvents());
                eventFilters.setOrganisationUnitUId(organisationUnit.getUId());
                mPullDhisSDKDataSourceStrategy.setEventFilters(eventFilters);
                List<Event> eventsByOrgUnit = D2.events().pull(eventFilters).subscribeOn(pullEventsThread).observeOn(pullEventsThread).toBlocking().single();
                events.addAll(eventsByOrgUnit);
            }
            callback.onSuccess(events);
        } catch (Exception e) {
            callback.onError(e);
        }
    }
}
Also used : OrganisationUnit(org.hisp.dhis.client.sdk.models.organisationunit.OrganisationUnit) Scheduler(rx.Scheduler) ArrayList(java.util.ArrayList) Event(org.hisp.dhis.client.sdk.models.event.Event) NetworkException(org.eyeseetea.malariacare.domain.exception.NetworkException) NetworkException(org.eyeseetea.malariacare.domain.exception.NetworkException) EventFilters(org.hisp.dhis.client.sdk.core.event.EventFilters)

Aggregations

NetworkException (org.eyeseetea.malariacare.domain.exception.NetworkException)3 ArrayList (java.util.ArrayList)1 Survey (org.eyeseetea.malariacare.data.database.model.Survey)1 Value (org.eyeseetea.malariacare.data.database.model.Value)1 UserAccount (org.eyeseetea.malariacare.domain.entity.UserAccount)1 ConversionException (org.eyeseetea.malariacare.domain.exception.ConversionException)1 ImportSummaryErrorException (org.eyeseetea.malariacare.domain.exception.ImportSummaryErrorException)1 SurveysToPushNotFoundException (org.eyeseetea.malariacare.domain.exception.SurveysToPushNotFoundException)1 Configuration (org.hisp.dhis.client.sdk.core.common.network.Configuration)1 EventFilters (org.hisp.dhis.client.sdk.core.event.EventFilters)1 Event (org.hisp.dhis.client.sdk.models.event.Event)1 OrganisationUnit (org.hisp.dhis.client.sdk.models.organisationunit.OrganisationUnit)1 Scheduler (rx.Scheduler)1 Func1 (rx.functions.Func1)1