Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

WIP: Company DB #143

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions app/src/main/java/protect/card_locker/Company.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package protect.card_locker;

import com.google.zxing.BarcodeFormat;

import java.util.ArrayList;
import java.util.List;

import protect.card_locker.translationRules.AddPrefixTranslationRule;
import protect.card_locker.translationRules.TranslationRule;

public class Company {
private final String name;
private final List<BarcodeFormat> barcodeFormats;
private final List<TranslationRule> translationRuleList;

private Company(final Builder builder) {
name = builder.name;
barcodeFormats = builder.barcodeFormats;
translationRuleList = builder.translationRuleList;
}

public String getName() {
return name;
}

public String cardIDToBarcode(String cardID) {
String barcode = cardID;
for (int i = 0; i < translationRuleList.size(); i++) {
barcode = translationRuleList.get(i).apply(barcode);
}

return barcode;
}

public String BarcodeToCardID(String barcode) {
String cardID = barcode;
for (int i = translationRuleList.size() - 1; i > 0; i--) {
cardID = translationRuleList.get(i).undo(cardID);
}

return cardID;
}

static class Builder {
private final String name;
private final List<BarcodeFormat> barcodeFormats = new ArrayList<>();
private final List<TranslationRule> translationRuleList = new ArrayList<>();

public Builder(String name) {
this.name = name;
}

public Builder addBarcodeFormat(final BarcodeFormat barcodeFormat) {
barcodeFormats.add(barcodeFormat);
return this;
}

public Builder addPrefix(final String prefix) {
translationRuleList.add(new AddPrefixTranslationRule(prefix));
return this;
}

public Company create() {
return new Company(this);
}
}
}
115 changes: 115 additions & 0 deletions app/src/main/java/protect/card_locker/CompanyDBHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package protect.card_locker;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.google.zxing.BarcodeFormat;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

public class CompanyDBHelper extends SQLiteOpenHelper
{
public static final String DATABASE_NAME = "CatimaCompanyList.db";
public static final int DATABASE_VERSION = 1;

static class LoyaltyCardProgramDbCountries
{
public static final String TABLE = "countries";
public static final String ID = "_id";
}

static class LoyaltyCardProgramDbIds
{
public static final String TABLE = "programs";
public static final String ID = "_id";
public static final String NAME = "name";
}

static class LoyaltyCardProgramDbIdsCountries
{
public static final String TABLE = "programCountries";
public static final String programId = "programId";
public static final String countryId = "countryId";
}

public CompanyDBHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db)
{
// drop all tables
db.execSQL("drop table if exists " + LoyaltyCardProgramDbCountries.TABLE);
db.execSQL("drop table if exists " + LoyaltyCardProgramDbIds.TABLE);
db.execSQL("drop table if exists " + LoyaltyCardProgramDbIdsCountries.TABLE);

// create table for program countries
db.execSQL("create table " + CompanyDBHelper.LoyaltyCardProgramDbCountries.TABLE + "(" +
CompanyDBHelper.LoyaltyCardProgramDbCountries.ID + " TEXT primary key not null)");

// create table for programs
db.execSQL("create table " + LoyaltyCardProgramDbIds.TABLE + "(" +
LoyaltyCardProgramDbIds.ID + " int primary key not null," +
LoyaltyCardProgramDbIds.NAME + " TEXT not null)");

// create associative table for programs in countries
db.execSQL("create table " + LoyaltyCardProgramDbIdsCountries.TABLE + "(" +
LoyaltyCardProgramDbIdsCountries.programId + " TEXT," +
LoyaltyCardProgramDbIdsCountries.countryId + " TEXT," +
"primary key (" + LoyaltyCardProgramDbIdsCountries.programId + "," + LoyaltyCardProgramDbIdsCountries.countryId + "))");

// create all entries
addCompany(0, new Company.Builder("Albert Heijn").addBarcodeFormat(BarcodeFormat.EAN_13).create(), createLocaleList(Arrays.asList("NL")));
addCompany(1, new Company.Builder("Air Miles").addBarcodeFormat(BarcodeFormat.EAN_13).addPrefix("470").create(), createLocaleList(Arrays.asList("NL")));
addCompany(2, new Company.Builder("HEMA").addBarcodeFormat(BarcodeFormat.QR_CODE).create(), createLocaleList(Arrays.asList("NL")));
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }

private List<Locale> createLocaleList(List<String> locales) {
List<Locale> localeList = new ArrayList<>();
for (String locale : locales) {
localeList.add(new Locale("en", locale));
}

return localeList;
}

private void addCompany(Integer id, Company company, List<Locale> locales) {
SQLiteDatabase db = getWritableDatabase();

// Create programs
// TODO: Insert supported barcodes and translations rules
ContentValues contentValues = new ContentValues();
contentValues.put(LoyaltyCardProgramDbIds.ID, id);
contentValues.put(LoyaltyCardProgramDbIds.NAME, company.getName());
db.insert(CompanyDBHelper.LoyaltyCardProgramDbIds.TABLE, null, contentValues);

// Couple to locales
ContentValues localeContentValues;
ContentValues programLocales;
for (Locale locale: locales) {
String countryCode = locale.getCountry();

// Create locale table if it doesn't exist
localeContentValues = new ContentValues();
localeContentValues.put(LoyaltyCardProgramDbCountries.ID, countryCode);

db.insertWithOnConflict(LoyaltyCardProgramDbCountries.TABLE, "", localeContentValues, SQLiteDatabase.CONFLICT_IGNORE);

// Couple to programs
programLocales = new ContentValues();
programLocales.put(LoyaltyCardProgramDbIdsCountries.countryId, countryCode);
programLocales.put(LoyaltyCardProgramDbIdsCountries.programId, id);
db.insert(LoyaltyCardProgramDbIdsCountries.TABLE, "", programLocales);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package protect.card_locker.translationRules;

public class AddPrefixTranslationRule implements TranslationRule {
String prefix;

public AddPrefixTranslationRule(String prefix) {
this.prefix = prefix;
}

@Override
public String apply(String value) {
return this.prefix + value;
}

@Override
public String undo(String value) {
if (!value.startsWith(this.prefix)) {
throw new IllegalArgumentException();
}

return value.substring(this.prefix.length());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package protect.card_locker.translationRules;

public interface TranslationRule {
String apply(String value);
String undo(String value);
}