mirror of
https://github.com/CatimaLoyalty/Android.git
synced 2026-04-05 08:03:47 -04:00
Create Voucher Vault import code and test
This commit is contained in:
@@ -20,14 +20,14 @@ public class DBHelper extends SQLiteOpenHelper
|
||||
public static final int ORIGINAL_DATABASE_VERSION = 1;
|
||||
public static final int DATABASE_VERSION = 8;
|
||||
|
||||
static class LoyaltyCardDbGroups
|
||||
public static class LoyaltyCardDbGroups
|
||||
{
|
||||
public static final String TABLE = "groups";
|
||||
public static final String ID = "_id";
|
||||
public static final String ORDER = "orderId";
|
||||
}
|
||||
|
||||
static class LoyaltyCardDbIds
|
||||
public static class LoyaltyCardDbIds
|
||||
{
|
||||
public static final String TABLE = "cards";
|
||||
public static final String ID = "_id";
|
||||
@@ -43,7 +43,7 @@ public class DBHelper extends SQLiteOpenHelper
|
||||
public static final String STAR_STATUS = "starstatus";
|
||||
}
|
||||
|
||||
static class LoyaltyCardDbIdsGroups
|
||||
public static class LoyaltyCardDbIdsGroups
|
||||
{
|
||||
public static final String TABLE = "cardsGroups";
|
||||
public static final String cardID = "cardId";
|
||||
|
||||
@@ -13,6 +13,9 @@ import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import protect.card_locker.importexport.MultiFormatExporter;
|
||||
import protect.card_locker.importexport.MultiFormatImporter;
|
||||
|
||||
class ImportExportTask extends AsyncTask<Void, Void, Boolean>
|
||||
{
|
||||
private static final String TAG = "Catima";
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package protect.card_locker;
|
||||
package protect.card_locker.importexport;
|
||||
|
||||
import android.database.Cursor;
|
||||
|
||||
@@ -8,6 +8,10 @@ import org.apache.commons.csv.CSVPrinter;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
|
||||
import protect.card_locker.DBHelper;
|
||||
import protect.card_locker.Group;
|
||||
import protect.card_locker.LoyaltyCard;
|
||||
|
||||
/**
|
||||
* Class for exporting the database into CSV (Comma Separate Values)
|
||||
* format.
|
||||
@@ -1,4 +1,4 @@
|
||||
package protect.card_locker;
|
||||
package protect.card_locker.importexport;
|
||||
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
|
||||
@@ -15,6 +15,10 @@ import java.util.Currency;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import protect.card_locker.DBHelper;
|
||||
import protect.card_locker.FormatException;
|
||||
import protect.card_locker.Group;
|
||||
|
||||
/**
|
||||
* Class for importing a database from CSV (Comma Separate Values)
|
||||
* formatted data.
|
||||
@@ -1,8 +1,10 @@
|
||||
package protect.card_locker;
|
||||
package protect.card_locker.importexport;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
|
||||
import protect.card_locker.DBHelper;
|
||||
|
||||
/**
|
||||
* Interface for a class which can export the contents of the database
|
||||
* in a given format.
|
||||
@@ -1,7 +1,13 @@
|
||||
package protect.card_locker;
|
||||
package protect.card_locker.importexport;
|
||||
|
||||
import org.json.JSONException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.text.ParseException;
|
||||
|
||||
import protect.card_locker.DBHelper;
|
||||
import protect.card_locker.FormatException;
|
||||
|
||||
/**
|
||||
* Interface for a class which can import the contents of a stream
|
||||
@@ -15,5 +21,5 @@ public interface DatabaseImporter
|
||||
* @throws IOException
|
||||
* @throws FormatException
|
||||
*/
|
||||
void importData(DBHelper db, InputStreamReader input) throws IOException, FormatException, InterruptedException;
|
||||
void importData(DBHelper db, InputStreamReader input) throws IOException, FormatException, InterruptedException, JSONException, ParseException;
|
||||
}
|
||||
@@ -1,10 +1,15 @@
|
||||
package protect.card_locker;
|
||||
package protect.card_locker.importexport;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
|
||||
import protect.card_locker.DBHelper;
|
||||
import protect.card_locker.DataFormat;
|
||||
import protect.card_locker.importexport.CsvDatabaseExporter;
|
||||
import protect.card_locker.importexport.DatabaseExporter;
|
||||
|
||||
public class MultiFormatExporter
|
||||
{
|
||||
private static final String TAG = "Catima";
|
||||
@@ -1,9 +1,18 @@
|
||||
package protect.card_locker;
|
||||
package protect.card_locker.importexport;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import org.json.JSONException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.text.ParseException;
|
||||
|
||||
import protect.card_locker.DBHelper;
|
||||
import protect.card_locker.DataFormat;
|
||||
import protect.card_locker.FormatException;
|
||||
import protect.card_locker.importexport.CsvDatabaseImporter;
|
||||
import protect.card_locker.importexport.DatabaseImporter;
|
||||
|
||||
public class MultiFormatImporter
|
||||
{
|
||||
@@ -38,7 +47,7 @@ public class MultiFormatImporter
|
||||
importer.importData(db, input);
|
||||
return true;
|
||||
}
|
||||
catch(IOException | FormatException | InterruptedException e)
|
||||
catch(IOException | FormatException | InterruptedException | JSONException | ParseException e)
|
||||
{
|
||||
Log.e(TAG, "Failed to import data", e);
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
package protect.card_locker.importexport;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.graphics.Color;
|
||||
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.math.BigDecimal;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Currency;
|
||||
import java.util.Date;
|
||||
|
||||
import protect.card_locker.DBHelper;
|
||||
import protect.card_locker.FormatException;
|
||||
|
||||
/**
|
||||
* Class for importing a database from CSV (Comma Separate Values)
|
||||
* formatted data.
|
||||
*
|
||||
* The database's loyalty cards are expected to appear in the CSV data.
|
||||
* A header is expected for the each table showing the names of the columns.
|
||||
*/
|
||||
public class VoucherVaultImporter implements DatabaseImporter
|
||||
{
|
||||
public void importData(DBHelper db, InputStreamReader input) throws IOException, FormatException, JSONException, ParseException {
|
||||
BufferedReader bufferedReader = new BufferedReader(input);
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String line;
|
||||
while ((line = bufferedReader.readLine()) != null) {
|
||||
sb.append(line);
|
||||
}
|
||||
JSONArray jsonArray = new JSONArray(sb.toString());
|
||||
|
||||
SQLiteDatabase database = db.getWritableDatabase();
|
||||
database.beginTransaction();
|
||||
|
||||
// See https://github.com/tim-smart/vouchervault/issues/4#issuecomment-788226503 for more info
|
||||
for (int i = 0; i < jsonArray.length(); i++) {
|
||||
JSONObject jsonCard = jsonArray.getJSONObject(i);
|
||||
|
||||
String store = jsonCard.getString("description");
|
||||
|
||||
Date expiry = null;
|
||||
if (!jsonCard.isNull("expires")) {
|
||||
@SuppressLint("SimpleDateFormat") SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
|
||||
expiry = dateFormat.parse(jsonCard.getString("expires"));
|
||||
}
|
||||
|
||||
BigDecimal balance;
|
||||
if (!jsonCard.isNull("balance")) {
|
||||
balance = new BigDecimal(String.valueOf(jsonCard.getDouble("balance")));
|
||||
} else {
|
||||
balance = new BigDecimal("0");
|
||||
}
|
||||
|
||||
Currency balanceType = Currency.getInstance("USD");
|
||||
|
||||
String cardId = jsonCard.getString("code");
|
||||
|
||||
String barcodeType = null;
|
||||
|
||||
String codeTypeFromJSON = jsonCard.getString("codeType");
|
||||
switch (codeTypeFromJSON) {
|
||||
case "CODE128":
|
||||
barcodeType = BarcodeFormat.CODE_128.name();
|
||||
break;
|
||||
case "CODE39":
|
||||
barcodeType = BarcodeFormat.CODE_39.name();
|
||||
break;
|
||||
case "EAN13":
|
||||
barcodeType = BarcodeFormat.EAN_13.name();
|
||||
break;
|
||||
case "QR":
|
||||
barcodeType = BarcodeFormat.QR_CODE.name();
|
||||
break;
|
||||
case "TEXT":
|
||||
break;
|
||||
default:
|
||||
throw new FormatException("Unknown barcode type found: " + codeTypeFromJSON);
|
||||
}
|
||||
|
||||
int headerColor;
|
||||
|
||||
String colorFromJSON = jsonCard.getString("color");
|
||||
switch (colorFromJSON) {
|
||||
case "GREY":
|
||||
headerColor = Color.GRAY;
|
||||
break;
|
||||
case "BLUE":
|
||||
headerColor = Color.BLUE;
|
||||
break;
|
||||
case "GREEN":
|
||||
headerColor = Color.GREEN;
|
||||
break;
|
||||
case "ORANGE":
|
||||
headerColor = Color.rgb(255, 165, 0);
|
||||
break;
|
||||
case "PURPLE":
|
||||
headerColor = Color.rgb(128, 0, 128);
|
||||
break;
|
||||
case "RED":
|
||||
headerColor = Color.RED;
|
||||
break;
|
||||
case "YELLOW":
|
||||
headerColor = Color.YELLOW;
|
||||
break;
|
||||
default:
|
||||
throw new FormatException("Unknown colour type foun: " + colorFromJSON);
|
||||
}
|
||||
|
||||
db.insertLoyaltyCard(store, "", expiry, balance, balanceType, cardId, barcodeType, headerColor, 0);
|
||||
}
|
||||
|
||||
database.setTransactionSuccessful();
|
||||
database.endTransaction();
|
||||
database.close();
|
||||
|
||||
bufferedReader.close();
|
||||
}
|
||||
}
|
||||
@@ -3,10 +3,12 @@ package protect.card_locker;
|
||||
import android.app.Activity;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.graphics.Color;
|
||||
import android.os.Environment;
|
||||
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -26,11 +28,17 @@ import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.math.BigDecimal;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.ParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Currency;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import protect.card_locker.importexport.MultiFormatExporter;
|
||||
import protect.card_locker.importexport.MultiFormatImporter;
|
||||
import protect.card_locker.importexport.VoucherVaultImporter;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
@@ -112,20 +120,65 @@ public class ImportExportTest
|
||||
boolean result = (id != -1);
|
||||
assertTrue(result);
|
||||
|
||||
LoyaltyCard card = db.getLoyaltyCard((int) id);
|
||||
assertEquals("No Expiry", card.store);
|
||||
assertEquals("", card.note);
|
||||
assertEquals(null, card.expiry);
|
||||
assertEquals(new BigDecimal("0"), card.balance);
|
||||
assertEquals(null, card.balanceType);
|
||||
assertEquals(BARCODE_DATA, card.cardId);
|
||||
assertEquals(BARCODE_TYPE, card.barcodeType);
|
||||
assertEquals(Integer.valueOf(0), card.headerColor);
|
||||
assertEquals(0, card.starStatus);
|
||||
|
||||
id = db.insertLoyaltyCard("Past", "", new Date((long) 1), new BigDecimal("0"), null, BARCODE_DATA, BARCODE_TYPE, 0, 0);
|
||||
result = (id != -1);
|
||||
assertTrue(result);
|
||||
|
||||
card = db.getLoyaltyCard((int) id);
|
||||
assertEquals("Past", card.store);
|
||||
assertEquals("", card.note);
|
||||
assertTrue(card.expiry.before(new Date()));
|
||||
assertEquals(new BigDecimal("0"), card.balance);
|
||||
assertEquals(null, card.balanceType);
|
||||
assertEquals(BARCODE_DATA, card.cardId);
|
||||
assertEquals(BARCODE_TYPE, card.barcodeType);
|
||||
assertEquals(Integer.valueOf(0), card.headerColor);
|
||||
assertEquals(0, card.starStatus);
|
||||
|
||||
id = db.insertLoyaltyCard("Today", "", new Date(), new BigDecimal("0"), null, BARCODE_DATA, BARCODE_TYPE, 0, 0);
|
||||
result = (id != -1);
|
||||
assertTrue(result);
|
||||
|
||||
card = db.getLoyaltyCard((int) id);
|
||||
assertEquals("Today", card.store);
|
||||
assertEquals("", card.note);
|
||||
assertTrue(card.expiry.before(new Date(new Date().getTime()+86400)));
|
||||
assertTrue(card.expiry.after(new Date(new Date().getTime()-86400)));
|
||||
assertEquals(new BigDecimal("0"), card.balance);
|
||||
assertEquals(null, card.balanceType);
|
||||
assertEquals(BARCODE_DATA, card.cardId);
|
||||
assertEquals(BARCODE_TYPE, card.barcodeType);
|
||||
assertEquals(Integer.valueOf(0), card.headerColor);
|
||||
assertEquals(0, card.starStatus);
|
||||
|
||||
// This will break after 19 January 2038
|
||||
// If someone is still maintaining this code base by then: I love you
|
||||
id = db.insertLoyaltyCard("Future", "", new Date(2147483648L), new BigDecimal("0"), null, BARCODE_DATA, BARCODE_TYPE, 0, 0);
|
||||
id = db.insertLoyaltyCard("Future", "", new Date(2147483648000L), new BigDecimal("0"), null, BARCODE_DATA, BARCODE_TYPE, 0, 0);
|
||||
result = (id != -1);
|
||||
assertTrue(result);
|
||||
|
||||
card = db.getLoyaltyCard((int) id);
|
||||
assertEquals("Future", card.store);
|
||||
assertEquals("", card.note);
|
||||
assertTrue(card.expiry.after(new Date(new Date().getTime()+86400)));
|
||||
assertEquals(new BigDecimal("0"), card.balance);
|
||||
assertEquals(null, card.balanceType);
|
||||
assertEquals(BARCODE_DATA, card.cardId);
|
||||
assertEquals(BARCODE_TYPE, card.barcodeType);
|
||||
assertEquals(Integer.valueOf(0), card.headerColor);
|
||||
assertEquals(0, card.starStatus);
|
||||
|
||||
assertEquals(4, db.getLoyaltyCardCount());
|
||||
}
|
||||
|
||||
@@ -830,59 +883,62 @@ public class ImportExportTest
|
||||
clearDatabase();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void importVoucherVault() throws IOException, FormatException, JSONException, ParseException {
|
||||
String jsonText = "[\n" +
|
||||
" {\n" +
|
||||
" \"uuid\": \"ae1ae525-3f27-481e-853a-8c30b7fa12d8\",\n" +
|
||||
" \"description\": \"Clothes Store\",\n" +
|
||||
" \"code\": \"123456\",\n" +
|
||||
" \"codeType\": \"CODE128\",\n" +
|
||||
" \"expires\": null,\n" +
|
||||
" \"removeOnceExpired\": true,\n" +
|
||||
" \"balance\": null,\n" +
|
||||
" \"color\": \"GREY\"\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"uuid\": \"29a5d3b3-eace-4311-a15c-4c7e6a010531\",\n" +
|
||||
" \"description\": \"Department Store\",\n" +
|
||||
" \"code\": \"26846363\",\n" +
|
||||
" \"codeType\": \"CODE39\",\n" +
|
||||
" \"expires\": \"2021-03-26T00:00:00.000\",\n" +
|
||||
" \"removeOnceExpired\": true,\n" +
|
||||
" \"balance\": 3.5,\n" +
|
||||
" \"color\": \"PURPLE\"\n" +
|
||||
" }\n" +
|
||||
"]";
|
||||
|
||||
private void checkLoyaltyCardsExpiry()
|
||||
{
|
||||
Cursor cursor = db.getLoyaltyCardCursor();
|
||||
cursor.moveToNext();
|
||||
LoyaltyCard card = LoyaltyCard.toLoyaltyCard(cursor);
|
||||
assertEquals("Never", card.store);
|
||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(jsonText.getBytes(StandardCharsets.UTF_8));
|
||||
InputStreamReader inStream = new InputStreamReader(inputStream);
|
||||
|
||||
// Import the Voucher Vault data
|
||||
new VoucherVaultImporter().importData(db, inStream);
|
||||
assertEquals(2, db.getLoyaltyCardCount());
|
||||
|
||||
LoyaltyCard card = db.getLoyaltyCard(1);
|
||||
|
||||
assertEquals("Clothes Store", card.store);
|
||||
assertEquals("", card.note);
|
||||
assertEquals(null, card.expiry);
|
||||
assertEquals(new BigDecimal("0"), card.balance);
|
||||
assertEquals(null, card.balanceType);
|
||||
assertEquals(BARCODE_DATA, card.cardId);
|
||||
assertEquals(BARCODE_TYPE, card.barcodeType);
|
||||
assertEquals(Integer.valueOf(0), card.headerColor);
|
||||
assertEquals(Currency.getInstance("USD"), card.balanceType);
|
||||
assertEquals("123456", card.cardId);
|
||||
assertEquals(BarcodeFormat.CODE_128.name(), card.barcodeType);
|
||||
assertEquals(0, card.starStatus);
|
||||
assertEquals(Color.GRAY, (long) card.headerColor);
|
||||
|
||||
cursor.moveToNext();
|
||||
card = LoyaltyCard.toLoyaltyCard(cursor);
|
||||
assertEquals("Past", card.store);
|
||||
card = db.getLoyaltyCard(2);
|
||||
|
||||
assertEquals("Department Store", card.store);
|
||||
assertEquals("", card.note);
|
||||
assertTrue(card.expiry.before(new Date()));
|
||||
assertEquals(new BigDecimal("0"), card.balance);
|
||||
assertEquals(null, card.balanceType);
|
||||
assertEquals(BARCODE_DATA, card.cardId);
|
||||
assertEquals(BARCODE_TYPE, card.barcodeType);
|
||||
assertEquals(Integer.valueOf(0), card.headerColor);
|
||||
assertEquals(new Date(1616713200000L), card.expiry);
|
||||
assertEquals(new BigDecimal("3.5"), card.balance);
|
||||
assertEquals(Currency.getInstance("USD"), card.balanceType);
|
||||
assertEquals("26846363", card.cardId);
|
||||
assertEquals(BarcodeFormat.CODE_39.name(), card.barcodeType);
|
||||
assertEquals(0, card.starStatus);
|
||||
assertEquals(Color.rgb(128, 0, 128), (long) card.headerColor);
|
||||
|
||||
cursor.moveToNext();
|
||||
card = LoyaltyCard.toLoyaltyCard(cursor);
|
||||
assertEquals("Today", card.store);
|
||||
assertEquals("", card.note);
|
||||
assertTrue(card.expiry.before(new Date(new Date().getTime()+86400)));
|
||||
assertTrue(card.expiry.after(new Date(new Date().getTime()-86400)));
|
||||
assertEquals(new BigDecimal("0"), card.balance);
|
||||
assertEquals(null, card.balanceType);
|
||||
assertEquals(BARCODE_DATA, card.cardId);
|
||||
assertEquals(BARCODE_TYPE, card.barcodeType);
|
||||
assertEquals(Integer.valueOf(0), card.headerColor);
|
||||
assertEquals(0, card.starStatus);
|
||||
|
||||
cursor.moveToNext();
|
||||
card = LoyaltyCard.toLoyaltyCard(cursor);
|
||||
assertEquals("Future", card.store);
|
||||
assertEquals("", card.note);
|
||||
assertTrue(card.expiry.after(new Date(new Date().getTime()+86400)));
|
||||
assertEquals(new BigDecimal("0"), card.balance);
|
||||
assertEquals(null, card.balanceType);
|
||||
assertEquals(BARCODE_DATA, card.cardId);
|
||||
assertEquals(BARCODE_TYPE, card.barcodeType);
|
||||
assertEquals(Integer.valueOf(0), card.headerColor);
|
||||
assertEquals(0, card.starStatus);
|
||||
|
||||
cursor.close();
|
||||
clearDatabase();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user