Skip to content

Commit

Permalink
Commodities are not only currencies.
Browse files Browse the repository at this point in the history
  • Loading branch information
pnemonic78 committed Sep 17, 2024
1 parent 0d42424 commit ed58211
Show file tree
Hide file tree
Showing 24 changed files with 1,384 additions and 655 deletions.
9 changes: 6 additions & 3 deletions app/src/main/java/org/gnucash/android/db/DatabaseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,14 @@ public class DatabaseHelper extends SQLiteOpenHelper {
public static final String COMMODITIES_TABLE_CREATE = "CREATE TABLE " + DatabaseSchema.CommodityEntry.TABLE_NAME + " ("
+ CommodityEntry._ID + " integer primary key autoincrement, "
+ CommodityEntry.COLUMN_UID + " varchar(255) not null UNIQUE, "
+ CommodityEntry.COLUMN_NAMESPACE + " varchar(255) not null default " + Commodity.Namespace.ISO4217.name() + ", "
+ CommodityEntry.COLUMN_NAMESPACE + " varchar(255) not null default '" + Commodity.COMMODITY_ISO4217 + "', "
+ CommodityEntry.COLUMN_FULLNAME + " varchar(255) not null, "
+ CommodityEntry.COLUMN_MNEMONIC + " varchar(255) not null, "
+ CommodityEntry.COLUMN_LOCAL_SYMBOL + " varchar(255) not null default '', "
+ CommodityEntry.COLUMN_CUSIP + " varchar(255), "
+ CommodityEntry.COLUMN_SMALLEST_FRACTION + " integer not null, "
+ CommodityEntry.COLUMN_QUOTE_FLAG + " integer not null, "
+ CommodityEntry.COLUMN_QUOTE_SOURCE + " varchar(255), "
+ CommodityEntry.COLUMN_QUOTE_TZ + " varchar(100), "
+ CommodityEntry.COLUMN_CREATED_AT + " TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, "
+ CommodityEntry.COLUMN_MODIFIED_AT + " TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP "
+ ");" + createUpdatedAtTrigger(CommodityEntry.TABLE_NAME);
Expand Down Expand Up @@ -260,7 +261,9 @@ public void onOpen(SQLiteDatabase db) {
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
MigrationHelper.migrate(db, oldVersion, newVersion);
}


/**
Expand Down
11 changes: 8 additions & 3 deletions app/src/main/java/org/gnucash/android/db/DatabaseSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class DatabaseSchema {
* Version number of database containing accounts and transactions info.
* With any change to the database schema, this number must increase
*/
public static final int DATABASE_VERSION = 15;
public static final int DATABASE_VERSION = 16;

//no instances are to be instantiated
private DatabaseSchema() {
Expand Down Expand Up @@ -206,9 +206,14 @@ public static abstract class CommodityEntry implements CommonColumns {
public static final String COLUMN_CUSIP = "cusip";

/**
* TRUE if prices are to be downloaded for this commodity from a quote source
* Prices are to be downloaded for this commodity from a quote source.
*/
public static final String COLUMN_QUOTE_FLAG = "quote_flag";
public static final String COLUMN_QUOTE_SOURCE = "quote_source";

/**
* Time zone of the quote source.
*/
public static final String COLUMN_QUOTE_TZ = "quote_tz";

public static final String INDEX_UID = "commodities_uid_index";
}
Expand Down
35 changes: 32 additions & 3 deletions app/src/main/java/org/gnucash/android/db/MigrationHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Timestamp;
import java.util.TimeZone;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import timber.log.Timber;

/**
* Collection of helper methods which are used during database migrations
*
Expand All @@ -52,7 +52,7 @@ static void importCommodities(SQLiteDatabase db) throws SAXException, ParserConf
XMLReader xr = sp.getXMLReader();

InputStream commoditiesInputStream = GnuCashApplication.getAppContext().getResources()
.openRawResource(R.raw.iso_4217_currencies);
.openRawResource(R.raw.iso_4217_currencies);
BufferedInputStream bos = new BufferedInputStream(commoditiesInputStream);

/* Create handler to handle XML Tags ( extends DefaultHandler ) */
Expand All @@ -61,4 +61,33 @@ static void importCommodities(SQLiteDatabase db) throws SAXException, ParserConf
xr.setContentHandler(handler);
xr.parse(new InputSource(bos));
}

public static void migrate(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < 16) {
migrateTo16(db);
}
}

