Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drop DB before importing #1389

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions app/src/main/java/protect/card_locker/DBHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import java.util.Arrays;
import java.util.Currency;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Catima.db";
Expand Down Expand Up @@ -323,6 +325,37 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}

public static void clearDatabase(final SQLiteDatabase db) {
db.execSQL("DELETE FROM " + LoyaltyCardDbGroups.TABLE);
db.execSQL("DELETE FROM " + LoyaltyCardDbIds.TABLE);
db.execSQL("DELETE FROM " + LoyaltyCardDbIdsGroups.TABLE);
}

public static void clearImageFiles(Context context, final Set<String> before, final Set<String> after) {
Set<String> deleted = new HashSet<>(before);
if (after != null) {
deleted.removeAll(after);
}
for (String name : deleted) {
context.deleteFile(name);
}
}

public static Set<String> imageFiles(Context context, final SQLiteDatabase database) {
Set<String> files = new HashSet<>();
Cursor cardCursor = getLoyaltyCardCursor(database);
while (cardCursor.moveToNext()) {
LoyaltyCard card = LoyaltyCard.toLoyaltyCard(cardCursor);
for (ImageLocationType imageLocationType : ImageLocationType.values()) {
String name = Utils.getCardImageFileName(card.id, imageLocationType);
if (Utils.retrieveCardImageAsFile(context, name).exists()) {
files.add(name);
}
}
}
return files;
}

