2019-11-18 23:51:27 +01:00
|
|
|
package org.mercury_im.messenger.data.repository;
|
2019-11-03 06:33:52 +01:00
|
|
|
|
2019-11-22 23:35:14 +01:00
|
|
|
import org.mercury_im.messenger.data.model.AccountModel;
|
2019-11-18 23:51:27 +01:00
|
|
|
import org.mercury_im.messenger.entity.Account;
|
|
|
|
import org.mercury_im.messenger.data.util.Optional;
|
2019-11-25 15:17:49 +01:00
|
|
|
import org.mercury_im.messenger.entity.IAccount;
|
2019-11-18 23:51:27 +01:00
|
|
|
import org.mercury_im.messenger.util.ThreadUtils;
|
|
|
|
import org.mercury_im.messenger.data.mapping.AccountMapping;
|
2019-11-03 06:33:52 +01:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
import javax.inject.Named;
|
|
|
|
|
|
|
|
import io.reactivex.Completable;
|
|
|
|
import io.reactivex.Maybe;
|
|
|
|
import io.reactivex.Observable;
|
|
|
|
import io.reactivex.Scheduler;
|
|
|
|
import io.reactivex.Single;
|
|
|
|
import io.requery.Persistable;
|
|
|
|
import io.requery.query.ResultDelegate;
|
|
|
|
import io.requery.reactivex.ReactiveEntityStore;
|
|
|
|
|
2019-11-23 21:16:03 +01:00
|
|
|
public class XmppAccountRepository
|
2019-11-18 00:04:28 +01:00
|
|
|
extends RequeryRepository
|
|
|
|
implements AccountRepository {
|
2019-11-03 06:33:52 +01:00
|
|
|
|
|
|
|
@Inject
|
2019-12-01 19:56:13 +01:00
|
|
|
AccountMapping accountMapping;
|
2019-11-23 21:16:03 +01:00
|
|
|
|
|
|
|
@Inject
|
|
|
|
public XmppAccountRepository(ReactiveEntityStore<Persistable> data,
|
|
|
|
@Named(value = ThreadUtils.SCHEDULER_IO) Scheduler subscriberScheduler,
|
|
|
|
@Named(value = ThreadUtils.SCHEDULER_UI) Scheduler observerScheduler) {
|
2019-11-03 06:33:52 +01:00
|
|
|
super(data, subscriberScheduler, observerScheduler);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-11-18 00:04:28 +01:00
|
|
|
public Single<Account> insertAccount(Account account) {
|
2019-12-01 19:56:13 +01:00
|
|
|
return data().insert(accountMapping.entityToModel(account, new AccountModel()))
|
|
|
|
.map(model -> accountMapping.modelToEntity(model, account))
|
2019-11-03 06:33:52 +01:00
|
|
|
.subscribeOn(subscriberScheduler())
|
|
|
|
.observeOn(observerScheduler());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-11-18 00:04:28 +01:00
|
|
|
public Observable<Optional<Account>> observeAccount(long accountId) {
|
2019-11-03 06:33:52 +01:00
|
|
|
return data().select(AccountModel.class)
|
|
|
|
.where(AccountModel.ID.eq(accountId))
|
|
|
|
.get().observableResult()
|
2019-12-01 19:56:13 +01:00
|
|
|
.map(result -> new Optional<>(accountMapping.modelToEntity(result.firstOrNull(), new IAccount())))
|
2019-11-03 06:33:52 +01:00
|
|
|
.subscribeOn(subscriberScheduler())
|
|
|
|
.observeOn(observerScheduler());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-11-18 00:04:28 +01:00
|
|
|
public Maybe<Account> getAccount(long accountId) {
|
2019-11-03 06:33:52 +01:00
|
|
|
return data().select(AccountModel.class)
|
|
|
|
.where(AccountModel.ID.eq(accountId))
|
|
|
|
.get().maybe()
|
2019-12-01 19:56:13 +01:00
|
|
|
.map(model -> accountMapping.modelToEntity(model, new IAccount()))
|
2019-11-03 06:33:52 +01:00
|
|
|
.subscribeOn(subscriberScheduler())
|
|
|
|
.observeOn(observerScheduler());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-11-18 00:04:28 +01:00
|
|
|
public Observable<Optional<Account>> observeAccountByAddress(String address) {
|
2019-11-03 06:33:52 +01:00
|
|
|
return data().select(AccountModel.class)
|
2019-11-18 00:04:28 +01:00
|
|
|
.where(AccountModel.ADDRESS.eq(address))
|
2019-11-03 06:33:52 +01:00
|
|
|
.get().observableResult()
|
2019-12-01 19:56:13 +01:00
|
|
|
.map(result -> new Optional<>(accountMapping.modelToEntity(result.firstOrNull(), new IAccount())))
|
2019-11-03 06:33:52 +01:00
|
|
|
.subscribeOn(subscriberScheduler())
|
|
|
|
.observeOn(observerScheduler());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-11-18 00:04:28 +01:00
|
|
|
public Maybe<Account> getAccountByAddress(String address) {
|
2019-11-03 06:33:52 +01:00
|
|
|
return data().select(AccountModel.class)
|
2019-11-18 00:04:28 +01:00
|
|
|
.where(AccountModel.ADDRESS.eq(address))
|
2019-11-03 06:33:52 +01:00
|
|
|
.get().maybe()
|
2019-12-01 19:56:13 +01:00
|
|
|
.map(model -> accountMapping.modelToEntity(model, new IAccount()))
|
2019-11-03 06:33:52 +01:00
|
|
|
.subscribeOn(subscriberScheduler())
|
|
|
|
.observeOn(observerScheduler());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-11-18 00:04:28 +01:00
|
|
|
public Observable<List<Account>> observeAllAccounts() {
|
2019-11-03 06:33:52 +01:00
|
|
|
return data().select(AccountModel.class)
|
|
|
|
.get().observableResult()
|
|
|
|
.map(ResultDelegate::toList)
|
|
|
|
.map(list -> {
|
2019-11-18 00:04:28 +01:00
|
|
|
List<Account> entities = new ArrayList<>(list.size());
|
2019-11-22 23:35:14 +01:00
|
|
|
for (AccountModel model : list) {
|
2019-12-01 19:56:13 +01:00
|
|
|
entities.add(accountMapping.modelToEntity(model, new IAccount()));
|
2019-11-22 23:35:14 +01:00
|
|
|
}
|
2019-11-03 06:33:52 +01:00
|
|
|
return entities;
|
|
|
|
})
|
|
|
|
.subscribeOn(subscriberScheduler())
|
|
|
|
.observeOn(observerScheduler());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-11-18 00:04:28 +01:00
|
|
|
public Single<Account> updateAccount(Account account) {
|
2019-11-03 06:33:52 +01:00
|
|
|
// Since we cannot access setId() of AccountModel, we have to query the model by ID and update it manually.
|
|
|
|
// https://github.com/requery/requery/issues/616#issuecomment-315685460
|
|
|
|
|
|
|
|
// fetch model
|
|
|
|
return data().select(AccountModel.class)
|
|
|
|
.where(AccountModel.ID.eq(account.getId()))
|
|
|
|
.get().maybe().toSingle() // to single
|
2019-12-02 02:25:48 +01:00
|
|
|
.flatMap(model -> {
|
2019-12-01 21:16:01 +01:00
|
|
|
// copy changes from the entity to the model
|
|
|
|
model = accountMapping.entityToModel(account, model);
|
2019-11-03 06:33:52 +01:00
|
|
|
// write the updated model back
|
2019-12-02 02:25:48 +01:00
|
|
|
return data().update(model);
|
|
|
|
}).map(model -> accountMapping.modelToEntity(model, account))
|
2019-11-03 06:33:52 +01:00
|
|
|
.subscribeOn(subscriberScheduler())
|
|
|
|
.observeOn(observerScheduler());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-11-18 00:04:28 +01:00
|
|
|
public Single<Account> upsertAccount(Account account) {
|
2019-11-03 06:33:52 +01:00
|
|
|
// Try to fetch model
|
|
|
|
return data().select(AccountModel.class)
|
|
|
|
.where(AccountModel.ID.eq(account.getId()))
|
|
|
|
.get().maybe()
|
|
|
|
// If it does not exist, create a new model from the entity
|
2019-12-01 19:56:13 +01:00
|
|
|
.switchIfEmpty(data().insert(accountMapping.entityToModel(account, new AccountModel())))
|
2019-12-02 02:25:48 +01:00
|
|
|
.flatMap(model -> {
|
2019-11-03 06:33:52 +01:00
|
|
|
// update the model
|
2019-12-01 21:16:01 +01:00
|
|
|
model = accountMapping.entityToModel(account, model);
|
2019-11-03 06:33:52 +01:00
|
|
|
// write the updated model back
|
2019-12-02 02:25:48 +01:00
|
|
|
return data().update(model);
|
2019-11-03 06:33:52 +01:00
|
|
|
})
|
2019-12-02 02:25:48 +01:00
|
|
|
.map(model -> accountMapping.modelToEntity(model, account))
|
2019-11-03 06:33:52 +01:00
|
|
|
.subscribeOn(subscriberScheduler())
|
|
|
|
.observeOn(observerScheduler());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-11-18 00:04:28 +01:00
|
|
|
public Completable deleteAccount(Account account) {
|
2019-11-03 06:33:52 +01:00
|
|
|
return data().delete(AccountModel.class)
|
|
|
|
.where(AccountModel.ID.eq(account.getId()))
|
|
|
|
.get().single().ignoreElement() // to completable
|
|
|
|
.subscribeOn(subscriberScheduler())
|
|
|
|
.observeOn(observerScheduler());
|
|
|
|
}
|
|
|
|
}
|