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 static Pattern reHigherAccount = Pattern.compile("^[^:]+:");
33 private final LedgerAccount parent;
35 private long profileId;
37 private String shortName;
39 private boolean expanded;
40 private List<LedgerAmount> amounts;
41 private boolean hasSubAccounts;
42 private boolean amountsExpanded;
44 public LedgerAccount(String name, @Nullable LedgerAccount parent) {
46 if (parent != null && !name.startsWith(parent.getName() + ":"))
47 throw new IllegalStateException(
48 String.format("Account name '%s' doesn't match parent account '%s'", name,
53 public static String extractParentName(@NonNull String accName) {
54 int colonPos = accName.lastIndexOf(':');
56 return null; // no parent account -- this is a top-level account
58 return accName.substring(0, colonPos);
61 static public LedgerAccount fromDBO(AccountWithAmounts in, LedgerAccount parent) {
62 LedgerAccount res = new LedgerAccount(in.account.getName(), parent);
63 res.dbId = in.account.getId();
64 res.profileId = in.account.getProfileId();
65 res.setName(in.account.getName());
66 res.setExpanded(in.account.isExpanded());
67 res.setAmountsExpanded(in.account.isAmountsExpanded());
69 res.amounts = new ArrayList<>();
70 for (AccountValue val : in.amounts) {
71 res.amounts.add(new LedgerAmount(val.getValue(), val.getCurrency()));
77 public int hashCode() {
78 return name.hashCode();
81 public boolean equals(@Nullable Object obj) {
85 if (!(obj instanceof LedgerAccount))
88 LedgerAccount acc = (LedgerAccount) obj;
89 if (!name.equals(acc.name))
92 if (!getAmountsString().equals(acc.getAmountsString()))
95 return expanded == acc.expanded && amountsExpanded == acc.amountsExpanded;
97 // an account is visible if:
98 // - it has an expanded visible parent or is a top account
99 public boolean isVisible() {
103 return (parent.isExpanded() && parent.isVisible());
105 public boolean isParentOf(LedgerAccount potentialChild) {
106 return potentialChild.getName()
107 .startsWith(name + ":");
109 private void stripName() {
110 String[] split = name.split(":");
111 shortName = split[split.length - 1];
112 level = split.length - 1;
114 public String getName() {
117 public void setName(String name) {
121 public void addAmount(float amount, @NonNull String currency) {
123 amounts = new ArrayList<>();
124 amounts.add(new LedgerAmount(amount, currency));
126 public void addAmount(float amount) {
127 this.addAmount(amount, "");
129 public int getAmountCount() { return (amounts != null) ? amounts.size() : 0; }
130 public String getAmountsString() {
131 if ((amounts == null) || amounts.isEmpty())
134 StringBuilder builder = new StringBuilder();
135 for (LedgerAmount amount : amounts) {
136 String amt = amount.toString();
137 if (builder.length() > 0)
138 builder.append('\n');
142 return builder.toString();
144 public String getAmountsString(int limit) {
145 if ((amounts == null) || amounts.isEmpty())
149 StringBuilder builder = new StringBuilder();
150 for (LedgerAmount amount : amounts) {
151 String amt = amount.toString();
152 if (builder.length() > 0)
153 builder.append('\n');
156 if (included == limit)
160 return builder.toString();
162 public int getLevel() {
166 public String getShortName() {
169 public String getParentName() {
170 return (parent == null) ? null : parent.getName();
172 public boolean hasSubAccounts() {
173 return hasSubAccounts;
175 public void setHasSubAccounts(boolean hasSubAccounts) {
176 this.hasSubAccounts = hasSubAccounts;
178 public boolean isExpanded() {
181 public void setExpanded(boolean expanded) {
182 this.expanded = expanded;
184 public void toggleExpanded() {
185 expanded = !expanded;
187 public void removeAmounts() {
191 public boolean amountsExpanded() { return amountsExpanded; }
192 public void setAmountsExpanded(boolean flag) { amountsExpanded = flag; }
193 public void toggleAmountsExpanded() { amountsExpanded = !amountsExpanded; }
194 public void propagateAmountsTo(LedgerAccount acc) {
195 for (LedgerAmount a : amounts)
196 a.propagateToAccount(acc);
198 public List<LedgerAmount> getAmounts() {
202 public Account toDBO() {
203 Account dbo = new Account();
205 dbo.setNameUpper(name.toUpperCase());
206 dbo.setParentName(extractParentName(name));
209 dbo.setProfileId(profileId);
210 dbo.setExpanded(expanded);
211 dbo.setAmountsExpanded(amountsExpanded);
215 public long getId() {