/**
* Upgrade the database to version 16.
*
* @param db the database.
*/
private static void migrateTo16(SQLiteDatabase db) {
Timber.i("Upgrading database to version 16");

String sqlAddQuoteSource = "ALTER TABLE " + DatabaseSchema.CommodityEntry.TABLE_NAME +
" ADD COLUMN " + DatabaseSchema.CommodityEntry.COLUMN_QUOTE_SOURCE + " varchar(255)";
String sqlAddQuoteTZ = "ALTER TABLE " + DatabaseSchema.CommodityEntry.TABLE_NAME +
" ADD COLUMN " + DatabaseSchema.CommodityEntry.COLUMN_QUOTE_TZ + " varchar(100)";

try {
db.beginTransaction();
db.execSQL(sqlAddQuoteSource);
db.execSQL(sqlAddQuoteTZ);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ public Account buildModelInstance(@NonNull final Cursor c) {
* @param c Cursor pointing to account record in database
* @return {@link Account} object constructed from database record
*/
private Account buildSimpleAccountInstance(Cursor c) {
public Account buildSimpleAccountInstance(Cursor c) {
Account account = new Account(c.getString(c.getColumnIndexOrThrow(AccountEntry.COLUMN_NAME)));
populateBaseModelAttributes(c, account);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public CommoditiesDbAdapter(SQLiteDatabase db, boolean initCommon) {
CommodityEntry.COLUMN_LOCAL_SYMBOL,
CommodityEntry.COLUMN_CUSIP,
CommodityEntry.COLUMN_SMALLEST_FRACTION,
CommodityEntry.COLUMN_QUOTE_FLAG
CommodityEntry.COLUMN_QUOTE_SOURCE,
CommodityEntry.COLUMN_QUOTE_TZ
});
if (initCommon) {
initCommon();
Expand Down Expand Up @@ -73,13 +74,22 @@ public static CommoditiesDbAdapter getInstance() {
protected @NonNull SQLiteStatement setBindings(@NonNull SQLiteStatement stmt, @NonNull final Commodity commodity) {
stmt.clearBindings();
stmt.bindString(1, commodity.getFullname());
stmt.bindString(2, commodity.getNamespace().name());
stmt.bindString(2, commodity.getNamespace());
stmt.bindString(3, commodity.getMnemonic());
stmt.bindString(4, commodity.getLocalSymbol());
stmt.bindString(5, commodity.getCusip());
if (commodity.getLocalSymbol() != null) {
stmt.bindString(4, commodity.getLocalSymbol());
}
if (commodity.getCusip() != null) {
stmt.bindString(5, commodity.getCusip());
}
stmt.bindLong(6, commodity.getSmallestFraction());
stmt.bindLong(7, commodity.getQuoteFlag());
stmt.bindString(8, commodity.getUID());
if (commodity.getQuoteSource() != null) {
stmt.bindString(7, commodity.getQuoteSource());
}
if (commodity.getQuoteTimeZoneId() != null) {
stmt.bindString(8, commodity.getQuoteTimeZoneId());
}
stmt.bindString(9, commodity.getUID());

return stmt;
}
Expand All @@ -93,12 +103,14 @@ public Commodity buildModelInstance(@NonNull final Cursor cursor) {
String localSymbol = cursor.getString(cursor.getColumnIndexOrThrow(CommodityEntry.COLUMN_LOCAL_SYMBOL));

int fraction = cursor.getInt(cursor.getColumnIndexOrThrow(CommodityEntry.COLUMN_SMALLEST_FRACTION));
int quoteFlag = cursor.getInt(cursor.getColumnIndexOrThrow(CommodityEntry.COLUMN_QUOTE_FLAG));
String quoteSource = cursor.getString(cursor.getColumnIndexOrThrow(CommodityEntry.COLUMN_QUOTE_SOURCE));
String quoteTZ = cursor.getString(cursor.getColumnIndexOrThrow(CommodityEntry.COLUMN_QUOTE_TZ));

Commodity commodity = new Commodity(fullname, mnemonic, fraction);
commodity.setNamespace(Commodity.Namespace.valueOf(namespace));
commodity.setNamespace(namespace);
commodity.setCusip(cusip);
commodity.setQuoteFlag(quoteFlag);
commodity.setQuoteSource(quoteSource);
commodity.setQuoteTimeZone(quoteTZ);
commodity.setLocalSymbol(localSymbol);
populateBaseModelAttributes(cursor, commodity);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static PricesDbAdapter getInstance() {
stmt.clearBindings();
stmt.bindString(1, price.getCommodityUID());
stmt.bindString(2, price.getCurrencyUID());
stmt.bindString(3, price.getDate().toString());
stmt.bindString(3, TimestampHelper.getUtcStringFromTimestamp(price.getDate()));
if (price.getSource() != null) {
stmt.bindString(4, price.getSource());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import static org.gnucash.android.db.DatabaseSchema.SplitEntry;
import static org.gnucash.android.db.DatabaseSchema.TransactionEntry;
import static org.gnucash.android.model.Commodity.NO_CURRENCY_CODE;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
Expand Down Expand Up @@ -227,7 +228,7 @@ private Money calculateSplitBalance(List<String> accountUIDList, String currency
long amount_num = cursor.getLong(0);
long amount_denom = cursor.getLong(1);
String commodityCode = cursor.getString(2);
if (commodityCode.equals("XXX") || amount_num == 0) {
if (commodityCode.equals(NO_CURRENCY_CODE) || amount_num == 0) {
// ignore custom currency
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,8 @@ private void moveExportToOwnCloud(ExportParams exportParams, Exporter exporter,
}

private static String getFileLastModifiedTimestamp(String path) {
Long timeStampLong = new File(path).lastModified() / 1000;
return timeStampLong.toString();
long timeStampLong = new File(path).lastModified() / 1000;
return Long.toString(timeStampLong);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class CsvTransactionsExporter(context: Context,
} else {
writer.writeToken(null)
}
writer.writeEndToken(split.quantity!!.div(split.value!!).formattedStringWithoutSymbol())
writer.writeEndToken((split.quantity!! / split.value!!).formattedStringWithoutSymbol())
}
}

Expand Down
Loading

0 comments on commit ed58211

Please sign in to comment.