From: Damyan Ivanov Date: Thu, 4 Apr 2019 04:10:42 +0000 (+0300) Subject: methods for converting of run-time data structures to json-serializable ones X-Git-Tag: v0.9~18 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=d7feaff3ca4490495ca721b29b77c1646591a34c;p=mobile-ledger.git methods for converting of run-time data structures to json-serializable ones --- diff --git a/app/src/main/java/net/ktnx/mobileledger/json/ParsedAmount.java b/app/src/main/java/net/ktnx/mobileledger/json/ParsedAmount.java index 0a8b8c85..4d38c55b 100644 --- a/app/src/main/java/net/ktnx/mobileledger/json/ParsedAmount.java +++ b/app/src/main/java/net/ktnx/mobileledger/json/ParsedAmount.java @@ -25,8 +25,15 @@ public class ParsedAmount { private ParsedQuantity aquantity; private boolean aismultiplier; private ParsedStyle astyle; + private ParsedPrice aprice = new ParsedPrice(); public ParsedAmount() { } + public ParsedPrice getAprice() { + return aprice; + } + public void setAprice(ParsedPrice aprice) { + this.aprice = aprice; + } public String getAcommodity() { return acommodity; } diff --git a/app/src/main/java/net/ktnx/mobileledger/json/ParsedLedgerTransaction.java b/app/src/main/java/net/ktnx/mobileledger/json/ParsedLedgerTransaction.java index 62cf1d0a..8743780c 100644 --- a/app/src/main/java/net/ktnx/mobileledger/json/ParsedLedgerTransaction.java +++ b/app/src/main/java/net/ktnx/mobileledger/json/ParsedLedgerTransaction.java @@ -23,16 +23,55 @@ import net.ktnx.mobileledger.model.LedgerTransaction; import net.ktnx.mobileledger.utils.Globals; import java.text.ParseException; +import java.util.ArrayList; import java.util.Date; import java.util.List; @JsonIgnoreProperties(ignoreUnknown = true) public class ParsedLedgerTransaction { - private String tdate, tdate2, tdescription, tcomment; + private String tdate; + private String tdate2; + private String tdescription; + private String tcomment; + private String tcode = ""; + private String tstatus = "Unmarked"; + private String tprecedingcomment; private int tindex; private List tpostings; + private List ttags = new ArrayList<>(); + private ParsedSourcePos tsourcepos = new ParsedSourcePos(); public ParsedLedgerTransaction() { } + public String getTcode() { + return tcode; + } + public void setTcode(String tcode) { + this.tcode = tcode; + } + public String getTstatus() { + return tstatus; + } + public void setTstatus(String tstatus) { + this.tstatus = tstatus; + } + public List getTtags() { + return ttags; + } + public void setTtags(List ttags) { + this.ttags = ttags; + } + public ParsedSourcePos getTsourcepos() { + return tsourcepos; + } + public void setTsourcepos(ParsedSourcePos tsourcepos) { + this.tsourcepos = tsourcepos; + } + public String getTprecedingcomment() { + return tprecedingcomment; + } + public void setTprecedingcomment(String tprecedingcomment) { + this.tprecedingcomment = tprecedingcomment; + } public String getTdate() { return tdate; } diff --git a/app/src/main/java/net/ktnx/mobileledger/json/ParsedPosting.java b/app/src/main/java/net/ktnx/mobileledger/json/ParsedPosting.java index ad2e981c..92de52d7 100644 --- a/app/src/main/java/net/ktnx/mobileledger/json/ParsedPosting.java +++ b/app/src/main/java/net/ktnx/mobileledger/json/ParsedPosting.java @@ -21,14 +21,64 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import net.ktnx.mobileledger.model.LedgerTransactionAccount; +import java.util.ArrayList; import java.util.List; @JsonIgnoreProperties(ignoreUnknown = true) public class ParsedPosting { + private Void pbalanceassertion; + private String pstatus = "Unmarked"; private String paccount; private List pamount; + private String pdate = null; + private String ptype = "RegularPosting"; + private String pcomment = ""; + private ArrayList ptags = new ArrayList<>(); + private String poriginal = null; public ParsedPosting() { } + public String getPdate() { + return pdate; + } + public void setPdate(String pdate) { + this.pdate = pdate; + } + public String getPtype() { + return ptype; + } + public void setPtype(String ptype) { + this.ptype = ptype; + } + public String getPcomment() { + return pcomment; + } + public void setPcomment(String pcomment) { + this.pcomment = pcomment; + } + public ArrayList getPtags() { + return ptags; + } + public void setPtags(ArrayList ptags) { + this.ptags = ptags; + } + public String getPoriginal() { + return poriginal; + } + public void setPoriginal(String poriginal) { + this.poriginal = poriginal; + } + public String getPstatus() { + return pstatus; + } + public void setPstatus(String pstatus) { + this.pstatus = pstatus; + } + public Void getPbalanceassertion() { + return pbalanceassertion; + } + public void setPbalanceassertion(Void pbalanceassertion) { + this.pbalanceassertion = pbalanceassertion; + } public String getPaccount() { return paccount; } diff --git a/app/src/main/java/net/ktnx/mobileledger/json/ParsedPrice.java b/app/src/main/java/net/ktnx/mobileledger/json/ParsedPrice.java new file mode 100644 index 00000000..3ff6bde8 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/ParsedPrice.java @@ -0,0 +1,31 @@ +/* + * Copyright © 2019 Damyan Ivanov. + * This file is part of MoLe. + * MoLe is free software: you can distribute it and/or modify it + * under the term of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your opinion), any later version. + * + * MoLe is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License terms for details. + * + * You should have received a copy of the GNU General Public License + * along with MoLe. If not, see . + */ + +package net.ktnx.mobileledger.json; + +class ParsedPrice { + private String tag; + public ParsedPrice() { + tag = "NoPrice"; + } + public String getTag() { + return tag; + } + public void setTag(String tag) { + this.tag = tag; + } +} diff --git a/app/src/main/java/net/ktnx/mobileledger/json/ParsedSourcePos.java b/app/src/main/java/net/ktnx/mobileledger/json/ParsedSourcePos.java new file mode 100644 index 00000000..fa8dfb94 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/ParsedSourcePos.java @@ -0,0 +1,43 @@ +/* + * Copyright © 2019 Damyan Ivanov. + * This file is part of MoLe. + * MoLe is free software: you can distribute it and/or modify it + * under the term of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your opinion), any later version. + * + * MoLe is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License terms for details. + * + * You should have received a copy of the GNU General Public License + * along with MoLe. If not, see . + */ + +package net.ktnx.mobileledger.json; + +import java.util.ArrayList; +import java.util.List; + +class ParsedSourcePos { + private String tag = "JournalSourcePos"; + private List contents; + public ParsedSourcePos() { + contents = new ArrayList<>(); + contents.add(""); + contents.add(new Integer[]{1, 1}); + } + public String getTag() { + return tag; + } + public void setTag(String tag) { + this.tag = tag; + } + public List getContents() { + return contents; + } + public void setContents(List contents) { + this.contents = contents; + } +} diff --git a/app/src/main/java/net/ktnx/mobileledger/model/LedgerTransaction.java b/app/src/main/java/net/ktnx/mobileledger/model/LedgerTransaction.java index fd2a8407..689d619a 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/LedgerTransaction.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/LedgerTransaction.java @@ -21,6 +21,8 @@ import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.util.Log; +import net.ktnx.mobileledger.json.ParsedLedgerTransaction; +import net.ktnx.mobileledger.json.ParsedPosting; import net.ktnx.mobileledger.utils.Digest; import net.ktnx.mobileledger.utils.Globals; @@ -186,4 +188,21 @@ public class LedgerTransaction { public void finishLoading() { dataLoaded = true; } + public ParsedLedgerTransaction toParsedLedgerTransaction() { + ParsedLedgerTransaction result = new ParsedLedgerTransaction(); + result.setTcomment(""); + result.setTprecedingcomment(""); + + ArrayList postings = new ArrayList<>(); + for (LedgerTransactionAccount acc : accounts) { + if (!acc.getAccountName().isEmpty()) postings.add(acc.asParsedPosting()); + } + + result.setTpostings(postings); + result.setTdate(Globals.formatIsoDate(date)); + result.setTdate2(null); + result.setTindex(1); + result.setTdescription(description); + return result; + } } diff --git a/app/src/main/java/net/ktnx/mobileledger/model/LedgerTransactionAccount.java b/app/src/main/java/net/ktnx/mobileledger/model/LedgerTransactionAccount.java index ad6c94c2..6f8c3b44 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/LedgerTransactionAccount.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/LedgerTransactionAccount.java @@ -17,6 +17,13 @@ package net.ktnx.mobileledger.model; +import net.ktnx.mobileledger.json.ParsedAmount; +import net.ktnx.mobileledger.json.ParsedPosting; +import net.ktnx.mobileledger.json.ParsedQuantity; +import net.ktnx.mobileledger.json.ParsedStyle; + +import java.util.ArrayList; + import androidx.annotation.NonNull; public class LedgerTransactionAccount { @@ -85,4 +92,25 @@ public class LedgerTransactionAccount { return sb.toString(); } + public ParsedPosting asParsedPosting() { + ParsedPosting result = new ParsedPosting(); + result.setPaccount(accountName); + ArrayList amounts = new ArrayList<>(); + ParsedAmount amt = new ParsedAmount(); + amt.setAcommodity((currency == null) ? "" : currency); + amt.setAismultiplier(false); + ParsedQuantity qty = new ParsedQuantity(); + qty.setDecimalPlaces(2); + qty.setDecimalMantissa(Math.round(amount * 100)); + amt.setAquantity(qty); + ParsedStyle style = new ParsedStyle(); + style.setAscommodityside('L'); + style.setAscommodityspaced(false); + style.setAsprecision(2); + style.setAsdecimalpoint('.'); + amt.setAstyle(style); + amounts.add(amt); + result.setPamount(amounts); + return result; + } }