forked from openMF/android-client
/
DatabaseHelperCenter.java
263 lines (228 loc) · 9.47 KB
/
DatabaseHelperCenter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package com.mifos.api.local.databasehelper;
import android.os.AsyncTask;
import android.support.annotation.Nullable;
import com.mifos.objects.SearchedEntity;
import com.mifos.objects.accounts.CenterAccounts;
import com.mifos.objects.accounts.loan.LoanAccount;
import com.mifos.objects.accounts.savings.SavingsAccount;
import com.mifos.objects.client.Page;
import com.mifos.objects.group.Center;
import com.mifos.objects.group.CenterDate;
import com.mifos.objects.group.CenterWithAssociations;
import com.mifos.objects.group.Center_Table;
import com.mifos.objects.group.Group;
import com.mifos.objects.group.Group_Table;
import com.mifos.objects.response.SaveResponse;
import com.mifos.services.data.CenterPayload;
import com.mifos.services.data.CenterPayload_Table;
import com.mifos.utils.Constants;
import com.raizlabs.android.dbflow.sql.language.Delete;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Singleton;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Func0;
/**
* Created by Rajan Maurya on 28/6/16.
*/
@Singleton
public class DatabaseHelperCenter {
@Inject
public DatabaseHelperCenter() {
}
/**
* Saving Centers in Database using DBFlow.
* save() method save the value reference to primary key if its exist the update if not the
* insert.
*
* @param centerPage
* @return null
*/
@Nullable
public Observable<Void> saveAllCenters(final Page<Center> centerPage) {
AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() {
@Override
public void run() {
for (Center center : centerPage.getPageItems()) {
center.save();
}
}
});
return null;
}
/**
* Reading All Centers from table of Center and return the CenterList
*
* @return List Of Centers
*/
//TODO Implement Observable Transaction to load Center List
public Observable<Page<Center>> readAllCenters() {
return Observable.create(new Observable.OnSubscribe<Page<Center>>() {
@Override
public void call(Subscriber<? super Page<Center>> subscriber) {
Page<Center> centerPage = new Page<>();
centerPage.setPageItems(SQLite.select()
.from(Center.class)
.queryList());
subscriber.onNext(centerPage);
subscriber.onCompleted();
}
});
}
public Observable<List<SearchedEntity>> readAllCentersByQuery(
final String query, final Boolean exactMatch) {
return Observable.create(new Observable.OnSubscribe<List<SearchedEntity>>() {
@Override
public void call(Subscriber<? super List<SearchedEntity>> subscriber) {
List<Center> centerList;
List<SearchedEntity> searchedEntities = new ArrayList<SearchedEntity>();
if (exactMatch) {
centerList = SQLite.select()
.from(Center.class)
.where(Center_Table.name.like(query))
.queryList();
} else {
centerList = SQLite.select()
.from(Center.class)
.where(Center_Table.name.like(("%" + query + "%")))
.queryList();
}
for (Center center : centerList) {
SearchedEntity searchedEntity = new SearchedEntity();
searchedEntity.setEntityId(center.getId());
searchedEntity.setEntityAccountNo(center.getAccountNo());
searchedEntity.setEntityType(Constants.SEARCH_ENTITY_CENTER);
searchedEntity.setEntityName(center.getName());
searchedEntity.setParentId(center.getOfficeId());
searchedEntity.setParentName(center.getOfficeName());
searchedEntities.add(searchedEntity);
}
subscriber.onNext(searchedEntities);
subscriber.onCompleted();
}
});
}
public Observable<SaveResponse> saveCenterPayload(final CenterPayload centerPayload) {
return Observable.defer(new Func0<Observable<SaveResponse>>() {
@Override
public Observable<SaveResponse> call() {
centerPayload.save();
return Observable.just(new SaveResponse());
}
});
}
public Observable<List<CenterPayload>> readAllCenterPayload() {
return Observable.defer(new Func0<Observable<List<CenterPayload>>>() {
@Override
public Observable<List<CenterPayload>> call() {
List<CenterPayload> centerPayloads = SQLite.select()
.from(CenterPayload.class)
.queryList();
return Observable.just(centerPayloads);
}
});
}
/**
* This Method Fetch the Groups that are attached to the Center.
* @param centerId Center Id
* @return CenterWithAssociations
*/
public Observable<CenterWithAssociations> getCenterAssociateGroups(final int centerId) {
return Observable.defer(new Func0<Observable<CenterWithAssociations>>() {
@Override
public Observable<CenterWithAssociations> call() {
List<Group> groups = SQLite.select()
.from(Group.class)
.where(Group_Table.centerId.eq(centerId))
.queryList();
CenterWithAssociations centerWithAssociations = new CenterWithAssociations();
centerWithAssociations.setGroupMembers(groups);
return Observable.just(centerWithAssociations);
}
});
}
/**
* This Method Saving the Single Center in the Database
*
* @param center
* @return Observable.just(Center)
*/
public Observable<Center> saveCenter(final Center center) {
return Observable.defer(new Func0<Observable<Center>>() {
@Override
public Observable<Center> call() {
if (center.getActivationDate().size() != 0) {
CenterDate centerDate = new CenterDate(center.getId(), 0,
center.getActivationDate().get(0),
center.getActivationDate().get(1),
center.getActivationDate().get(2));
center.setCenterDate(centerDate);
}
center.save();
return Observable.just(center);
}
});
}
/**
* This Method for deleting the center payload from the Database according to Id and
* again fetch the center List from the Database CenterPayload_Table
* @param id is Id of the Center Payload in which reference center was saved into Database
* @return List<CenterPayload></>
*/
public Observable<List<CenterPayload>> deleteAndUpdateCenterPayloads(final int id) {
return Observable.defer(new Func0<Observable<List<CenterPayload>>>() {
@Override
public Observable<List<CenterPayload>> call() {
Delete.table(CenterPayload.class, CenterPayload_Table.id.eq(id));
List<CenterPayload> groupPayloads = SQLite.select()
.from(CenterPayload.class)
.queryList();
return Observable.just(groupPayloads);
}
});
}
public Observable<CenterPayload> updateDatabaseCenterPayload(
final CenterPayload centerPayload) {
return Observable.defer(new Func0<Observable<CenterPayload>>() {
@Override
public Observable<CenterPayload> call() {
centerPayload.update();
return Observable.just(centerPayload);
}
});
}
/**
* This Method write the CenterAccounts in tho DB. According to Schema Defined in Model
*
* @param centerAccounts Model of List of LoanAccount and SavingAccount
* @param centerId Center Id
* @return CenterAccounts
*/
public Observable<CenterAccounts> saveCenterAccounts(final CenterAccounts centerAccounts,
final int centerId) {
return Observable.defer(new Func0<Observable<CenterAccounts>>() {
@Override
public Observable<CenterAccounts> call() {
List<LoanAccount> loanAccounts = centerAccounts.getLoanAccounts();
List<SavingsAccount> savingsAccounts = centerAccounts.getSavingsAccounts();
List<LoanAccount> memberLoanAccounts = centerAccounts.getMemberLoanAccounts();
for (LoanAccount loanAccount : loanAccounts) {
loanAccount.setCenterId(centerId);
loanAccount.save();
}
for (SavingsAccount savingsAccount : savingsAccounts) {
savingsAccount.setCenterId(centerId);
savingsAccount.save();
}
for (LoanAccount memberLoanAccount : memberLoanAccounts) {
memberLoanAccount.setCenterId(centerId);
memberLoanAccount.save();
}
return Observable.just(centerAccounts);
}
});
}
}