Skip to content

Commit 65d0b50

Browse files
committed
Merge branch 'feature/csv_export' into develop
2 parents 131c534 + 034bf52 commit 65d0b50

44 files changed

Lines changed: 640 additions & 5 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CONTRIBUTORS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ The following people (in alphabetical order) contributed (commits on GitHub) to
2727
* Falk Brockmann
2828
* Felipe Morato
2929
* Geert Janssens
30+
* Gleb Semyannikov
3031
* Jörg Möller
3132
* Israel Buitron
3233
* Jesse Shieh
@@ -54,6 +55,8 @@ The following people (in alphabetical order) contributed (commits on GitHub) to
5455
* Stephan Windmüller
5556
* Terry Chung
5657
* thesebas thesebas@thesebas.net
58+
* Timur Badretdinov
59+
* Timur Khuzin
5760
* Vladimir Rutsky
5861
* Weslly Oliveira
5962
* windwarrior lennartbuit@gmail.com

app/src/main/java/org/gnucash/android/export/ExportAsyncTask.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
import org.gnucash.android.db.adapter.DatabaseAdapter;
5757
import org.gnucash.android.db.adapter.SplitsDbAdapter;
5858
import org.gnucash.android.db.adapter.TransactionsDbAdapter;
59+
import org.gnucash.android.export.csv.CsvAccountExporter;
60+
import org.gnucash.android.export.csv.CsvTransactionsExporter;
5961
import org.gnucash.android.export.ofx.OfxExporter;
6062
import org.gnucash.android.export.qif.QifExporter;
6163
import org.gnucash.android.export.xml.GncXmlExporter;
@@ -213,7 +215,7 @@ private void dismissProgressDialog() {
213215

214216
/**
215217
* Returns an exporter corresponding to the user settings.
216-
* @return Object of one of {@link QifExporter}, {@link OfxExporter} or {@link GncXmlExporter}
218+
* @return Object of one of {@link QifExporter}, {@link OfxExporter} or {@link GncXmlExporter}, {@Link CsvAccountExporter} or {@Link CsvTransactionsExporter}
217219
*/
218220
private Exporter getExporter() {
219221
switch (mExportParams.getExportFormat()) {
@@ -224,8 +226,11 @@ private Exporter getExporter() {
224226
return new OfxExporter(mExportParams, mDb);
225227

226228
case XML:
227-
default:
228229
return new GncXmlExporter(mExportParams, mDb);
230+
case CSVA:
231+
return new CsvAccountExporter(mExportParams, mDb);
232+
default:
233+
return new CsvTransactionsExporter(mExportParams, mDb);
229234
}
230235
}
231236

app/src/main/java/org/gnucash/android/export/ExportFormat.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
public enum ExportFormat {
2323
QIF("Quicken Interchange Format"),
2424
OFX("Open Financial eXchange"),
25-
XML("GnuCash XML");
25+
XML("GnuCash XML"),
26+
CSVA("GnuCash accounts CSV"),
27+
CSVT("GnuCash transactions CSV");
2628

2729
/**
2830
* Full name of the export format acronym
@@ -45,6 +47,10 @@ public String getExtension(){
4547
return ".ofx";
4648
case XML:
4749
return ".gnca";
50+
case CSVA:
51+
return ".csv";
52+
case CSVT:
53+
return ".csv";
4854
default:
4955
return ".txt";
5056
}

app/src/main/java/org/gnucash/android/export/ExportParams.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ public String getDescription(){
7878
*/
7979
private String mExportLocation;
8080

81+
/**
82+
* CSV-separator char
83+
*/
84+
private char mCsvSeparator = ',';
85+
8186
/**
8287
* Creates a new set of paramters and specifies the export format
8388
* @param format Format to use when exporting the transactions
@@ -169,6 +174,22 @@ public void setExportLocation(String exportLocation){
169174
mExportLocation = exportLocation;
170175
}
171176

177+
/**
178+
* Get the CSV-separator char
179+
* @return CSV-separator char
180+
*/
181+
public char getCsvSeparator(){
182+
return mCsvSeparator;
183+
}
184+
185+
/**
186+
* Set the CSV-separator char
187+
* @param separator CSV-separator char
188+
*/
189+
public void setCsvSeparator(char separator) {
190+
mCsvSeparator = separator;
191+
}
192+
172193
@Override
173194
public String toString() {
174195
return "Export all transactions created since " + TimestampHelper.getUtcStringFromTimestamp(mExportStartTime) + " UTC"

app/src/main/java/org/gnucash/android/export/Exporter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,10 @@ public static String sanitizeFilename(String inputName) {
160160
*/
161161
public static String buildExportFilename(ExportFormat format, String bookName) {
162162
return EXPORT_FILENAME_DATE_FORMAT.format(new Date(System.currentTimeMillis()))
163-
+ "_gnucash_export_" + sanitizeFilename(bookName) + format.getExtension();
163+
+ "_gnucash_export_" + sanitizeFilename(bookName) +
164+
(format==ExportFormat.CSVA?"_accounts_":"") +
165+
(format==ExportFormat.CSVT?"_transactions_":"") +
166+
format.getExtension();
164167
}
165168

166169
/**
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* Copyright (c) 2018 Semyannikov Gleb <nightdevgame@gmail.com>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.gnucash.android.export.csv;
18+
19+
import android.database.sqlite.SQLiteDatabase;
20+
import com.crashlytics.android.Crashlytics;
21+
import org.gnucash.android.export.ExportParams;
22+
import org.gnucash.android.export.Exporter;
23+
import org.gnucash.android.model.Account;
24+
25+
import java.io.BufferedOutputStream;
26+
import java.io.FileOutputStream;
27+
import java.io.IOException;
28+
import java.io.OutputStreamWriter;
29+
import java.util.ArrayList;
30+
import java.util.List;
31+
32+
/**
33+
* Creates a GnuCash CSV account representation of the accounts and transactions
34+
*
35+
* @author Semyannikov Gleb <nightdevgame@gmail.com>
36+
*/
37+
public class CsvAccountExporter extends Exporter{
38+
private char mCsvSeparator;
39+
40+
/**
41+
* Construct a new exporter with export parameters
42+
* @param params Parameters for the export
43+
*/
44+
public CsvAccountExporter(ExportParams params) {
45+
super(params, null);
46+
mCsvSeparator = params.getCsvSeparator();
47+
LOG_TAG = "GncXmlExporter";
48+
}
49+
50+
/**
51+
* Overloaded constructor.
52+
* Creates an exporter with an already open database instance.
53+
* @param params Parameters for the export
54+
* @param db SQLite database
55+
*/
56+
public CsvAccountExporter(ExportParams params, SQLiteDatabase db) {
57+
super(params, db);
58+
mCsvSeparator = params.getCsvSeparator();
59+
LOG_TAG = "GncXmlExporter";
60+
}
61+
62+
@Override
63+
public List<String> generateExport() throws ExporterException {
64+
OutputStreamWriter writerStream = null;
65+
CsvWriter writer = null;
66+
String outputFile = getExportCacheFilePath();
67+
try {
68+
FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
69+
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
70+
writerStream = new OutputStreamWriter(bufferedOutputStream);
71+
writer = new CsvWriter(writerStream);
72+
generateExport(writer);
73+
} catch (IOException ex){
74+
Crashlytics.log("Error exporting CSV");
75+
Crashlytics.logException(ex);
76+
} finally {
77+
if (writerStream != null) {
78+
try {
79+
writerStream.close();
80+
} catch (IOException e) {
81+
throw new ExporterException(mExportParams, e);
82+
}
83+
}
84+
}
85+
86+
List<String> exportedFiles = new ArrayList<>();
87+
exportedFiles.add(outputFile);
88+
89+
return exportedFiles;
90+
}
91+
92+
public void generateExport(final CsvWriter writer) throws ExporterException {
93+
try {
94+
String separator = mCsvSeparator + "";
95+
List<String> names = new ArrayList<String>();
96+
names.add("type");
97+
names.add("full_name");
98+
names.add("name");
99+
names.add("code");
100+
names.add("description");
101+
names.add("color");
102+
names.add("notes");
103+
names.add("commoditym");
104+
names.add("commodityn");
105+
names.add("hidden");
106+
names.add("tax");
107+
names.add("place_holder");
108+
109+
List<Account> accounts = mAccountsDbAdapter.getAllRecords();
110+
111+
for(int i = 0; i < names.size(); i++) {
112+
writer.write(names.get(i) + separator);
113+
}
114+
writer.write("\n");
115+
for(int i = 0; i < accounts.size(); i++) {
116+
Account account = accounts.get(i);
117+
118+
writer.write(account.getAccountType().toString() + separator);
119+
writer.write(account.getFullName() + separator);
120+
writer.write(account.getName() + separator);
121+
122+
//Code
123+
writer.write(separator);
124+
125+
writer.write(account.getDescription() + separator);
126+
writer.write(account.getColor() + separator);
127+
128+
//Notes
129+
writer.write(separator);
130+
131+
writer.write(account.getCommodity().getCurrencyCode() + separator);
132+
writer.write("CURRENCY" + separator);
133+
writer.write(account.isHidden()?"T":"F" + separator);
134+
135+
writer.write("F" + separator);
136+
137+
writer.write(account.isPlaceholderAccount()?"T":"F" + separator);
138+
139+
writer.write("\n");
140+
}
141+
} catch (Exception e) {
142+
Crashlytics.logException(e);
143+
throw new ExporterException(mExportParams, e);
144+
}
145+
}
146+
}

0 commit comments

Comments
 (0)