2 * Copyright © 2024 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.model;
20 import androidx.annotation.NonNull;
21 import androidx.annotation.Nullable;
23 import net.ktnx.mobileledger.db.Account;
24 import net.ktnx.mobileledger.db.AccountValue;
25 import net.ktnx.mobileledger.db.AccountWithAmounts;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.regex.Pattern;
31 public class LedgerAccount {
32 private static final char ACCOUNT_DELIMITER = ':';
33 static Pattern reHigherAccount = Pattern.compile("^[^:]+:");
34 private final LedgerAccount parent;
36 private long profileId;
38 private String shortName;
40 private boolean expanded;
41 private List<LedgerAmount> amounts;
42 private boolean hasSubAccounts;
43 private boolean amountsExpanded;
45 public LedgerAccount(String name, @Nullable LedgerAccount 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,
54 public static String extractParentName(@NonNull String accName) {
55 int colonPos = accName.lastIndexOf(ACCOUNT_DELIMITER);
57 return null; // no parent account -- this is a top-level account
59 return accName.substring(0, colonPos);
61 public static boolean isParentOf(@NonNull String possibleParent, @NonNull String accountName) {
62 return accountName.startsWith(possibleParent + ':');
65 static public LedgerAccount fromDBO(AccountWithAmounts in, LedgerAccount parent) {
66 LedgerAccount res = new LedgerAccount(in.account.getName(), parent);
67 res.dbId = in.account.getId();
68 res.profileId = in.account.getProfileId();
69 res.setName(in.account.getName());
70 res.setExpanded(in.account.isExpanded());
71 res.setAmountsExpanded(in.account.isAmountsExpanded());
73 res.amounts = new ArrayList<>();
74 for (AccountValue val : in.amounts) {
75 res.amounts.add(new LedgerAmount(val.getValue(), val.getCurrency()));
80 public static int determineLevel(String accName) {
82 int delimiterPosition = accName.indexOf(ACCOUNT_DELIMITER);
83 while (delimiterPosition >= 0) {
85 delimiterPosition = accName.indexOf(ACCOUNT_DELIMITER, delimiterPosition + 1);
90 public int hashCode() {
91 return name.hashCode();
94 public boolean equals(@Nullable Object obj) {
98 if (!(obj instanceof LedgerAccount))
101 LedgerAccount acc = (LedgerAccount) obj;
102 if (!name.equals(acc.name))
105 if (!getAmountsString().equals(acc.getAmountsString()))
108 return expanded == acc.expanded && amountsExpanded == acc.amountsExpanded;
110 // an account is visible if:
111 // - it has an expanded visible parent or is a top account
112 public boolean isVisible() {
116 return (parent.isExpanded() && parent.isVisible());
118 public boolean isParentOf(LedgerAccount potentialChild) {
119 return potentialChild.getName()
120 .startsWith(name + ":");
122 private void stripName() {
123 String[] split = name.split(":");
124 shortName = split[split.length - 1];
125 level = split.length - 1;
127 public String getName() {
130 public void setName(String name) {
134 public void addAmount(float amount, @NonNull String currency) {
136 amounts = new ArrayList<>();
137 amounts.add(new LedgerAmount(amount, currency));
139 public void addAmount(float amount) {
140 this.addAmount(amount, "");
142 public int getAmountCount() { return (amounts != null) ? amounts.size() : 0; }
143 public String getAmountsString() {
144 if ((amounts == null) || amounts.isEmpty())
147 StringBuilder builder = new StringBuilder();
148 for (LedgerAmount amount : amounts) {
149 String amt = amount.toString();
150 if (builder.length() > 0)
151 builder.append('\n');
155 return builder.toString();
157 public String getAmountsString(int limit) {
158 if ((amounts == null) || amounts.isEmpty())
162 StringBuilder builder = new StringBuilder();
163 for (LedgerAmount amount : amounts) {
164 String amt = amount.toString();
165 if (builder.length() > 0)
166 builder.append('\n');
169 if (included == limit)
173 return builder.toString();
175 public int getLevel() {
179 public String getShortName() {
182 public String getParentName() {
183 return (parent == null) ? null : parent.getName();
185 public boolean hasSubAccounts() {
186 return hasSubAccounts;
188 public void setHasSubAccounts(boolean hasSubAccounts) {
189 this.hasSubAccounts = hasSubAccounts;
191 public boolean isExpanded() {
194 public void setExpanded(boolean expanded) {
195 this.expanded = expanded;
197 public void toggleExpanded() {
198 expanded = !expanded;
200 public void removeAmounts() {
204 public boolean amountsExpanded() {return amountsExpanded;}
205 public void setAmountsExpanded(boolean flag) {amountsExpanded = flag;}
206 public void toggleAmountsExpanded() {amountsExpanded = !amountsExpanded;}
207 public void propagateAmountsTo(LedgerAccount acc) {
208 for (LedgerAmount a : amounts)
209 a.propagateToAccount(acc);
211 public boolean allAmountsAreZero() {
212 for (LedgerAmount a : amounts) {
213 if (a.getAmount() != 0)
219 public List<LedgerAmount> getAmounts() {
223 public Account toDBO() {
224 Account dbo = new Account();
226 dbo.setNameUpper(name.toUpperCase());
227 dbo.setParentName(extractParentName(name));
230 dbo.setProfileId(profileId);
231 dbo.setExpanded(expanded);
232 dbo.setAmountsExpanded(amountsExpanded);
237 public AccountWithAmounts toDBOWithAmounts() {
238 AccountWithAmounts dbo = new AccountWithAmounts();
239 dbo.account = toDBO();
241 dbo.amounts = new ArrayList<>();
242 for (LedgerAmount amt : getAmounts()) {
243 AccountValue val = new AccountValue();
244 val.setCurrency(amt.getCurrency());
245 val.setValue(amt.getAmount());
246 dbo.amounts.add(val);
251 public long getId() {