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