Skip to content

Commit 494ca49

Browse files
committed
Fixed: Transaction notes neither imported nor exported
Refactored transactions - "name" becomes "description", and "description" becomes "notes", to match GnuCash XML nomenclature
1 parent 67d24c5 commit 494ca49

17 files changed

+157
-174
lines changed

app/src/org/gnucash/android/db/AccountsDbAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,7 @@ public List<Transaction> getAllOpeningBalanceTransactions(){
965965
continue;
966966

967967
Transaction transaction = new Transaction(mContext.getString(R.string.account_name_opening_balances));
968-
transaction.setDescription(getName(id));
968+
transaction.setNote(getName(id));
969969
transaction.setCurrencyCode(currencyCode);
970970
TransactionType transactionType = Transaction.getTypeForBalance(getAccountType(accountUID),
971971
balance.isNegative());

app/src/org/gnucash/android/db/DatabaseHelper.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import android.util.Log;
2626
import android.widget.Toast;
2727
import org.gnucash.android.export.ExportFormat;
28-
import org.gnucash.android.model.Account;
2928
import org.gnucash.android.model.AccountType;
3029

3130
import static org.gnucash.android.db.DatabaseSchema.*;
@@ -79,8 +78,8 @@ public class DatabaseHelper extends SQLiteOpenHelper {
7978
private static final String TRANSACTIONS_TABLE_CREATE = "create table " + TransactionEntry.TABLE_NAME + " ("
8079
+ TransactionEntry._ID + " integer primary key autoincrement, "
8180
+ TransactionEntry.COLUMN_UID + " varchar(255) not null, "
82-
+ TransactionEntry.COLUMN_NAME + " varchar(255), "
83-
+ TransactionEntry.COLUMN_DESCRIPTION + " text, "
81+
+ TransactionEntry.COLUMN_DESCRIPTION + " varchar(255), "
82+
+ TransactionEntry.COLUMN_NOTES + " text, "
8483
+ TransactionEntry.COLUMN_TIMESTAMP + " integer not null, "
8584
+ TransactionEntry.COLUMN_EXPORTED + " tinyint default 0, "
8685
+ TransactionEntry.COLUMN_CURRENCY + " varchar(255) not null, "

app/src/org/gnucash/android/db/DatabaseSchema.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ public static abstract class AccountEntry implements CommonColumns {
6868
public static abstract class TransactionEntry implements CommonColumns {
6969

7070
public static final String TABLE_NAME = "transactions";
71-
72-
public static final String COLUMN_NAME = "name";
73-
public static final String COLUMN_DESCRIPTION = "description";
71+
//The actual names of columns for description and notes are unlike the variable names because of legacy
72+
//We will not change them now for backwards compatibility reasons. But the variable names make sense
73+
public static final String COLUMN_DESCRIPTION = "name";
74+
public static final String COLUMN_NOTES = "description";
7475
public static final String COLUMN_CURRENCY = "currency_code";
7576
public static final String COLUMN_TIMESTAMP = "timestamp";
7677
public static final String COLUMN_EXPORTED = "is_exported";

app/src/org/gnucash/android/db/TransactionsDbAdapter.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import static org.gnucash.android.db.DatabaseSchema.*;
3030

3131
import java.util.ArrayList;
32-
import java.util.Currency;
3332
import java.util.List;
3433

3534
/**
@@ -75,10 +74,10 @@ public void close() {
7574
*/
7675
public long addTransaction(Transaction transaction){
7776
ContentValues contentValues = new ContentValues();
78-
contentValues.put(TransactionEntry.COLUMN_NAME, transaction.getName());
77+
contentValues.put(TransactionEntry.COLUMN_DESCRIPTION, transaction.getDescription());
7978
contentValues.put(TransactionEntry.COLUMN_UID, transaction.getUID());
8079
contentValues.put(TransactionEntry.COLUMN_TIMESTAMP, transaction.getTimeMillis());
81-
contentValues.put(TransactionEntry.COLUMN_DESCRIPTION, transaction.getDescription());
80+
contentValues.put(TransactionEntry.COLUMN_NOTES, transaction.getNote());
8281
contentValues.put(TransactionEntry.COLUMN_EXPORTED, transaction.isExported() ? 1 : 0);
8382
contentValues.put(TransactionEntry.COLUMN_CURRENCY, transaction.getCurrencyCode());
8483
contentValues.put(TransactionEntry.COLUMN_RECURRENCE_PERIOD, transaction.getRecurrencePeriod());
@@ -264,11 +263,11 @@ public int getTotalTransactionsCount(){
264263
* @return {@link Transaction} object constructed from database record
265264
*/
266265
public Transaction buildTransactionInstance(Cursor c){
267-
String name = c.getString(c.getColumnIndexOrThrow(TransactionEntry.COLUMN_NAME));
266+
String name = c.getString(c.getColumnIndexOrThrow(TransactionEntry.COLUMN_DESCRIPTION));
268267
Transaction transaction = new Transaction(name);
269268
transaction.setUID(c.getString(c.getColumnIndexOrThrow(TransactionEntry.COLUMN_UID)));
270269
transaction.setTime(c.getLong(c.getColumnIndexOrThrow(TransactionEntry.COLUMN_TIMESTAMP)));
271-
transaction.setDescription(c.getString(c.getColumnIndexOrThrow(TransactionEntry.COLUMN_DESCRIPTION)));
270+
transaction.setNote(c.getString(c.getColumnIndexOrThrow(TransactionEntry.COLUMN_NOTES)));
272271
transaction.setExported(c.getInt(c.getColumnIndexOrThrow(TransactionEntry.COLUMN_EXPORTED)) == 1);
273272

274273
long recurrencePeriod = c.getLong(c.getColumnIndexOrThrow(TransactionEntry.COLUMN_RECURRENCE_PERIOD));
@@ -465,15 +464,15 @@ public Cursor fetchRecord(long rowId) {
465464
* @return Cursor to the data set containing all matching transactions
466465
*/
467466
public Cursor fetchTransactionsStartingWith(String prefix){
468-
StringBuffer stringBuffer = new StringBuffer(TransactionEntry.COLUMN_NAME)
467+
StringBuffer stringBuffer = new StringBuffer(TransactionEntry.COLUMN_DESCRIPTION)
469468
.append(" LIKE '").append(prefix).append("%'");
470469
String selection = stringBuffer.toString();
471470

472471
Cursor c = mDb.query(TransactionEntry.TABLE_NAME,
473-
new String[]{TransactionEntry._ID, TransactionEntry.COLUMN_NAME},
472+
new String[]{TransactionEntry._ID, TransactionEntry.COLUMN_DESCRIPTION},
474473
selection,
475474
null, null, null,
476-
TransactionEntry.COLUMN_NAME + " ASC");
475+
TransactionEntry.COLUMN_DESCRIPTION + " ASC");
477476
return c;
478477
}
479478

app/src/org/gnucash/android/export/xml/GncXmlExporter.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,32 +96,32 @@ private void generateGncXml() throws ParserConfigurationException {
9696
rootElement.setAttribute("xmlns:ts", "http://www.gnucash.org/XML/ts");
9797

9898
Element bookCountNode = mDocument.createElement(GncXmlHelper.TAG_COUNT_DATA);
99-
bookCountNode.setAttribute("cd:type", "book");
99+
bookCountNode.setAttribute(GncXmlHelper.ATTR_KEY_CD_TYPE, GncXmlHelper.ATTR_VALUE_BOOK);
100100
bookCountNode.appendChild(mDocument.createTextNode("1"));
101101
rootElement.appendChild(bookCountNode);
102102

103103
Element bookNode = mDocument.createElement(GncXmlHelper.TAG_BOOK);
104-
bookNode.setAttribute("version", GncXmlHelper.BOOK_VERSION);
104+
bookNode.setAttribute(GncXmlHelper.ATTR_KEY_VERSION, GncXmlHelper.BOOK_VERSION);
105105
rootElement.appendChild(bookNode);
106106

107107
Element bookIdNode = mDocument.createElement(GncXmlHelper.TAG_BOOK_ID);
108-
bookIdNode.setAttribute("type", "guid");
108+
bookIdNode.setAttribute(GncXmlHelper.ATTR_KEY_TYPE, GncXmlHelper.ATTR_VALUE_GUID);
109109
bookIdNode.appendChild(mDocument.createTextNode(UUID.randomUUID().toString().replaceAll("-", "")));
110110
bookNode.appendChild(bookIdNode);
111111

112112
Element cmdtyCountData = mDocument.createElement(GncXmlHelper.TAG_COUNT_DATA);
113-
cmdtyCountData.setAttribute("cd:type", "commodity");
113+
cmdtyCountData.setAttribute(GncXmlHelper.ATTR_KEY_CD_TYPE, "commodity");
114114
cmdtyCountData.appendChild(mDocument.createTextNode(String.valueOf(mAccountsDbAdapter.getCurrencies().size())));
115115
bookNode.appendChild(cmdtyCountData);
116116

117117
Element accountCountNode = mDocument.createElement(GncXmlHelper.TAG_COUNT_DATA);
118-
accountCountNode.setAttribute("cd:type", "account");
118+
accountCountNode.setAttribute(GncXmlHelper.ATTR_KEY_CD_TYPE, "account");
119119
int accountCount = mAccountsDbAdapter.getTotalAccountCount();
120120
accountCountNode.appendChild(mDocument.createTextNode(String.valueOf(accountCount)));
121121
bookNode.appendChild(accountCountNode);
122122

123123
Element transactionCountNode = mDocument.createElement(GncXmlHelper.TAG_COUNT_DATA);
124-
transactionCountNode.setAttribute("cd:type", "transaction");
124+
transactionCountNode.setAttribute(GncXmlHelper.ATTR_KEY_CD_TYPE, "transaction");
125125
int transactionCount = mTransactionsDbAdapter.getTotalTransactionsCount();
126126
transactionCountNode.appendChild(mDocument.createTextNode(String.valueOf(transactionCount)));
127127
bookNode.appendChild(transactionCountNode);

app/src/org/gnucash/android/export/xml/GncXmlHelper.java

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import org.gnucash.android.model.Money;
44
import org.gnucash.android.model.Split;
55
import org.gnucash.android.model.TransactionType;
6+
import org.w3c.dom.Document;
7+
import org.w3c.dom.Element;
68

79
import java.math.BigDecimal;
810
import java.text.ParseException;
@@ -16,14 +18,22 @@
1618
*/
1719
public abstract class GncXmlHelper {
1820
public static final String TAG_PREFIX = "gnc:";
21+
22+
public static final String ATTR_KEY_CD_TYPE = "cd:type";
23+
public static final String ATTR_KEY_TYPE = "type";
24+
public static final String ATTR_KEY_VERSION = "version";
25+
public static final String ATTR_VALUE_STRING = "string";
26+
public static final String ATTR_VALUE_GUID = "guid";
27+
public static final String ATTR_VALUE_BOOK = "book";
28+
public static final String ATTR_VALUE_GDATE = "gdate";
29+
1930
/*
2031
Qualified GnuCash XML tag names
2132
*/
2233
public static final String TAG_ROOT = "gnc-v2";
2334
public static final String TAG_BOOK = "gnc:book";
2435
public static final String TAG_BOOK_ID = "book:id";
2536
public static final String TAG_COUNT_DATA = "gnc:count-data";
26-
public static final String ATTRIBUTE_CD_TYPE = "cd:type";
2737

2838
public static final String TAG_COMMODITY = "gnc:commodity";
2939
public static final String TAG_NAME = "act:name";
@@ -46,9 +56,10 @@ public abstract class GncXmlHelper {
4656
public static final String TAG_DATE_POSTED = "trn:date-posted";
4757
public static final String TAG_DATE = "ts:date";
4858
public static final String TAG_DATE_ENTERED = "trn:date-entered";
49-
public static final String TAG_TRX_DESCRIPTION = "trn:description";
50-
public static final String TAG_TRX_SPLITS = "trn:splits";
51-
public static final String TAG_TRX_SPLIT = "trn:split";
59+
public static final String TAG_TRN_DESCRIPTION = "trn:description";
60+
public static final String TAG_TRN_SPLITS = "trn:splits";
61+
public static final String TAG_TRN_SPLIT = "trn:split";
62+
public static final String TAG_TRN_SLOTS = "trn:slots";
5263

5364
public static final String TAG_SPLIT_ID = "split:id";
5465
public static final String TAG_SPLIT_MEMO = "split:memo";
@@ -61,15 +72,21 @@ public abstract class GncXmlHelper {
6172
public static final String TAG_RECURRENCE_PERIOD = "trn:recurrence_period";
6273

6374
public static final String BOOK_VERSION = "2.0.0";
64-
public static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
75+
public static final SimpleDateFormat TIME_FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
76+
77+
78+
public static final String KEY_PLACEHOLDER = "placeholder";
79+
public static final String KEY_COLOR = "color";
80+
public static final String KEY_FAVORITE = "favorite";
81+
public static final String KEY_NOTES = "notes";
6582

6683

6784
/**
6885
* Formats dates for the GnuCash XML format
6986
* @param milliseconds Milliseconds since epoch
7087
*/
7188
public static String formatDate(long milliseconds){
72-
return DATE_FORMATTER.format(new Date(milliseconds));
89+
return TIME_FORMATTER.format(new Date(milliseconds));
7390
}
7491

7592
/**
@@ -79,7 +96,7 @@ public static String formatDate(long milliseconds){
7996
* @throws ParseException if the date string could not be parsed e.g. because of different format
8097
*/
8198
public static long parseDate(String dateString) throws ParseException {
82-
Date date = DATE_FORMATTER.parse(dateString);
99+
Date date = TIME_FORMATTER.parse(dateString);
83100
return date.getTime();
84101
}
85102

@@ -106,4 +123,25 @@ public static BigDecimal parseMoney(String amountString){
106123

107124
return numerator.divide(denominator);
108125
}
126+
127+
/**
128+
* Helper method for creating slot key-value pairs in the GnuCash XML structure.
129+
* <p>This method is only a helper for creating slots whose values are of string type</p>
130+
* @param doc {@link org.w3c.dom.Document} for creating nodes
131+
* @param key Slot key as string
132+
* @param value Slot value as String
133+
* @return Element node containing the key-value pair
134+
*/
135+
public static Element createSlot(Document doc, String key, String value){
136+
Element slotNode = doc.createElement(TAG_SLOT);
137+
Element slotKeyNode = doc.createElement(TAG_SLOT_KEY);
138+
slotKeyNode.appendChild(doc.createTextNode(key));
139+
Element slotValueNode = doc.createElement(TAG_SLOT_VALUE);
140+
slotValueNode.setAttribute(ATTR_KEY_TYPE, ATTR_VALUE_STRING);
141+
slotValueNode.appendChild(doc.createTextNode(value));
142+
slotNode.appendChild(slotKeyNode);
143+
slotNode.appendChild(slotValueNode);
144+
145+
return slotNode;
146+
}
109147
}

app/src/org/gnucash/android/importer/GncXmlHandler.java

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,15 @@
1919
import android.content.Context;
2020
import android.database.sqlite.SQLiteDatabase;
2121
import android.util.Log;
22-
import android.widget.Toast;
23-
import org.gnucash.android.R;
2422
import org.gnucash.android.app.GnuCashApplication;
2523
import org.gnucash.android.db.TransactionsDbAdapter;
2624
import org.gnucash.android.export.xml.GncXmlHelper;
2725
import org.gnucash.android.model.*;
2826
import org.gnucash.android.db.AccountsDbAdapter;
2927
import org.xml.sax.Attributes;
30-
import org.xml.sax.InputSource;
3128
import org.xml.sax.SAXException;
32-
import org.xml.sax.XMLReader;
3329
import org.xml.sax.helpers.DefaultHandler;
3430

35-
import javax.xml.parsers.ParserConfigurationException;
36-
import javax.xml.parsers.SAXParser;
37-
import javax.xml.parsers.SAXParserFactory;
38-
import java.io.*;
3931
import java.text.ParseException;
4032
import java.util.Currency;
4133
import java.util.regex.Pattern;
@@ -59,20 +51,8 @@ public class GncXmlHandler extends DefaultHandler {
5951
private static final String LOG_TAG = "GnuCashAccountImporter";
6052

6153
/**
62-
* Value for placeholder slots in GnuCash account structure file
54+
* Adapter for saving the imported accounts
6355
*/
64-
private static final String PLACEHOLDER_KEY = "placeholder";
65-
66-
/**
67-
* Value of color slots in GnuCash account structure file
68-
*/
69-
private static final String COLOR_KEY = "color";
70-
71-
/**
72-
* Value of favorite slots in GnuCash account structure file
73-
*/
74-
private static final String FAVORITE_KEY = "favorite";
75-
7656
AccountsDbAdapter mAccountsDbAdapter;
7757

7858
/**
@@ -100,6 +80,7 @@ public class GncXmlHandler extends DefaultHandler {
10080
boolean mInFavoriteSlot = false;
10181
boolean mISO4217Currency = false;
10282
boolean mIsDatePosted = false;
83+
boolean mIsNote = false;
10384

10485
private Context mContext;
10586
private TransactionsDbAdapter mTransactionsDbAdapter;
@@ -133,7 +114,7 @@ public void startElement(String uri, String localName,
133114
mTransaction = new Transaction(""); //dummy name will be replaced
134115
}
135116

136-
if (qualifiedName.equalsIgnoreCase(GncXmlHelper.TAG_TRX_SPLIT)){
117+
if (qualifiedName.equalsIgnoreCase(GncXmlHelper.TAG_TRN_SPLIT)){
137118
mSplit = new Split(Money.getZeroInstance(),"");
138119
}
139120

@@ -190,16 +171,20 @@ public void endElement(String uri, String localName, String qualifiedName) throw
190171
}
191172

192173
if (qualifiedName.equalsIgnoreCase(GncXmlHelper.TAG_SLOT_KEY)){
193-
if (characterString.equals(PLACEHOLDER_KEY)){
174+
if (characterString.equals(GncXmlHelper.KEY_PLACEHOLDER)){
194175
mInPlaceHolderSlot = true;
195176
}
196-
if (characterString.equals(COLOR_KEY)){
177+
if (characterString.equals(GncXmlHelper.KEY_COLOR)){
197178
mInColorSlot = true;
198179
}
199180

200-
if (characterString.equals(FAVORITE_KEY)){
181+
if (characterString.equals(GncXmlHelper.KEY_FAVORITE)){
201182
mInFavoriteSlot = true;
202183
}
184+
185+
if (characterString.equals(GncXmlHelper.KEY_NOTES)){
186+
mIsNote = true;
187+
}
203188
}
204189

205190
if (qualifiedName.equalsIgnoreCase(GncXmlHelper.TAG_SLOT_VALUE)){
@@ -231,6 +216,13 @@ public void endElement(String uri, String localName, String qualifiedName) throw
231216
mAccount.setFavorite(Boolean.parseBoolean(characterString));
232217
mInFavoriteSlot = false;
233218
}
219+
220+
if (mIsNote){
221+
if (mTransaction != null){
222+
mTransaction.setNote(characterString);
223+
mIsNote = false;
224+
}
225+
}
234226
}
235227

236228

@@ -239,8 +231,8 @@ public void endElement(String uri, String localName, String qualifiedName) throw
239231
mTransaction.setUID(characterString);
240232
}
241233

242-
if (qualifiedName.equalsIgnoreCase(GncXmlHelper.TAG_TRX_DESCRIPTION)){
243-
mTransaction.setName(characterString);
234+
if (qualifiedName.equalsIgnoreCase(GncXmlHelper.TAG_TRN_DESCRIPTION)){
235+
mTransaction.setDescription(characterString);
244236
}
245237

246238
if (qualifiedName.equalsIgnoreCase(GncXmlHelper.TAG_DATE)){
@@ -277,7 +269,7 @@ public void endElement(String uri, String localName, String qualifiedName) throw
277269
mSplit.setAccountUID(characterString);
278270
}
279271

280-
if (qualifiedName.equals(GncXmlHelper.TAG_TRX_SPLIT)){
272+
if (qualifiedName.equals(GncXmlHelper.TAG_TRN_SPLIT)){
281273
mTransaction.addSplit(mSplit);
282274
}
283275

0 commit comments

Comments
 (0)