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.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);
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());
70 res.amounts = new ArrayList<>();
71 for (AccountValue val : in.amounts) {
72 res.amounts.add(new LedgerAmount(val.getValue(), val.getCurrency()));
77 public static int determineLevel(String accName) {
79 int delimiterPosition = accName.indexOf(ACCOUNT_DELIMITER);
80 while (delimiterPosition >= 0) {
82 delimiterPosition = accName.indexOf(ACCOUNT_DELIMITER, delimiterPosition + 1);
87 public int hashCode() {
88 return name.hashCode();
91 public boolean equals(@Nullable Object obj) {
95 if (!(obj instanceof LedgerAccount))
98 LedgerAccount acc = (LedgerAccount) obj;
99 if (!name.equals(acc.name))
102 if (!getAmountsString().equals(acc.getAmountsString()))
105 return expanded == acc.expanded && amountsExpanded == acc.amountsExpanded;
107 // an account is visible if:
108 // - it has an expanded visible parent or is a top account
109 public boolean isVisible() {
113 return (parent.isExpanded() && parent.isVisible());
115 public boolean isParentOf(LedgerAccount potentialChild) {
116 return potentialChild.getName()
117 .startsWith(name + ":");
119 private void stripName() {
120 String[] split = name.split(":");
121 shortName = split[split.length - 1];
122 level = split.length - 1;
124 public String getName() {
127 public void setName(String name) {
131 public void addAmount(float amount, @NonNull String currency) {
133 amounts = new ArrayList<>();
134 amounts.add(new LedgerAmount(amount, currency));
136 public void addAmount(float amount) {
137 this.addAmount(amount, "");
139 public int getAmountCount() { return (amounts != null) ? amounts.size() : 0; }
140 public String getAmountsString() {
141 if ((amounts == null) || amounts.isEmpty())
144 StringBuilder builder = new StringBuilder();
145 for (LedgerAmount amount : amounts) {
146 String amt = amount.toString();
147 if (builder.length() > 0)
148 builder.append('\n');
152 return builder.toString();
154 public String getAmountsString(int limit) {
155 if ((amounts == null) || amounts.isEmpty())
159 StringBuilder builder = new StringBuilder();
160 for (LedgerAmount amount : amounts) {
161 String amt = amount.toString();
162 if (builder.length() > 0)
163 builder.append('\n');
166 if (included == limit)
170 return builder.toString();
172 public int getLevel() {
176 public String getShortName() {
179 public String getParentName() {
180 return (parent == null) ? null : parent.getName();
182 public boolean hasSubAccounts() {
183 return hasSubAccounts;
185 public void setHasSubAccounts(boolean hasSubAccounts) {
186 this.hasSubAccounts = hasSubAccounts;
188 public boolean isExpanded() {
191 public void setExpanded(boolean expanded) {
192 this.expanded = expanded;
194 public void toggleExpanded() {
195 expanded = !expanded;
197 public void removeAmounts() {
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);
208 public List<LedgerAmount> getAmounts() {
212 public Account toDBO() {
213 Account dbo = new Account();
215 dbo.setNameUpper(name.toUpperCase());
216 dbo.setParentName(extractParentName(name));
219 dbo.setProfileId(profileId);
220 dbo.setExpanded(expanded);
221 dbo.setAmountsExpanded(amountsExpanded);
226 public AccountWithAmounts toDBOWithAmounts() {
227 AccountWithAmounts dbo = new AccountWithAmounts();
228 dbo.account = toDBO();
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);
240 public long getId() {