From 87181c0cb019c0a1a0a0ce2a4592795f87b5957c Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Mon, 28 Dec 2020 11:39:23 +0200 Subject: [PATCH] API version-dependent transaction sending --- .../async/SendTransactionTask.java | 68 ++++++------------- .../net/ktnx/mobileledger/json/Gateway.java | 39 +++++++++++ .../ktnx/mobileledger/json/v1_14/Gateway.java | 38 +++++++++++ .../ktnx/mobileledger/json/v1_15/Gateway.java | 38 +++++++++++ .../mobileledger/json/v1_19_1/Gateway.java | 38 +++++++++++ 5 files changed, 175 insertions(+), 46 deletions(-) create mode 100644 app/src/main/java/net/ktnx/mobileledger/json/Gateway.java create mode 100644 app/src/main/java/net/ktnx/mobileledger/json/v1_14/Gateway.java create mode 100644 app/src/main/java/net/ktnx/mobileledger/json/v1_15/Gateway.java create mode 100644 app/src/main/java/net/ktnx/mobileledger/json/v1_19_1/Gateway.java 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 dc0c2487..14d41f9f 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java @@ -20,9 +20,8 @@ package net.ktnx.mobileledger.async; import android.os.AsyncTask; import android.util.Log; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.ObjectWriter; - +import net.ktnx.mobileledger.json.API; +import net.ktnx.mobileledger.json.Gateway; import net.ktnx.mobileledger.model.LedgerTransaction; import net.ktnx.mobileledger.model.LedgerTransactionAccount; import net.ktnx.mobileledger.model.MobileLedgerProfile; @@ -74,35 +73,14 @@ public class SendTransactionTask extends AsyncTask. + */ + +package net.ktnx.mobileledger.json; + +import com.fasterxml.jackson.core.JsonProcessingException; + +import net.ktnx.mobileledger.model.LedgerTransaction; + +abstract public class Gateway { + public static Gateway forApiVersion(API apiVersion) { + switch (apiVersion) { + case v1_14: + return new net.ktnx.mobileledger.json.v1_14.Gateway(); + case v1_15: + return new net.ktnx.mobileledger.json.v1_15.Gateway(); + case v1_19_1: + return new net.ktnx.mobileledger.json.v1_19_1.Gateway(); + default: + throw new RuntimeException("Unsupported JSON API version " + apiVersion); + } + } + public abstract String transactionSaveRequest(LedgerTransaction ledgerTransaction) + throws JsonProcessingException; +} diff --git a/app/src/main/java/net/ktnx/mobileledger/json/v1_14/Gateway.java b/app/src/main/java/net/ktnx/mobileledger/json/v1_14/Gateway.java new file mode 100644 index 00000000..25712af6 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/v1_14/Gateway.java @@ -0,0 +1,38 @@ +/* + * Copyright © 2020 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.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; + +import net.ktnx.mobileledger.model.LedgerTransaction; + +public class Gateway extends net.ktnx.mobileledger.json.Gateway { + @Override + public String transactionSaveRequest(LedgerTransaction ledgerTransaction) + throws JsonProcessingException { + net.ktnx.mobileledger.json.v1_14.ParsedLedgerTransaction jsonTransaction = + net.ktnx.mobileledger.json.v1_14.ParsedLedgerTransaction.fromLedgerTransaction( + ledgerTransaction); + ObjectMapper mapper = new ObjectMapper(); + ObjectWriter writer = + mapper.writerFor(net.ktnx.mobileledger.json.v1_14.ParsedLedgerTransaction.class); + return writer.writeValueAsString(jsonTransaction); + } +} diff --git a/app/src/main/java/net/ktnx/mobileledger/json/v1_15/Gateway.java b/app/src/main/java/net/ktnx/mobileledger/json/v1_15/Gateway.java new file mode 100644 index 00000000..a4a1268e --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/v1_15/Gateway.java @@ -0,0 +1,38 @@ +/* + * Copyright © 2020 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.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; + +import net.ktnx.mobileledger.model.LedgerTransaction; + +public class Gateway extends net.ktnx.mobileledger.json.Gateway { + @Override + public String transactionSaveRequest(LedgerTransaction ledgerTransaction) + throws JsonProcessingException { + net.ktnx.mobileledger.json.v1_15.ParsedLedgerTransaction jsonTransaction = + net.ktnx.mobileledger.json.v1_15.ParsedLedgerTransaction.fromLedgerTransaction( + ledgerTransaction); + ObjectMapper mapper = new ObjectMapper(); + ObjectWriter writer = + mapper.writerFor(net.ktnx.mobileledger.json.v1_15.ParsedLedgerTransaction.class); + return writer.writeValueAsString(jsonTransaction); + } +} diff --git a/app/src/main/java/net/ktnx/mobileledger/json/v1_19_1/Gateway.java b/app/src/main/java/net/ktnx/mobileledger/json/v1_19_1/Gateway.java new file mode 100644 index 00000000..239ec2d1 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/json/v1_19_1/Gateway.java @@ -0,0 +1,38 @@ +/* + * Copyright © 2020 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_19_1; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; + +import net.ktnx.mobileledger.model.LedgerTransaction; + +public class Gateway extends net.ktnx.mobileledger.json.Gateway { + @Override + public String transactionSaveRequest(LedgerTransaction ledgerTransaction) + throws JsonProcessingException { + net.ktnx.mobileledger.json.v1_19_1.ParsedLedgerTransaction jsonTransaction = + net.ktnx.mobileledger.json.v1_19_1.ParsedLedgerTransaction.fromLedgerTransaction( + ledgerTransaction); + ObjectMapper mapper = new ObjectMapper(); + ObjectWriter writer = + mapper.writerFor(net.ktnx.mobileledger.json.v1_19_1.ParsedLedgerTransaction.class); + return writer.writeValueAsString(jsonTransaction); + } +} -- 2.39.2