Search in sources :

Example 1 with BackendBaseExceptionWithContent

use of org.infobip.mobile.messaging.mobile.common.exceptions.BackendBaseExceptionWithContent in project mobile-messaging-sdk-android by infobip.

the class UserDataReporter method sync.

public void sync(final MobileMessaging.ResultListener listener, final UserData userData) {
    if (userData == null) {
        return;
    }
    mobileMessagingCore.saveUnreportedUserData(userData);
    new MRetryableTask<UserData, UserData>() {

        @Override
        public UserData run(UserData[] userData) {
            if (StringUtils.isBlank(mobileMessagingCore.getPushRegistrationId())) {
                MobileMessagingLogger.w("Can't report system data without valid registration");
                throw InternalSdkError.NO_VALID_REGISTRATION.getException();
            }
            UserDataReport request = UserDataMapper.toUserDataReport(userData[0].getPredefinedUserData(), userData[0].getCustomUserData());
            MobileMessagingLogger.v("USER DATA >>>", request);
            UserDataReport response = mobileApiData.reportUserData(userData[0].getExternalUserId(), request);
            MobileMessagingLogger.v("USER DATA <<<", response);
            return UserDataMapper.fromUserDataReport(userData[0].getExternalUserId(), response.getPredefinedUserData(), response.getCustomUserData());
        }

        @Override
        public void after(UserData userData) {
            mobileMessagingCore.setUserDataReported(userData);
            broadcaster.userDataReported(userData);
            if (listener != null) {
                listener.onResult(userData);
            }
        }

        @Override
        public void error(Throwable error) {
            MobileMessagingLogger.e("MobileMessaging API returned error (user data)! ", error);
            mobileMessagingCore.setLastHttpException(error);
            stats.reportError(MobileMessagingStatsError.USER_DATA_SYNC_ERROR);
            if (error instanceof BackendBaseExceptionWithContent) {
                BackendBaseExceptionWithContent errorWithContent = (BackendBaseExceptionWithContent) error;
                mobileMessagingCore.setUserDataReported(errorWithContent.getContent(UserData.class));
                if (listener != null) {
                    listener.onError(MobileMessagingError.createFrom(error));
                }
            } else if (error instanceof BackendInvalidParameterException) {
                mobileMessagingCore.setUserDataReportedWithError();
                if (listener != null) {
                    listener.onError(MobileMessagingError.createFrom(error));
                }
            } else {
                MobileMessagingLogger.v("User data synchronization will be postponed to a later time due to communication error");
                if (listener != null) {
                    listener.onResult(UserData.merge(mobileMessagingCore.getUserData(), userData));
                }
            }
            broadcaster.error(MobileMessagingError.createFrom(error));
        }
    }.retryWith(retryPolicy(listener)).execute(executor, userData);
}
Also used : MRetryableTask(org.infobip.mobile.messaging.mobile.common.MRetryableTask) BackendInvalidParameterException(org.infobip.mobile.messaging.mobile.common.exceptions.BackendInvalidParameterException) UserData(org.infobip.mobile.messaging.UserData) UserDataReport(org.infobip.mobile.messaging.api.data.UserDataReport) BackendBaseExceptionWithContent(org.infobip.mobile.messaging.mobile.common.exceptions.BackendBaseExceptionWithContent)

Aggregations

UserData (org.infobip.mobile.messaging.UserData)1 UserDataReport (org.infobip.mobile.messaging.api.data.UserDataReport)1 MRetryableTask (org.infobip.mobile.messaging.mobile.common.MRetryableTask)1 BackendBaseExceptionWithContent (org.infobip.mobile.messaging.mobile.common.exceptions.BackendBaseExceptionWithContent)1 BackendInvalidParameterException (org.infobip.mobile.messaging.mobile.common.exceptions.BackendInvalidParameterException)1