]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java
06ac4da110f61acb3a73c1309f3da1332a88f671
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / model / LedgerAccount.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.model;
19
20 import androidx.annotation.NonNull;
21 import androidx.annotation.Nullable;
22
23 import net.ktnx.mobileledger.db.Account;
24 import net.ktnx.mobileledger.db.AccountValue;
25 import net.ktnx.mobileledger.db.AccountWithAmounts;
26
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.regex.Pattern;
30
31 public class LedgerAccount {
32     private static final char ACCOUNT_DELIMITER = ':';
33     static Pattern reHigherAccount = Pattern.compile("^[^:]+:");
34     private final LedgerAccount parent;
35     private long dbId;
36     private long profileId;
37     private String name;
38     private String shortName;
39     private int level;
40     private boolean expanded;
41     private List<LedgerAmount> amounts;
42     private boolean hasSubAccounts;
43     private boolean amountsExpanded;
44
45     public LedgerAccount(String name, @Nullable LedgerAccount parent) {
46         this.parent = parent;
47         if (parent != null && !name.startsWith(parent.getName() + ":"))
48             throw new IllegalStateException(
49                     String.format("Account name '%s' doesn't match parent account '%s'", name,
50                             parent.getName()));
51         this.setName(name);
52     }
53     @Nullable
54     public static String extractParentName(@NonNull String accName) {
55         int colonPos = accName.lastIndexOf(ACCOUNT_DELIMITER);
56         if (colonPos < 0)
57             return null;    // no parent account -- this is a top-level account
58         else
59             return accName.substring(0, colonPos);
60     }
61     @NonNull
62     static public LedgerAccount fromDBO(AccountWithAmounts in, LedgerAccount parent) {
63         LedgerAccount res = new LedgerAccount(in.account.getName(), parent);
64         res.dbId = in.account.getId();
65         res.profileId = in.account.getProfileId();
66         res.setName(in.account.getName());
67         res.setExpanded(in.account.isExpanded());
68         res.setAmountsExpanded(in.account.isAmountsExpanded());
69
70         res.amounts = new ArrayList<>();
71         for (AccountValue val : in.amounts) {
72             res.amounts.add(new LedgerAmount(val.getValue(), val.getCurrency()));
73         }
74
75         return res;
76     }
77     public static int determineLevel(String accName) {
78         int level = 0;
79         int delimiterPosition = accName.indexOf(ACCOUNT_DELIMITER);
80         while (delimiterPosition >= 0) {
81             level++;
82             delimiterPosition = accName.indexOf(ACCOUNT_DELIMITER, delimiterPosition + 1);
83         }
84         return level;
85     }
86     @Override
87     public int hashCode() {
88         return name.hashCode();
89     }
90     @Override
91     public boolean equals(@Nullable Object obj) {
92         if (obj == null)
93             return false;
94
95         if (!(obj instanceof LedgerAccount))
96             return false;
97
98         LedgerAccount acc = (LedgerAccount) obj;
99         if (!name.equals(acc.name))
100             return false;
101
102         if (!getAmountsString().equals(acc.getAmountsString()))
103             return false;
104
105         return expanded == acc.expanded && amountsExpanded == acc.amountsExpanded;
106     }
107     // an account is visible if:
108     //  - it has an expanded visible parent or is a top account
109     public boolean isVisible() {
110         if (parent == null)
111             return true;
112
113         return (parent.isExpanded() && parent.isVisible());
114     }
115     public boolean isParentOf(LedgerAccount potentialChild) {
116         return potentialChild.getName()
117                              .startsWith(name + ":");
118     }
119     private void stripName() {
120         String[] split = name.split(":");
121         shortName = split[split.length - 1];
122         level = split.length - 1;
123     }
124     public String getName() {
125         return name;
126     }
127     public void setName(String name) {
128         this.name = name;
129         stripName();
130     }
131     public void addAmount(float amount, @NonNull String currency) {
132         if (amounts == null)
133             amounts = new ArrayList<>();
134         amounts.add(new LedgerAmount(amount, currency));
135     }
136     public void addAmount(float amount) {
137         this.addAmount(amount, "");
138     }
139     public int getAmountCount() { return (amounts != null) ? amounts.size() : 0; }
140     public String getAmountsString() {
141         if ((amounts == null) || amounts.isEmpty())
142             return "";
143
144         StringBuilder builder = new StringBuilder();
145         for (LedgerAmount amount : amounts) {
146             String amt = amount.toString();
147             if (builder.length() > 0)
148                 builder.append('\n');
149             builder.append(amt);
150         }
151
152         return builder.toString();
153     }
154     public String getAmountsString(int limit) {
155         if ((amounts == null) || amounts.isEmpty())
156             return "";
157
158         int included = 0;
159         StringBuilder builder = new StringBuilder();
160         for (LedgerAmount amount : amounts) {
161             String amt = amount.toString();
162             if (builder.length() > 0)
163                 builder.append('\n');
164             builder.append(amt);
165             included++;
166             if (included == limit)
167                 break;
168         }
169
170         return builder.toString();
171     }
172     public int getLevel() {
173         return level;
174     }
175     @NonNull
176     public String getShortName() {
177         return shortName;
178     }
179     public String getParentName() {
180         return (parent == null) ? null : parent.getName();
181     }
182     public boolean hasSubAccounts() {
183         return hasSubAccounts;
184     }
185     public void setHasSubAccounts(boolean hasSubAccounts) {
186         this.hasSubAccounts = hasSubAccounts;
187     }
188     public boolean isExpanded() {
189         return expanded;
190     }
191     public void setExpanded(boolean expanded) {
192         this.expanded = expanded;
193     }
194     public void toggleExpanded() {
195         expanded = !expanded;
196     }
197     public void removeAmounts() {
198         if (amounts != null)
199             amounts.clear();
200     }
201     public boolean amountsExpanded() { return amountsExpanded; }
202     public void setAmountsExpanded(boolean flag) { amountsExpanded = flag; }
203     public void toggleAmountsExpanded() { amountsExpanded = !amountsExpanded; }
204     public void propagateAmountsTo(LedgerAccount acc) {
205         for (LedgerAmount a : amounts)
206             a.propagateToAccount(acc);
207     }
208     public List<LedgerAmount> getAmounts() {
209         return amounts;
210     }
211     @NonNull
212     public Account toDBO() {
213         Account dbo = new Account();
214         dbo.setName(name);
215         dbo.setNameUpper(name.toUpperCase());
216         dbo.setParentName(extractParentName(name));
217         dbo.setLevel(level);
218         dbo.setId(dbId);
219         dbo.setProfileId(profileId);
220         dbo.setExpanded(expanded);
221         dbo.setAmountsExpanded(amountsExpanded);
222
223         return dbo;
224     }
225     @NonNull
226     public AccountWithAmounts toDBOWithAmounts() {
227         AccountWithAmounts dbo = new AccountWithAmounts();
228         dbo.account = toDBO();
229
230         dbo.amounts = new ArrayList<>();
231         for (LedgerAmount amt : getAmounts()) {
232             AccountValue val = new AccountValue();
233             val.setCurrency(amt.getCurrency());
234             val.setValue(amt.getAmount());
235             dbo.amounts.add(val);
236         }
237
238         return dbo;
239     }
240     public long getId() {
241         return dbId;
242     }
243 }