Search in sources :

Example 1 with DiscardUnsyncedChangesStrategy

use of io.realm.mongodb.sync.DiscardUnsyncedChangesStrategy in project realm-java by realm.

the class SyncObjectServerFacade method getSyncConfigurationOptions.

@Override
public Object[] getSyncConfigurationOptions(RealmConfiguration config) {
    if (config instanceof SyncConfiguration) {
        SyncConfiguration syncConfig = (SyncConfiguration) config;
        User user = syncConfig.getUser();
        App app = user.getApp();
        String rosServerUrl = syncConfig.getServerUrl().toString();
        String rosUserIdentity = user.getId();
        String syncRealmAuthUrl = user.getApp().getConfiguration().getBaseUrl().toString();
        String rosUserProvider = user.getProviderType().getId();
        String syncUserRefreshToken = user.getRefreshToken();
        String syncUserAccessToken = user.getAccessToken();
        String deviceId = user.getDeviceId();
        byte sessionStopPolicy = syncConfig.getSessionStopPolicy().getNativeValue();
        String urlPrefix = syncConfig.getUrlPrefix();
        String customAuthorizationHeaderName = app.getConfiguration().getAuthorizationHeaderName();
        Map<String, String> customHeaders = app.getConfiguration().getCustomRequestHeaders();
        SyncClientResetStrategy clientResetStrategy = syncConfig.getSyncClientResetStrategy();
        // undefined value
        byte clientResetMode = -1;
        if (clientResetStrategy instanceof ManuallyRecoverUnsyncedChangesStrategy) {
            clientResetMode = OsRealmConfig.CLIENT_RESYNC_MODE_MANUAL;
        } else if (clientResetStrategy instanceof DiscardUnsyncedChangesStrategy) {
            clientResetMode = OsRealmConfig.CLIENT_RESYNC_MODE_DISCARD_LOCAL;
        }
        BeforeClientResetHandler beforeClientResetHandler = (localPtr, osRealmConfig) -> {
            NativeContext.execute(nativeContext -> {
                Realm before = realmInstanceFactory.createInstance(new OsSharedRealm(localPtr, osRealmConfig, nativeContext));
                ((DiscardUnsyncedChangesStrategy) clientResetStrategy).onBeforeReset(before);
            });
        };
        AfterClientResetHandler afterClientResetHandler = (localPtr, afterPtr, osRealmConfig) -> {
            NativeContext.execute(nativeContext -> {
                Realm before = realmInstanceFactory.createInstance(new OsSharedRealm(localPtr, osRealmConfig, nativeContext));
                Realm after = realmInstanceFactory.createInstance(new OsSharedRealm(afterPtr, osRealmConfig, nativeContext));
                ((DiscardUnsyncedChangesStrategy) clientResetStrategy).onAfterReset(before, after);
            });
        };
        long appNativePointer;
        // access it.
        try {
            if (osAppField == null) {
                synchronized (SyncObjectServerFacade.class) {
                    if (osAppField == null) {
                        Field field = App.class.getDeclaredField("osApp");
                        field.setAccessible(true);
                        osAppField = field;
                    }
                }
            }
            OsApp osApp = (OsApp) osAppField.get(app);
            appNativePointer = osApp.getNativePtr();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        // TODO Simplify. org.bson serialization only allows writing full documents, so the partition
        // key is embedded in a document with key 'value' and unwrapped in JNI.
        String encodedPartitionValue = null;
        if (syncConfig.isPartitionBasedSyncConfiguration()) {
            BsonValue partitionValue = syncConfig.getPartitionValue();
            switch(partitionValue.getBsonType()) {
                case STRING:
                case OBJECT_ID:
                case INT32:
                case INT64:
                case BINARY:
                case NULL:
                    encodedPartitionValue = JniBsonProtocol.encode(partitionValue, AppConfiguration.DEFAULT_BSON_CODEC_REGISTRY);
                    break;
                default:
                    throw new IllegalArgumentException("Unsupported type: " + partitionValue);
            }
        }
        int i = 0;
        Object[] configObj = new Object[SYNC_CONFIG_OPTIONS];
        configObj[i++] = rosUserIdentity;
        configObj[i++] = rosUserProvider;
        configObj[i++] = rosServerUrl;
        configObj[i++] = syncRealmAuthUrl;
        configObj[i++] = syncUserRefreshToken;
        configObj[i++] = syncUserAccessToken;
        configObj[i++] = deviceId;
        configObj[i++] = sessionStopPolicy;
        configObj[i++] = urlPrefix;
        configObj[i++] = customAuthorizationHeaderName;
        configObj[i++] = customHeaders;
        configObj[i++] = clientResetMode;
        configObj[i++] = beforeClientResetHandler;
        configObj[i++] = afterClientResetHandler;
        configObj[i++] = encodedPartitionValue;
        configObj[i++] = app.getSync();
        configObj[i++] = appNativePointer;
        return configObj;
    } else {
        return new Object[SYNC_CONFIG_OPTIONS];
    }
}
Also used : OsApp(io.realm.internal.objectstore.OsApp) App(io.realm.mongodb.App) Context(android.content.Context) SubscriptionSet(io.realm.mongodb.sync.SubscriptionSet) DownloadingRealmInterruptedException(io.realm.exceptions.DownloadingRealmInterruptedException) RealmException(io.realm.exceptions.RealmException) OsApp(io.realm.internal.objectstore.OsApp) DiscardUnsyncedChangesStrategy(io.realm.mongodb.sync.DiscardUnsyncedChangesStrategy) BsonValue(org.bson.BsonValue) MutableSubscriptionSet(io.realm.mongodb.sync.MutableSubscriptionSet) AppConfiguration(io.realm.mongodb.AppConfiguration) SuppressLint(android.annotation.SuppressLint) Map(java.util.Map) ManuallyRecoverUnsyncedChangesStrategy(io.realm.mongodb.sync.ManuallyRecoverUnsyncedChangesStrategy) User(io.realm.mongodb.User) Method(java.lang.reflect.Method) ConnectivityManager(android.net.ConnectivityManager) App(io.realm.mongodb.App) SyncClientResetStrategy(io.realm.mongodb.sync.SyncClientResetStrategy) Realm(io.realm.Realm) NetworkStateReceiver(io.realm.internal.network.NetworkStateReceiver) OsAsyncOpenTask(io.realm.internal.objectstore.OsAsyncOpenTask) IntentFilter(android.content.IntentFilter) AndroidCapabilities(io.realm.internal.android.AndroidCapabilities) Field(java.lang.reflect.Field) InvocationTargetException(java.lang.reflect.InvocationTargetException) JniBsonProtocol(io.realm.internal.jni.JniBsonProtocol) TimeUnit(java.util.concurrent.TimeUnit) RealmConfiguration(io.realm.RealmConfiguration) SyncConfiguration(io.realm.mongodb.sync.SyncConfiguration) Sync(io.realm.mongodb.sync.Sync) User(io.realm.mongodb.User) Field(java.lang.reflect.Field) Realm(io.realm.Realm) SyncConfiguration(io.realm.mongodb.sync.SyncConfiguration) DiscardUnsyncedChangesStrategy(io.realm.mongodb.sync.DiscardUnsyncedChangesStrategy) DownloadingRealmInterruptedException(io.realm.exceptions.DownloadingRealmInterruptedException) RealmException(io.realm.exceptions.RealmException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SuppressLint(android.annotation.SuppressLint) ManuallyRecoverUnsyncedChangesStrategy(io.realm.mongodb.sync.ManuallyRecoverUnsyncedChangesStrategy) SyncClientResetStrategy(io.realm.mongodb.sync.SyncClientResetStrategy) OsApp(io.realm.internal.objectstore.OsApp) BsonValue(org.bson.BsonValue)

Aggregations

SuppressLint (android.annotation.SuppressLint)1 Context (android.content.Context)1 IntentFilter (android.content.IntentFilter)1 ConnectivityManager (android.net.ConnectivityManager)1 Realm (io.realm.Realm)1 RealmConfiguration (io.realm.RealmConfiguration)1 DownloadingRealmInterruptedException (io.realm.exceptions.DownloadingRealmInterruptedException)1 RealmException (io.realm.exceptions.RealmException)1 AndroidCapabilities (io.realm.internal.android.AndroidCapabilities)1 JniBsonProtocol (io.realm.internal.jni.JniBsonProtocol)1 NetworkStateReceiver (io.realm.internal.network.NetworkStateReceiver)1 OsApp (io.realm.internal.objectstore.OsApp)1 OsAsyncOpenTask (io.realm.internal.objectstore.OsAsyncOpenTask)1 App (io.realm.mongodb.App)1 AppConfiguration (io.realm.mongodb.AppConfiguration)1 User (io.realm.mongodb.User)1 DiscardUnsyncedChangesStrategy (io.realm.mongodb.sync.DiscardUnsyncedChangesStrategy)1 ManuallyRecoverUnsyncedChangesStrategy (io.realm.mongodb.sync.ManuallyRecoverUnsyncedChangesStrategy)1 MutableSubscriptionSet (io.realm.mongodb.sync.MutableSubscriptionSet)1 SubscriptionSet (io.realm.mongodb.sync.SubscriptionSet)1