/*
* 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.model;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import net.ktnx.mobileledger.json.ParsedLedgerTransaction;
import net.ktnx.mobileledger.json.ParsedPosting;
import net.ktnx.mobileledger.utils.Digest;
import net.ktnx.mobileledger.utils.Globals;
import java.nio.charset.Charset;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
public class LedgerTransaction {
private static final String DIGEST_TYPE = "SHA-256";
public final Comparator comparator =
new Comparator() {
@Override
public int compare(LedgerTransactionAccount o1, LedgerTransactionAccount o2) {
int res = o1.getAccountName().compareTo(o2.getAccountName());
if (res != 0) return res;
res = o1.getCurrency().compareTo(o2.getCurrency());
if (res != 0) return res;
return Float.compare(o1.getAmount(), o2.getAmount());
}
};
private String profile;
private Integer id;
private Date date;
private String description;
private ArrayList accounts;
private String dataHash;
private boolean dataLoaded;
public LedgerTransaction(Integer id, String dateString, String description)
throws ParseException {
this(id, Globals.parseLedgerDate(dateString), description);
}
public LedgerTransaction(Integer id, Date date, String description) {
this.profile = Data.profile.get().getUuid();
this.id = id;
this.date = date;
this.description = description;
this.accounts = new ArrayList<>();
this.dataHash = null;
dataLoaded = false;
}
public LedgerTransaction(Date date, String description) {
this(null, date, description);
}
public LedgerTransaction(int id) {
this(id, (Date) null, null);
}
public LedgerTransaction(int id, String profileUUID) {
this.profile = profileUUID;
this.id = id;
this.date = null;
this.description = null;
this.accounts = new ArrayList<>();
this.dataHash = null;
this.dataLoaded = false;
}
public ArrayList getAccounts() {
return accounts;
}
public void addAccount(LedgerTransactionAccount item) {
accounts.add(item);
dataHash = null;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
dataHash = null;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
dataHash = null;
}
public int getId() {
return id;
}
protected void fillDataHash() {
if (dataHash != null) return;
try {
Digest sha = new Digest(DIGEST_TYPE);
StringBuilder data = new StringBuilder();
data.append(profile);
data.append(getId());
data.append('\0');
data.append(getDescription());
data.append('\0');
data.append(Globals.formatLedgerDate(getDate()));
data.append('\0');
for (LedgerTransactionAccount item : accounts) {
data.append(item.getAccountName());
data.append('\0');
data.append(item.getCurrency());
data.append('\0');
data.append(item.getAmount());
}
sha.update(data.toString().getBytes(Charset.forName("UTF-8")));
dataHash = sha.digestToHexString();
}
catch (NoSuchAlgorithmException e) {
throw new RuntimeException(
String.format("Unable to get instance of %s digest", DIGEST_TYPE), e);
}
}
public boolean existsInDb(SQLiteDatabase db) {
fillDataHash();
try (Cursor c = db
.rawQuery("SELECT 1 from transactions where data_hash = ?", new String[]{dataHash}))
{
boolean result = c.moveToFirst();
Log.d("db", String.format("Transaction %d (%s) %s", id, dataHash,
result ? "already present" : "not present"));
return result;
}
}
public void loadData(SQLiteDatabase db) {
if (dataLoaded) return;
try (Cursor cTr = db
.rawQuery("SELECT date, description from transactions WHERE profile=? AND id=?",
new String[]{profile, String.valueOf(id)}))
{
if (cTr.moveToFirst()) {
String dateString = cTr.getString(0);
try {
date = Globals.parseLedgerDate(dateString);
}
catch (ParseException e) {
e.printStackTrace();
throw new RuntimeException(
String.format("Error parsing date '%s' from " + "transacion %d",
dateString, id));
}
description = cTr.getString(1);
try (Cursor cAcc = db.rawQuery("SELECT account_name, amount, currency FROM " +
"transaction_accounts WHERE " +
"profile=? AND transaction_id = ?",
new String[]{profile, String.valueOf(id)}))
{
while (cAcc.moveToNext()) {
// Log.d("transactions",
// String.format("Loaded %d: %s %1.2f %s", id, cAcc.getString(0),
// cAcc.getFloat(1), cAcc.getString(2)));
addAccount(new LedgerTransactionAccount(cAcc.getString(0), cAcc.getFloat(1),
cAcc.getString(2)));
}
finishLoading();
}
}
}
}
public String getDataHash() {
return dataHash;
}
public void finishLoading() {
dataLoaded = true;
}
public ParsedLedgerTransaction toParsedLedgerTransaction() {
ParsedLedgerTransaction result = new ParsedLedgerTransaction();
result.setTcomment("");
result.setTprecedingcomment("");
ArrayList postings = new ArrayList<>();
for (LedgerTransactionAccount acc : accounts) {
if (!acc.getAccountName().isEmpty()) postings.add(acc.asParsedPosting());
}
result.setTpostings(postings);
result.setTdate(Globals.formatIsoDate(date));
result.setTdate2(null);
result.setTindex(1);
result.setTdescription(description);
return result;
}
}