From: Damyan Ivanov Date: Sat, 21 Dec 2019 21:22:14 +0000 (+0200) Subject: support both 1.14 and 1.15 JSON APIs X-Git-Tag: v0.12.0~140 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=54002a662d97289a739d3cdb9888bbab58a8064f;p=mobile-ledger.git support both 1.14 and 1.15 JSON APIs this is a major and somewhat messy rework the idea is that several APIs should be able to co-exist conversion between MoLe's internal structures and the JSON API is moved to the version-dependent JSON representations an 'auto' setting for the version (the default) attempts all available API versions starting from the latest HTTP status 400 is interpreted as "version mismatch" (same as 405) --- diff --git a/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java b/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java index 0354e5ed..5aebe219 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java @@ -26,11 +26,11 @@ import androidx.annotation.NonNull; import net.ktnx.mobileledger.App; import net.ktnx.mobileledger.err.HTTPException; -import net.ktnx.mobileledger.json.AccountListParser; -import net.ktnx.mobileledger.json.ParsedBalance; -import net.ktnx.mobileledger.json.ParsedLedgerAccount; -import net.ktnx.mobileledger.json.ParsedLedgerTransaction; -import net.ktnx.mobileledger.json.TransactionListParser; +import net.ktnx.mobileledger.json.v1_15.AccountListParser; +import net.ktnx.mobileledger.json.v1_15.ParsedBalance; +import net.ktnx.mobileledger.json.v1_15.ParsedLedgerAccount; +import net.ktnx.mobileledger.json.v1_15.ParsedLedgerTransaction; +import net.ktnx.mobileledger.json.v1_15.TransactionListParser; import net.ktnx.mobileledger.model.Data; import net.ktnx.mobileledger.model.LedgerAccount; import net.ktnx.mobileledger.model.LedgerTransaction; diff --git a/app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java b/app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java index 496624bf..5170b933 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java @@ -17,13 +17,15 @@ package net.ktnx.mobileledger.async; +import android.content.res.Resources; import android.os.AsyncTask; import android.util.Log; +import android.util.SparseArray; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectWriter; -import net.ktnx.mobileledger.json.ParsedLedgerTransaction; +import net.ktnx.mobileledger.R; import net.ktnx.mobileledger.model.LedgerTransaction; import net.ktnx.mobileledger.model.LedgerTransactionAccount; import net.ktnx.mobileledger.model.MobileLedgerProfile; @@ -70,18 +72,37 @@ public class SendTransactionTask extends AsyncTask= 2) - throw new IOException(String.format("aborting after %d tries", tried)); - sleep(100); - } + switch (mProfile.getApiVersion()) { + case auto: + Logger.debug("network", "Trying version 1.5."); + if (!send_1_15_OK()) { + Logger.debug("network", "Version 1.5 request failed. Trying with 1.14"); + if (!send_1_14_OK()) { + Logger.debug("network", "Version 1.14 failed too. Trying HTML form emulation"); + legacySendOkWithRetry(); + } + else { + Logger.debug("network", "Version 1.14 request succeeded"); + } + } + else { + Logger.debug("network", "Version 1.15 request succeeded"); + } + break; + case html: + legacySendOkWithRetry(); + break; + case pre_1_15: + send_1_14_OK(); + break; + case post_1_14: + send_1_15_OK(); + break; + default: + throw new IllegalStateException( + "Unexpected API version: " + mProfile.getApiVersion()); } } catch (Exception e) { @@ -251,10 +293,56 @@ public class SendTransactionTask extends AsyncTask= 2) + throw new IOException( + String.format("aborting after %d tries", tried)); + sleep(100); + } + } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); taskCallback.done(error); } + + public enum API { + auto(0), html(-1), pre_1_15(-2), post_1_14(-3); + private static SparseArray map = new SparseArray<>(); + + static { + for (API item : API.values()) { + map.put(item.value, item); + } + } + + private int value; + + API(int value) { + this.value = value; + } + public static API valueOf(int i) { + return map.get(i, auto); + } + public int toInt() { + return this.value; + } + public String getDescription(Resources resources) { + switch (this) { + case auto: + return resources.getString(R.string.api_auto); + case html: + return resources.getString(R.string.api_html); + case pre_1_15: + return resources.getString(R.string.api_pre_1_15); + case post_1_14: + return resources.getString(R.string.api_post_1_14); + default: + throw new IllegalStateException("Unexpected value: " + value); + } + } + } } diff --git a/app/src/main/java/net/ktnx/mobileledger/json/AccountListParser.java b/app/src/main/java/net/ktnx/mobileledger/json/AccountListParser.java deleted file mode 100644 index a21fc616..00000000 --- a/app/src/main/java/net/ktnx/mobileledger/json/AccountListParser.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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; - -import static net.ktnx.mobileledger.utils.Logger.debug; - -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 { - if (!iter.hasNext()) return null; - - ParsedLedgerAccount next = iter.next(); - - if (next.getAname().equalsIgnoreCase("root")) return nextAccount(); - - debug("accounts", String.format("Got account '%s'", next.getAname())); - return next; - } -} diff --git a/app/src/main/java/net/ktnx/mobileledger/json/ParsedAmount.java b/app/src/main/java/net/ktnx/mobileledger/json/ParsedAmount.java deleted file mode 100644 index e66258b1..00000000 --- a/app/src/main/java/net/ktnx/mobileledger/json/ParsedAmount.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * 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; - private ParsedPrice aprice; - public ParsedAmount() { - } - public ParsedPrice getAprice() { - return aprice; - } - public void setAprice(ParsedPrice aprice) { - this.aprice = aprice; - } - 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 deleted file mode 100644 index a355bbdf..00000000 --- a/app/src/main/java/net/ktnx/mobileledger/json/ParsedBalance.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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 androidx.annotation.NonNull; - -@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; - } - @NonNull - public String getAcommodity() { - return (acommodity == null) ? "" : 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 deleted file mode 100644 index d155c2bb..00000000 --- a/app/src/main/java/net/ktnx/mobileledger/json/ParsedLedgerAccount.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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 List aibalance; - private String aname; - public ParsedLedgerAccount() { - } - public List getAebalance() { - return aebalance; - } - public List getAibalance() { - return aibalance; - } - public void setAebalance(List aebalance) { - this.aebalance = aebalance; - } - public void setAibalance(List aibalance) { - this.aibalance = aibalance; - } - 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 index 7ba1421c..e649b39c 100644 --- a/app/src/main/java/net/ktnx/mobileledger/json/ParsedLedgerTransaction.java +++ b/app/src/main/java/net/ktnx/mobileledger/json/ParsedLedgerTransaction.java @@ -17,116 +17,10 @@ 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.ArrayList; -import java.util.Date; -import java.util.List; - -@JsonIgnoreProperties(ignoreUnknown = true) -public class ParsedLedgerTransaction { - private String tdate; - private String tdate2 = null; - 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; - } - 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; - if (tpostings != null) - for (ParsedPosting p : tpostings) { - p.setPtransaction_(tindex); - } - } - public List getTpostings() { - return tpostings; - } - public void setTpostings(List tpostings) { - this.tpostings = tpostings; - } - public void addPosting(ParsedPosting posting) { - posting.setPtransaction_(tindex); - tpostings.add(posting); - } - 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; - } +public interface ParsedLedgerTransaction { + LedgerTransaction asLedgerTransaction() throws ParseException; } diff --git a/app/src/main/java/net/ktnx/mobileledger/json/ParsedPosting.java b/app/src/main/java/net/ktnx/mobileledger/json/ParsedPosting.java deleted file mode 100644 index 308f01f4..00000000 --- a/app/src/main/java/net/ktnx/mobileledger/json/ParsedPosting.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * 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.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; - public String getPdate2() { - return pdate2; - } - public void setPdate2(String pdate2) { - this.pdate2 = pdate2; - } - private String pdate2 = null; - private String ptype = "RegularPosting"; - private String pcomment = ""; - private List> ptags = new ArrayList<>(); - private String poriginal = null; - private int ptransaction_; - public int getPtransaction_() { - return ptransaction_; - } - public void setPtransaction_(int ptransaction_) { - this.ptransaction_ = ptransaction_; - } - 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 List> getPtags() { - return ptags; - } - public void setPtags(List> 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; - } - 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/ParsedPrice.java b/app/src/main/java/net/ktnx/mobileledger/json/ParsedPrice.java deleted file mode 100644 index fe8154cd..00000000 --- a/app/src/main/java/net/ktnx/mobileledger/json/ParsedPrice.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * 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; - private Contents contents; - public ParsedPrice() { - tag = "NoPrice"; - } - public Contents getContents() { - return contents; - } - public void setContents(Contents contents) { - this.contents = contents; - } - public String getTag() { - return tag; - } - public void setTag(String tag) { - this.tag = tag; - } - private class Contents { - private ParsedPrice aprice; - private ParsedQuantity aquantity; - private String acommodity; - private boolean aismultiplier; - private ParsedStyle astyle; - public Contents() { - acommodity = ""; - } - public ParsedPrice getAprice() { - return aprice; - } - public void setAprice(ParsedPrice aprice) { - this.aprice = aprice; - } - 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 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/ParsedQuantity.java b/app/src/main/java/net/ktnx/mobileledger/json/ParsedQuantity.java deleted file mode 100644 index 50ce6782..00000000 --- a/app/src/main/java/net/ktnx/mobileledger/json/ParsedQuantity.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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 ParsedQuantity(String input) { - parseString(input); - } - 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)); - } - public void parseString(String input) { - int pointPos = input.indexOf('.'); - if (pointPos >= 0) { - String integral = input.replace(".", ""); - decimalMantissa = Long.valueOf(integral); - decimalPlaces = input.length() - pointPos - 1; - } - else { - decimalMantissa = Long.valueOf(input); - decimalPlaces = 0; - } - } -} diff --git a/app/src/main/java/net/ktnx/mobileledger/json/ParsedSourcePos.java b/app/src/main/java/net/ktnx/mobileledger/json/ParsedSourcePos.java deleted file mode 100644 index fa8dfb94..00000000 --- a/app/src/main/java/net/ktnx/mobileledger/json/ParsedSourcePos.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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/json/ParsedStyle.java b/app/src/main/java/net/ktnx/mobileledger/json/ParsedStyle.java deleted file mode 100644 index d0aaa45a..00000000 --- a/app/src/main/java/net/ktnx/mobileledger/json/ParsedStyle.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * 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 deleted file mode 100644 index fc17df66..00000000 --- a/app/src/main/java/net/ktnx/mobileledger/json/TransactionListParser.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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; - } -} diff --git a/app/src/main/java/net/ktnx/mobileledger/json/v1_14/AccountListParser.java b/app/src/main/java/net/ktnx/mobileledger/json/v1_14/AccountListParser.java new file mode 100644 index 00000000..e3f0e384 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/v1_14/AccountListParser.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.v1_14; + +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; + +import static net.ktnx.mobileledger.utils.Logger.debug; + +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() { + if (!iter.hasNext()) return null; + + ParsedLedgerAccount next = iter.next(); + + if (next.getAname().equalsIgnoreCase("root")) return nextAccount(); + + debug("accounts", String.format("Got account '%s'", next.getAname())); + return next; + } +} diff --git a/app/src/main/java/net/ktnx/mobileledger/json/v1_14/ParsedAmount.java b/app/src/main/java/net/ktnx/mobileledger/json/v1_14/ParsedAmount.java new file mode 100644 index 00000000..a77ea4d6 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/v1_14/ParsedAmount.java @@ -0,0 +1,62 @@ +/* + * 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.v1_14; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class ParsedAmount { + private String acommodity; + 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; + } + 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/v1_14/ParsedBalance.java b/app/src/main/java/net/ktnx/mobileledger/json/v1_14/ParsedBalance.java new file mode 100644 index 00000000..39cd1908 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/v1_14/ParsedBalance.java @@ -0,0 +1,50 @@ +/* + * 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.v1_14; + +import androidx.annotation.NonNull; + +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; + } + @NonNull + public String getAcommodity() { + return (acommodity == null) ? "" : 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/v1_14/ParsedLedgerAccount.java b/app/src/main/java/net/ktnx/mobileledger/json/v1_14/ParsedLedgerAccount.java new file mode 100644 index 00000000..d346cb76 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/v1_14/ParsedLedgerAccount.java @@ -0,0 +1,50 @@ +/* + * 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.v1_14; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +import java.util.List; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class ParsedLedgerAccount { + private List aebalance; + private List aibalance; + private String aname; + public ParsedLedgerAccount() { + } + public List getAebalance() { + return aebalance; + } + public List getAibalance() { + return aibalance; + } + public void setAebalance(List aebalance) { + this.aebalance = aebalance; + } + public void setAibalance(List aibalance) { + this.aibalance = aibalance; + } + public String getAname() { + return aname; + } + public void setAname(String aname) { + this.aname = aname; + } + +} diff --git a/app/src/main/java/net/ktnx/mobileledger/json/v1_14/ParsedLedgerTransaction.java b/app/src/main/java/net/ktnx/mobileledger/json/v1_14/ParsedLedgerTransaction.java new file mode 100644 index 00000000..e95c27c1 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/v1_14/ParsedLedgerTransaction.java @@ -0,0 +1,159 @@ +/* + * 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.v1_14; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +import net.ktnx.mobileledger.model.LedgerTransaction; +import net.ktnx.mobileledger.model.LedgerTransactionAccount; +import net.ktnx.mobileledger.utils.Globals; + +import java.text.ParseException; +import java.util.ArrayList; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.List; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class ParsedLedgerTransaction implements net.ktnx.mobileledger.json.ParsedLedgerTransaction { + private String tdate; + private String tdate2 = null; + 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 static ParsedLedgerTransaction fromLedgerTransaction(LedgerTransaction tr) { + ParsedLedgerTransaction + result = new ParsedLedgerTransaction(); + result.setTcomment(""); + result.setTprecedingcomment(""); + + ArrayList postings = new ArrayList<>(); + for (LedgerTransactionAccount acc : tr.getAccounts()) { + if (!acc.getAccountName() + .isEmpty()) + postings.add(ParsedPosting.fromLedgerAccount(acc)); + } + + result.setTpostings(postings); + Date transactionDate = tr.getDate(); + if (transactionDate == null) { + transactionDate = new GregorianCalendar().getTime(); + } + result.setTdate(Globals.formatIsoDate(transactionDate)); + result.setTdate2(null); + result.setTindex(1); + result.setTdescription(tr.getDescription()); + return result; + } + 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; + } + 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; + if (tpostings != null) + for (ParsedPosting p : tpostings) { + p.setPtransaction_(tindex); + } + } + public List getTpostings() { + return tpostings; + } + public void setTpostings(List tpostings) { + this.tpostings = tpostings; + } + public void addPosting(ParsedPosting posting) { + posting.setPtransaction_(tindex); + tpostings.add(posting); + } + 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/v1_14/ParsedPosting.java b/app/src/main/java/net/ktnx/mobileledger/json/v1_14/ParsedPosting.java new file mode 100644 index 00000000..21cc6dbf --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/v1_14/ParsedPosting.java @@ -0,0 +1,135 @@ +/* + * 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.v1_14; + +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 pdate2 = null; + private String ptype = "RegularPosting"; + private String pcomment = ""; + private List> ptags = new ArrayList<>(); + private String poriginal = null; + private int ptransaction_; + public ParsedPosting() { + } + public static ParsedPosting fromLedgerAccount(LedgerTransactionAccount acc) { + ParsedPosting result = new ParsedPosting(); + result.setPaccount(acc.getAccountName()); + ArrayList amounts = new ArrayList<>(); + ParsedAmount amt = new ParsedAmount(); + amt.setAcommodity((acc.getCurrency() == null) ? "" : acc.getCurrency()); + amt.setAismultiplier(false); + ParsedQuantity qty = new ParsedQuantity(); + qty.setDecimalPlaces(2); + qty.setDecimalMantissa(Math.round(acc.getAmount() * 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; + } + public String getPdate2() { + return pdate2; + } + public void setPdate2(String pdate2) { + this.pdate2 = pdate2; + } + public int getPtransaction_() { + return ptransaction_; + } + public void setPtransaction_(int ptransaction_) { + this.ptransaction_ = ptransaction_; + } + 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 List> getPtags() { + return ptags; + } + public void setPtags(List> 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; + } + 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/v1_14/ParsedPrice.java b/app/src/main/java/net/ktnx/mobileledger/json/v1_14/ParsedPrice.java new file mode 100644 index 00000000..7f457a7c --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/v1_14/ParsedPrice.java @@ -0,0 +1,78 @@ +/* + * 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.v1_14; + +class ParsedPrice { + private String tag; + private Contents contents; + public ParsedPrice() { + tag = "NoPrice"; + } + public Contents getContents() { + return contents; + } + public void setContents(Contents contents) { + this.contents = contents; + } + public String getTag() { + return tag; + } + public void setTag(String tag) { + this.tag = tag; + } + private class Contents { + private ParsedPrice aprice; + private ParsedQuantity aquantity; + private String acommodity; + private boolean aismultiplier; + private ParsedStyle astyle; + public Contents() { + acommodity = ""; + } + public ParsedPrice getAprice() { + return aprice; + } + public void setAprice(ParsedPrice aprice) { + this.aprice = aprice; + } + 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 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/v1_14/ParsedQuantity.java b/app/src/main/java/net/ktnx/mobileledger/json/v1_14/ParsedQuantity.java new file mode 100644 index 00000000..72b6fc61 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/v1_14/ParsedQuantity.java @@ -0,0 +1,58 @@ +/* + * 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.v1_14; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class ParsedQuantity { + private long decimalMantissa; + private int decimalPlaces; + public ParsedQuantity() { + } + public ParsedQuantity(String input) { + parseString(input); + } + 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)); + } + public void parseString(String input) { + int pointPos = input.indexOf('.'); + if (pointPos >= 0) { + String integral = input.replace(".", ""); + decimalMantissa = Long.valueOf(integral); + decimalPlaces = input.length() - pointPos - 1; + } + else { + decimalMantissa = Long.valueOf(input); + decimalPlaces = 0; + } + } +} diff --git a/app/src/main/java/net/ktnx/mobileledger/json/v1_14/ParsedSourcePos.java b/app/src/main/java/net/ktnx/mobileledger/json/v1_14/ParsedSourcePos.java new file mode 100644 index 00000000..1d553557 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/v1_14/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.v1_14; + +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/json/v1_14/ParsedStyle.java b/app/src/main/java/net/ktnx/mobileledger/json/v1_14/ParsedStyle.java new file mode 100644 index 00000000..798cb81c --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/v1_14/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.v1_14; + +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/v1_14/TransactionListParser.java b/app/src/main/java/net/ktnx/mobileledger/json/v1_14/TransactionListParser.java new file mode 100644 index 00000000..80566850 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/v1_14/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.v1_14; + +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; + } +} diff --git a/app/src/main/java/net/ktnx/mobileledger/json/v1_15/AccountListParser.java b/app/src/main/java/net/ktnx/mobileledger/json/v1_15/AccountListParser.java new file mode 100644 index 00000000..4206d0fc --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/v1_15/AccountListParser.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.v1_15; + +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; + +import static net.ktnx.mobileledger.utils.Logger.debug; + +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() { + if (!iter.hasNext()) return null; + + ParsedLedgerAccount next = iter.next(); + + if (next.getAname().equalsIgnoreCase("root")) return nextAccount(); + + debug("accounts", String.format("Got account '%s'", next.getAname())); + return next; + } +} diff --git a/app/src/main/java/net/ktnx/mobileledger/json/v1_15/ParsedAmount.java b/app/src/main/java/net/ktnx/mobileledger/json/v1_15/ParsedAmount.java new file mode 100644 index 00000000..3dee4ae9 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/v1_15/ParsedAmount.java @@ -0,0 +1,62 @@ +/* + * 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.v1_15; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class ParsedAmount { + private String acommodity; + private ParsedQuantity aquantity; + private boolean aismultiplier; + private ParsedStyle astyle; + private ParsedPrice aprice; + public ParsedAmount() { + } + public ParsedPrice getAprice() { + return aprice; + } + public void setAprice(ParsedPrice aprice) { + this.aprice = aprice; + } + 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/v1_15/ParsedBalance.java b/app/src/main/java/net/ktnx/mobileledger/json/v1_15/ParsedBalance.java new file mode 100644 index 00000000..24e9ccb0 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/v1_15/ParsedBalance.java @@ -0,0 +1,50 @@ +/* + * 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.v1_15; + +import androidx.annotation.NonNull; + +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; + } + @NonNull + public String getAcommodity() { + return (acommodity == null) ? "" : 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/v1_15/ParsedLedgerAccount.java b/app/src/main/java/net/ktnx/mobileledger/json/v1_15/ParsedLedgerAccount.java new file mode 100644 index 00000000..89f3ccce --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/v1_15/ParsedLedgerAccount.java @@ -0,0 +1,50 @@ +/* + * 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.v1_15; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +import java.util.List; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class ParsedLedgerAccount { + private List aebalance; + private List aibalance; + private String aname; + public ParsedLedgerAccount() { + } + public List getAebalance() { + return aebalance; + } + public List getAibalance() { + return aibalance; + } + public void setAebalance(List aebalance) { + this.aebalance = aebalance; + } + public void setAibalance(List aibalance) { + this.aibalance = aibalance; + } + public String getAname() { + return aname; + } + public void setAname(String aname) { + this.aname = aname; + } + +} diff --git a/app/src/main/java/net/ktnx/mobileledger/json/v1_15/ParsedLedgerTransaction.java b/app/src/main/java/net/ktnx/mobileledger/json/v1_15/ParsedLedgerTransaction.java new file mode 100644 index 00000000..2f6867dc --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/v1_15/ParsedLedgerTransaction.java @@ -0,0 +1,157 @@ +/* + * 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.v1_15; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +import net.ktnx.mobileledger.model.LedgerTransaction; +import net.ktnx.mobileledger.model.LedgerTransactionAccount; +import net.ktnx.mobileledger.utils.Globals; + +import java.text.ParseException; +import java.util.ArrayList; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.List; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class ParsedLedgerTransaction implements net.ktnx.mobileledger.json.ParsedLedgerTransaction { + private String tdate; + private String tdate2 = null; + 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 static ParsedLedgerTransaction fromLedgerTransaction(LedgerTransaction tr) { + ParsedLedgerTransaction result = new ParsedLedgerTransaction(); + result.setTcomment(""); + result.setTprecedingcomment(""); + + ArrayList postings = new ArrayList<>(); + for (LedgerTransactionAccount acc : tr.getAccounts()) { + if (!acc.getAccountName() + .isEmpty()) + postings.add(ParsedPosting.fromLedgerAccount(acc)); + } + + result.setTpostings(postings); + Date transactionDate = tr.getDate(); + if (transactionDate == null) { + transactionDate = new GregorianCalendar().getTime(); + } + result.setTdate(Globals.formatIsoDate(transactionDate)); + result.setTdate2(null); + result.setTindex(1); + result.setTdescription(tr.getDescription()); + return result; + } + 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; + } + 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; + if (tpostings != null) + for (ParsedPosting p : tpostings) { + p.setPtransaction_(tindex); + } + } + public List getTpostings() { + return tpostings; + } + public void setTpostings(List tpostings) { + this.tpostings = tpostings; + } + public void addPosting(ParsedPosting posting) { + posting.setPtransaction_(tindex); + tpostings.add(posting); + } + 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/v1_15/ParsedPosting.java b/app/src/main/java/net/ktnx/mobileledger/json/v1_15/ParsedPosting.java new file mode 100644 index 00000000..7c20a7ba --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/v1_15/ParsedPosting.java @@ -0,0 +1,135 @@ +/* + * 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.v1_15; + +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; + public String getPdate2() { + return pdate2; + } + public void setPdate2(String pdate2) { + this.pdate2 = pdate2; + } + private String pdate2 = null; + private String ptype = "RegularPosting"; + private String pcomment = ""; + private List> ptags = new ArrayList<>(); + private String poriginal = null; + private int ptransaction_; + public int getPtransaction_() { + return ptransaction_; + } + public void setPtransaction_(int ptransaction_) { + this.ptransaction_ = ptransaction_; + } + 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 List> getPtags() { + return ptags; + } + public void setPtags(List> 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; + } + 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()); + } + public static ParsedPosting fromLedgerAccount(LedgerTransactionAccount acc) { + ParsedPosting result = new ParsedPosting(); + result.setPaccount(acc.getAccountName()); + ArrayList amounts = new ArrayList<>(); + ParsedAmount amt = new ParsedAmount(); + amt.setAcommodity((acc.getCurrency() == null) ? "" : acc.getCurrency()); + amt.setAismultiplier(false); + ParsedQuantity qty = new ParsedQuantity(); + qty.setDecimalPlaces(2); + qty.setDecimalMantissa(Math.round(acc.getAmount() * 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; + } + +} diff --git a/app/src/main/java/net/ktnx/mobileledger/json/v1_15/ParsedPrice.java b/app/src/main/java/net/ktnx/mobileledger/json/v1_15/ParsedPrice.java new file mode 100644 index 00000000..409ed8b2 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/v1_15/ParsedPrice.java @@ -0,0 +1,78 @@ +/* + * 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.v1_15; + +class ParsedPrice { + private String tag; + private Contents contents; + public ParsedPrice() { + tag = "NoPrice"; + } + public Contents getContents() { + return contents; + } + public void setContents(Contents contents) { + this.contents = contents; + } + public String getTag() { + return tag; + } + public void setTag(String tag) { + this.tag = tag; + } + private class Contents { + private ParsedPrice aprice; + private ParsedQuantity aquantity; + private String acommodity; + private boolean aismultiplier; + private ParsedStyle astyle; + public Contents() { + acommodity = ""; + } + public ParsedPrice getAprice() { + return aprice; + } + public void setAprice(ParsedPrice aprice) { + this.aprice = aprice; + } + 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 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/v1_15/ParsedQuantity.java b/app/src/main/java/net/ktnx/mobileledger/json/v1_15/ParsedQuantity.java new file mode 100644 index 00000000..898d5974 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/v1_15/ParsedQuantity.java @@ -0,0 +1,58 @@ +/* + * 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.v1_15; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class ParsedQuantity { + private long decimalMantissa; + private int decimalPlaces; + public ParsedQuantity() { + } + public ParsedQuantity(String input) { + parseString(input); + } + 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)); + } + public void parseString(String input) { + int pointPos = input.indexOf('.'); + if (pointPos >= 0) { + String integral = input.replace(".", ""); + decimalMantissa = Long.valueOf(integral); + decimalPlaces = input.length() - pointPos - 1; + } + else { + decimalMantissa = Long.valueOf(input); + decimalPlaces = 0; + } + } +} diff --git a/app/src/main/java/net/ktnx/mobileledger/json/v1_15/ParsedSourcePos.java b/app/src/main/java/net/ktnx/mobileledger/json/v1_15/ParsedSourcePos.java new file mode 100644 index 00000000..9a783307 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/v1_15/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.v1_15; + +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/json/v1_15/ParsedStyle.java b/app/src/main/java/net/ktnx/mobileledger/json/v1_15/ParsedStyle.java new file mode 100644 index 00000000..554133c5 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/v1_15/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.v1_15; + +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/v1_15/TransactionListParser.java b/app/src/main/java/net/ktnx/mobileledger/json/v1_15/TransactionListParser.java new file mode 100644 index 00000000..c6210746 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/v1_15/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.v1_15; + +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; + } +} 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 252b32f4..69680367 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/LedgerTransaction.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/LedgerTransaction.java @@ -20,8 +20,6 @@ package net.ktnx.mobileledger.model; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; -import net.ktnx.mobileledger.json.ParsedLedgerTransaction; -import net.ktnx.mobileledger.json.ParsedPosting; import net.ktnx.mobileledger.utils.Digest; import net.ktnx.mobileledger.utils.Globals; @@ -31,7 +29,6 @@ import java.text.ParseException; import java.util.ArrayList; import java.util.Comparator; import java.util.Date; -import java.util.GregorianCalendar; public class LedgerTransaction { private static final String DIGEST_TYPE = "SHA-256"; @@ -191,25 +188,4 @@ 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); - Date transactionDate = date; - if (transactionDate == null) { - transactionDate = new GregorianCalendar().getTime(); - } - result.setTdate(Globals.formatIsoDate(transactionDate)); - 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 801e0169..89d6a83a 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/LedgerTransactionAccount.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/LedgerTransactionAccount.java @@ -19,13 +19,6 @@ package net.ktnx.mobileledger.model; import androidx.annotation.NonNull; -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; - public class LedgerTransactionAccount { private String accountName; private String shortAccountName; @@ -100,25 +93,4 @@ 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; - } } diff --git a/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java b/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java index e17948f3..2cd159c5 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java @@ -28,6 +28,7 @@ import androidx.annotation.Nullable; import net.ktnx.mobileledger.App; import net.ktnx.mobileledger.R; import net.ktnx.mobileledger.async.DbOpQueue; +import net.ktnx.mobileledger.async.SendTransactionTask; import net.ktnx.mobileledger.utils.Globals; import net.ktnx.mobileledger.utils.Logger; import net.ktnx.mobileledger.utils.MLDB; @@ -52,6 +53,7 @@ public final class MobileLedgerProfile { private int themeId; private int orderNo = -1; private FutureDates futureDates = FutureDates.None; + private SendTransactionTask.API apiVersion = SendTransactionTask.API.auto; public MobileLedgerProfile() { this.uuid = String.valueOf(UUID.randomUUID()); } @@ -70,6 +72,7 @@ public final class MobileLedgerProfile { themeId = origin.themeId; orderNo = origin.orderNo; futureDates = origin.futureDates; + apiVersion = origin.apiVersion; } // loads all profiles into Data.profiles // returns the profile with the given UUID @@ -120,6 +123,15 @@ public final class MobileLedgerProfile { db.endTransaction(); } } + public SendTransactionTask.API getApiVersion() { + return apiVersion; + } + public void setApiVersion(SendTransactionTask.API apiVersion) { + this.apiVersion = apiVersion; + } + public void setApiVersion(int apiVersion) { + this.apiVersion = SendTransactionTask.API.valueOf(apiVersion); + } public FutureDates getFutureDates() { return futureDates; } diff --git a/app/src/main/res/values-bg/strings.xml b/app/src/main/res/values-bg/strings.xml index 7402ed52..b1f6c4dc 100644 --- a/app/src/main/res/values-bg/strings.xml +++ b/app/src/main/res/values-bg/strings.xml @@ -125,5 +125,9 @@ Без ограничения Без въвеждане на бъдещи дати Въвеждане на дати в бъдещето + Автоматично откриване + Симулиране на заявка от браузър + версия 1.15 и по-нови + версии преди 1.15 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c18ada94..5f98037e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -167,4 +167,8 @@ Up to six months Up to a year Without restrictions + Simulate HTML form + version before 1.15 + version 1.15 and above + Detect automaticaly diff --git a/app/src/test/java/net/ktnx/mobileledger/json/ParsedQuantityTest.java b/app/src/test/java/net/ktnx/mobileledger/json/ParsedQuantityTest.java index 38e05f87..48415280 100644 --- a/app/src/test/java/net/ktnx/mobileledger/json/ParsedQuantityTest.java +++ b/app/src/test/java/net/ktnx/mobileledger/json/ParsedQuantityTest.java @@ -17,6 +17,8 @@ package net.ktnx.mobileledger.json; +import net.ktnx.mobileledger.json.v1_15.ParsedQuantity; + import org.junit.Test; import static junit.framework.TestCase.assertEquals;