Skip to content

Commit

Permalink
Fixes codinguser#467 and fixes codinguser#464.
Browse files Browse the repository at this point in the history
  • Loading branch information
alceurneto committed Feb 6, 2016
1 parent f10b4e4 commit 904f301
Show file tree
Hide file tree
Showing 34 changed files with 501 additions and 109 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ dependencies {

testCompile('org.robolectric:robolectric:3.0',
'junit:junit:4.12',
'joda-time:joda-time:2.7',
'org.assertj:assertj-core:1.7.1'
)
androidTestCompile ('com.android.support:support-annotations:' + androidSupportVersion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.gnucash.android.model.Split;
import org.gnucash.android.model.Transaction;
import org.gnucash.android.model.TransactionType;
import org.gnucash.android.util.TimestampHelper;

import java.math.BigDecimal;
import java.sql.Timestamp;
Expand Down Expand Up @@ -182,7 +183,7 @@ protected SQLiteStatement compileReplaceStatement(@NonNull final Account account
mReplaceStatement.bindLong(7, account.isFavorite() ? 1 : 0);
mReplaceStatement.bindString(8, account.getFullName());
mReplaceStatement.bindLong(9, account.isPlaceholderAccount() ? 1 : 0);
mReplaceStatement.bindString(10, account.getCreatedTimestamp().toString());
mReplaceStatement.bindString(10, TimestampHelper.getUtcStringForTimestamp(account.getCreatedTimestamp()));
mReplaceStatement.bindLong(11, account.isHidden() ? 1 : 0);
Commodity commodity = account.getCommodity();
if (commodity == null)
Expand Down Expand Up @@ -529,7 +530,7 @@ public List<Account> getExportableAccounts(Timestamp lastExportTimeStamp){
SplitEntry.COLUMN_ACCOUNT_UID,
new String[]{AccountEntry.TABLE_NAME + ".*"},
TransactionEntry.TABLE_NAME + "." + TransactionEntry.COLUMN_MODIFIED_AT + " > ?",
new String[]{lastExportTimeStamp.toString()},
new String[]{TimestampHelper.getUtcStringForTimestamp(lastExportTimeStamp)},
AccountEntry.TABLE_NAME + "." + AccountEntry.COLUMN_UID,
null,
null
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/org/gnucash/android/db/DatabaseAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import org.gnucash.android.db.DatabaseSchema.TransactionEntry;
import org.gnucash.android.model.AccountType;
import org.gnucash.android.model.BaseModel;
import org.gnucash.android.util.TimestampHelper;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -301,7 +301,7 @@ public List<Model> getAllRecords(){
*/
protected ContentValues populateBaseModelAttributes(@NonNull ContentValues contentValues, @NonNull Model model){
contentValues.put(CommonColumns.COLUMN_UID, model.getUID());
contentValues.put(CommonColumns.COLUMN_CREATED_AT, model.getCreatedTimestamp().toString());
contentValues.put(CommonColumns.COLUMN_CREATED_AT, TimestampHelper.getUtcStringForTimestamp(model.getCreatedTimestamp()));
//there is a trigger in the database for updated the modified_at column
/* Due to the use of SQL REPLACE syntax, we insert the created_at values each time
* (maintain the original creation time and not the time of creation of the replacement)
Expand All @@ -321,8 +321,8 @@ protected void populateBaseModelAttributes(Cursor cursor, BaseModel model){
String modified= cursor.getString(cursor.getColumnIndexOrThrow(CommonColumns.COLUMN_MODIFIED_AT));

model.setUID(uid);
model.setCreatedTimestamp(Timestamp.valueOf(created));
model.setModifiedTimestamp(Timestamp.valueOf(modified));
model.setCreatedTimestamp(TimestampHelper.getTimestampForUtcString(created));
model.setModifiedTimestamp(TimestampHelper.getTimestampForUtcString(modified));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class DatabaseSchema {
* Database version.
* With any change to the database schema, this number must increase
*/
public static final int DATABASE_VERSION = 11;
public static final int DATABASE_VERSION = 12;

/**
* Database version where Splits were introduced
Expand Down
56 changes: 46 additions & 10 deletions app/src/main/java/org/gnucash/android/db/MigrationHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;

Expand All @@ -42,6 +41,8 @@
import org.gnucash.android.model.Money;
import org.gnucash.android.model.ScheduledAction;
import org.gnucash.android.model.Transaction;
import org.gnucash.android.util.PreferencesHelper;
import org.gnucash.android.util.TimestampHelper;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
Expand All @@ -59,6 +60,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
Expand Down Expand Up @@ -645,7 +647,7 @@ static int upgradeDbToVersion8(SQLiteDatabase db) {
//================================ END TABLE MIGRATIONS ================================

// String timestamp to be used for all new created entities in migration
String timestamp = (new Timestamp(System.currentTimeMillis())).toString();
String timestamp = TimestampHelper.getUtcStringForTimestamp(TimestampHelper.getTimestampForNow());

//ScheduledActionDbAdapter scheduledActionDbAdapter = new ScheduledActionDbAdapter(db);
//SplitsDbAdapter splitsDbAdapter = new SplitsDbAdapter(db);
Expand Down Expand Up @@ -696,7 +698,7 @@ static int upgradeDbToVersion8(SQLiteDatabase db) {
while (cursor.moveToNext()){
contentValues.clear();
Timestamp timestampT = new Timestamp(cursor.getLong(cursor.getColumnIndexOrThrow(TransactionEntry.COLUMN_TIMESTAMP)));
contentValues.put(TransactionEntry.COLUMN_CREATED_AT, timestampT.toString());
contentValues.put(TransactionEntry.COLUMN_CREATED_AT, TimestampHelper.getUtcStringForTimestamp(timestampT));
long transactionId = cursor.getLong(cursor.getColumnIndexOrThrow(TransactionEntry._ID));
db.update(TransactionEntry.TABLE_NAME, contentValues, TransactionEntry._ID + "=" + transactionId, null);

Expand Down Expand Up @@ -1094,11 +1096,9 @@ static int upgradeDbToVersion10(SQLiteDatabase db){

boolean exportAll = Boolean.parseBoolean(tokens[2]);
if (exportAll){
params.setExportStartTime(Timestamp.valueOf(Exporter.TIMESTAMP_ZERO));
params.setExportStartTime(TimestampHelper.getTimestampForEpochZero());
} else {
String lastExportTimeStamp = PreferenceManager.getDefaultSharedPreferences(GnuCashApplication.getAppContext())
.getString(Exporter.PREF_LAST_EXPORT_TIME, Exporter.TIMESTAMP_ZERO);
Timestamp timestamp = Timestamp.valueOf(lastExportTimeStamp);
Timestamp timestamp = PreferencesHelper.getLastExportTime();
params.setExportStartTime(timestamp);
}

Expand Down Expand Up @@ -1143,10 +1143,9 @@ static int upgradeDbToVersion11(SQLiteDatabase db){
String tag = cursor.getString(cursor.getColumnIndexOrThrow(ScheduledActionEntry.COLUMN_TAG));
String[] tokens = tag.split(";");
try {
Timestamp timestamp = Timestamp.valueOf(tokens[2]);
Timestamp timestamp = TimestampHelper.getTimestampForUtcString(tokens[2]);
} catch (IllegalArgumentException ex) {
tokens[2] = PreferenceManager.getDefaultSharedPreferences(GnuCashApplication.getAppContext())
.getString(Exporter.PREF_LAST_EXPORT_TIME, Exporter.TIMESTAMP_ZERO);
tokens[2] = TimestampHelper.getUtcStringForTimestamp(PreferencesHelper.getLastExportTime());
} finally {
tag = TextUtils.join(";", tokens);
}
Expand All @@ -1170,4 +1169,41 @@ static int upgradeDbToVersion11(SQLiteDatabase db){
}
return oldVersion;
}

public static Timestamp subtractTimeZoneOffset(Timestamp timestamp, TimeZone timeZone) {
final long millisecondsToSubtract = Math.abs(timeZone.getOffset(timestamp.getTime()));
return new Timestamp(timestamp.getTime() - millisecondsToSubtract);
}

/**
* Upgrade database to version 12
* <p>
* Change last_export_time Android preference to current value - N
* where N is the absolute timezone offset for current user time zone.
* For details see #467.
* </p>
* @param db SQLite database
* @return 12 if upgrade was successful, 11 otherwise
*/
static int upgradeDbToVersion12(SQLiteDatabase db){
Log.i(MigrationHelper.LOG_TAG, "Upgrading database to version 12");

int oldVersion = 11;

try {

final Timestamp currentLastExportTime = PreferencesHelper.getLastExportTime();

final Timestamp updatedLastExportTime = subtractTimeZoneOffset(
currentLastExportTime, TimeZone.getDefault());
PreferencesHelper.setLastExportTime(updatedLastExportTime);

oldVersion = 12;

} catch (Exception ignored){
// Do nothing: here oldVersion = 11.
}

return oldVersion;
}
}
9 changes: 3 additions & 6 deletions app/src/main/java/org/gnucash/android/db/PricesDbAdapter.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package org.gnucash.android.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.support.annotation.NonNull;
import android.util.Log;
import android.util.Pair;

import org.gnucash.android.app.GnuCashApplication;
import org.gnucash.android.model.Price;

import java.sql.Timestamp;
import org.gnucash.android.util.TimestampHelper;

import static org.gnucash.android.db.DatabaseSchema.PriceEntry;

Expand Down Expand Up @@ -49,7 +46,7 @@ protected SQLiteStatement compileReplaceStatement(@NonNull final Price price) {
mReplaceStatement.bindString(1, price.getUID());
mReplaceStatement.bindString(2, price.getCommodityUID());
mReplaceStatement.bindString(3, price.getCurrencyUID());
mReplaceStatement.bindString(4, price.getDate().toString());
mReplaceStatement.bindString(4, TimestampHelper.getUtcStringForTimestamp(price.getDate()));
if (price.getSource() != null) {
mReplaceStatement.bindString(5, price.getSource());
}
Expand All @@ -73,7 +70,7 @@ public Price buildModelInstance(@NonNull final Cursor cursor) {
long valueDenom = cursor.getLong(cursor.getColumnIndexOrThrow(PriceEntry.COLUMN_VALUE_DENOM));

Price price = new Price(commodityUID, currencyUID);
price.setDate(Timestamp.valueOf(dateString));
price.setDate(TimestampHelper.getTimestampForUtcString(dateString));
price.setSource(source);
price.setType(type);
price.setValueNum(valueNum);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.gnucash.android.app.GnuCashApplication;
import org.gnucash.android.model.ScheduledAction;
import org.gnucash.android.util.TimestampHelper;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -102,7 +103,7 @@ protected SQLiteStatement compileReplaceStatement(@NonNull final ScheduledAction
mReplaceStatement.bindLong(6, schedxAction.getLastRun());
mReplaceStatement.bindLong(7, schedxAction.getPeriod());
mReplaceStatement.bindLong(8, schedxAction.isEnabled() ? 1 : 0);
mReplaceStatement.bindString(9, schedxAction.getCreatedTimestamp().toString());
mReplaceStatement.bindString(9, TimestampHelper.getUtcStringForTimestamp(schedxAction.getCreatedTimestamp()));
if (schedxAction.getTag() == null)
mReplaceStatement.bindNull(10);
else
Expand Down
8 changes: 3 additions & 5 deletions app/src/main/java/org/gnucash/android/db/SplitsDbAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,14 @@
import android.util.Pair;

import org.gnucash.android.app.GnuCashApplication;
import org.gnucash.android.model.AccountType;
import org.gnucash.android.model.Commodity;
import org.gnucash.android.model.Money;
import org.gnucash.android.model.Split;
import org.gnucash.android.model.TransactionType;
import org.gnucash.android.util.TimestampHelper;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Currency;
import java.util.List;

import static org.gnucash.android.db.DatabaseSchema.SplitEntry;
Expand Down Expand Up @@ -80,7 +78,7 @@ public void addRecord(@NonNull final Split split){

//modifying a split means modifying the accompanying transaction as well
updateRecord(TransactionEntry.TABLE_NAME, transactionId,
TransactionEntry.COLUMN_MODIFIED_AT, new Timestamp(System.currentTimeMillis()).toString());
TransactionEntry.COLUMN_MODIFIED_AT, TimestampHelper.getUtcStringForTimestamp(TimestampHelper.getTimestampForNow()));
}

@Override
Expand Down Expand Up @@ -109,7 +107,7 @@ protected SQLiteStatement compileReplaceStatement(@NonNull final Split split) {
mReplaceStatement.bindLong(5, split.getValue().getDenominator());
mReplaceStatement.bindLong(6, split.getQuantity().getNumerator());
mReplaceStatement.bindLong(7, split.getQuantity().getDenominator());
mReplaceStatement.bindString(8, split.getCreatedTimestamp().toString());
mReplaceStatement.bindString(8, TimestampHelper.getUtcStringForTimestamp(split.getCreatedTimestamp()));
mReplaceStatement.bindString(9, split.getAccountUID());
mReplaceStatement.bindString(10, split.getTransactionUID());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.gnucash.android.model.Money;
import org.gnucash.android.model.Split;
import org.gnucash.android.model.Transaction;
import org.gnucash.android.util.TimestampHelper;

import java.sql.Timestamp;
import java.util.ArrayList;
Expand Down Expand Up @@ -187,7 +188,7 @@ protected SQLiteStatement compileReplaceStatement(@NonNull final Transaction tra
commodity = CommoditiesDbAdapter.getInstance().getCommodity(transaction.getCurrencyCode());

mReplaceStatement.bindString(7, commodity.getUID());
mReplaceStatement.bindString(8, transaction.getCreatedTimestamp().toString());
mReplaceStatement.bindString(8, TimestampHelper.getUtcStringForTimestamp(transaction.getCreatedTimestamp()));

if (transaction.getScheduledActionUID() == null)
mReplaceStatement.bindNull(9);
Expand Down Expand Up @@ -643,11 +644,11 @@ public Timestamp getTimestampOfLastModification(){
new String[]{"MAX(" + TransactionEntry.COLUMN_MODIFIED_AT + ")"},
null, null, null, null, null);

Timestamp timestamp = new Timestamp(System.currentTimeMillis());
Timestamp timestamp = TimestampHelper.getTimestampForNow();
if (cursor.moveToFirst()){
String timeString = cursor.getString(0);
if (timeString != null){ //in case there were no transactions in the XML file (account structure only)
timestamp = Timestamp.valueOf(timeString);
timestamp = TimestampHelper.getTimestampForUtcString(timeString);
}
}
cursor.close();
Expand Down
14 changes: 5 additions & 9 deletions app/src/main/java/org/gnucash/android/export/ExportParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@

package org.gnucash.android.export;

import android.preference.PreferenceManager;

import org.gnucash.android.BuildConfig;
import org.gnucash.android.R;
import org.gnucash.android.app.GnuCashApplication;
import org.gnucash.android.ui.export.ExportFormFragment;
import org.gnucash.android.util.TimestampHelper;

import java.sql.Timestamp;

Expand Down Expand Up @@ -50,7 +46,7 @@ public enum ExportTarget {SD_CARD, SHARING, DROPBOX, GOOGLE_DRIVE }
/**
* All transactions created after this date will be exported
*/
private Timestamp mExportStartTime = Timestamp.valueOf(Exporter.TIMESTAMP_ZERO);
private Timestamp mExportStartTime = TimestampHelper.getTimestampForEpochZero();

/**
* Flag to determine if all transactions should be deleted after exporting is complete
Expand Down Expand Up @@ -138,7 +134,7 @@ public void setExportTarget(ExportTarget mExportTarget) {

@Override
public String toString() {
return "Export all transactions created since " + mExportStartTime.toString()
return "Export all transactions created since " + TimestampHelper.getUtcStringForTimestamp(mExportStartTime) + " UTC"
+ " as "+ mExportFormat.name() + " to " + mExportTarget.name();
}

Expand All @@ -151,7 +147,7 @@ public String toCsv(){
String separator = ";";

return mExportFormat.name() + separator + mExportTarget.name() + separator
+ mExportStartTime.toString() + separator
+ TimestampHelper.getUtcStringForTimestamp(mExportStartTime) + separator
+ Boolean.toString(mDeleteTransactionsAfterExport);
}

Expand All @@ -164,7 +160,7 @@ public static ExportParams parseCsv(String csvParams){
String[] tokens = csvParams.split(";");
ExportParams params = new ExportParams(ExportFormat.valueOf(tokens[0]));
params.setExportTarget(ExportTarget.valueOf(tokens[1]));
params.setExportStartTime(Timestamp.valueOf(tokens[2]));
params.setExportStartTime(TimestampHelper.getTimestampForUtcString(tokens[2]));
params.setDeleteTransactionsAfterExport(Boolean.parseBoolean(tokens[3]));

return params;
Expand Down
7 changes: 0 additions & 7 deletions app/src/main/java/org/gnucash/android/export/Exporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.gnucash.android.db.TransactionsDbAdapter;

import java.io.File;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
Expand Down Expand Up @@ -86,12 +85,6 @@ public abstract class Exporter {

private static final SimpleDateFormat EXPORT_FILENAME_DATE_FORMAT = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US);

/**
* last export time in preferences
*/
public static final String PREF_LAST_EXPORT_TIME = "last_export_time";

public static final String TIMESTAMP_ZERO = new Timestamp(0).toString();
/**
* Adapter for retrieving accounts to export
* Subclasses should close this object when they are done with exporting
Expand Down
Loading

0 comments on commit 904f301

Please sign in to comment.