From eccd11f1895e84dcc2a95db41934355311cebe2e Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Thu, 7 Mar 2019 05:36:36 +0200 Subject: [PATCH] parsers for account and transaction lists (hledger-web JSON API) --- .../mobileledger/json/AccountListParser.java | 40 +++++++++ .../ktnx/mobileledger/json/ParsedAmount.java | 55 ++++++++++++ .../ktnx/mobileledger/json/ParsedBalance.java | 47 ++++++++++ .../json/ParsedLedgerAccount.java | 43 ++++++++++ .../json/ParsedLedgerTransaction.java | 85 +++++++++++++++++++ .../ktnx/mobileledger/json/ParsedPosting.java | 49 +++++++++++ .../mobileledger/json/ParsedQuantity.java | 43 ++++++++++ .../ktnx/mobileledger/json/ParsedStyle.java | 61 +++++++++++++ .../json/TransactionListParser.java | 40 +++++++++ 9 files changed, 463 insertions(+) create mode 100644 app/src/main/java/net/ktnx/mobileledger/json/AccountListParser.java create mode 100644 app/src/main/java/net/ktnx/mobileledger/json/ParsedAmount.java create mode 100644 app/src/main/java/net/ktnx/mobileledger/json/ParsedBalance.java create mode 100644 app/src/main/java/net/ktnx/mobileledger/json/ParsedLedgerAccount.java create mode 100644 app/src/main/java/net/ktnx/mobileledger/json/ParsedLedgerTransaction.java create mode 100644 app/src/main/java/net/ktnx/mobileledger/json/ParsedPosting.java create mode 100644 app/src/main/java/net/ktnx/mobileledger/json/ParsedQuantity.java create mode 100644 app/src/main/java/net/ktnx/mobileledger/json/ParsedStyle.java create mode 100644 app/src/main/java/net/ktnx/mobileledger/json/TransactionListParser.java diff --git a/app/src/main/java/net/ktnx/mobileledger/json/AccountListParser.java b/app/src/main/java/net/ktnx/mobileledger/json/AccountListParser.java new file mode 100644 index 00000000..4dd2e375 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/AccountListParser.java @@ -0,0 +1,40 @@ +/* + * 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 com.fasterxml.jackson.databind.MappingIterator; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectReader; + +import java.io.IOException; +import java.io.InputStream; + +public class AccountListParser { + + private final MappingIterator iter; + + public AccountListParser(InputStream input) throws IOException { + ObjectMapper mapper = new ObjectMapper(); + ObjectReader reader = mapper.readerFor(ParsedLedgerAccount.class); + + iter = reader.readValues(input); + } + public ParsedLedgerAccount nextAccount() throws IOException { + return iter.hasNext() ? iter.next() : null; + } +} diff --git a/app/src/main/java/net/ktnx/mobileledger/json/ParsedAmount.java b/app/src/main/java/net/ktnx/mobileledger/json/ParsedAmount.java new file mode 100644 index 00000000..0a8b8c85 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/ParsedAmount.java @@ -0,0 +1,55 @@ +/* + * 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 com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class ParsedAmount { + private String acommodity; + private ParsedQuantity aquantity; + private boolean aismultiplier; + private ParsedStyle astyle; + public ParsedAmount() { + } + public String getAcommodity() { + return acommodity; + } + public void setAcommodity(String acommodity) { + this.acommodity = acommodity; + } + public ParsedQuantity getAquantity() { + return aquantity; + } + public void setAquantity(ParsedQuantity aquantity) { + this.aquantity = aquantity; + } + public boolean isAismultiplier() { + return aismultiplier; + } + public void setAismultiplier(boolean aismultiplier) { + this.aismultiplier = aismultiplier; + } + public ParsedStyle getAstyle() { + return astyle; + } + public void setAstyle(ParsedStyle astyle) { + this.astyle = astyle; + } + +} diff --git a/app/src/main/java/net/ktnx/mobileledger/json/ParsedBalance.java b/app/src/main/java/net/ktnx/mobileledger/json/ParsedBalance.java new file mode 100644 index 00000000..63a0e05c --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/ParsedBalance.java @@ -0,0 +1,47 @@ +/* + * 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 com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class ParsedBalance { + private ParsedQuantity aquantity; + private String acommodity; + private ParsedStyle astyle; + public ParsedBalance() { + } + public ParsedQuantity getAquantity() { + return aquantity; + } + public void setAquantity(ParsedQuantity aquantity) { + this.aquantity = aquantity; + } + public String getAcommodity() { + return acommodity; + } + public void setAcommodity(String acommodity) { + this.acommodity = acommodity; + } + public ParsedStyle getAstyle() { + return astyle; + } + public void setAstyle(ParsedStyle astyle) { + this.astyle = astyle; + } +} diff --git a/app/src/main/java/net/ktnx/mobileledger/json/ParsedLedgerAccount.java b/app/src/main/java/net/ktnx/mobileledger/json/ParsedLedgerAccount.java new file mode 100644 index 00000000..dd19e27d --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/ParsedLedgerAccount.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 com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +import java.util.List; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class ParsedLedgerAccount { + private List aebalance; + private String aname; + public ParsedLedgerAccount() { + } + public List getAebalance() { + return aebalance; + } + public void setAebalance(List aebalance) { + this.aebalance = aebalance; + } + public String getAname() { + return aname; + } + public void setAname(String aname) { + this.aname = aname; + } + +} diff --git a/app/src/main/java/net/ktnx/mobileledger/json/ParsedLedgerTransaction.java b/app/src/main/java/net/ktnx/mobileledger/json/ParsedLedgerTransaction.java new file mode 100644 index 00000000..62cf1d0a --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/ParsedLedgerTransaction.java @@ -0,0 +1,85 @@ +/* + * 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 com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +import net.ktnx.mobileledger.model.LedgerTransaction; +import net.ktnx.mobileledger.utils.Globals; + +import java.text.ParseException; +import java.util.Date; +import java.util.List; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class ParsedLedgerTransaction { + private String tdate, tdate2, tdescription, tcomment; + private int tindex; + private List tpostings; + public ParsedLedgerTransaction() { + } + public String getTdate() { + return tdate; + } + public void setTdate(String tdate) { + this.tdate = tdate; + } + public String getTdate2() { + return tdate2; + } + public void setTdate2(String tdate2) { + this.tdate2 = tdate2; + } + public String getTdescription() { + return tdescription; + } + public void setTdescription(String tdescription) { + this.tdescription = tdescription; + } + public String getTcomment() { + return tcomment; + } + public void setTcomment(String tcomment) { + this.tcomment = tcomment; + } + public int getTindex() { + return tindex; + } + public void setTindex(int tindex) { + this.tindex = tindex; + } + public List getTpostings() { + return tpostings; + } + public void setTpostings(List tpostings) { + this.tpostings = tpostings; + } + public LedgerTransaction asLedgerTransaction() throws ParseException { + Date date = Globals.parseIsoDate(tdate); + LedgerTransaction tr = new LedgerTransaction(tindex, date, tdescription); + + List postings = tpostings; + + if (postings != null) { + for (ParsedPosting p : postings) { + tr.addAccount(p.asLedgerAccount()); + } + } + return tr; + } +} diff --git a/app/src/main/java/net/ktnx/mobileledger/json/ParsedPosting.java b/app/src/main/java/net/ktnx/mobileledger/json/ParsedPosting.java new file mode 100644 index 00000000..ad2e981c --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/ParsedPosting.java @@ -0,0 +1,49 @@ +/* + * 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 com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +import net.ktnx.mobileledger.model.LedgerTransactionAccount; + +import java.util.List; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class ParsedPosting { + private String paccount; + private List pamount; + public ParsedPosting() { + } + public String getPaccount() { + return paccount; + } + public void setPaccount(String paccount) { + this.paccount = paccount; + } + public List getPamount() { + return pamount; + } + public void setPamount(List pamount) { + this.pamount = pamount; + } + public LedgerTransactionAccount asLedgerAccount() { + ParsedAmount amt = pamount.get(0); + return new LedgerTransactionAccount(paccount, amt.getAquantity().asFloat(), + amt.getAcommodity()); + } +} diff --git a/app/src/main/java/net/ktnx/mobileledger/json/ParsedQuantity.java b/app/src/main/java/net/ktnx/mobileledger/json/ParsedQuantity.java new file mode 100644 index 00000000..0dfc583f --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/ParsedQuantity.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 com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class ParsedQuantity { + private long decimalMantissa; + private int decimalPlaces; + public ParsedQuantity() { + } + public long getDecimalMantissa() { + return decimalMantissa; + } + public void setDecimalMantissa(long decimalMantissa) { + this.decimalMantissa = decimalMantissa; + } + public int getDecimalPlaces() { + return decimalPlaces; + } + public void setDecimalPlaces(int decimalPlaces) { + this.decimalPlaces = decimalPlaces; + } + public float asFloat() { + return (float) (decimalMantissa * Math.pow(10, -decimalPlaces)); + } +} diff --git a/app/src/main/java/net/ktnx/mobileledger/json/ParsedStyle.java b/app/src/main/java/net/ktnx/mobileledger/json/ParsedStyle.java new file mode 100644 index 00000000..d0aaa45a --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/ParsedStyle.java @@ -0,0 +1,61 @@ +/* + * 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 com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class ParsedStyle { + private int asprecision; + private char asdecimalpoint; + private char ascommodityside; + private int digitgroups; + private boolean ascommodityspaced; + public ParsedStyle() { + } + public int getAsprecision() { + return asprecision; + } + public void setAsprecision(int asprecision) { + this.asprecision = asprecision; + } + public char getAsdecimalpoint() { + return asdecimalpoint; + } + public void setAsdecimalpoint(char asdecimalpoint) { + this.asdecimalpoint = asdecimalpoint; + } + public char getAscommodityside() { + return ascommodityside; + } + public void setAscommodityside(char ascommodityside) { + this.ascommodityside = ascommodityside; + } + public int getDigitgroups() { + return digitgroups; + } + public void setDigitgroups(int digitgroups) { + this.digitgroups = digitgroups; + } + public boolean isAscommodityspaced() { + return ascommodityspaced; + } + public void setAscommodityspaced(boolean ascommodityspaced) { + this.ascommodityspaced = ascommodityspaced; + } +} diff --git a/app/src/main/java/net/ktnx/mobileledger/json/TransactionListParser.java b/app/src/main/java/net/ktnx/mobileledger/json/TransactionListParser.java new file mode 100644 index 00000000..fc17df66 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/TransactionListParser.java @@ -0,0 +1,40 @@ +/* + * 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 com.fasterxml.jackson.databind.MappingIterator; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectReader; + +import java.io.IOException; +import java.io.InputStream; + +public class TransactionListParser { + + private final MappingIterator iter; + + public TransactionListParser(InputStream input) throws IOException { + + ObjectMapper mapper = new ObjectMapper(); + ObjectReader reader = mapper.readerFor(ParsedLedgerTransaction.class); + iter = reader.readValues(input); + } + public ParsedLedgerTransaction nextTransaction() { + return iter.hasNext() ? iter.next() : null; + } +} -- 2.39.2