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 android.content.res.Resources;
21 import android.graphics.Color;
22 import android.graphics.Typeface;
23 import android.text.SpannableString;
24 import android.text.Spanned;
25 import android.text.style.ForegroundColorSpan;
26 import android.text.style.StyleSpan;
27 import android.text.style.UnderlineSpan;
29 import androidx.annotation.NonNull;
31 import net.ktnx.mobileledger.R;
32 import net.ktnx.mobileledger.db.TemplateAccount;
33 import net.ktnx.mobileledger.db.TemplateBase;
34 import net.ktnx.mobileledger.db.TemplateHeader;
35 import net.ktnx.mobileledger.utils.Logger;
36 import net.ktnx.mobileledger.utils.Misc;
38 import org.jetbrains.annotations.Contract;
39 import org.jetbrains.annotations.NotNull;
41 import java.util.Locale;
42 import java.util.Objects;
43 import java.util.regex.Matcher;
44 import java.util.regex.Pattern;
45 import java.util.regex.PatternSyntaxException;
47 abstract public class TemplateDetailsItem {
48 private final Type type;
50 protected long position;
52 protected TemplateDetailsItem(Type type) {
56 public static @NotNull TemplateDetailsItem.Header createHeader() {
59 public static @NotNull TemplateDetailsItem.Header createHeader(Header origin) {
60 return new Header(origin);
63 public static @NotNull TemplateDetailsItem.AccountRow createAccountRow() {
64 return new AccountRow();
66 public static TemplateDetailsItem fromRoomObject(TemplateBase p) {
67 if (p instanceof TemplateHeader) {
68 TemplateHeader ph = (TemplateHeader) p;
69 Header header = createHeader();
70 header.setId(ph.getId());
71 header.setName(ph.getName());
72 header.setPattern(ph.getRegularExpression());
73 header.setTestText(ph.getTestText());
75 if (ph.getTransactionDescriptionMatchGroup() == null)
76 header.setTransactionDescription(ph.getTransactionDescription());
78 header.setTransactionDescriptionMatchGroup(
79 ph.getTransactionDescriptionMatchGroup());
81 if (ph.getTransactionCommentMatchGroup() == null)
82 header.setTransactionComment(ph.getTransactionComment());
84 header.setTransactionCommentMatchGroup(ph.getTransactionCommentMatchGroup());
86 if (ph.getDateDayMatchGroup() == null)
87 header.setDateDay(ph.getDateDay());
89 header.setDateDayMatchGroup(ph.getDateDayMatchGroup());
91 if (ph.getDateMonthMatchGroup() == null)
92 header.setDateMonth(ph.getDateMonth());
94 header.setDateMonthMatchGroup(ph.getDateMonthMatchGroup());
96 if (ph.getDateYearMatchGroup() == null)
97 header.setDateYear(ph.getDateYear());
99 header.setDateYearMatchGroup(ph.getDateYearMatchGroup());
101 header.setFallback(ph.isFallback());
105 else if (p instanceof TemplateAccount) {
106 TemplateAccount pa = (TemplateAccount) p;
107 AccountRow acc = createAccountRow();
108 acc.setId(pa.getId());
109 acc.setPosition(pa.getPosition());
111 if (pa.getAccountNameMatchGroup() == null)
112 acc.setAccountName(Misc.nullIsEmpty(pa.getAccountName()));
114 acc.setAccountNameMatchGroup(pa.getAccountNameMatchGroup());
116 if (pa.getAccountCommentMatchGroup() == null)
117 acc.setAccountComment(Misc.nullIsEmpty(pa.getAccountComment()));
119 acc.setAccountCommentMatchGroup(pa.getAccountCommentMatchGroup());
121 if (pa.getCurrencyMatchGroup() == null) {
122 acc.setCurrency(pa.getCurrencyObject());
125 acc.setCurrencyMatchGroup(pa.getCurrencyMatchGroup());
127 final Integer amountMatchGroup = pa.getAmountMatchGroup();
128 if (amountMatchGroup != null && amountMatchGroup > 0) {
129 acc.setAmountMatchGroup(amountMatchGroup);
130 final Boolean negateAmount = pa.getNegateAmount();
131 acc.setNegateAmount(negateAmount != null && negateAmount);
134 acc.setAmount(pa.getAmount());
139 throw new IllegalStateException("Unexpected item class " + p.getClass());
142 public Header asHeaderItem() {
143 ensureType(Type.HEADER);
144 return (Header) this;
146 public AccountRow asAccountRowItem() {
147 ensureType(Type.ACCOUNT_ITEM);
148 return (AccountRow) this;
150 private void ensureType(Type type) {
151 if (this.type != type)
152 throw new IllegalStateException(
153 String.format("Type is %s, but %s is required", this.type.toString(),
156 void ensureTrue(boolean flag) {
158 throw new IllegalStateException(
159 "Literal value requested, but it is matched via a pattern group");
161 void ensureFalse(boolean flag) {
163 throw new IllegalStateException("Matching group requested, but the value is a literal");
165 public long getId() {
168 public void setId(Long id) {
171 public void setId(int id) {
174 public long getPosition() {
177 public void setPosition(long position) {
178 this.position = position;
180 abstract public String getProblem(@NonNull Resources r, int patternGroupCount);
181 public Type getType() {
185 HEADER(TYPE.header), ACCOUNT_ITEM(TYPE.accountItem);
195 static class PossiblyMatchedValue<T> {
196 private boolean literalValue;
198 private int matchGroup;
199 public PossiblyMatchedValue() {
203 public PossiblyMatchedValue(@NonNull PossiblyMatchedValue<T> origin) {
204 literalValue = origin.literalValue;
205 value = origin.value;
206 matchGroup = origin.matchGroup;
209 public static PossiblyMatchedValue<Integer> withLiteralInt(Integer initialValue) {
210 PossiblyMatchedValue<Integer> result = new PossiblyMatchedValue<>();
211 result.setValue(initialValue);
215 public static PossiblyMatchedValue<Float> withLiteralFloat(Float initialValue) {
216 PossiblyMatchedValue<Float> result = new PossiblyMatchedValue<>();
217 result.setValue(initialValue);
220 public static PossiblyMatchedValue<Short> withLiteralShort(Short initialValue) {
221 PossiblyMatchedValue<Short> result = new PossiblyMatchedValue<>();
222 result.setValue(initialValue);
226 public static PossiblyMatchedValue<String> withLiteralString(String initialValue) {
227 PossiblyMatchedValue<String> result = new PossiblyMatchedValue<>();
228 result.setValue(initialValue);
231 public void copyFrom(@NonNull PossiblyMatchedValue<T> origin) {
232 literalValue = origin.literalValue;
233 value = origin.value;
234 matchGroup = origin.matchGroup;
236 public T getValue() {
238 throw new IllegalStateException("Value is not literal");
241 public void setValue(T newValue) {
245 public boolean hasLiteralValue() {
248 public int getMatchGroup() {
250 throw new IllegalStateException("Value is literal");
253 public void setMatchGroup(int group) {
254 this.matchGroup = group;
255 literalValue = false;
257 public boolean equals(PossiblyMatchedValue<T> other) {
258 if (!other.literalValue == literalValue)
262 return other.value == null;
263 return value.equals(other.value);
266 return matchGroup == other.matchGroup;
268 public void switchToLiteral() {
271 public String toString() {
276 return value.toString();
278 return "grp:" + matchGroup;
281 public boolean isEmpty() {
283 return value == null || Misc.emptyIsNull(value.toString()) == null;
285 return matchGroup > 0;
289 public static class TYPE {
290 public static final int header = 0;
291 public static final int accountItem = 1;
294 public static class AccountRow extends TemplateDetailsItem {
295 private final PossiblyMatchedValue<String> accountName =
296 PossiblyMatchedValue.withLiteralString("");
297 private final PossiblyMatchedValue<String> accountComment =
298 PossiblyMatchedValue.withLiteralString("");
299 private final PossiblyMatchedValue<Float> amount =
300 PossiblyMatchedValue.withLiteralFloat(null);
301 private final PossiblyMatchedValue<net.ktnx.mobileledger.db.Currency> currency =
302 new PossiblyMatchedValue<>();
303 private boolean negateAmount;
304 public AccountRow() {
305 super(Type.ACCOUNT_ITEM);
307 public AccountRow(AccountRow origin) {
308 super(Type.ACCOUNT_ITEM);
310 position = origin.position;
311 accountName.copyFrom(origin.accountName);
312 accountComment.copyFrom(origin.accountComment);
313 amount.copyFrom(origin.amount);
314 currency.copyFrom(origin.currency);
315 negateAmount = origin.negateAmount;
317 public boolean isNegateAmount() {
320 public void setNegateAmount(boolean negateAmount) {
321 this.negateAmount = negateAmount;
323 public int getAccountCommentMatchGroup() {
324 return accountComment.getMatchGroup();
326 public void setAccountCommentMatchGroup(int group) {
327 accountComment.setMatchGroup(group);
329 public String getAccountComment() {
330 return accountComment.getValue();
332 public void setAccountComment(String comment) {
333 this.accountComment.setValue(comment);
335 public int getCurrencyMatchGroup() {
336 return currency.getMatchGroup();
338 public void setCurrencyMatchGroup(int group) {
339 currency.setMatchGroup(group);
341 public net.ktnx.mobileledger.db.Currency getCurrency() {
342 return currency.getValue();
344 public void setCurrency(net.ktnx.mobileledger.db.Currency currency) {
345 this.currency.setValue(currency);
347 public int getAccountNameMatchGroup() {
348 return accountName.getMatchGroup();
350 public void setAccountNameMatchGroup(int group) {
351 accountName.setMatchGroup(group);
353 public String getAccountName() {
354 return accountName.getValue();
356 public void setAccountName(String accountName) {
357 this.accountName.setValue(accountName);
359 public boolean hasLiteralAccountName() { return accountName.hasLiteralValue(); }
360 public boolean hasLiteralAmount() {
361 return amount.hasLiteralValue();
363 public int getAmountMatchGroup() {
364 return amount.getMatchGroup();
366 public void setAmountMatchGroup(int group) {
367 amount.setMatchGroup(group);
369 public Float getAmount() {
370 return amount.getValue();
372 public void setAmount(Float amount) {
373 this.amount.setValue(amount);
375 public String getProblem(@NonNull Resources r, int patternGroupCount) {
376 if (Misc.emptyIsNull(accountName.getValue()) == null)
377 return r.getString(R.string.account_name_is_empty);
378 if (!amount.hasLiteralValue() &&
379 (amount.getMatchGroup() < 1 || amount.getMatchGroup() > patternGroupCount))
380 return r.getString(R.string.invalid_matching_group_number);
384 public boolean hasLiteralAccountComment() {
385 return accountComment.hasLiteralValue();
387 public boolean equalContents(AccountRow o) {
388 if (position != o.position) {
389 Logger.debug("cmpAcc",
390 String.format(Locale.US, "[%d] != [%d]: pos %d != pos %d", getId(),
391 o.getId(), position, o.position));
394 return amount.equals(o.amount) && accountName.equals(o.accountName) &&
395 position == o.position && accountComment.equals(o.accountComment) &&
396 negateAmount == o.negateAmount;
398 public void switchToLiteralAmount() {
399 amount.switchToLiteral();
401 public void switchToLiteralAccountName() {
402 accountName.switchToLiteral();
404 public void switchToLiteralAccountComment() {
405 accountComment.switchToLiteral();
407 public TemplateAccount toDBO(@NonNull Long patternId) {
408 TemplateAccount result = new TemplateAccount(id, patternId, position);
410 if (accountName.hasLiteralValue())
411 result.setAccountName(accountName.getValue());
413 result.setAccountNameMatchGroup(accountName.getMatchGroup());
415 if (accountComment.hasLiteralValue())
416 result.setAccountComment(accountComment.getValue());
418 result.setAccountCommentMatchGroup(accountComment.getMatchGroup());
420 if (amount.hasLiteralValue()) {
421 result.setAmount(amount.getValue());
422 result.setNegateAmount(null);
425 result.setAmountMatchGroup(amount.getMatchGroup());
426 result.setNegateAmount(negateAmount ? true : null);
431 public boolean isEmpty() {
432 return accountName.isEmpty() && accountComment.isEmpty() && amount.isEmpty();
436 public static class Header extends TemplateDetailsItem {
437 private String pattern = "";
438 private String testText = "";
439 private String name = "";
440 private Pattern compiledPattern;
441 private String patternError;
442 private PossiblyMatchedValue<String> transactionDescription =
443 PossiblyMatchedValue.withLiteralString("");
444 private PossiblyMatchedValue<String> transactionComment =
445 PossiblyMatchedValue.withLiteralString("");
446 private PossiblyMatchedValue<Integer> dateYear = PossiblyMatchedValue.withLiteralInt(null);
447 private PossiblyMatchedValue<Integer> dateMonth = PossiblyMatchedValue.withLiteralInt(null);
448 private PossiblyMatchedValue<Integer> dateDay = PossiblyMatchedValue.withLiteralInt(null);
449 private SpannableString testMatch;
450 private boolean isFallback;
454 public Header(Header origin) {
458 testText = origin.testText;
459 testMatch = origin.testMatch;
460 setPattern(origin.pattern);
462 transactionDescription = new PossiblyMatchedValue<>(origin.transactionDescription);
463 transactionComment = new PossiblyMatchedValue<>(origin.transactionComment);
465 dateYear = new PossiblyMatchedValue<>(origin.dateYear);
466 dateMonth = new PossiblyMatchedValue<>(origin.dateMonth);
467 dateDay = new PossiblyMatchedValue<>(origin.dateDay);
469 isFallback = origin.isFallback;
471 private static StyleSpan capturedSpan() { return new StyleSpan(Typeface.BOLD); }
472 private static UnderlineSpan matchedSpan() { return new UnderlineSpan(); }
473 private static ForegroundColorSpan notMatchedSpan() {
474 return new ForegroundColorSpan(Color.GRAY);
476 public boolean isFallback() {
479 public void setFallback(boolean fallback) {
480 this.isFallback = fallback;
482 public String getName() {
485 public void setName(String name) {
488 public String getPattern() {
491 public void setPattern(String pattern) {
492 this.pattern = pattern;
494 this.compiledPattern = Pattern.compile(pattern);
497 catch (PatternSyntaxException ex) {
498 patternError = ex.getDescription();
499 compiledPattern = null;
501 testMatch = new SpannableString(testText);
502 if (!testText.isEmpty())
503 testMatch.setSpan(notMatchedSpan(), 0, testText.length() - 1,
504 Spanned.SPAN_INCLUSIVE_INCLUSIVE);
509 public String toString() {
510 return super.toString() +
511 String.format(" name[%s] pat[%s] test[%s] tran[%s] com[%s]", name, pattern,
512 testText, transactionDescription, transactionComment);
514 public String getTestText() {
517 public void setTestText(String testText) {
518 this.testText = testText;
522 public String getTransactionDescription() {
523 return transactionDescription.getValue();
525 public void setTransactionDescription(String transactionDescription) {
526 this.transactionDescription.setValue(transactionDescription);
528 public String getTransactionComment() {
529 return transactionComment.getValue();
531 public void setTransactionComment(String transactionComment) {
532 this.transactionComment.setValue(transactionComment);
534 public Integer getDateYear() {
535 return dateYear.getValue();
537 public void setDateYear(Integer dateYear) {
538 this.dateYear.setValue(dateYear);
540 public Integer getDateMonth() {
541 return dateMonth.getValue();
543 public void setDateMonth(Integer dateMonth) {
544 this.dateMonth.setValue(dateMonth);
546 public Integer getDateDay() {
547 return dateDay.getValue();
549 public void setDateDay(Integer dateDay) {
550 this.dateDay.setValue(dateDay);
552 public int getDateYearMatchGroup() {
553 return dateYear.getMatchGroup();
555 public void setDateYearMatchGroup(int dateYearMatchGroup) {
556 this.dateYear.setMatchGroup(dateYearMatchGroup);
558 public int getDateMonthMatchGroup() {
559 return dateMonth.getMatchGroup();
561 public void setDateMonthMatchGroup(int dateMonthMatchGroup) {
562 this.dateMonth.setMatchGroup(dateMonthMatchGroup);
564 public int getDateDayMatchGroup() {
565 return dateDay.getMatchGroup();
567 public void setDateDayMatchGroup(int dateDayMatchGroup) {
568 this.dateDay.setMatchGroup(dateDayMatchGroup);
570 public boolean hasLiteralDateYear() {
571 return dateYear.hasLiteralValue();
573 public boolean hasLiteralDateMonth() {
574 return dateMonth.hasLiteralValue();
576 public boolean hasLiteralDateDay() {
577 return dateDay.hasLiteralValue();
579 public boolean hasLiteralTransactionDescription() { return transactionDescription.hasLiteralValue(); }
580 public boolean hasLiteralTransactionComment() { return transactionComment.hasLiteralValue(); }
581 public String getProblem(@NonNull Resources r, int patternGroupCount) {
582 if (patternError != null)
583 return r.getString(R.string.pattern_has_errors) + ": " + patternError;
584 if (Misc.emptyIsNull(pattern) == null)
585 return r.getString(R.string.pattern_is_empty);
587 if (!dateYear.hasLiteralValue() && compiledPattern != null &&
588 (dateDay.getMatchGroup() < 1 || dateDay.getMatchGroup() > patternGroupCount))
589 return r.getString(R.string.invalid_matching_group_number);
591 if (!dateMonth.hasLiteralValue() && compiledPattern != null &&
592 (dateMonth.getMatchGroup() < 1 || dateMonth.getMatchGroup() > patternGroupCount))
593 return r.getString(R.string.invalid_matching_group_number);
595 if (!dateDay.hasLiteralValue() && compiledPattern != null &&
596 (dateDay.getMatchGroup() < 1 || dateDay.getMatchGroup() > patternGroupCount))
597 return r.getString(R.string.invalid_matching_group_number);
602 public boolean equalContents(Header o) {
603 if (!dateDay.equals(o.dateDay))
605 if (!dateMonth.equals(o.dateMonth))
607 if (!dateYear.equals(o.dateYear))
609 if (!transactionDescription.equals(o.transactionDescription))
611 if (!transactionComment.equals(o.transactionComment))
614 return Misc.equalStrings(name, o.name) && Misc.equalStrings(pattern, o.pattern) &&
615 Misc.equalStrings(testText, o.testText) &&
616 Misc.equalStrings(patternError, o.patternError) &&
617 Objects.equals(testMatch, o.testMatch) && isFallback == o.isFallback;
619 public String getMatchGroupText(int group) {
620 if (compiledPattern != null && testText != null) {
621 Matcher m = compiledPattern.matcher(testText);
623 return m.group(group);
628 public Pattern getCompiledPattern() {
629 return compiledPattern;
631 public void switchToLiteralTransactionDescription() {
632 transactionDescription.switchToLiteral();
634 public void switchToLiteralTransactionComment() {
635 transactionComment.switchToLiteral();
637 public int getTransactionDescriptionMatchGroup() {
638 return transactionDescription.getMatchGroup();
640 public void setTransactionDescriptionMatchGroup(int group) {
641 transactionDescription.setMatchGroup(group);
643 public int getTransactionCommentMatchGroup() {
644 return transactionComment.getMatchGroup();
646 public void setTransactionCommentMatchGroup(int group) {
647 transactionComment.setMatchGroup(group);
649 public void switchToLiteralDateYear() {
650 dateYear.switchToLiteral();
652 public void switchToLiteralDateMonth() {
653 dateMonth.switchToLiteral();
655 public void switchToLiteralDateDay() { dateDay.switchToLiteral(); }
656 public TemplateHeader toDBO() {
657 TemplateHeader result = new TemplateHeader(id, name, pattern);
659 if (Misc.emptyIsNull(testText) != null)
660 result.setTestText(testText);
662 if (transactionDescription.hasLiteralValue())
663 result.setTransactionDescription(transactionDescription.getValue());
665 result.setTransactionDescriptionMatchGroup(transactionDescription.getMatchGroup());
667 if (transactionComment.hasLiteralValue())
668 result.setTransactionComment(transactionComment.getValue());
670 result.setTransactionCommentMatchGroup(transactionComment.getMatchGroup());
672 if (dateYear.hasLiteralValue())
673 result.setDateYear(dateYear.getValue());
675 result.setDateYearMatchGroup(dateYear.getMatchGroup());
677 if (dateMonth.hasLiteralValue())
678 result.setDateMonth(dateMonth.getValue());
680 result.setDateMonthMatchGroup(dateMonth.getMatchGroup());
682 if (dateDay.hasLiteralValue())
683 result.setDateDay(dateDay.getValue());
685 result.setDateDayMatchGroup(dateDay.getMatchGroup());
687 result.setFallback(isFallback);
691 public SpannableString getTestMatch() {
694 public void checkPatternMatch() {
698 if (pattern != null) {
700 if (Misc.emptyIsNull(testText) != null) {
701 SpannableString ss = new SpannableString(testText);
702 Matcher m = compiledPattern.matcher(testText);
705 ss.setSpan(notMatchedSpan(), 0, m.start(),
706 Spanned.SPAN_INCLUSIVE_INCLUSIVE);
707 if (m.end() < testText.length() - 1)
708 ss.setSpan(notMatchedSpan(), m.end(), testText.length(),
709 Spanned.SPAN_INCLUSIVE_INCLUSIVE);
711 ss.setSpan(matchedSpan(), m.start(0), m.end(0),
712 Spanned.SPAN_INCLUSIVE_INCLUSIVE);
714 if (m.groupCount() > 0) {
715 for (int g = 1; g <= m.groupCount(); g++) {
716 ss.setSpan(capturedSpan(), m.start(g), m.end(g),
717 Spanned.SPAN_INCLUSIVE_INCLUSIVE);
722 patternError = "Pattern does not match";
723 ss.setSpan(new ForegroundColorSpan(Color.GRAY), 0,
724 testText.length() - 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
730 catch (PatternSyntaxException e) {
731 this.compiledPattern = null;
732 this.patternError = e.getMessage();
736 patternError = "Missing pattern";
739 public String getPatternError() {
742 public SpannableString testMatch() {