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