2 * Copyright © 2021 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.async;
20 import android.content.Context;
21 import android.net.Uri;
22 import android.util.JsonReader;
23 import android.util.JsonToken;
25 import net.ktnx.mobileledger.dao.CurrencyDAO;
26 import net.ktnx.mobileledger.dao.ProfileDAO;
27 import net.ktnx.mobileledger.dao.TemplateHeaderDAO;
28 import net.ktnx.mobileledger.db.Currency;
29 import net.ktnx.mobileledger.db.DB;
30 import net.ktnx.mobileledger.db.Profile;
31 import net.ktnx.mobileledger.db.TemplateAccount;
32 import net.ktnx.mobileledger.db.TemplateHeader;
33 import net.ktnx.mobileledger.db.TemplateWithAccounts;
34 import net.ktnx.mobileledger.model.Data;
35 import net.ktnx.mobileledger.utils.Misc;
37 import java.io.BufferedReader;
38 import java.io.FileInputStream;
39 import java.io.FileNotFoundException;
40 import java.io.IOException;
41 import java.io.InputStreamReader;
42 import java.util.ArrayList;
43 import java.util.List;
45 public class ConfigReader extends ConfigIO {
46 private final OnDoneListener onDoneListener;
48 public ConfigReader(Context context, Uri uri, OnErrorListener onErrorListener,
49 OnDoneListener onDoneListener) throws FileNotFoundException {
50 super(context, uri, onErrorListener);
52 this.onDoneListener = onDoneListener;
55 protected String getStreamMode() {
59 protected void initStream() {
60 r = new JsonReader(new BufferedReader(
61 new InputStreamReader(new FileInputStream(pfd.getFileDescriptor()))));
64 protected void processStream() throws IOException {
65 List<Currency> commodities = null;
66 List<Profile> profiles = null;
67 List<TemplateWithAccounts> templates = null;
68 String currentProfile = null;
71 String item = r.nextName();
73 case Keys.COMMODITIES:
74 commodities = readCommodities(r);
77 profiles = readProfiles(r);
80 templates = readTemplates(r);
82 case Keys.CURRENT_PROFILE:
83 currentProfile = r.nextString();
86 throw new RuntimeException("unexpected top-level item " + item);
91 restoreCommodities(commodities);
92 restoreProfiles(profiles);
93 restoreTemplates(templates);
95 if (Data.getProfile() == null && currentProfile != null) {
98 .getByUuidSync(currentProfile);
100 Data.postCurrentProfile(p);
103 if (onDoneListener != null)
104 Misc.onMainThread(onDoneListener::done);
106 private void restoreTemplates(List<TemplateWithAccounts> list) {
110 TemplateHeaderDAO dao = DB.get()
113 for (TemplateWithAccounts t : list) {
114 if (dao.getTemplateWithAccountsByUuidSync(t.header.getUuid()) == null)
118 private void restoreProfiles(List<Profile> list) {
122 ProfileDAO dao = DB.get()
125 for (Profile p : list) {
126 if (dao.getByUuidSync(p.getUuid()) == null)
130 private void restoreCommodities(List<Currency> list) {
134 CurrencyDAO dao = DB.get()
137 for (Currency c : list) {
138 if (dao.getByNameSync(c.getName()) == null)
142 private TemplateAccount readTemplateAccount(JsonReader r) throws IOException {
144 TemplateAccount result = new TemplateAccount(0L, 0L, 0L);
145 while (r.peek() != JsonToken.END_OBJECT) {
146 String item = r.nextName();
149 result.setAccountName(r.nextString());
151 case Keys.NAME_GROUP:
152 result.setAccountNameMatchGroup(r.nextInt());
155 result.setAccountComment(r.nextString());
157 case Keys.COMMENT_GROUP:
158 result.setAccountCommentMatchGroup(r.nextInt());
161 result.setAmount((float) r.nextDouble());
163 case Keys.AMOUNT_GROUP:
164 result.setAmountMatchGroup(r.nextInt());
166 case Keys.NEGATE_AMOUNT:
167 result.setNegateAmount(r.nextBoolean());
170 result.setCurrency(r.nextLong());
172 case Keys.CURRENCY_GROUP:
173 result.setCurrencyMatchGroup(r.nextInt());
177 throw new IllegalStateException("Unexpected template account item: " + item);
184 private TemplateWithAccounts readTemplate(JsonReader r) throws IOException {
187 TemplateHeader t = new TemplateHeader(0L, "", "");
188 List<TemplateAccount> accounts = new ArrayList<>();
190 while (r.peek() != JsonToken.END_OBJECT) {
191 String item = r.nextName();
194 t.setUuid(r.nextString());
197 t.setName(r.nextString());
200 t.setRegularExpression(r.nextString());
203 t.setTestText(r.nextString());
206 t.setDateYear(r.nextInt());
208 case Keys.DATE_YEAR_GROUP:
209 t.setDateYearMatchGroup(r.nextInt());
211 case Keys.DATE_MONTH:
212 t.setDateMonth(r.nextInt());
214 case Keys.DATE_MONTH_GROUP:
215 t.setDateMonthMatchGroup(r.nextInt());
218 t.setDateDay(r.nextInt());
220 case Keys.DATE_DAY_GROUP:
221 t.setDateDayMatchGroup(r.nextInt());
223 case Keys.TRANSACTION:
224 t.setTransactionDescription(r.nextString());
226 case Keys.TRANSACTION_GROUP:
227 t.setTransactionDescriptionMatchGroup(r.nextInt());
230 t.setTransactionComment(r.nextString());
232 case Keys.COMMENT_GROUP:
233 t.setTransactionCommentMatchGroup(r.nextInt());
235 case Keys.IS_FALLBACK:
236 t.setFallback(r.nextBoolean());
240 while (r.peek() == JsonToken.BEGIN_OBJECT) {
241 accounts.add(readTemplateAccount(r));
246 throw new RuntimeException("Unknown template header item: " + item);
251 TemplateWithAccounts result = new TemplateWithAccounts();
253 result.accounts = accounts;
256 private List<TemplateWithAccounts> readTemplates(JsonReader r) throws IOException {
257 List<TemplateWithAccounts> list = new ArrayList<>();
260 while (r.peek() == JsonToken.BEGIN_OBJECT) {
261 list.add(readTemplate(r));
267 private List<Currency> readCommodities(JsonReader r) throws IOException {
268 List<Currency> list = new ArrayList<>();
271 while (r.peek() == JsonToken.BEGIN_OBJECT) {
272 Currency c = new Currency();
275 while (r.peek() != JsonToken.END_OBJECT) {
276 final String item = r.nextName();
279 c.setName(r.nextString());
282 c.setPosition(r.nextString());
285 c.setHasGap(r.nextBoolean());
288 throw new RuntimeException("Unknown commodity key: " + item);
295 throw new RuntimeException("Missing commodity name");
303 private List<Profile> readProfiles(JsonReader r) throws IOException {
304 List<Profile> list = new ArrayList<>();
306 while (r.peek() == JsonToken.BEGIN_OBJECT) {
307 Profile p = new Profile();
309 while (r.peek() != JsonToken.END_OBJECT) {
310 String item = r.nextName();
314 p.setUuid(r.nextString());
317 p.setName(r.nextString());
320 p.setUrl(r.nextString());
323 p.setUseAuthentication(r.nextBoolean());
326 p.setAuthUser(r.nextString());
329 p.setAuthPassword(r.nextString());
332 p.setApiVersion(r.nextInt());
335 p.setPermitPosting(r.nextBoolean());
337 case Keys.DEFAULT_COMMODITY:
338 p.setDefaultCommodity(r.nextString());
340 case Keys.SHOW_COMMODITY:
341 p.setShowCommodityByDefault(r.nextBoolean());
343 case Keys.SHOW_COMMENTS:
344 p.setShowCommentsByDefault(r.nextBoolean());
346 case Keys.FUTURE_DATES:
347 p.setFutureDates(r.nextInt());
349 case Keys.PREF_ACCOUNT:
350 p.setPreferredAccountsFilter(r.nextString());
353 p.setTheme(r.nextInt());
358 throw new IllegalStateException("Unexpected profile item: " + item);
369 abstract static public class OnDoneListener {
370 public abstract void done();