private static ContentValues generateFTSContentValues(final int id, final String store, final String note) {
// FTS on Android is severely limited and can only search for word starting with a certain string
// So for each word, we grab every single substring
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import java.util.ArrayList;
import java.util.Currency;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import protect.card_locker.CatimaBarcode;
import protect.card_locker.DBHelper;
Expand All @@ -39,7 +41,8 @@
* A header is expected for the each table showing the names of the columns.
*/
public class CatimaImporter implements Importer {
public void importData(Context context, SQLiteDatabase database, InputStream input, char[] password) throws IOException, FormatException, InterruptedException {
public Set<String> importData(Context context, SQLiteDatabase database, InputStream input, char[] password) throws IOException, FormatException, InterruptedException {
Set<String> imageFiles = new HashSet<>();
InputStream bufferedInputStream = new BufferedInputStream(input);
bufferedInputStream.mark(100);

Expand All @@ -57,6 +60,7 @@ public void importData(Context context, SQLiteDatabase database, InputStream inp
importCSV(context, database, zipInputStream);
} else if (fileName.endsWith(".png")) {
Utils.saveCardImage(context, ZipUtils.readImage(zipInputStream), fileName);
imageFiles.add(fileName);
} else {
throw new FormatException("Unexpected file in import: " + fileName);
}
Expand All @@ -69,6 +73,8 @@ public void importData(Context context, SQLiteDatabase database, InputStream inp
}

input.close();

return imageFiles;
}

public void importCSV(Context context, SQLiteDatabase database, InputStream input) throws IOException, FormatException, InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.util.Set;

import protect.card_locker.CatimaBarcode;
import protect.card_locker.DBHelper;
Expand All @@ -31,7 +32,7 @@
* A header is expected for the each table showing the names of the columns.
*/
public class FidmeImporter implements Importer {
public void importData(Context context, SQLiteDatabase database, InputStream input, char[] password) throws IOException, FormatException, JSONException, ParseException {
public Set<String> importData(Context context, SQLiteDatabase database, InputStream input, char[] password) throws IOException, FormatException, JSONException, ParseException {
// We actually retrieve a .zip file
ZipInputStream zipInputStream = new ZipInputStream(input, password);

Expand Down Expand Up @@ -70,6 +71,8 @@ public void importData(Context context, SQLiteDatabase database, InputStream inp
}

zipInputStream.close();

return null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.Set;

import protect.card_locker.FormatException;

Expand All @@ -23,5 +24,5 @@ public interface Importer {
* @throws IOException
* @throws FormatException
*/
void importData(Context context, SQLiteDatabase database, InputStream input, char[] password) throws IOException, FormatException, InterruptedException, JSONException, ParseException;
Set<String> importData(Context context, SQLiteDatabase database, InputStream input, char[] password) throws IOException, FormatException, InterruptedException, JSONException, ParseException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import net.lingala.zip4j.exception.ZipException;

import java.io.InputStream;
import java.util.Set;

import protect.card_locker.DBHelper;

public class MultiFormatImporter {
private static final String TAG = "Catima";
Expand All @@ -17,6 +20,8 @@ public class MultiFormatImporter {
* <p>
* The input stream is not closed, and doing so is the
* responsibility of the caller.
* <p>
* NB: this deletes all existing data!
*
* @return ImportExportResult.Success if the database was successfully imported,
* or another result otherwise. If no Success, no data was written to
Expand All @@ -43,9 +48,12 @@ public static ImportExportResult importData(Context context, SQLiteDatabase data
String error = null;
if (importer != null) {
database.beginTransaction();
Set<String> imageFilesBefore = DBHelper.imageFiles(context, database);
DBHelper.clearDatabase(database);
try {
importer.importData(context, database, input, password);
Set<String> imageFilesAfter = importer.importData(context, database, input, password);
database.setTransactionSuccessful();
DBHelper.clearImageFiles(context, imageFilesBefore, imageFilesAfter);
return new ImportExportResult(ImportExportResultType.Success);
} catch (ZipException e) {
if (e.getType().equals(ZipException.Type.WRONG_PASSWORD)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import protect.card_locker.CatimaBarcode;
import protect.card_locker.DBHelper;
Expand All @@ -42,7 +44,8 @@
public class StocardImporter implements Importer {
private static final String TAG = "Catima";

public void importData(Context context, SQLiteDatabase database, InputStream input, char[] password) throws IOException, FormatException, JSONException, ParseException {
public Set<String> importData(Context context, SQLiteDatabase database, InputStream input, char[] password) throws IOException, FormatException, JSONException, ParseException {
Set<String> imageFiles = new HashSet<>();
HashMap<String, HashMap<String, Object>> loyaltyCardHashMap = new HashMap<>();
HashMap<String, HashMap<String, Object>> providers = new HashMap<>();

Expand Down Expand Up @@ -233,18 +236,26 @@ public void importData(Context context, SQLiteDatabase database, InputStream inp
long loyaltyCardInternalId = DBHelper.insertLoyaltyCard(database, store, note, null, null, BigDecimal.valueOf(0), null, cardId, null, barcodeType, headerColor, 0, null,0);

if (cardIcon != null) {
Utils.saveCardImage(context, cardIcon, (int) loyaltyCardInternalId, ImageLocationType.icon);
String fileName = Utils.getCardImageFileName((int) loyaltyCardInternalId, ImageLocationType.icon);
Utils.saveCardImage(context, cardIcon, fileName);
imageFiles.add(fileName);
}

if (loyaltyCardData.containsKey("frontImage")) {
Utils.saveCardImage(context, (Bitmap) loyaltyCardData.get("frontImage"), (int) loyaltyCardInternalId, ImageLocationType.front);
String fileName = Utils.getCardImageFileName((int) loyaltyCardInternalId, ImageLocationType.front);
Utils.saveCardImage(context, (Bitmap) loyaltyCardData.get("frontImage"), fileName);
imageFiles.add(fileName);
}
if (loyaltyCardData.containsKey("backImage")) {
Utils.saveCardImage(context, (Bitmap) loyaltyCardData.get("backImage"), (int) loyaltyCardInternalId, ImageLocationType.back);
String fileName = Utils.getCardImageFileName((int) loyaltyCardInternalId, ImageLocationType.back);
Utils.saveCardImage(context, (Bitmap) loyaltyCardData.get("backImage"), fileName);
imageFiles.add(fileName);
}
}

zipInputStream.close();

return imageFiles;
}

private boolean startsWith(String[] full, String[] start, int minExtraLength) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.text.SimpleDateFormat;
import java.util.Currency;
import java.util.Date;
import java.util.Set;
import java.util.TimeZone;

import protect.card_locker.CatimaBarcode;
Expand All @@ -36,7 +37,7 @@
* A header is expected for the each table showing the names of the columns.
*/
public class VoucherVaultImporter implements Importer {
public void importData(Context context, SQLiteDatabase database, InputStream input, char[] password) throws IOException, FormatException, JSONException, ParseException {
public Set<String> importData(Context context, SQLiteDatabase database, InputStream input, char[] password) throws IOException, FormatException, JSONException, ParseException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));

StringBuilder sb = new StringBuilder();
Expand Down Expand Up @@ -130,5 +131,7 @@ public void importData(Context context, SQLiteDatabase database, InputStream inp
}

bufferedReader.close();

return null;
}
}
16 changes: 16 additions & 0 deletions app/src/main/res/layout/import_export_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@
android:textSize="@dimen/text_size_medium"
android:text="@string/importOptionFilesystemExplanation"/>

<TextView
android:id="@+id/importWarningFileSystem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:textSize="@dimen/text_size_medium"
android:text="@string/importWarning"/>

<Button
android:id="@+id/importOptionFilesystemButton"
android:layout_width="match_parent"
Expand Down Expand Up @@ -119,6 +127,14 @@
android:textSize="@dimen/text_size_medium"
android:text="@string/importOptionApplicationExplanation"/>

<TextView
android:id="@+id/importWarningApplication"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:textSize="@dimen/text_size_medium"
android:text="@string/importWarning"/>

<Button
android:id="@+id/importOptionApplicationButton"
android:layout_width="match_parent"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -321,4 +321,5 @@
<string name="sharedpreference_card_details_show_balance" translatable="false">sharedpreference_card_details_show_balance</string>
<string name="sharedpreference_card_details_show_validity" translatable="false">sharedpreference_card_details_show_validity</string>
<string name="sharedpreference_card_details_show_name_below_thumbnail" translatable="false">sharedpreference_card_details_show_name_below_thumbnail</string>
<string name="importWarning">Warning: importing will delete all your existing data.</string>
</resources>