From 3ac8bfea78e8cb6f2a457223a573a4c94fd949b7 Mon Sep 17 00:00:00 2001 From: Altonss <66519591+Altonss@users.noreply.github.com> Date: Sat, 24 Sep 2022 01:38:10 +0200 Subject: [PATCH 1/3] DB: migrate to unique IDs for groups --- .../java/protect/card_locker/DBHelper.java | 146 +++++++++++---- .../main/java/protect/card_locker/Group.java | 29 +-- .../card_locker/GroupCursorAdapter.java | 2 +- .../card_locker/LoyaltyCardEditActivity.java | 10 +- .../card_locker/LoyaltyCardViewActivity.java | 2 +- .../protect/card_locker/MainActivity.java | 20 +- .../card_locker/ManageGroupActivity.java | 20 +- .../card_locker/ManageGroupCursorAdapter.java | 2 +- .../card_locker/ManageGroupsActivity.java | 11 +- .../protect/card_locker/ScanActivity.java | 8 +- .../importexport/CatimaExporter.java | 8 +- .../importexport/CatimaImporter.java | 173 +++++++++++++++++- .../protect/card_locker/DatabaseTest.java | 91 ++++++--- .../protect/card_locker/ImportExportTest.java | 84 ++++++--- .../protect/card_locker/MainActivityTest.java | 8 +- 15 files changed, 450 insertions(+), 164 deletions(-) diff --git a/app/src/main/java/protect/card_locker/DBHelper.java b/app/src/main/java/protect/card_locker/DBHelper.java index 53ec2ea5ca..ee3dfe899e 100644 --- a/app/src/main/java/protect/card_locker/DBHelper.java +++ b/app/src/main/java/protect/card_locker/DBHelper.java @@ -21,11 +21,12 @@ public class DBHelper extends SQLiteOpenHelper { public static final String DATABASE_NAME = "Catima.db"; public static final int ORIGINAL_DATABASE_VERSION = 1; - public static final int DATABASE_VERSION = 16; + public static final int DATABASE_VERSION = 17; public static class LoyaltyCardDbGroups { public static final String TABLE = "groups"; public static final String ID = "_id"; + public static final String NAME = "name"; public static final String ORDER = "orderId"; } @@ -87,7 +88,8 @@ public DBHelper(Context context) { public void onCreate(SQLiteDatabase db) { // create table for card groups db.execSQL("CREATE TABLE " + LoyaltyCardDbGroups.TABLE + "(" + - LoyaltyCardDbGroups.ID + " TEXT primary key not null," + + LoyaltyCardDbGroups.ID + " INTEGER primary key autoincrement," + + LoyaltyCardDbGroups.NAME + " TEXT unique not null," + LoyaltyCardDbGroups.ORDER + " INTEGER DEFAULT '0')"); // create table for cards @@ -112,7 +114,7 @@ public void onCreate(SQLiteDatabase db) { // create associative table for cards in groups db.execSQL("CREATE TABLE " + LoyaltyCardDbIdsGroups.TABLE + "(" + LoyaltyCardDbIdsGroups.cardID + " INTEGER," + - LoyaltyCardDbIdsGroups.groupID + " TEXT," + + LoyaltyCardDbIdsGroups.groupID + " INTEGER," + "primary key (" + LoyaltyCardDbIdsGroups.cardID + "," + LoyaltyCardDbIdsGroups.groupID + "))"); // create FTS search table @@ -321,6 +323,78 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("ALTER TABLE " + LoyaltyCardDbIds.TABLE + " ADD COLUMN " + LoyaltyCardDbIds.VALID_FROM + " INTEGER"); } + + if (oldVersion < 17 && newVersion >= 17) { + // SQLite doesn't support modify column + // So we need to create temp columns + // https://www.sqlite.org/faq.html#q11 + db.beginTransaction(); + + db.execSQL("CREATE TEMPORARY TABLE tmpDbGroups (" + + LoyaltyCardDbGroups.ID + " INTEGER primary key autoincrement," + + LoyaltyCardDbGroups.NAME + " TEXT not null," + + LoyaltyCardDbGroups.ORDER + " INTEGER DEFAULT '0' )"); + + db.execSQL("INSERT INTO tmpDbGroups (" + + LoyaltyCardDbGroups.NAME + " ," + + LoyaltyCardDbGroups.ORDER + ")" + + " SELECT " + + LoyaltyCardDbGroups.ID + " ," + + LoyaltyCardDbGroups.ORDER + + " FROM " + LoyaltyCardDbGroups.TABLE); + + db.execSQL("DROP TABLE " + LoyaltyCardDbGroups.TABLE); + + db.execSQL("CREATE TEMPORARY TABLE tmpDbIdsGroups (" + + LoyaltyCardDbIdsGroups.cardID + " INTEGER," + + LoyaltyCardDbIdsGroups.groupID + " TEXT," + + "primary key (" + LoyaltyCardDbIdsGroups.cardID + "," + LoyaltyCardDbIdsGroups.groupID + "))"); + + db.execSQL("INSERT INTO tmpDbIdsGroups (" + + LoyaltyCardDbIdsGroups.cardID + " ," + + LoyaltyCardDbIdsGroups.groupID + ")" + + " SELECT " + + LoyaltyCardDbIdsGroups.cardID + " ," + + LoyaltyCardDbIdsGroups.groupID + + " FROM " + LoyaltyCardDbIdsGroups.TABLE); + + db.execSQL("DROP TABLE " + LoyaltyCardDbIdsGroups.TABLE); + + db.execSQL("CREATE TABLE " + LoyaltyCardDbGroups.TABLE + "(" + + LoyaltyCardDbGroups.ID + " INTEGER primary key autoincrement," + + LoyaltyCardDbGroups.NAME + " TEXT not null," + + LoyaltyCardDbGroups.ORDER + " INTEGER DEFAULT '0' )"); + + db.execSQL("INSERT INTO " + LoyaltyCardDbGroups.TABLE + "(" + + LoyaltyCardDbGroups.ID + " ," + + LoyaltyCardDbGroups.NAME + " ," + + LoyaltyCardDbGroups.ORDER + ")" + + " SELECT " + + LoyaltyCardDbGroups.ID + " ," + + LoyaltyCardDbGroups.NAME + " ," + + LoyaltyCardDbGroups.ORDER + + " FROM tmpDbGroups"); + + db.execSQL("CREATE TABLE " + LoyaltyCardDbIdsGroups.TABLE + "(" + + LoyaltyCardDbIdsGroups.cardID + " INTEGER," + + LoyaltyCardDbIdsGroups.groupID + " INTEGER," + + "primary key (" + LoyaltyCardDbIdsGroups.cardID + "," + LoyaltyCardDbIdsGroups.groupID + "))"); + + db.execSQL("INSERT INTO " + LoyaltyCardDbIdsGroups.TABLE + "(" + + LoyaltyCardDbIdsGroups.cardID + " ," + + LoyaltyCardDbIdsGroups.groupID + ")" + + " SELECT " + + "idsGroups." + LoyaltyCardDbIdsGroups.cardID + " ," + + "groups." + LoyaltyCardDbGroups.ID + + " FROM tmpDbIdsGroups AS idsGroups JOIN " + LoyaltyCardDbGroups.TABLE + " AS groups ON " + + "idsGroups." + LoyaltyCardDbIdsGroups.groupID + "=" + "groups." + LoyaltyCardDbGroups.NAME); + + db.execSQL("DROP TABLE tmpDbGroups"); + db.execSQL("DROP TABLE tmpDbIdsGroups"); + + db.setTransactionSuccessful(); + db.endTransaction(); + } } private static ContentValues generateFTSContentValues(final int id, final String store, final String note) { @@ -534,7 +608,7 @@ public static List getLoyaltyCardGroups(SQLiteDatabase database, final in Cursor data = database.rawQuery("select * from " + LoyaltyCardDbGroups.TABLE + " g " + " LEFT JOIN " + LoyaltyCardDbIdsGroups.TABLE + " ig ON ig." + LoyaltyCardDbIdsGroups.groupID + " = g." + LoyaltyCardDbGroups.ID + " where " + LoyaltyCardDbIdsGroups.cardID + "=?" + - " ORDER BY " + LoyaltyCardDbIdsGroups.groupID, withArgs(id)); + " ORDER BY " + LoyaltyCardDbGroups.NAME, withArgs(id)); List groups = new ArrayList<>(); @@ -602,14 +676,14 @@ public static int getArchivedCardsCount(SQLiteDatabase database) { whereAttrs(LoyaltyCardDbIds.ARCHIVE_STATUS), withArgs(1)); } - public static int getArchivedCardsCount(SQLiteDatabase database, final String groupName) { + public static int getArchivedCardsCount(SQLiteDatabase database, final int groupId) { Cursor data = database.rawQuery( "select * from " + LoyaltyCardDbIds.TABLE + " c " + " LEFT JOIN " + LoyaltyCardDbIdsGroups.TABLE + " cg " + " ON c." + LoyaltyCardDbIds.ID + " = cg." + LoyaltyCardDbIdsGroups.cardID + " where " + LoyaltyCardDbIds.ARCHIVE_STATUS + " = 1" + " AND " + LoyaltyCardDbIdsGroups.groupID + "= ?", - withArgs(groupName) + withArgs(groupId) ); int count = data.getCount(); @@ -717,7 +791,7 @@ public static int getLoyaltyCardCount(SQLiteDatabase database) { */ public static Cursor getGroupCursor(SQLiteDatabase database) { return database.rawQuery("select * from " + LoyaltyCardDbGroups.TABLE + - " ORDER BY " + LoyaltyCardDbGroups.ORDER + " ASC," + LoyaltyCardDbGroups.ID + " COLLATE NOCASE ASC", null, null); + " ORDER BY " + LoyaltyCardDbGroups.ORDER + " ASC," + LoyaltyCardDbGroups.NAME + " ASC", null, null); } public static List getGroups(SQLiteDatabase database) { @@ -747,16 +821,29 @@ public static void reorderGroups(SQLiteDatabase database, final List grou contentValues.put(LoyaltyCardDbGroups.ORDER, order); database.update(LoyaltyCardDbGroups.TABLE, contentValues, - whereAttrs(LoyaltyCardDbGroups.ID), - withArgs(group._id)); + whereAttrs(LoyaltyCardDbGroups.ID), withArgs(group._id)); order++; } } - public static Group getGroup(SQLiteDatabase database, final String groupName) { + public static Group getGroup(SQLiteDatabase database, final int groupId) { Cursor data = database.query(LoyaltyCardDbGroups.TABLE, null, - whereAttrs(LoyaltyCardDbGroups.ID), withArgs(groupName), null, null, null); + whereAttrs(LoyaltyCardDbGroups.ID), withArgs(groupId), null, null, null); + + Group group = null; + if (data.getCount() == 1) { + data.moveToFirst(); + group = Group.toGroup(data); + } + data.close(); + + return group; + } + + public static Group getGroupByName(SQLiteDatabase database, String groupName) { + Cursor data = database.query(LoyaltyCardDbGroups.TABLE, null, + whereAttrs(LoyaltyCardDbGroups.NAME), withArgs(groupName), null, null, null); Group group = null; if (data.getCount() == 1) { @@ -772,9 +859,9 @@ public static int getGroupCount(SQLiteDatabase database) { return (int) DatabaseUtils.queryNumEntries(database, LoyaltyCardDbGroups.TABLE); } - public static List getGroupCardIds(SQLiteDatabase database, final String groupName) { + public static List getGroupCardIds(SQLiteDatabase database, final int groupId) { Cursor data = database.query(LoyaltyCardDbIdsGroups.TABLE, withArgs(LoyaltyCardDbIdsGroups.cardID), - whereAttrs(LoyaltyCardDbIdsGroups.groupID), withArgs(groupName), null, null, null); + whereAttrs(LoyaltyCardDbIdsGroups.groupID), withArgs(groupId), null, null, null); List cardIds = new ArrayList<>(); if (!data.moveToFirst()) { @@ -793,36 +880,27 @@ public static List getGroupCardIds(SQLiteDatabase database, final Strin } public static long insertGroup(SQLiteDatabase database, final String name) { - if (name.isEmpty()) return -1; + if (name.isEmpty() || getGroupByName(database, name)!= null) return -1; ContentValues contentValues = new ContentValues(); - contentValues.put(LoyaltyCardDbGroups.ID, name); + contentValues.put(LoyaltyCardDbGroups.NAME, name); contentValues.put(LoyaltyCardDbGroups.ORDER, getGroupCount(database)); return database.insert(LoyaltyCardDbGroups.TABLE, null, contentValues); } - public static boolean updateGroup(SQLiteDatabase database, final String groupName, final String newName) { - if (newName.isEmpty()) return false; + public static boolean updateGroup(SQLiteDatabase database, final int groupId, final String newName) { + if (newName.isEmpty() || getGroupByName(database, newName)!= null) return false; boolean success = false; ContentValues groupContentValues = new ContentValues(); - groupContentValues.put(LoyaltyCardDbGroups.ID, newName); - - ContentValues lookupContentValues = new ContentValues(); - lookupContentValues.put(LoyaltyCardDbIdsGroups.groupID, newName); + groupContentValues.put(LoyaltyCardDbGroups.NAME, newName); database.beginTransaction(); try { // Update group name int groupsChanged = database.update(LoyaltyCardDbGroups.TABLE, groupContentValues, - whereAttrs(LoyaltyCardDbGroups.ID), - withArgs(groupName)); - - // Also update lookup tables - database.update(LoyaltyCardDbIdsGroups.TABLE, lookupContentValues, - whereAttrs(LoyaltyCardDbIdsGroups.groupID), - withArgs(groupName)); + whereAttrs(LoyaltyCardDbGroups.ID), withArgs(groupId)); if (groupsChanged == 1) { database.setTransactionSuccessful(); @@ -836,20 +914,18 @@ public static boolean updateGroup(SQLiteDatabase database, final String groupNam return success; } - public static boolean deleteGroup(SQLiteDatabase database, final String groupName) { + public static boolean deleteGroup(SQLiteDatabase database, final int groupId) { boolean success = false; database.beginTransaction(); try { // Delete group int groupsDeleted = database.delete(LoyaltyCardDbGroups.TABLE, - whereAttrs(LoyaltyCardDbGroups.ID), - withArgs(groupName)); + whereAttrs(LoyaltyCardDbGroups.ID), withArgs(groupId)); // And delete lookup table entries associated with this group database.delete(LoyaltyCardDbIdsGroups.TABLE, - whereAttrs(LoyaltyCardDbIdsGroups.groupID), - withArgs(groupName)); + whereAttrs(LoyaltyCardDbIdsGroups.groupID), withArgs(groupId)); if (groupsDeleted == 1) { database.setTransactionSuccessful(); @@ -865,9 +941,9 @@ public static boolean deleteGroup(SQLiteDatabase database, final String groupNam return success; } - public static int getGroupCardCount(SQLiteDatabase database, final String groupName) { + public static int getGroupCardCount(SQLiteDatabase database, final int groupId) { return (int) DatabaseUtils.queryNumEntries(database, LoyaltyCardDbIdsGroups.TABLE, - whereAttrs(LoyaltyCardDbIdsGroups.groupID), withArgs(groupName)); + whereAttrs(LoyaltyCardDbIdsGroups.groupID),withArgs(groupId)); } static private String whereAttrs(String... attrs) { diff --git a/app/src/main/java/protect/card_locker/Group.java b/app/src/main/java/protect/card_locker/Group.java index 90d8da69d6..54953811c4 100644 --- a/app/src/main/java/protect/card_locker/Group.java +++ b/app/src/main/java/protect/card_locker/Group.java @@ -5,36 +5,21 @@ import androidx.annotation.Nullable; public class Group { - public final String _id; + public final int _id; + public final String name; public final int order; - public Group(final String _id, final int order) { + public Group(final int _id, final String name, final int order) { this._id = _id; + this.name = name; this.order = order; } public static Group toGroup(Cursor cursor) { - String _id = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbGroups.ID)); + int _id = cursor.getInt(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbGroups.ID)); + String name = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbGroups.NAME)); int order = cursor.getInt(cursor.getColumnIndexOrThrow(DBHelper.LoyaltyCardDbGroups.ORDER)); - return new Group(_id, order); - } - - @Override - public boolean equals(@Nullable Object obj) { - if (obj == null) { - return false; - } - if (!(obj instanceof Group)) { - return false; - } - Group anotherGroup = (Group) obj; - return _id.equals(anotherGroup._id) && order == anotherGroup.order; - } - - @Override - public int hashCode() { - String combined = _id + "_" + order; - return combined.hashCode(); + return new Group(_id, name, order); } } diff --git a/app/src/main/java/protect/card_locker/GroupCursorAdapter.java b/app/src/main/java/protect/card_locker/GroupCursorAdapter.java index 2e7926d75a..9747505891 100644 --- a/app/src/main/java/protect/card_locker/GroupCursorAdapter.java +++ b/app/src/main/java/protect/card_locker/GroupCursorAdapter.java @@ -47,7 +47,7 @@ public GroupCursorAdapter.GroupListItemViewHolder onCreateViewHolder(@NonNull Vi public void onBindViewHolder(GroupListItemViewHolder inputHolder, Cursor inputCursor) { Group group = Group.toGroup(inputCursor); - inputHolder.mName.setText(group._id); + inputHolder.mName.setText(group.name); int groupCardCount = DBHelper.getGroupCardCount(mDatabase, group._id); int archivedCardCount = DBHelper.getArchivedCardsCount(mDatabase, group._id); diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java b/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java index 9bb33d285b..ddc5164a3c 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java @@ -158,7 +158,7 @@ public class LoyaltyCardEditActivity extends CatimaAppCompatActivity implements String cardId; String barcodeId; String barcodeType; - String addGroup; + Integer addGroup; Uri importLoyaltyCardUri = null; @@ -246,7 +246,7 @@ private void extractIntentFields(Intent intent) { cardId = b != null ? b.getString(BUNDLE_CARDID) : null; barcodeId = b != null ? b.getString(BUNDLE_BARCODEID) : null; barcodeType = b != null ? b.getString(BUNDLE_BARCODETYPE) : null; - addGroup = b != null ? b.getString(BUNDLE_ADDGROUP) : null; + addGroup = b != null ? b.getInt(BUNDLE_ADDGROUP) : null; importLoyaltyCardUri = intent.getData(); @@ -816,15 +816,15 @@ public void onResume() { LayoutChipChoiceBinding chipChoiceBinding = LayoutChipChoiceBinding .inflate(LayoutInflater.from(groupsChips.getContext()), groupsChips, false); Chip chip = chipChoiceBinding.getRoot(); - chip.setText(group._id); + chip.setText(group.name); chip.setTag(group); - if (group._id.equals(addGroup)) { + if (group._id == addGroup) { chip.setChecked(true); } else { chip.setChecked(false); for (Group loyaltyCardGroup : loyaltyCardGroups) { - if (loyaltyCardGroup._id.equals(group._id)) { + if (loyaltyCardGroup._id == group._id) { chip.setChecked(true); break; } diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java b/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java index e155c5118e..a8c3d882a2 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java @@ -361,7 +361,7 @@ private void showInfoDialog() { if (loyaltyCardGroups.size() > 0) { List groupNames = new ArrayList<>(); for (Group group : loyaltyCardGroups) { - groupNames.add(group._id); + groupNames.add(group.name); } padSpannableString(infoText); diff --git a/app/src/main/java/protect/card_locker/MainActivity.java b/app/src/main/java/protect/card_locker/MainActivity.java index 905925aae9..64e9ada17f 100644 --- a/app/src/main/java/protect/card_locker/MainActivity.java +++ b/app/src/main/java/protect/card_locker/MainActivity.java @@ -334,8 +334,8 @@ public void onClick(DialogInterface dialog, int whichButton) { BarcodeValues barcodeValues = Utils.parseSetBarcodeActivityResult(Utils.BARCODE_SCAN, result.getResultCode(), intent, this); Bundle inputBundle = intent.getExtras(); - String group = inputBundle != null ? inputBundle.getString(LoyaltyCardEditActivity.BUNDLE_ADDGROUP) : null; - processBarcodeValues(barcodeValues, group); + int groupId = inputBundle != null ? inputBundle.getInt(LoyaltyCardEditActivity.BUNDLE_ADDGROUP) : null; + processBarcodeValues(barcodeValues, groupId); }); mSettingsLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> { @@ -414,8 +414,12 @@ protected void onResume() { addButton.setOnClickListener(v -> { Intent intent = new Intent(getApplicationContext(), ScanActivity.class); Bundle bundle = new Bundle(); - if (selectedTab != 0) { - bundle.putString(LoyaltyCardEditActivity.BUNDLE_ADDGROUP, groupsTabLayout.getTabAt(selectedTab).getText().toString()); + TabLayout.Tab tab = groupsTabLayout.getTabAt(selectedTab); + if (tab != null) { + Group group = (Group) groupsTabLayout.getTabAt(selectedTab).getTag(); + if (group != null) { + bundle.putInt(LoyaltyCardEditActivity.BUNDLE_ADDGROUP, group._id); + } } intent.putExtras(bundle); mBarcodeScannerLauncher.launch(intent); @@ -499,7 +503,7 @@ private void updateLoyaltyCardList(boolean updateCount) { } } - private void processBarcodeValues(BarcodeValues barcodeValues, String group) { + private void processBarcodeValues(BarcodeValues barcodeValues, Integer groupId) { if (barcodeValues.isEmpty()) { throw new IllegalArgumentException("barcodesValues may not be empty"); } @@ -508,8 +512,8 @@ private void processBarcodeValues(BarcodeValues barcodeValues, String group) { Bundle newBundle = new Bundle(); newBundle.putString(LoyaltyCardEditActivity.BUNDLE_BARCODETYPE, barcodeValues.format()); newBundle.putString(LoyaltyCardEditActivity.BUNDLE_CARDID, barcodeValues.content()); - if (group != null) { - newBundle.putString(LoyaltyCardEditActivity.BUNDLE_ADDGROUP, group); + if (groupId != null) { + newBundle.putInt(LoyaltyCardEditActivity.BUNDLE_ADDGROUP, groupId); } newIntent.putExtras(newBundle); startActivity(newIntent); @@ -583,7 +587,7 @@ public void updateTabGroups(TabLayout groupsTabLayout) { for (Group group : newGroups) { TabLayout.Tab tab = groupsTabLayout.newTab(); - tab.setText(group._id); + tab.setText(group.name); tab.setTag(group); groupsTabLayout.addTab(tab, false); } diff --git a/app/src/main/java/protect/card_locker/ManageGroupActivity.java b/app/src/main/java/protect/card_locker/ManageGroupActivity.java index a161bf922a..13beee2c56 100644 --- a/app/src/main/java/protect/card_locker/ManageGroupActivity.java +++ b/app/src/main/java/protect/card_locker/ManageGroupActivity.java @@ -78,8 +78,8 @@ public void afterTextChanged(Editable s) { mGroupNameText.setError(getResources().getText(R.string.group_name_is_empty)); return; } - if (!mGroup._id.equals(currentGroupName)) { - if (DBHelper.getGroup(mDatabase, currentGroupName) != null) { + if (!mGroup.name.equals(currentGroupName)) { + if (DBHelper.getGroupByName(mDatabase, currentGroupName) != null) { mGroupNameNotInUse = false; mGroupNameText.setError(getResources().getText(R.string.group_name_already_in_use)); } else { @@ -90,17 +90,15 @@ public void afterTextChanged(Editable s) { }); Intent intent = getIntent(); - String groupId = intent.getStringExtra("group"); - if (groupId == null) { - throw (new IllegalArgumentException("this activity expects a group loaded into it's intent")); - } + final Bundle b = intent.getExtras(); + Integer groupId = b.getInt("group"); Log.d("groupId", "groupId: " + groupId); mGroup = DBHelper.getGroup(mDatabase, groupId); if (mGroup == null) { throw (new IllegalArgumentException("cannot load group " + groupId + " from database")); } - mGroupNameText.setText(mGroup._id); - setTitle(getString(R.string.editGroup, mGroup._id)); + mGroupNameText.setText(mGroup.name); + setTitle(getString(R.string.editGroup, mGroup.name)); mAdapter = new ManageGroupCursorAdapter(this, null, this, mGroup); mCardList.setAdapter(mAdapter); registerForContextMenu(mCardList); @@ -114,7 +112,7 @@ public void afterTextChanged(Editable s) { saveButton.setOnClickListener(v -> { String currentGroupName = mGroupNameText.getText().toString().trim(); - if (!currentGroupName.equals(mGroup._id)) { + if (!currentGroupName.equals(mGroup.name)) { if (currentGroupName.length() == 0) { Toast.makeText(getApplicationContext(), R.string.group_name_is_empty, Toast.LENGTH_SHORT).show(); return; @@ -126,7 +124,7 @@ public void afterTextChanged(Editable s) { } mAdapter.commitToDatabase(); - if (!currentGroupName.equals(mGroup._id)) { + if (!currentGroupName.equals(mGroup.name)) { DBHelper.updateGroup(mDatabase, mGroup._id, currentGroupName); } Toast.makeText(getApplicationContext(), R.string.group_updated, Toast.LENGTH_SHORT).show(); @@ -224,7 +222,7 @@ public boolean onSupportNavigateUp() { } private boolean hasChanged() { - return mAdapter.hasChanged() || !mGroup._id.equals(mGroupNameText.getText().toString().trim()); + return mAdapter.hasChanged() || !mGroup.name.equals(mGroupNameText.getText().toString().trim()); } @Override diff --git a/app/src/main/java/protect/card_locker/ManageGroupCursorAdapter.java b/app/src/main/java/protect/card_locker/ManageGroupCursorAdapter.java index 9e198719b1..fdbfbadf11 100644 --- a/app/src/main/java/protect/card_locker/ManageGroupCursorAdapter.java +++ b/app/src/main/java/protect/card_locker/ManageGroupCursorAdapter.java @@ -18,7 +18,7 @@ public class ManageGroupCursorAdapter extends LoyaltyCardCursorAdapter { public ManageGroupCursorAdapter(Context inputContext, Cursor inputCursor, CardAdapterListener inputListener, Group group) { super(inputContext, inputCursor, inputListener); - mGroup = new Group(group._id, group.order); + mGroup = new Group(group._id, group.name, group.order); mInGroupOverlay = new HashMap<>(); mDatabase = new DBHelper(inputContext).getWritableDatabase(); } diff --git a/app/src/main/java/protect/card_locker/ManageGroupsActivity.java b/app/src/main/java/protect/card_locker/ManageGroupsActivity.java index 592c670062..5c20b24cf5 100644 --- a/app/src/main/java/protect/card_locker/ManageGroupsActivity.java +++ b/app/src/main/java/protect/card_locker/ManageGroupsActivity.java @@ -120,7 +120,7 @@ private void setGroupNameError(EditText input) { return; } - if (DBHelper.getGroup(mDatabase, string) != null) { + if (DBHelper.getGroupByName(mDatabase, string) != null) { input.setError(getString(R.string.group_name_already_in_use)); return; } @@ -184,7 +184,7 @@ private void moveGroup(View view, boolean up) { List groups = DBHelper.getGroups(mDatabase); final String groupName = getGroupName(view); - int currentIndex = DBHelper.getGroup(mDatabase, groupName).order; + int currentIndex = DBHelper.getGroupByName(mDatabase, groupName).order; int newIndex; // Reinsert group in correct position @@ -225,20 +225,23 @@ public void onMoveUpButtonClicked(View view) { @Override public void onEditButtonClicked(View view) { Intent intent = new Intent(this, ManageGroupActivity.class); - intent.putExtra("group", getGroupName(view)); + final String groupName = getGroupName(view); + Group group = DBHelper.getGroupByName(mDatabase, groupName); + intent.putExtra("group", group._id); startActivity(intent); } @Override public void onDeleteButtonClicked(View view) { final String groupName = getGroupName(view); + Group group = DBHelper.getGroupByName(mDatabase, groupName); AlertDialog.Builder builder = new MaterialAlertDialogBuilder(this); builder.setTitle(R.string.deleteConfirmationGroup); builder.setMessage(groupName); builder.setPositiveButton(getString(R.string.ok), (dialog, which) -> { - DBHelper.deleteGroup(mDatabase, groupName); + DBHelper.deleteGroup(mDatabase, group._id); updateGroupList(); // Delete may change ordering, so invalidate invalidateHomescreenActiveTab(); diff --git a/app/src/main/java/protect/card_locker/ScanActivity.java b/app/src/main/java/protect/card_locker/ScanActivity.java index 41cf3c1fca..9339fb37f0 100644 --- a/app/src/main/java/protect/card_locker/ScanActivity.java +++ b/app/src/main/java/protect/card_locker/ScanActivity.java @@ -58,7 +58,7 @@ public class ScanActivity extends CatimaAppCompatActivity { private DecoratedBarcodeView barcodeScannerView; private String cardId; - private String addGroup; + private Integer addGroup; private boolean torch = false; private ActivityResultLauncher manualAddLauncher; @@ -68,7 +68,7 @@ public class ScanActivity extends CatimaAppCompatActivity { private void extractIntentFields(Intent intent) { final Bundle b = intent.getExtras(); cardId = b != null ? b.getString(LoyaltyCardEditActivity.BUNDLE_CARDID) : null; - addGroup = b != null ? b.getString(LoyaltyCardEditActivity.BUNDLE_ADDGROUP) : null; + addGroup = b != null ? b.getInt(LoyaltyCardEditActivity.BUNDLE_ADDGROUP) : null; Log.d(TAG, "Scan activity: id=" + cardId); } @@ -109,7 +109,7 @@ public void barcodeResult(BarcodeResult result) { scanResultBundle.putString(BarcodeSelectorActivity.BARCODE_CONTENTS, result.getText()); scanResultBundle.putString(BarcodeSelectorActivity.BARCODE_FORMAT, result.getBarcodeFormat().name()); if (addGroup != null) { - scanResultBundle.putString(LoyaltyCardEditActivity.BUNDLE_ADDGROUP, addGroup); + scanResultBundle.putInt(LoyaltyCardEditActivity.BUNDLE_ADDGROUP, addGroup); } scanResult.putExtras(scanResultBundle); ScanActivity.this.setResult(RESULT_OK, scanResult); @@ -204,7 +204,7 @@ private void handleActivityResult(int requestCode, int resultCode, Intent intent manualResultBundle.putString(BarcodeSelectorActivity.BARCODE_CONTENTS, barcodeValues.content()); manualResultBundle.putString(BarcodeSelectorActivity.BARCODE_FORMAT, barcodeValues.format()); if (addGroup != null) { - manualResultBundle.putString(LoyaltyCardEditActivity.BUNDLE_ADDGROUP, addGroup); + manualResultBundle.putInt(LoyaltyCardEditActivity.BUNDLE_ADDGROUP, addGroup); } manualResult.putExtras(manualResultBundle); ScanActivity.this.setResult(RESULT_OK, manualResult); diff --git a/app/src/main/java/protect/card_locker/importexport/CatimaExporter.java b/app/src/main/java/protect/card_locker/importexport/CatimaExporter.java index 263782d8df..bcc02f267b 100644 --- a/app/src/main/java/protect/card_locker/importexport/CatimaExporter.java +++ b/app/src/main/java/protect/card_locker/importexport/CatimaExporter.java @@ -99,19 +99,21 @@ private void writeCSV(SQLiteDatabase database, OutputStreamWriter output) throws CSVPrinter printer = new CSVPrinter(output, CSVFormat.RFC4180); // Print the version - printer.printRecord("2"); + printer.printRecord("3"); printer.println(); // Print the header for groups - printer.printRecord(DBHelper.LoyaltyCardDbGroups.ID); + printer.printRecord(DBHelper.LoyaltyCardDbGroups.ID, + DBHelper.LoyaltyCardDbGroups.NAME, + DBHelper.LoyaltyCardDbGroups.ORDER); Cursor groupCursor = DBHelper.getGroupCursor(database); while (groupCursor.moveToNext()) { Group group = Group.toGroup(groupCursor); - printer.printRecord(group._id); + printer.printRecord(group._id, group.name, group.order); if (Thread.currentThread().isInterrupted()) { throw new InterruptedException(); diff --git a/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java b/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java index 52d521781c..3ae37dbaca 100644 --- a/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java +++ b/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java @@ -22,7 +22,9 @@ import java.util.ArrayList; import java.util.Currency; import java.util.Date; +import java.util.Hashtable; import java.util.List; +import java.util.TreeMap; import protect.card_locker.CatimaBarcode; import protect.card_locker.DBHelper; @@ -82,6 +84,9 @@ public void importCSV(Context context, SQLiteDatabase database, InputStream inpu case 2: parseV2(context, database, bufferedReader); break; + case 3: + parseV3(context, database, bufferedReader); + break; default: throw new FormatException(String.format("No code to parse version %s", version)); } @@ -168,6 +173,70 @@ public void parseV2(Context context, SQLiteDatabase database, BufferedReader inp } } + public void parseV3(Context context, SQLiteDatabase database, BufferedReader input) throws IOException, FormatException, InterruptedException { + int part = 0; + StringBuilder stringPart = new StringBuilder(); + Hashtable groupsTable = null; + + try { + while (true) { + String tmp = input.readLine(); + + if (tmp == null || tmp.isEmpty()) { + boolean sectionParsed = false; + + switch (part) { + case 0: + // This is the version info, ignore + sectionParsed = true; + break; + case 1: + try { + groupsTable = parseV3Groups(database, stringPart.toString()); + sectionParsed = true; + } catch (FormatException e) { + // We may have a multiline field, try again + } + break; + case 2: + try { + parseV2Cards(context, database, stringPart.toString()); + sectionParsed = true; + } catch (FormatException e) { + // We may have a multiline field, try again + } + break; + case 3: + try { + parseV3CardGroups(database, stringPart.toString(), groupsTable); + sectionParsed = true; + } catch (FormatException e) { + // We may have a multiline field, try again + } + break; + default: + throw new FormatException("Issue parsing CSV data, too many parts for v3 parsing"); + } + + if (tmp == null) { + break; + } + + if (sectionParsed) { + part += 1; + stringPart = new StringBuilder(); + } else { + stringPart.append(tmp).append('\n'); + } + } else { + stringPart.append(tmp).append('\n'); + } + } + } catch (FormatException e) { + throw new FormatException("Issue parsing CSV data", e); + } + } + public void parseV2Groups(SQLiteDatabase database, String data) throws IOException, FormatException, InterruptedException { // Parse groups final CSVParser groupParser = new CSVParser(new StringReader(data), CSVFormat.RFC4180.builder().setHeader().build()); @@ -189,8 +258,33 @@ public void parseV2Groups(SQLiteDatabase database, String data) throws IOExcepti } for (CSVRecord record : records) { - importGroup(database, record); + importGroupV2(database, record); + } + } + + public Hashtable parseV3Groups(SQLiteDatabase database, String data) throws IOException, FormatException, InterruptedException { + // Parse groups + final CSVParser groupParser = new CSVParser(new StringReader(data), CSVFormat.RFC4180.builder().setHeader().build()); + + List records = new ArrayList<>(); + + try { + for (CSVRecord record : groupParser) { + records.add(record); + + if (Thread.currentThread().isInterrupted()) { + throw new InterruptedException(); + } + } + } catch (IllegalArgumentException | IllegalStateException e) { + throw new FormatException("Issue parsing CSV data", e); + } finally { + groupParser.close(); } + + Hashtable groupsTable = importGroupV3(database, records); + + return groupsTable; } public void parseV2Cards(Context context, SQLiteDatabase database, String data) throws IOException, FormatException, InterruptedException { @@ -239,7 +333,32 @@ public void parseV2CardGroups(SQLiteDatabase database, String data) throws IOExc } for (CSVRecord record : records) { - importCardGroupMapping(database, record); + importCardGroupMappingV2(database, record); + } + } + + public void parseV3CardGroups(SQLiteDatabase database, String data, Hashtable groupsTable) throws IOException, FormatException, InterruptedException { + // Parse card group mappings + final CSVParser cardGroupParser = new CSVParser(new StringReader(data), CSVFormat.RFC4180.builder().setHeader().build()); + + List records = new ArrayList<>(); + + try { + for (CSVRecord record : cardGroupParser) { + records.add(record); + + if (Thread.currentThread().isInterrupted()) { + throw new InterruptedException(); + } + } + } catch (IllegalArgumentException | IllegalStateException e) { + throw new FormatException("Issue parsing CSV data", e); + } finally { + cardGroupParser.close(); + } + + for (CSVRecord record : records) { + importCardGroupMappingV3(database, record, groupsTable); } } @@ -381,21 +500,46 @@ private void importLoyaltyCard(SQLiteDatabase database, CSVRecord record) * Import a single group into the database using the given * session. */ - private void importGroup(SQLiteDatabase database, CSVRecord record) throws FormatException { - String id = CSVHelpers.extractString(DBHelper.LoyaltyCardDbGroups.ID, record, null); + private void importGroupV2(SQLiteDatabase database, CSVRecord record) throws FormatException { + String name = CSVHelpers.extractString(DBHelper.LoyaltyCardDbGroups.ID, record, null); - if (id == null) { + if (name == null) { throw new FormatException("Group has no ID: " + record); } - DBHelper.insertGroup(database, id); + DBHelper.insertGroup(database, name); + } + + /** + * Import a single group from the V3 scheme (database v16) into the database using the given + * session. + */ + private Hashtable importGroupV3(SQLiteDatabase database, List records) throws FormatException { + Hashtable groupsTable = new Hashtable<>(); + TreeMap sortedGroups = new TreeMap(); + for (CSVRecord record : records) { + Integer id = CSVHelpers.extractInt(DBHelper.LoyaltyCardDbGroups.ID, record, false); + String name = CSVHelpers.extractString(DBHelper.LoyaltyCardDbGroups.NAME, record, null); + Integer order = CSVHelpers.extractInt(DBHelper.LoyaltyCardDbGroups.ORDER, record, false); + + if (id == null) { + throw new FormatException("Group has no ID: " + record); + } + + DBHelper.insertGroup(database, name); + sortedGroups.put(order, DBHelper.getGroupByName(database, name)); + groupsTable.put(id, name); + } + List sortedGroupList = new ArrayList<>(sortedGroups.values()); + DBHelper.reorderGroups(database, sortedGroupList); + return groupsTable; } /** * Import a single card to group mapping into the database using the given * session. */ - private void importCardGroupMapping(SQLiteDatabase database, CSVRecord record) throws FormatException { + private void importCardGroupMappingV2(SQLiteDatabase database, CSVRecord record) throws FormatException { int cardId = CSVHelpers.extractInt(DBHelper.LoyaltyCardDbIdsGroups.cardID, record); String groupId = CSVHelpers.extractString(DBHelper.LoyaltyCardDbIdsGroups.groupID, record, null); @@ -404,7 +548,22 @@ private void importCardGroupMapping(SQLiteDatabase database, CSVRecord record) t } List cardGroups = DBHelper.getLoyaltyCardGroups(database, cardId); + String groupName = CSVHelpers.extractString(DBHelper.LoyaltyCardDbIdsGroups.groupID, record, null); + cardGroups.add(DBHelper.getGroupByName(database, groupName)); + DBHelper.setLoyaltyCardGroups(database, cardId, cardGroups); + } + + /** + * Import a single card to group mapping from V3 scheme (database v16) into the database using the given + * session. + */ + private void importCardGroupMappingV3(SQLiteDatabase database, CSVRecord record, Hashtable groupsTable) throws FormatException { + Integer cardId = CSVHelpers.extractInt(DBHelper.LoyaltyCardDbIdsGroups.cardID, record, false); + List cardGroups = DBHelper.getLoyaltyCardGroups(database, cardId); + + Integer groupId = CSVHelpers.extractInt(DBHelper.LoyaltyCardDbIdsGroups.groupID, record, false); cardGroups.add(DBHelper.getGroup(database, groupId)); + DBHelper.setLoyaltyCardGroups(database, cardId, cardGroups); } } diff --git a/app/src/test/java/protect/card_locker/DatabaseTest.java b/app/src/test/java/protect/card_locker/DatabaseTest.java index fb756ffecd..60a3e256a9 100644 --- a/app/src/test/java/protect/card_locker/DatabaseTest.java +++ b/app/src/test/java/protect/card_locker/DatabaseTest.java @@ -297,14 +297,17 @@ public void addRemoveOneGroup() { assertTrue(result); assertEquals(1, DBHelper.getGroupCount(mDatabase)); - Group group = DBHelper.getGroup(mDatabase, "group one"); - assertNotNull(group); - assertEquals("group one", group._id); - - result = DBHelper.deleteGroup(mDatabase, "group one"); + Group groupByName = DBHelper.getGroupByName(mDatabase, "group one"); + assertNotNull(groupByName); + assertEquals("group one", groupByName.name); + Group groupByID = DBHelper.getGroup(mDatabase, groupByName._id); + assertNotNull(groupByID); + assertEquals("group one", groupByID.name); + + result = DBHelper.deleteGroup(mDatabase, groupByID._id); assertTrue(result); assertEquals(0, DBHelper.getGroupCount(mDatabase)); - assertNull(DBHelper.getGroup(mDatabase, "group one")); + assertNull(DBHelper.getGroup(mDatabase, groupByID._id)); } @Test @@ -323,7 +326,8 @@ public void updateGroup() { assertEquals(1, DBHelper.getGroupCount(mDatabase)); // Add card to group - Group group = DBHelper.getGroup(mDatabase, "group one"); + Group groupByName = DBHelper.getGroupByName(mDatabase, "group one"); + Group group = DBHelper.getGroup(mDatabase, groupByName._id); List groupList1 = new ArrayList<>(); groupList1.add(group); DBHelper.setLoyaltyCardGroups(mDatabase, 1, groupList1); @@ -331,36 +335,41 @@ public void updateGroup() { // Ensure the card has one group and the group has one card List cardGroups = DBHelper.getLoyaltyCardGroups(mDatabase, (int) id); assertEquals(1, cardGroups.size()); - assertEquals("group one", cardGroups.get(0)._id); - assertEquals(1, DBHelper.getGroupCardCount(mDatabase, "group one")); + assertEquals("group one", cardGroups.get(0).name); + assertEquals(1, DBHelper.getGroupCardCount(mDatabase, group._id)); // Rename group - result = DBHelper.updateGroup(mDatabase, "group one", "group one renamed"); + result = DBHelper.updateGroup(mDatabase, group._id, "group one renamed"); assertTrue(result); assertEquals(1, DBHelper.getGroupCount(mDatabase)); // Group one no longer exists - group = DBHelper.getGroup(mDatabase,"group one"); - assertNull(group); + Group groupByNameDeleted = DBHelper.getGroupByName(mDatabase,"group one"); + assertNull(groupByNameDeleted); + Group groupById = DBHelper.getGroup(mDatabase,group._id); + assertNotNull(groupById); // But group one renamed does - Group group2 = DBHelper.getGroup(mDatabase, "group one renamed"); - assertNotNull(group2); - assertEquals("group one renamed", group2._id); + Group groupRenamedByName = DBHelper.getGroupByName(mDatabase, "group one renamed"); + assertNotNull(groupRenamedByName); + assertEquals("group one renamed", groupRenamedByName.name); + Group groupRenamedById = DBHelper.getGroup(mDatabase, group._id); + assertNotNull(groupRenamedById); + assertEquals("group one renamed", groupRenamedById.name); // And card is in "group one renamed" // Ensure the card has one group and the group has one card cardGroups = DBHelper.getLoyaltyCardGroups(mDatabase, (int) id); assertEquals(1, cardGroups.size()); - assertEquals("group one renamed", cardGroups.get(0)._id); - assertEquals(1, DBHelper.getGroupCardCount(mDatabase, "group one renamed")); + assertEquals("group one renamed", cardGroups.get(0).name); + assertEquals(1, DBHelper.getGroupCardCount(mDatabase, groupRenamedById._id)); } @Test public void updateMissingGroup() { assertEquals(0, DBHelper.getGroupCount(mDatabase)); - boolean result = DBHelper.updateGroup(mDatabase, "group one", "new name"); + boolean result = DBHelper.updateGroup(mDatabase, 1, "new name"); assertEquals(false, result); assertEquals(0, DBHelper.getGroupCount(mDatabase)); } @@ -381,9 +390,12 @@ public void duplicateGroupName() { assertTrue(result); assertEquals(1, DBHelper.getGroupCount(mDatabase)); - Group group = DBHelper.getGroup(mDatabase, "group one"); + Group groupByName = DBHelper.getGroupByName(mDatabase, "group one"); + assertNotNull(groupByName); + assertEquals("group one", groupByName.name); + Group group = DBHelper.getGroup(mDatabase, groupByName._id); assertNotNull(group); - assertEquals("group one", group._id); + assertEquals("group one", group.name); // Should fail on duplicate long id2 = DBHelper.insertGroup(mDatabase, "group one"); @@ -405,18 +417,29 @@ public void updateGroupDuplicate() { assertEquals(2, DBHelper.getGroupCount(mDatabase)); // Should fail when trying to rename group two to one - boolean result3 = DBHelper.updateGroup(mDatabase, "group two", "group one"); + Group group2_initByName = DBHelper.getGroupByName(mDatabase, "group two"); + boolean result3ByName = DBHelper.updateGroup(mDatabase, group2_initByName._id, "group one"); + assertFalse(result3ByName); + assertEquals(2, DBHelper.getGroupCount(mDatabase)); + Group group2_init = DBHelper.getGroup(mDatabase, group2_initByName._id); + boolean result3 = DBHelper.updateGroup(mDatabase, group2_init._id, "group one"); assertFalse(result3); assertEquals(2, DBHelper.getGroupCount(mDatabase)); // Rename failed so both should still be the same - Group group = DBHelper.getGroup(mDatabase, "group one"); + Group groupByName = DBHelper.getGroupByName(mDatabase, "group one"); + assertNotNull(groupByName); + assertEquals("group one", groupByName.name); + Group group = DBHelper.getGroup(mDatabase, groupByName._id); assertNotNull(group); - assertEquals("group one", group._id); + assertEquals("group one", group.name); - Group group2 = DBHelper.getGroup(mDatabase, "group two"); + Group group2ByName = DBHelper.getGroupByName(mDatabase, "group two"); + assertNotNull(group2ByName); + assertEquals("group two", group2ByName.name); + Group group2 = DBHelper.getGroup(mDatabase, group2ByName._id); assertNotNull(group2); - assertEquals("group two", group2._id); + assertEquals("group two", group2.name); } @Test @@ -440,7 +463,10 @@ public void cardAddAndRemoveGroups() { assertEquals(2, DBHelper.getGroupCount(mDatabase)); - Group group1 = DBHelper.getGroup(mDatabase, "one"); + Group group1ByName = DBHelper.getGroupByName(mDatabase, "one"); + Group group2ByName = DBHelper.getGroupByName(mDatabase, "two"); + Group group1 = DBHelper.getGroup(mDatabase, group1ByName._id); + Group group2 = DBHelper.getGroup(mDatabase, group2ByName._id); // Card has no groups by default List cardGroups = DBHelper.getLoyaltyCardGroups(mDatabase, 1); @@ -453,16 +479,21 @@ public void cardAddAndRemoveGroups() { List cardGroups1 = DBHelper.getLoyaltyCardGroups(mDatabase, 1); assertEquals(1, cardGroups1.size()); + assertEquals(cardGroups1.get(0)._id, group1ByName._id); assertEquals(cardGroups1.get(0)._id, group1._id); - assertEquals(1, DBHelper.getGroupCardCount(mDatabase, "one")); - assertEquals(0, DBHelper.getGroupCardCount(mDatabase, "two")); + assertEquals(1, DBHelper.getGroupCardCount(mDatabase, group1ByName._id)); + assertEquals(1, DBHelper.getGroupCardCount(mDatabase, group1._id)); + assertEquals(0, DBHelper.getGroupCardCount(mDatabase, group2ByName._id)); + assertEquals(0, DBHelper.getGroupCardCount(mDatabase, group2._id)); // Remove groups DBHelper.setLoyaltyCardGroups(mDatabase, 1, new ArrayList()); List cardGroups2 = DBHelper.getLoyaltyCardGroups(mDatabase, 1); assertEquals(0, cardGroups2.size()); - assertEquals(0, DBHelper.getGroupCardCount(mDatabase, "one")); - assertEquals(0, DBHelper.getGroupCardCount(mDatabase, "two")); + assertEquals(0, DBHelper.getGroupCardCount(mDatabase, group1ByName._id)); + assertEquals(0, DBHelper.getGroupCardCount(mDatabase, group1._id)); + assertEquals(0, DBHelper.getGroupCardCount(mDatabase, group2ByName._id)); + assertEquals(0, DBHelper.getGroupCardCount(mDatabase, group2._id)); } @Test diff --git a/app/src/test/java/protect/card_locker/ImportExportTest.java b/app/src/test/java/protect/card_locker/ImportExportTest.java index 3bd6adfe7d..c279f541a5 100644 --- a/app/src/test/java/protect/card_locker/ImportExportTest.java +++ b/app/src/test/java/protect/card_locker/ImportExportTest.java @@ -33,6 +33,7 @@ import java.time.Duration; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.Currency; import java.util.Date; import java.util.HashMap; @@ -297,7 +298,7 @@ private void checkGroups() { String expectedGroupName = String.format("group, \"%4d", index); - assertEquals(expectedGroupName, group._id); + assertEquals(expectedGroupName, group.name); index--; } @@ -400,7 +401,7 @@ private List groupsToGroupNames(List groups) { List groupNames = new ArrayList<>(); for (Group group : groups) { - groupNames.add(group._id); + groupNames.add(group.name); } return groupNames; @@ -417,25 +418,25 @@ public void multipleCardsExportImportWithGroups() throws IOException { List emptyGroup = new ArrayList<>(); List groupsForOne = new ArrayList<>(); - groupsForOne.add(DBHelper.getGroup(mDatabase, "group, \" 1")); + groupsForOne.add(DBHelper.getGroupByName(mDatabase, "group, \" 1")); List groupsForTwo = new ArrayList<>(); - groupsForTwo.add(DBHelper.getGroup(mDatabase, "group, \" 1")); - groupsForTwo.add(DBHelper.getGroup(mDatabase, "group, \" 2")); + groupsForTwo.add(DBHelper.getGroupByName(mDatabase, "group, \" 1")); + groupsForTwo.add(DBHelper.getGroupByName(mDatabase, "group, \" 2")); List groupsForThree = new ArrayList<>(); - groupsForThree.add(DBHelper.getGroup(mDatabase, "group, \" 1")); - groupsForThree.add(DBHelper.getGroup(mDatabase, "group, \" 2")); - groupsForThree.add(DBHelper.getGroup(mDatabase, "group, \" 3")); + groupsForThree.add(DBHelper.getGroupByName(mDatabase, "group, \" 1")); + groupsForThree.add(DBHelper.getGroupByName(mDatabase, "group, \" 2")); + groupsForThree.add(DBHelper.getGroupByName(mDatabase, "group, \" 3")); List groupsForFour = new ArrayList<>(); - groupsForFour.add(DBHelper.getGroup(mDatabase, "group, \" 1")); - groupsForFour.add(DBHelper.getGroup(mDatabase, "group, \" 2")); - groupsForFour.add(DBHelper.getGroup(mDatabase, "group, \" 3")); + groupsForFour.add(DBHelper.getGroupByName(mDatabase, "group, \" 1")); + groupsForFour.add(DBHelper.getGroupByName(mDatabase, "group, \" 2")); + groupsForFour.add(DBHelper.getGroupByName(mDatabase, "group, \" 3")); List groupsForFive = new ArrayList<>(); - groupsForFive.add(DBHelper.getGroup(mDatabase,"group, \" 1")); - groupsForFive.add(DBHelper.getGroup(mDatabase, "group, \" 3")); + groupsForFive.add(DBHelper.getGroupByName(mDatabase,"group, \" 1")); + groupsForFive.add(DBHelper.getGroupByName(mDatabase, "group, \" 3")); DBHelper.setLoyaltyCardGroups(mDatabase, 1, groupsForOne); DBHelper.setLoyaltyCardGroups(mDatabase, 2, groupsForTwo); @@ -464,12 +465,37 @@ public void multipleCardsExportImportWithGroups() throws IOException { checkLoyaltyCards(); checkGroups(); - - assertEquals(groupsToGroupNames(groupsForOne), groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 1))); - assertEquals(groupsToGroupNames(groupsForTwo), groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 2))); - assertEquals(groupsToGroupNames(groupsForThree), groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 3))); - assertEquals(groupsToGroupNames(groupsForFour), groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 4))); - assertEquals(groupsToGroupNames(groupsForFive), groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 5))); + // card 1 + List groupsForOneInitial = groupsToGroupNames(groupsForOne); + List groupsForOneImport = groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 1)); + Collections.sort(groupsForOneInitial); + Collections.sort(groupsForOneImport); + assertEquals(groupsForOneInitial, groupsForOneImport); + // card 2 + List groupsForTwoInitial = groupsToGroupNames(groupsForTwo); + List groupsForTwoImport = groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 2)); + Collections.sort(groupsForTwoInitial); + Collections.sort(groupsForTwoImport); + assertEquals(groupsForTwoInitial, groupsForTwoImport); + // card 3 + List groupsForThreeInitial = groupsToGroupNames(groupsForThree); + List groupsForThreeImport = groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 3)); + Collections.sort(groupsForThreeInitial); + Collections.sort(groupsForThreeImport); + assertEquals(groupsForThreeInitial, groupsForThreeImport); + // card 4 + List groupsForFourInitial = groupsToGroupNames(groupsForFour); + List groupsForFourImport = groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 4)); + Collections.sort(groupsForFourInitial); + Collections.sort(groupsForFourImport); + assertEquals(groupsForFourInitial, groupsForFourImport); + // card 5 + List groupsForFiveInitial = groupsToGroupNames(groupsForFive); + List groupsForFiveImport = groupsToGroupNames(DBHelper.getLoyaltyCardGroups(mDatabase, 5)); + Collections.sort(groupsForFiveInitial); + Collections.sort(groupsForFiveImport); + assertEquals(groupsForFiveInitial, groupsForFiveImport); + // cards 6 to 10 assertEquals(emptyGroup, DBHelper.getLoyaltyCardGroups(mDatabase, 6)); assertEquals(emptyGroup, DBHelper.getLoyaltyCardGroups(mDatabase, 7)); assertEquals(emptyGroup, DBHelper.getLoyaltyCardGroups(mDatabase, 8)); @@ -800,7 +826,7 @@ public void exportImportV2Zip() throws FileNotFoundException { int loyaltyCardId = (int) DBHelper.insertLoyaltyCard(mDatabase, "Card 1", "Note 1", new Date(1601510400), new Date(1618053234), new BigDecimal("100"), Currency.getInstance("USD"), "1234", "5432", CatimaBarcode.fromBarcode(BarcodeFormat.QR_CODE), 1, 0, null,0); loyaltyCardHashMap.put(loyaltyCardId, DBHelper.getLoyaltyCard(mDatabase, loyaltyCardId)); DBHelper.insertGroup(mDatabase, "One"); - List groups = Arrays.asList(DBHelper.getGroup(mDatabase, "One")); + List groups = Arrays.asList(DBHelper.getGroupByName(mDatabase, "One")); DBHelper.setLoyaltyCardGroups(mDatabase, loyaltyCardId, groups); loyaltyCardGroups.put(loyaltyCardId, groups); Utils.saveCardImage(activity.getApplicationContext(), bitmap1, loyaltyCardId, ImageLocationType.front); @@ -901,20 +927,20 @@ public void importV2CSV() { assertEquals(3, DBHelper.getGroupCount(mDatabase)); // Check all groups - Group healthGroup = DBHelper.getGroup(mDatabase, "Health"); + Group healthGroup = DBHelper.getGroupByName(mDatabase, "Health"); assertNotNull(healthGroup); - assertEquals(1, DBHelper.getGroupCardCount(mDatabase, "Health")); - assertEquals(Arrays.asList(4), DBHelper.getGroupCardIds(mDatabase, "Health")); + assertEquals(1, DBHelper.getGroupCardCount(mDatabase, healthGroup._id)); + assertEquals(Arrays.asList(4), DBHelper.getGroupCardIds(mDatabase, healthGroup._id)); - Group foodGroup = DBHelper.getGroup(mDatabase, "Food"); + Group foodGroup = DBHelper.getGroupByName(mDatabase, "Food"); assertNotNull(foodGroup); - assertEquals(2, DBHelper.getGroupCardCount(mDatabase, "Food")); - assertEquals(Arrays.asList(3, 5), DBHelper.getGroupCardIds(mDatabase, "Food")); + assertEquals(2, DBHelper.getGroupCardCount(mDatabase, foodGroup._id)); + assertEquals(Arrays.asList(3, 5), DBHelper.getGroupCardIds(mDatabase, foodGroup._id)); - Group fashionGroup = DBHelper.getGroup(mDatabase, "Fashion"); + Group fashionGroup = DBHelper.getGroupByName(mDatabase, "Fashion"); assertNotNull(fashionGroup); - assertEquals(2, DBHelper.getGroupCardCount(mDatabase, "Fashion")); - assertEquals(Arrays.asList(8, 6), DBHelper.getGroupCardIds(mDatabase, "Fashion")); + assertEquals(2, DBHelper.getGroupCardCount(mDatabase, fashionGroup._id)); + assertEquals(Arrays.asList(8, 6), DBHelper.getGroupCardIds(mDatabase, fashionGroup._id)); // Check all cards LoyaltyCard card1 = DBHelper.getLoyaltyCard(mDatabase, 1); diff --git a/app/src/test/java/protect/card_locker/MainActivityTest.java b/app/src/test/java/protect/card_locker/MainActivityTest.java index c76386c83e..0a609957bf 100644 --- a/app/src/test/java/protect/card_locker/MainActivityTest.java +++ b/app/src/test/java/protect/card_locker/MainActivityTest.java @@ -194,7 +194,8 @@ public void testGroups() { assertEquals("Alphabetical two", groupTabs.getTabAt(2).getText().toString()); // Removing a group should also change the list - DBHelper.deleteGroup(database, "Alphabetical two"); + Group group2 = DBHelper.getGroupByName(database, "Alphabetical two"); + DBHelper.deleteGroup(database, group2._id); activityController.pause(); activityController.resume(); assertEquals(2, groupTabs.getTabCount()); @@ -202,7 +203,8 @@ public void testGroups() { assertEquals("One", groupTabs.getTabAt(1).getText().toString()); // Removing the last group should make the tabs disappear - DBHelper.deleteGroup(database, "One"); + Group group1 = DBHelper.getGroupByName(database, "One"); + DBHelper.deleteGroup(database, group1._id); activityController.pause(); activityController.resume(); assertEquals(0, groupTabs.getTabCount()); @@ -229,7 +231,7 @@ public void testFiltering() { DBHelper.insertGroup(database, "Group one"); List groups = new ArrayList<>(); - groups.add(DBHelper.getGroup(database, "Group one")); + groups.add(DBHelper.getGroupByName(database, "Group one")); DBHelper.setLoyaltyCardGroups(database, 1, groups); activityController.pause(); From fe7fa4bd4e278dfdd8a8986147dca4189ffa3635 Mon Sep 17 00:00:00 2001 From: FC Stegerman Date: Thu, 1 Jun 2023 19:49:55 +0200 Subject: [PATCH 2/3] WIP: cleanup --- .../java/protect/card_locker/DBHelper.java | 50 ++++++++++------ .../protect/card_locker/MainActivity.java | 2 +- .../importexport/CatimaImporter.java | 57 ++++++++----------- 3 files changed, 57 insertions(+), 52 deletions(-) diff --git a/app/src/main/java/protect/card_locker/DBHelper.java b/app/src/main/java/protect/card_locker/DBHelper.java index ee3dfe899e..9024278171 100644 --- a/app/src/main/java/protect/card_locker/DBHelper.java +++ b/app/src/main/java/protect/card_locker/DBHelper.java @@ -336,10 +336,10 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { LoyaltyCardDbGroups.ORDER + " INTEGER DEFAULT '0' )"); db.execSQL("INSERT INTO tmpDbGroups (" + - LoyaltyCardDbGroups.NAME + " ," + + LoyaltyCardDbGroups.NAME + ", " + LoyaltyCardDbGroups.ORDER + ")" + " SELECT " + - LoyaltyCardDbGroups.ID + " ," + + LoyaltyCardDbGroups.ID + ", " + LoyaltyCardDbGroups.ORDER + " FROM " + LoyaltyCardDbGroups.TABLE); @@ -348,13 +348,15 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("CREATE TEMPORARY TABLE tmpDbIdsGroups (" + LoyaltyCardDbIdsGroups.cardID + " INTEGER," + LoyaltyCardDbIdsGroups.groupID + " TEXT," + - "primary key (" + LoyaltyCardDbIdsGroups.cardID + "," + LoyaltyCardDbIdsGroups.groupID + "))"); + " primary key (" + + LoyaltyCardDbIdsGroups.cardID + ", " + + LoyaltyCardDbIdsGroups.groupID + "))"); db.execSQL("INSERT INTO tmpDbIdsGroups (" + - LoyaltyCardDbIdsGroups.cardID + " ," + + LoyaltyCardDbIdsGroups.cardID + ", " + LoyaltyCardDbIdsGroups.groupID + ")" + " SELECT " + - LoyaltyCardDbIdsGroups.cardID + " ," + + LoyaltyCardDbIdsGroups.cardID + ", " + LoyaltyCardDbIdsGroups.groupID + " FROM " + LoyaltyCardDbIdsGroups.TABLE); @@ -366,28 +368,32 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { LoyaltyCardDbGroups.ORDER + " INTEGER DEFAULT '0' )"); db.execSQL("INSERT INTO " + LoyaltyCardDbGroups.TABLE + "(" + - LoyaltyCardDbGroups.ID + " ," + - LoyaltyCardDbGroups.NAME + " ," + + LoyaltyCardDbGroups.ID + ", " + + LoyaltyCardDbGroups.NAME + ", " + LoyaltyCardDbGroups.ORDER + ")" + " SELECT " + - LoyaltyCardDbGroups.ID + " ," + - LoyaltyCardDbGroups.NAME + " ," + + LoyaltyCardDbGroups.ID + ", " + + LoyaltyCardDbGroups.NAME + ", " + LoyaltyCardDbGroups.ORDER + " FROM tmpDbGroups"); db.execSQL("CREATE TABLE " + LoyaltyCardDbIdsGroups.TABLE + "(" + LoyaltyCardDbIdsGroups.cardID + " INTEGER," + LoyaltyCardDbIdsGroups.groupID + " INTEGER," + - "primary key (" + LoyaltyCardDbIdsGroups.cardID + "," + LoyaltyCardDbIdsGroups.groupID + "))"); + " primary key (" + + LoyaltyCardDbIdsGroups.cardID + ", " + + LoyaltyCardDbIdsGroups.groupID + "))"); db.execSQL("INSERT INTO " + LoyaltyCardDbIdsGroups.TABLE + "(" + - LoyaltyCardDbIdsGroups.cardID + " ," + + LoyaltyCardDbIdsGroups.cardID + ", " + LoyaltyCardDbIdsGroups.groupID + ")" + " SELECT " + - "idsGroups." + LoyaltyCardDbIdsGroups.cardID + " ," + + "idsGroups." + LoyaltyCardDbIdsGroups.cardID + ", " + "groups." + LoyaltyCardDbGroups.ID + - " FROM tmpDbIdsGroups AS idsGroups JOIN " + LoyaltyCardDbGroups.TABLE + " AS groups ON " + - "idsGroups." + LoyaltyCardDbIdsGroups.groupID + "=" + "groups." + LoyaltyCardDbGroups.NAME); + " FROM tmpDbIdsGroups AS idsGroups JOIN " + + LoyaltyCardDbGroups.TABLE + " AS groups ON " + + "idsGroups." + LoyaltyCardDbIdsGroups.groupID + "=" + + "groups." + LoyaltyCardDbGroups.NAME); db.execSQL("DROP TABLE tmpDbGroups"); db.execSQL("DROP TABLE tmpDbIdsGroups"); @@ -880,16 +886,26 @@ public static List getGroupCardIds(SQLiteDatabase database, final int g } public static long insertGroup(SQLiteDatabase database, final String name) { - if (name.isEmpty() || getGroupByName(database, name)!= null) return -1; + if (name.isEmpty() || getGroupByName(database, name) != null) return -1; + + ContentValues contentValues = new ContentValues(); + contentValues.put(LoyaltyCardDbGroups.NAME, name); + contentValues.put(LoyaltyCardDbGroups.ORDER, getGroupCount(database)); + return database.insert(LoyaltyCardDbGroups.TABLE, null, contentValues); + } + + public static long insertGroup(SQLiteDatabase database, final int groupId, final String name) { + if (name.isEmpty() || getGroup(database, groupId) != null || getGroupByName(database, name) != null) return -1; ContentValues contentValues = new ContentValues(); + contentValues.put(LoyaltyCardDbGroups.ID, groupId); contentValues.put(LoyaltyCardDbGroups.NAME, name); contentValues.put(LoyaltyCardDbGroups.ORDER, getGroupCount(database)); return database.insert(LoyaltyCardDbGroups.TABLE, null, contentValues); } public static boolean updateGroup(SQLiteDatabase database, final int groupId, final String newName) { - if (newName.isEmpty() || getGroupByName(database, newName)!= null) return false; + if (newName.isEmpty() || getGroupByName(database, newName) != null) return false; boolean success = false; @@ -943,7 +959,7 @@ public static boolean deleteGroup(SQLiteDatabase database, final int groupId) { public static int getGroupCardCount(SQLiteDatabase database, final int groupId) { return (int) DatabaseUtils.queryNumEntries(database, LoyaltyCardDbIdsGroups.TABLE, - whereAttrs(LoyaltyCardDbIdsGroups.groupID),withArgs(groupId)); + whereAttrs(LoyaltyCardDbIdsGroups.groupID), withArgs(groupId)); } static private String whereAttrs(String... attrs) { diff --git a/app/src/main/java/protect/card_locker/MainActivity.java b/app/src/main/java/protect/card_locker/MainActivity.java index 64e9ada17f..ab75ee742f 100644 --- a/app/src/main/java/protect/card_locker/MainActivity.java +++ b/app/src/main/java/protect/card_locker/MainActivity.java @@ -416,7 +416,7 @@ protected void onResume() { Bundle bundle = new Bundle(); TabLayout.Tab tab = groupsTabLayout.getTabAt(selectedTab); if (tab != null) { - Group group = (Group) groupsTabLayout.getTabAt(selectedTab).getTag(); + Group group = (Group) tab.getTag(); if (group != null) { bundle.putInt(LoyaltyCardEditActivity.BUNDLE_ADDGROUP, group._id); } diff --git a/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java b/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java index 3ae37dbaca..bf8e850fd7 100644 --- a/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java +++ b/app/src/main/java/protect/card_locker/importexport/CatimaImporter.java @@ -22,7 +22,6 @@ import java.util.ArrayList; import java.util.Currency; import java.util.Date; -import java.util.Hashtable; import java.util.List; import java.util.TreeMap; @@ -176,7 +175,6 @@ public void parseV2(Context context, SQLiteDatabase database, BufferedReader inp public void parseV3(Context context, SQLiteDatabase database, BufferedReader input) throws IOException, FormatException, InterruptedException { int part = 0; StringBuilder stringPart = new StringBuilder(); - Hashtable groupsTable = null; try { while (true) { @@ -192,7 +190,7 @@ public void parseV3(Context context, SQLiteDatabase database, BufferedReader inp break; case 1: try { - groupsTable = parseV3Groups(database, stringPart.toString()); + parseV3Groups(database, stringPart.toString()); sectionParsed = true; } catch (FormatException e) { // We may have a multiline field, try again @@ -208,7 +206,7 @@ public void parseV3(Context context, SQLiteDatabase database, BufferedReader inp break; case 3: try { - parseV3CardGroups(database, stringPart.toString(), groupsTable); + parseV3CardGroups(database, stringPart.toString()); sectionParsed = true; } catch (FormatException e) { // We may have a multiline field, try again @@ -262,7 +260,7 @@ public void parseV2Groups(SQLiteDatabase database, String data) throws IOExcepti } } - public Hashtable parseV3Groups(SQLiteDatabase database, String data) throws IOException, FormatException, InterruptedException { + public void parseV3Groups(SQLiteDatabase database, String data) throws IOException, FormatException, InterruptedException { // Parse groups final CSVParser groupParser = new CSVParser(new StringReader(data), CSVFormat.RFC4180.builder().setHeader().build()); @@ -282,9 +280,7 @@ public Hashtable parseV3Groups(SQLiteDatabase database, String d groupParser.close(); } - Hashtable groupsTable = importGroupV3(database, records); - - return groupsTable; + importGroupsV3(database, records); } public void parseV2Cards(Context context, SQLiteDatabase database, String data) throws IOException, FormatException, InterruptedException { @@ -337,7 +333,7 @@ public void parseV2CardGroups(SQLiteDatabase database, String data) throws IOExc } } - public void parseV3CardGroups(SQLiteDatabase database, String data, Hashtable groupsTable) throws IOException, FormatException, InterruptedException { + public void parseV3CardGroups(SQLiteDatabase database, String data) throws IOException, FormatException, InterruptedException { // Parse card group mappings final CSVParser cardGroupParser = new CSVParser(new StringReader(data), CSVFormat.RFC4180.builder().setHeader().build()); @@ -358,7 +354,7 @@ public void parseV3CardGroups(SQLiteDatabase database, String data, Hashtable importGroupV3(SQLiteDatabase database, List records) throws FormatException { - Hashtable groupsTable = new Hashtable<>(); + private void importGroupsV3(SQLiteDatabase database, List records) throws FormatException { TreeMap sortedGroups = new TreeMap(); for (CSVRecord record : records) { - Integer id = CSVHelpers.extractInt(DBHelper.LoyaltyCardDbGroups.ID, record, false); + int id = CSVHelpers.extractInt(DBHelper.LoyaltyCardDbGroups.ID, record); String name = CSVHelpers.extractString(DBHelper.LoyaltyCardDbGroups.NAME, record, null); - Integer order = CSVHelpers.extractInt(DBHelper.LoyaltyCardDbGroups.ORDER, record, false); + int order = CSVHelpers.extractInt(DBHelper.LoyaltyCardDbGroups.ORDER, record); - if (id == null) { - throw new FormatException("Group has no ID: " + record); + if (name == null) { + throw new FormatException("Group has no name: " + record); } - DBHelper.insertGroup(database, name); - sortedGroups.put(order, DBHelper.getGroupByName(database, name)); - groupsTable.put(id, name); + DBHelper.insertGroup(database, id, name); + sortedGroups.put(order, DBHelper.getGroup(database, id)); } List sortedGroupList = new ArrayList<>(sortedGroups.values()); DBHelper.reorderGroups(database, sortedGroupList); - return groupsTable; } /** @@ -541,29 +533,26 @@ private Hashtable importGroupV3(SQLiteDatabase database, List cardGroups = DBHelper.getLoyaltyCardGroups(database, cardId); - String groupName = CSVHelpers.extractString(DBHelper.LoyaltyCardDbIdsGroups.groupID, record, null); - cardGroups.add(DBHelper.getGroupByName(database, groupName)); + cardGroups.add(DBHelper.getGroupByName(database, name)); DBHelper.setLoyaltyCardGroups(database, cardId, cardGroups); } /** - * Import a single card to group mapping from V3 scheme (database v16) into the database using the given + * Import a single card to group mapping from V3 scheme (database v17) into the database using the given * session. */ - private void importCardGroupMappingV3(SQLiteDatabase database, CSVRecord record, Hashtable groupsTable) throws FormatException { - Integer cardId = CSVHelpers.extractInt(DBHelper.LoyaltyCardDbIdsGroups.cardID, record, false); + private void importCardGroupMappingV3(SQLiteDatabase database, CSVRecord record) throws FormatException { + int cardId = CSVHelpers.extractInt(DBHelper.LoyaltyCardDbIdsGroups.cardID, record); + int groupId = CSVHelpers.extractInt(DBHelper.LoyaltyCardDbIdsGroups.groupID, record); List cardGroups = DBHelper.getLoyaltyCardGroups(database, cardId); - - Integer groupId = CSVHelpers.extractInt(DBHelper.LoyaltyCardDbIdsGroups.groupID, record, false); cardGroups.add(DBHelper.getGroup(database, groupId)); - DBHelper.setLoyaltyCardGroups(database, cardId, cardGroups); } } From b1742f8cc71eceb41be8e46e40af7de97b437e29 Mon Sep 17 00:00:00 2001 From: FC Stegerman Date: Thu, 1 Jun 2023 20:54:22 +0200 Subject: [PATCH 3/3] add some missing whitespace after commas --- .../protect/card_locker/DatabaseTest.java | 24 +++++++++---------- .../protect/card_locker/ImportExportTest.java | 20 ++++++++-------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/app/src/test/java/protect/card_locker/DatabaseTest.java b/app/src/test/java/protect/card_locker/DatabaseTest.java index 60a3e256a9..c71bc7b91c 100644 --- a/app/src/test/java/protect/card_locker/DatabaseTest.java +++ b/app/src/test/java/protect/card_locker/DatabaseTest.java @@ -42,7 +42,7 @@ public void setUp() { @Test public void addRemoveOneGiftCard() { assertEquals(0, DBHelper.getLoyaltyCardCount(mDatabase)); - long id = DBHelper.insertLoyaltyCard(mDatabase, "store", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), DEFAULT_HEADER_COLOR, 0, null,0); + long id = DBHelper.insertLoyaltyCard(mDatabase, "store", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), DEFAULT_HEADER_COLOR, 0, null, 0); boolean result = (id != -1); assertTrue(result); assertEquals(1, DBHelper.getLoyaltyCardCount(mDatabase)); @@ -70,7 +70,7 @@ public void addRemoveOneGiftCard() { @Test public void updateGiftCard() { - long id = DBHelper.insertLoyaltyCard(mDatabase, "store", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), DEFAULT_HEADER_COLOR, 0, null,0); + long id = DBHelper.insertLoyaltyCard(mDatabase, "store", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), DEFAULT_HEADER_COLOR, 0, null, 0); boolean result = (id != -1); assertTrue(result); assertEquals(1, DBHelper.getLoyaltyCardCount(mDatabase)); @@ -97,7 +97,7 @@ public void updateGiftCard() { @Test public void updateGiftCardOnlyStar() { - long id = DBHelper.insertLoyaltyCard(mDatabase, "store", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), DEFAULT_HEADER_COLOR, 0, null,0); + long id = DBHelper.insertLoyaltyCard(mDatabase, "store", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), DEFAULT_HEADER_COLOR, 0, null, 0); boolean result = (id != -1); assertTrue(result); assertEquals(1, DBHelper.getLoyaltyCardCount(mDatabase)); @@ -134,7 +134,7 @@ public void updateMissingGiftCard() { @Test public void emptyGiftCardValues() { - long id = DBHelper.insertLoyaltyCard(mDatabase, "", "", null, null, new BigDecimal("0"), null, "", null, null, null, 0, null,0); + long id = DBHelper.insertLoyaltyCard(mDatabase, "", "", null, null, new BigDecimal("0"), null, "", null, null, null, 0, null, 0); boolean result = (id != -1); assertTrue(result); assertEquals(1, DBHelper.getLoyaltyCardCount(mDatabase)); @@ -163,7 +163,7 @@ public void giftCardsViaCursor() { // that they are sorted for (int index = CARDS_TO_ADD - 1; index >= 0; index--) { long id = DBHelper.insertLoyaltyCard(mDatabase, "store" + index, "note" + index, null, null, new BigDecimal("0"), null, "cardId" + index, - null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), index, 0, null,0); + null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), index, 0, null, 0); boolean result = (id != -1); assertTrue(result); } @@ -209,10 +209,10 @@ public void giftCardsViaCursor() { for (int index = CARDS_TO_ADD - 1; index >= 0; index--) { if (index == CARDS_TO_ADD - 1) { id = DBHelper.insertLoyaltyCard(mDatabase, "store" + index, "note" + index, null, null, new BigDecimal("0"), null, "cardId" + index, - null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), index, 1, null,0); + null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), index, 1, null, 0); } else { id = DBHelper.insertLoyaltyCard(mDatabase, "store" + index, "note" + index, null, null, new BigDecimal("0"), null, "cardId" + index, - null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), index, 0, null,0); + null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), index, 0, null, 0); } boolean result = (id != -1); assertTrue(result); @@ -314,7 +314,7 @@ public void addRemoveOneGroup() { public void updateGroup() { // Create card assertEquals(0, DBHelper.getLoyaltyCardCount(mDatabase)); - long id = DBHelper.insertLoyaltyCard(mDatabase, "store", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), DEFAULT_HEADER_COLOR, 0, null,0); + long id = DBHelper.insertLoyaltyCard(mDatabase, "store", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), DEFAULT_HEADER_COLOR, 0, null, 0); boolean result = (id != -1); assertTrue(result); assertEquals(1, DBHelper.getLoyaltyCardCount(mDatabase)); @@ -344,9 +344,9 @@ public void updateGroup() { assertEquals(1, DBHelper.getGroupCount(mDatabase)); // Group one no longer exists - Group groupByNameDeleted = DBHelper.getGroupByName(mDatabase,"group one"); + Group groupByNameDeleted = DBHelper.getGroupByName(mDatabase, "group one"); assertNull(groupByNameDeleted); - Group groupById = DBHelper.getGroup(mDatabase,group._id); + Group groupById = DBHelper.getGroup(mDatabase, group._id); assertNotNull(groupById); // But group one renamed does @@ -446,7 +446,7 @@ public void updateGroupDuplicate() { public void cardAddAndRemoveGroups() { // Create card assertEquals(0, DBHelper.getLoyaltyCardCount(mDatabase)); - long id = DBHelper.insertLoyaltyCard(mDatabase, "store", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), DEFAULT_HEADER_COLOR, 0, null,0); + long id = DBHelper.insertLoyaltyCard(mDatabase, "store", "note", null, null, new BigDecimal("0"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), DEFAULT_HEADER_COLOR, 0, null, 0); boolean result = (id != -1); assertTrue(result); assertEquals(1, DBHelper.getLoyaltyCardCount(mDatabase)); @@ -546,7 +546,7 @@ public void databaseUpgradeFromVersion1() { @Test public void updateGiftCardOnlyBalance() { - long id = DBHelper.insertLoyaltyCard(mDatabase, "store", "note", null, null, new BigDecimal("100"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), DEFAULT_HEADER_COLOR, 0, null,0); + long id = DBHelper.insertLoyaltyCard(mDatabase, "store", "note", null, null, new BigDecimal("100"), null, "cardId", null, CatimaBarcode.fromBarcode(BarcodeFormat.UPC_A), DEFAULT_HEADER_COLOR, 0, null, 0); boolean result = (id != -1); assertTrue(result); assertEquals(1, DBHelper.getLoyaltyCardCount(mDatabase)); diff --git a/app/src/test/java/protect/card_locker/ImportExportTest.java b/app/src/test/java/protect/card_locker/ImportExportTest.java index c279f541a5..97f29a4974 100644 --- a/app/src/test/java/protect/card_locker/ImportExportTest.java +++ b/app/src/test/java/protect/card_locker/ImportExportTest.java @@ -79,7 +79,7 @@ private void addLoyaltyCards(int cardsToAdd) { for (int index = cardsToAdd; index > 0; index--) { String storeName = String.format("store, \"%4d", index); String note = String.format("note, \"%4d", index); - long id = DBHelper.insertLoyaltyCard(mDatabase, storeName, note, null, null, new BigDecimal(String.valueOf(index)), null, BARCODE_DATA, null, BARCODE_TYPE, index, 0, null,0); + long id = DBHelper.insertLoyaltyCard(mDatabase, storeName, note, null, null, new BigDecimal(String.valueOf(index)), null, BARCODE_DATA, null, BARCODE_TYPE, index, 0, null, 0); boolean result = (id != -1); assertTrue(result); } @@ -93,7 +93,7 @@ private void addLoyaltyCardsFiveStarred() { for (int index = cardsToAdd; index > 4; index--) { String storeName = String.format("store, \"%4d", index); String note = String.format("note, \"%4d", index); - long id = DBHelper.insertLoyaltyCard(mDatabase, storeName, note, null, null, new BigDecimal(String.valueOf(index)), null, BARCODE_DATA, null, BARCODE_TYPE, index, 1, null,0); + long id = DBHelper.insertLoyaltyCard(mDatabase, storeName, note, null, null, new BigDecimal(String.valueOf(index)), null, BARCODE_DATA, null, BARCODE_TYPE, index, 1, null, 0); boolean result = (id != -1); assertTrue(result); } @@ -101,7 +101,7 @@ private void addLoyaltyCardsFiveStarred() { String storeName = String.format("store, \"%4d", index); String note = String.format("note, \"%4d", index); //if index is even - long id = DBHelper.insertLoyaltyCard(mDatabase, storeName, note, null, null, new BigDecimal(String.valueOf(index)), null, BARCODE_DATA, null, BARCODE_TYPE, index, 0, null,0); + long id = DBHelper.insertLoyaltyCard(mDatabase, storeName, note, null, null, new BigDecimal(String.valueOf(index)), null, BARCODE_DATA, null, BARCODE_TYPE, index, 0, null, 0); boolean result = (id != -1); assertTrue(result); } @@ -110,7 +110,7 @@ private void addLoyaltyCardsFiveStarred() { @Test public void addLoyaltyCardsWithExpiryNeverPastTodayFuture() { - long id = DBHelper.insertLoyaltyCard(mDatabase, "No Expiry", "", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, 0, 0, null,0); + long id = DBHelper.insertLoyaltyCard(mDatabase, "No Expiry", "", null, null, new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, 0, 0, null, 0); boolean result = (id != -1); assertTrue(result); @@ -127,7 +127,7 @@ public void addLoyaltyCardsWithExpiryNeverPastTodayFuture() { assertEquals(Integer.valueOf(0), card.headerColor); assertEquals(0, card.starStatus); - id = DBHelper.insertLoyaltyCard(mDatabase, "Past", "", null, new Date((long) 1), new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, 0, 0, null,0); + id = DBHelper.insertLoyaltyCard(mDatabase, "Past", "", null, new Date((long) 1), new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, 0, 0, null, 0); result = (id != -1); assertTrue(result); @@ -144,7 +144,7 @@ public void addLoyaltyCardsWithExpiryNeverPastTodayFuture() { assertEquals(Integer.valueOf(0), card.headerColor); assertEquals(0, card.starStatus); - id = DBHelper.insertLoyaltyCard(mDatabase, "Today", "", null, new Date(), new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, 0, 0, null,0); + id = DBHelper.insertLoyaltyCard(mDatabase, "Today", "", null, new Date(), new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, 0, 0, null, 0); result = (id != -1); assertTrue(result); @@ -164,7 +164,7 @@ public void addLoyaltyCardsWithExpiryNeverPastTodayFuture() { // This will break after 19 January 2038 // If someone is still maintaining this code base by then: I love you - id = DBHelper.insertLoyaltyCard(mDatabase, "Future", "", null, new Date(2147483648000L), new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, 0, 0, null,0); + id = DBHelper.insertLoyaltyCard(mDatabase, "Future", "", null, new Date(2147483648000L), new BigDecimal("0"), null, BARCODE_DATA, null, BARCODE_TYPE, 0, 0, null, 0); result = (id != -1); assertTrue(result); @@ -435,7 +435,7 @@ public void multipleCardsExportImportWithGroups() throws IOException { groupsForFour.add(DBHelper.getGroupByName(mDatabase, "group, \" 3")); List groupsForFive = new ArrayList<>(); - groupsForFive.add(DBHelper.getGroupByName(mDatabase,"group, \" 1")); + groupsForFive.add(DBHelper.getGroupByName(mDatabase, "group, \" 1")); groupsForFive.add(DBHelper.getGroupByName(mDatabase, "group, \" 3")); DBHelper.setLoyaltyCardGroups(mDatabase, 1, groupsForOne); @@ -823,7 +823,7 @@ public void exportImportV2Zip() throws FileNotFoundException { HashMap loyaltyCardIconImages = new HashMap<>(); // Create card 1 - int loyaltyCardId = (int) DBHelper.insertLoyaltyCard(mDatabase, "Card 1", "Note 1", new Date(1601510400), new Date(1618053234), new BigDecimal("100"), Currency.getInstance("USD"), "1234", "5432", CatimaBarcode.fromBarcode(BarcodeFormat.QR_CODE), 1, 0, null,0); + int loyaltyCardId = (int) DBHelper.insertLoyaltyCard(mDatabase, "Card 1", "Note 1", new Date(1601510400), new Date(1618053234), new BigDecimal("100"), Currency.getInstance("USD"), "1234", "5432", CatimaBarcode.fromBarcode(BarcodeFormat.QR_CODE), 1, 0, null, 0); loyaltyCardHashMap.put(loyaltyCardId, DBHelper.getLoyaltyCard(mDatabase, loyaltyCardId)); DBHelper.insertGroup(mDatabase, "One"); List groups = Arrays.asList(DBHelper.getGroupByName(mDatabase, "One")); @@ -837,7 +837,7 @@ public void exportImportV2Zip() throws FileNotFoundException { loyaltyCardIconImages.put(loyaltyCardId, bitmap1); // Create card 2 - loyaltyCardId = (int) DBHelper.insertLoyaltyCard(mDatabase, "Card 2", "", null, null, new BigDecimal(0), null, "123456", null, null, 2, 1, null,0); + loyaltyCardId = (int) DBHelper.insertLoyaltyCard(mDatabase, "Card 2", "", null, null, new BigDecimal(0), null, "123456", null, null, 2, 1, null, 0); loyaltyCardHashMap.put(loyaltyCardId, DBHelper.getLoyaltyCard(mDatabase, loyaltyCardId)); // Export everything