]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/async/ConfigWriter.java
methods for deleting all DB tables
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / async / ConfigWriter.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.async;
19
20 import android.content.Context;
21 import android.net.Uri;
22 import android.util.JsonWriter;
23
24 import net.ktnx.mobileledger.db.Currency;
25 import net.ktnx.mobileledger.db.DB;
26 import net.ktnx.mobileledger.db.Profile;
27 import net.ktnx.mobileledger.db.TemplateAccount;
28 import net.ktnx.mobileledger.db.TemplateWithAccounts;
29 import net.ktnx.mobileledger.json.API;
30 import net.ktnx.mobileledger.model.Data;
31 import net.ktnx.mobileledger.utils.Misc;
32
33 import java.io.BufferedWriter;
34 import java.io.FileNotFoundException;
35 import java.io.FileOutputStream;
36 import java.io.IOException;
37 import java.io.OutputStreamWriter;
38 import java.util.List;
39
40 public class ConfigWriter extends ConfigIO {
41     private final OnDoneListener onDoneListener;
42     private JsonWriter w;
43     public ConfigWriter(Context context, Uri uri, OnErrorListener onErrorListener,
44                         OnDoneListener onDoneListener) throws FileNotFoundException {
45         super(context, uri, onErrorListener);
46
47         this.onDoneListener = onDoneListener;
48     }
49     @Override
50     protected String getStreamMode() {
51         return "w";
52     }
53     @Override
54     protected void initStream() {
55         w = new JsonWriter(new BufferedWriter(
56                 new OutputStreamWriter(new FileOutputStream(pfd.getFileDescriptor()))));
57         w.setIndent("  ");
58     }
59     @Override
60     protected void processStream() throws IOException {
61         w.beginObject();
62         writeCommodities(w);
63         writeProfiles(w);
64         writeCurrentProfile(w);
65         writeConfigTemplates(w);
66         w.endObject();
67         w.flush();
68
69         if (onDoneListener != null)
70             Misc.onMainThread(onDoneListener::done);
71     }
72     private void writeKey(JsonWriter w, String key, String value) throws IOException {
73         if (value != null)
74             w.name(key)
75              .value(value);
76     }
77     private void writeKey(JsonWriter w, String key, Integer value) throws IOException {
78         if (value != null)
79             w.name(key)
80              .value(value);
81     }
82     private void writeKey(JsonWriter w, String key, Long value) throws IOException {
83         if (value != null)
84             w.name(key)
85              .value(value);
86     }
87     private void writeKey(JsonWriter w, String key, Float value) throws IOException {
88         if (value != null)
89             w.name(key)
90              .value(value);
91     }
92     private void writeKey(JsonWriter w, String key, Boolean value) throws IOException {
93         if (value != null)
94             w.name(key)
95              .value(value);
96     }
97     private void writeConfigTemplates(JsonWriter w) throws IOException {
98         List<TemplateWithAccounts> templates = DB.get()
99                                                  .getTemplateDAO()
100                                                  .getAllTemplatesWithAccountsSync();
101
102         if (templates.isEmpty())
103             return;
104
105         w.name("templates")
106          .beginArray();
107         for (TemplateWithAccounts t : templates) {
108             w.beginObject();
109
110             w.name(Keys.UUID)
111              .value(t.header.getUuid());
112             w.name(Keys.NAME)
113              .value(t.header.getName());
114             w.name(Keys.REGEX)
115              .value(t.header.getRegularExpression());
116             writeKey(w, Keys.TEST_TEXT, t.header.getTestText());
117             writeKey(w, Keys.DATE_YEAR, t.header.getDateYear());
118             writeKey(w, Keys.DATE_YEAR_GROUP, t.header.getDateYearMatchGroup());
119             writeKey(w, Keys.DATE_MONTH, t.header.getDateMonth());
120             writeKey(w, Keys.DATE_MONTH_GROUP, t.header.getDateMonthMatchGroup());
121             writeKey(w, Keys.DATE_DAY, t.header.getDateDay());
122             writeKey(w, Keys.DATE_DAY_GROUP, t.header.getDateDayMatchGroup());
123             writeKey(w, Keys.TRANSACTION, t.header.getTransactionDescription());
124             writeKey(w, Keys.TRANSACTION_GROUP, t.header.getTransactionDescriptionMatchGroup());
125             writeKey(w, Keys.COMMENT, t.header.getTransactionComment());
126             writeKey(w, Keys.COMMENT_GROUP, t.header.getTransactionCommentMatchGroup());
127             w.name(Keys.IS_FALLBACK)
128              .value(t.header.isFallback());
129             if (t.accounts.size() > 0) {
130                 w.name(Keys.ACCOUNTS)
131                  .beginArray();
132                 for (TemplateAccount a : t.accounts) {
133                     w.beginObject();
134
135                     writeKey(w, Keys.NAME, a.getAccountName());
136                     writeKey(w, Keys.NAME_GROUP, a.getAccountNameMatchGroup());
137                     writeKey(w, Keys.COMMENT, a.getAccountComment());
138                     writeKey(w, Keys.COMMENT_GROUP, a.getAccountCommentMatchGroup());
139                     writeKey(w, Keys.AMOUNT, a.getAmount());
140                     writeKey(w, Keys.AMOUNT_GROUP, a.getAmountMatchGroup());
141                     writeKey(w, Keys.NEGATE_AMOUNT, a.getNegateAmount());
142                     writeKey(w, Keys.CURRENCY, a.getCurrency());
143                     writeKey(w, Keys.CURRENCY_GROUP, a.getCurrencyMatchGroup());
144
145                     w.endObject();
146                 }
147                 w.endArray();
148             }
149
150             w.endObject();
151         }
152         w.endArray();
153     }
154     private void writeCommodities(JsonWriter w) throws IOException {
155         List<Currency> list = DB.get()
156                                 .getCurrencyDAO()
157                                 .getAllSync();
158         if (list.isEmpty())
159             return;
160         w.name(Keys.COMMODITIES)
161          .beginArray();
162         for (Currency c : list) {
163             w.beginObject();
164             writeKey(w, Keys.NAME, c.getName());
165             writeKey(w, Keys.POSITION, c.getPosition());
166             writeKey(w, Keys.HAS_GAP, c.getHasGap());
167             w.endObject();
168         }
169         w.endArray();
170     }
171     private void writeProfiles(JsonWriter w) throws IOException {
172         List<Profile> profiles = DB.get()
173                                    .getProfileDAO()
174                                    .getAllOrderedSync();
175
176         if (profiles.isEmpty())
177             return;
178
179         w.name(Keys.PROFILES)
180          .beginArray();
181         for (Profile p : profiles) {
182             w.beginObject();
183
184             w.name(Keys.NAME)
185              .value(p.getName());
186             w.name(Keys.UUID)
187              .value(p.getUuid());
188             w.name(Keys.URL)
189              .value(p.getUrl());
190             w.name(Keys.USE_AUTH)
191              .value(p.useAuthentication());
192             if (p.useAuthentication()) {
193                 w.name(Keys.AUTH_USER)
194                  .value(p.getAuthUser());
195                 w.name(Keys.AUTH_PASS)
196                  .value(p.getAuthPassword());
197             }
198             if (p.getApiVersion() != API.auto.toInt())
199                 w.name(Keys.API_VER)
200                  .value(p.getApiVersion());
201             w.name(Keys.CAN_POST)
202              .value(p.permitPosting());
203             if (p.permitPosting()) {
204                 String defaultCommodity = p.getDefaultCommodity();
205                 if (!defaultCommodity.isEmpty())
206                     w.name(Keys.DEFAULT_COMMODITY)
207                      .value(defaultCommodity);
208                 w.name(Keys.SHOW_COMMODITY)
209                  .value(p.getShowCommodityByDefault());
210                 w.name(Keys.SHOW_COMMENTS)
211                  .value(p.getShowCommentsByDefault());
212                 w.name(Keys.FUTURE_DATES)
213                  .value(p.getFutureDates());
214                 w.name(Keys.PREF_ACCOUNT)
215                  .value(p.getPreferredAccountsFilter());
216             }
217             w.name(Keys.COLOUR)
218              .value(p.getTheme());
219
220             w.endObject();
221         }
222         w.endArray();
223     }
224     private void writeCurrentProfile(JsonWriter w) throws IOException {
225         Profile currentProfile = Data.getProfile();
226         if (currentProfile == null)
227             return;
228
229         w.name(Keys.CURRENT_PROFILE)
230          .value(currentProfile.getUuid());
231     }
232
233     abstract static public class OnDoneListener {
234         public abstract void done();
235     }
236 }