Skip to content

Commit

Permalink
Imported file name.
Browse files Browse the repository at this point in the history
  • Loading branch information
pnemonic78 committed Jun 23, 2024
1 parent dadf236 commit f8a9588
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.net.Uri;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
Expand Down Expand Up @@ -56,7 +57,6 @@ public BooksDbAdapter(SQLiteDatabase db) {
BookEntry.COLUMN_TEMPLATE_GUID,
BookEntry.COLUMN_SOURCE_URI,
BookEntry.COLUMN_ACTIVE,
BookEntry.COLUMN_UID,
BookEntry.COLUMN_LAST_SYNC
});
}
Expand Down Expand Up @@ -93,15 +93,15 @@ public Book buildModelInstance(@NonNull Cursor cursor) {
@Override
protected @NonNull SQLiteStatement setBindings(@NonNull SQLiteStatement stmt, @NonNull final Book book) {
stmt.clearBindings();
String displayName = book.getDisplayName() == null ? generateDefaultBookName() : book.getDisplayName();
String displayName = TextUtils.isEmpty(book.getDisplayName()) ? generateDefaultBookName() : book.getDisplayName();
stmt.bindString(1, displayName);
stmt.bindString(2, book.getRootAccountUID());
stmt.bindString(3, book.getRootTemplateUID());
if (book.getSourceUri() != null)
stmt.bindString(4, book.getSourceUri().toString());
stmt.bindLong(5, book.isActive() ? 1L : 0L);
stmt.bindString(6, book.getUID());
stmt.bindString(7, TimestampHelper.getUtcStringFromTimestamp(book.getLastSync()));
stmt.bindString(6, TimestampHelper.getUtcStringFromTimestamp(book.getLastSync()));
stmt.bindString(7, book.getUID());
return stmt;
}

Expand Down Expand Up @@ -317,11 +317,10 @@ public static boolean isBookDatabase(String databaseName) {

String sql = "SELECT COUNT(*) FROM " + mTableName + " WHERE " + BookEntry.COLUMN_DISPLAY_NAME + " = ?";
SQLiteStatement statement = mDb.compileStatement(sql);
Context context = GnuCashApplication.getAppContext();

while (true) {
Context context = GnuCashApplication.getAppContext();
String name = context.getString(R.string.book_default_name, bookCount);
//String name = "Book" + " " + bookCount;

statement.clearBindings();
statement.bindString(1, name);
Expand Down
13 changes: 11 additions & 2 deletions app/src/main/java/org/gnucash/android/importer/GncXmlHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,7 @@ private void saveToDatabase() {
mAccountsDbAdapter.enableForeignKey(false);
Timber.d("before clean up db");
mAccountsDbAdapter.deleteAllRecords();
Timber.d("deb clean up done %d ns", System.nanoTime() - startTime);
Timber.d("db clean up done %d ns", System.nanoTime() - startTime);
long nAccounts = mAccountsDbAdapter.bulkAddRecords(mAccountList, DatabaseAdapter.UpdateMethod.insert);
Timber.d("%d accounts inserted", nAccounts);
//We need to add scheduled actions first because there is a foreign key constraint on transactions
Expand Down Expand Up @@ -1015,7 +1015,16 @@ private void saveToDatabase() {
* @return GUID of the newly imported book
*/
public @NonNull String getBookUID() {
return mBook.getUID();
return getBook().getUID();
}

/**
* Returns the just-imported book
*
* @return the newly imported book
*/
public @NonNull Book getBook() {
return mBook;
}

/**
Expand Down
16 changes: 14 additions & 2 deletions app/src/main/java/org/gnucash/android/importer/GncXmlImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.gnucash.android.importer;

import org.gnucash.android.db.adapter.TransactionsDbAdapter;
import org.gnucash.android.model.Book;
import org.gnucash.android.util.PreferencesHelper;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
Expand Down Expand Up @@ -48,6 +49,16 @@ public class GncXmlImporter {
* @return GUID of the book into which the XML was imported
*/
public static String parse(InputStream gncXmlInputStream) throws ParserConfigurationException, SAXException, IOException {
return parseBook(gncXmlInputStream).getUID();
}

/**
* Parse GnuCash XML input and populates the database
*
* @param gncXmlInputStream InputStream source of the GnuCash XML file
* @return the book into which the XML was imported
*/
public static Book parseBook(InputStream gncXmlInputStream) throws ParserConfigurationException, SAXException, IOException {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
Expand All @@ -71,12 +82,13 @@ public static String parse(InputStream gncXmlInputStream) throws ParserConfigura
long endTime = System.nanoTime();
Timber.d("%d ns spent on importing the file", endTime - startTime);

String bookUID = handler.getBookUID();
Book book = handler.getBook();
String bookUID = book.getUID();
PreferencesHelper.setLastExportTime(
TransactionsDbAdapter.getInstance().getTimestampOfLastModification(),
bookUID
);

return bookUID;
return book;
}
}
34 changes: 16 additions & 18 deletions app/src/main/java/org/gnucash/android/importer/ImportAsyncTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.gnucash.android.R;
import org.gnucash.android.db.DatabaseSchema;
import org.gnucash.android.db.adapter.BooksDbAdapter;
import org.gnucash.android.model.Book;
import org.gnucash.android.ui.common.GnucashProgressDialog;
import org.gnucash.android.ui.util.TaskDelegate;
import org.gnucash.android.util.BackupManager;
Expand Down Expand Up @@ -83,34 +84,31 @@ protected String doInBackground(Uri... uris) {

Uri uri = uris[0];
ContentResolver contentResolver = mContext.getContentResolver();
Book book;
String bookUID;
try {
InputStream accountInputStream = contentResolver.openInputStream(uri);
bookUID = GncXmlImporter.parse(accountInputStream);
book = GncXmlImporter.parseBook(accountInputStream);
book.setSourceUri(uri);
bookUID = book.getUID();
} catch (final Throwable e) {
Timber.e(e, "Error importing: %s", uri);

mContext.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(mContext,
mContext.getString(R.string.toast_error_importing_accounts) + "\n" + e.getLocalizedMessage(),
Toast.LENGTH_LONG).show();
}
});

return null;
}

String displayName = ContentExtKt.getDocumentName(uri, mContext);
// Remove short file type extension, e.g. ".xml" or ".gnca".
int indexFileType = displayName.lastIndexOf('.');
if ((indexFileType > 0) && (indexFileType + 5 >= displayName.length())) {
displayName = displayName.substring(0, indexFileType);
}
ContentValues contentValues = new ContentValues();
contentValues.put(DatabaseSchema.BookEntry.COLUMN_DISPLAY_NAME, displayName);
contentValues.put(DatabaseSchema.BookEntry.COLUMN_SOURCE_URI, uri.toString());

String displayName = book.getDisplayName();
if (TextUtils.isEmpty(displayName)) {
displayName = ContentExtKt.getDocumentName(uri, mContext);
// Remove short file type extension, e.g. ".xml" or ".gnca".
int indexFileType = displayName.lastIndexOf('.');
if ((indexFileType > 0) && (indexFileType + 5 >= displayName.length())) {
displayName = displayName.substring(0, indexFileType);
}
contentValues.put(DatabaseSchema.BookEntry.COLUMN_DISPLAY_NAME, displayName);
}
BooksDbAdapter.getInstance().updateRecord(bookUID, contentValues);

//set the preferences to their default values
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/gnucash/android/util/ContentExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ private const val INDEX_DOCUMENT_NAME = 0
fun Uri.getDocumentName(context: Context?): String {
var name: String = this.authority ?: this.host ?: ""
val lastPath = this.lastPathSegment
if ((lastPath != null) && lastPath.indexOf('.') > 0) {
if (!lastPath.isNullOrEmpty()) {
name = lastPath
val indexSlash = lastPath.lastIndexOf('/')
if ((indexSlash >= 0) && (indexSlash < name.lastIndex)) {
Expand Down

0 comments on commit f8a9588

Please sign in to comment.