]> git.ktnx.net Git - mobile-ledger-staging.git/blob - app/src/main/java/net/ktnx/mobileledger/json/v1_14/ParsedLedgerTransaction.java
add support for transaction-level comments in the JSON-based parser
[mobile-ledger-staging.git] / app / src / main / java / net / ktnx / mobileledger / json / v1_14 / ParsedLedgerTransaction.java
1 /*
2  * Copyright © 2019 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.json.v1_14;
19
20 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
21
22 import net.ktnx.mobileledger.model.LedgerTransaction;
23 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
24 import net.ktnx.mobileledger.utils.Globals;
25 import net.ktnx.mobileledger.utils.Misc;
26
27 import java.text.ParseException;
28 import java.util.ArrayList;
29 import java.util.Date;
30 import java.util.GregorianCalendar;
31 import java.util.List;
32
33 @JsonIgnoreProperties(ignoreUnknown = true)
34 public class ParsedLedgerTransaction implements net.ktnx.mobileledger.json.ParsedLedgerTransaction {
35     private String tdate;
36     private String tdate2 = null;
37     private String tdescription;
38     private String tcomment;
39     private String tcode = "";
40     private String tstatus = "Unmarked";
41     private String tprecedingcomment = "";
42     private int tindex;
43     private List<ParsedPosting> tpostings;
44     private List<List<String>> ttags = new ArrayList<>();
45     private ParsedSourcePos
46             tsourcepos = new ParsedSourcePos();
47     public ParsedLedgerTransaction() {
48     }
49     public static ParsedLedgerTransaction fromLedgerTransaction(LedgerTransaction tr) {
50         ParsedLedgerTransaction
51                 result = new ParsedLedgerTransaction();
52         result.setTcomment(tr.getComment());
53         result.setTprecedingcomment("");
54
55         ArrayList<ParsedPosting> postings = new ArrayList<>();
56         for (LedgerTransactionAccount acc : tr.getAccounts()) {
57             if (!acc.getAccountName()
58                     .isEmpty())
59                 postings.add(ParsedPosting.fromLedgerAccount(acc));
60         }
61
62         result.setTpostings(postings);
63         Date transactionDate = tr.getDate();
64         if (transactionDate == null) {
65             transactionDate = new GregorianCalendar().getTime();
66         }
67         result.setTdate(Globals.formatIsoDate(transactionDate));
68         result.setTdate2(null);
69         result.setTindex(1);
70         result.setTdescription(tr.getDescription());
71         return result;
72     }
73     public String getTcode() {
74         return tcode;
75     }
76     public void setTcode(String tcode) {
77         this.tcode = tcode;
78     }
79     public String getTstatus() {
80         return tstatus;
81     }
82     public void setTstatus(String tstatus) {
83         this.tstatus = tstatus;
84     }
85     public List<List<String>> getTtags() {
86         return ttags;
87     }
88     public void setTtags(List<List<String>> ttags) {
89         this.ttags = ttags;
90     }
91     public ParsedSourcePos getTsourcepos() {
92         return tsourcepos;
93     }
94     public void setTsourcepos(ParsedSourcePos tsourcepos) {
95         this.tsourcepos = tsourcepos;
96     }
97     public String getTprecedingcomment() {
98         return tprecedingcomment;
99     }
100     public void setTprecedingcomment(String tprecedingcomment) {
101         this.tprecedingcomment = tprecedingcomment;
102     }
103     public String getTdate() {
104         return tdate;
105     }
106     public void setTdate(String tdate) {
107         this.tdate = tdate;
108     }
109     public String getTdate2() {
110         return tdate2;
111     }
112     public void setTdate2(String tdate2) {
113         this.tdate2 = tdate2;
114     }
115     public String getTdescription() {
116         return tdescription;
117     }
118     public void setTdescription(String tdescription) {
119         this.tdescription = tdescription;
120     }
121     public String getTcomment() {
122         return tcomment;
123     }
124     public void setTcomment(String tcomment) {
125         this.tcomment = tcomment;
126     }
127     public int getTindex() {
128         return tindex;
129     }
130     public void setTindex(int tindex) {
131         this.tindex = tindex;
132         if (tpostings != null)
133             for (ParsedPosting p : tpostings) {
134                 p.setPtransaction_(tindex);
135             }
136     }
137     public List<ParsedPosting> getTpostings() {
138         return tpostings;
139     }
140     public void setTpostings(List<ParsedPosting> tpostings) {
141         this.tpostings = tpostings;
142     }
143     public void addPosting(ParsedPosting posting) {
144         posting.setPtransaction_(tindex);
145         tpostings.add(posting);
146     }
147     public LedgerTransaction asLedgerTransaction() throws ParseException {
148         Date date = Globals.parseIsoDate(tdate);
149         LedgerTransaction tr = new LedgerTransaction(tindex, date, tdescription);
150         tr.setComment(Misc.trim(Misc.emptyIsNull(tcomment)));
151
152         List<ParsedPosting> postings = tpostings;
153
154         if (postings != null) {
155             for (ParsedPosting p : postings) {
156                 tr.addAccount(p.asLedgerAccount());
157             }
158         }
159         return tr;
160     }
161 }