Skip to content

Commit dfc9b78

Browse files
committed
add support for transmission codes
1 parent 4d66c26 commit dfc9b78

File tree

6 files changed

+82
-10
lines changed

6 files changed

+82
-10
lines changed

core/src/androidTest/assets/product2.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@
77
"price": 399,
88
"decimalDigits": 2,
99
"taxCategory": "normal",
10-
"scannableCodes": [
10+
"eans": [
1111
"1"
1212
],
13+
"codes": [
14+
{
15+
"code": "1",
16+
"transmissionCode": "000001"
17+
}
18+
],
1319
"productType": "default"
1420
}

core/src/androidTest/java/io/snabble/sdk/ProductDatabaseTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Set;
2323
import java.util.concurrent.CountDownLatch;
2424

25+
import static org.junit.Assert.assertArrayEquals;
2526
import static org.junit.Assert.assertEquals;
2627
import static org.junit.Assert.assertFalse;
2728
import static org.junit.Assert.assertNull;
@@ -175,9 +176,13 @@ public void testFindBySkuOnline() {
175176
ProductDatabase productDatabase = project.getProductDatabase();
176177
final Product product = findBySkuBlocking(productDatabase, "online1");
177178
assertEquals(product.getSku(), "online1");
179+
assertArrayEquals(product.getScannableCodes(), new String[]{"0"});
180+
assertEquals(product.getTransmissionCode("0"), "0");
178181

179182
final Product product2 = findBySkuBlocking(productDatabase, "online2");
180183
assertEquals(product2.getSku(), "online2");
184+
assertArrayEquals(product2.getScannableCodes(), new String[]{"1"});
185+
assertEquals(product2.getTransmissionCode("1"), "000001");
181186

182187
assertNull(findBySkuBlocking(productDatabase, "unknownCode"));
183188
}
@@ -372,4 +377,15 @@ public void testFindProductWithEan13ByEan8() throws IOException, Snabble.Snabble
372377
String[] codes = product.getScannableCodes();
373378
Assert.assertTrue(ArrayUtils.contains(codes, "0000040084015"));
374379
}
380+
381+
@Test
382+
public void testTransmissionCodeIsSameOnOldDbVersion() throws IOException, Snabble.SnabbleException {
383+
setupSdkWithDb("demoDb_1_6.sqlite3");
384+
385+
ProductDatabase productDatabase = project.getProductDatabase();
386+
Product product = productDatabase.findBySku("48");
387+
Assert.assertNotNull(product);
388+
389+
Assert.assertEquals(product.getTransmissionCode(product.getScannableCodes()[0]), product.getScannableCodes()[0]);
390+
}
375391
}

core/src/main/java/io/snabble/sdk/Product.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import java.math.BigDecimal;
66
import java.math.RoundingMode;
7+
import java.util.HashMap;
8+
import java.util.Map;
79

810
/**
911
* Class that holds all of the product information.
@@ -98,6 +100,7 @@ public static SaleRestriction fromDatabaseField(long dbType, long value){
98100
private String subtitle;
99101
private String basePrice;
100102
private SaleRestriction saleRestriction = SaleRestriction.NONE;
103+
private Map<String, String> transmissionCodes;
101104
private boolean saleStop;
102105

103106
/**
@@ -120,6 +123,17 @@ public String[] getScannableCodes() {
120123
return scannableCodes;
121124
}
122125

126+
public String getTransmissionCode(String code) {
127+
if(transmissionCodes != null) {
128+
String newCode = transmissionCodes.get(code);
129+
if(newCode != null) {
130+
return newCode;
131+
}
132+
}
133+
134+
return code;
135+
}
136+
123137
public String[] getWeighedItemIds() {
124138
return weighedItemIds;
125139
}
@@ -332,6 +346,15 @@ public Builder setSaleStop(boolean saleStop){
332346
return this;
333347
}
334348

349+
public Builder addTransmissionCode(String fromCode, String transmissionCode){
350+
if(product.transmissionCodes == null) {
351+
product.transmissionCodes = new HashMap<>();
352+
}
353+
354+
product.transmissionCodes.put(fromCode, transmissionCode);
355+
return this;
356+
}
357+
335358
public Product build() {
336359
if (product.scannableCodes == null) {
337360
product.scannableCodes = new String[0];

core/src/main/java/io/snabble/sdk/ProductApi.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.io.IOException;
1414
import java.io.InputStream;
1515
import java.nio.charset.Charset;
16+
import java.util.Map;
1617
import java.util.concurrent.CountDownLatch;
1718

1819
import io.snabble.sdk.utils.Logger;
@@ -36,16 +37,21 @@ private static class ApiProduct {
3637
private boolean deleted;
3738
private String imageUrl;
3839
private String productType;
39-
@SerializedName(value = "eans", alternate = "scannableCodes")
4040
private String[] eans;
4141
private int price;
4242
private int discountedPrice;
4343
private String basePrice;
4444
private boolean saleStop;
45+
private ApiScannableCode[] codes;
4546
private Product.SaleRestriction saleRestriction = Product.SaleRestriction.NONE;
4647
private ApiWeighing weighing;
4748
}
4849

50+
private static class ApiScannableCode {
51+
private String code;
52+
private String transmissionCode;
53+
}
54+
4955
private static class ApiWeighing {
5056
private String[] weighedItemIds;
5157
private boolean weighByCustomer;
@@ -377,6 +383,12 @@ private Product toProduct(ApiProduct apiProduct, Product depositProduct, Product
377383
.setSaleRestriction(apiProduct.saleRestriction)
378384
.setSaleStop(apiProduct.saleStop);
379385

386+
if(apiProduct.codes != null) {
387+
for (ApiScannableCode apiScannableCode : apiProduct.codes) {
388+
builder.addTransmissionCode(apiScannableCode.code, apiScannableCode.transmissionCode);
389+
}
390+
}
391+
380392
if (apiProduct.weighing != null) {
381393
builder.setWeighedItemIds(apiProduct.weighing.weighedItemIds);
382394

core/src/main/java/io/snabble/sdk/ProductDatabase.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -714,8 +714,11 @@ public Product productAtCursor(Cursor cursor) {
714714
builder.setDepositProduct(findBySku(depositSku));
715715

716716
String codes = cursor.getString(7);
717+
String[] scannableCodes = null;
718+
717719
if (codes != null) {
718-
builder.setScannableCodes(codes.split(","));
720+
scannableCodes = codes.split(",");
721+
builder.setScannableCodes(scannableCodes);
719722
}
720723

721724
builder.setPrice(cursor.getInt(8))
@@ -739,6 +742,18 @@ public Product productAtCursor(Cursor cursor) {
739742
builder.setBundleProducts(findBundlesOfProduct(builder.build()));
740743
}
741744

745+
if(scannableCodes != null && schemaVersionMajor >= 1 && schemaVersionMinor >= 11) {
746+
String transmissionCodesStr = cursor.getString(16);
747+
if (transmissionCodesStr != null) {
748+
String[] transmissionCodes = transmissionCodesStr.split(",");
749+
for(int i=0; i<transmissionCodes.length; i++) {
750+
String tc = transmissionCodes[i];
751+
if(!tc.equals("")){
752+
builder.addTransmissionCode(scannableCodes[i], tc);
753+
}
754+
}
755+
}
756+
}
742757
return builder.build();
743758
}
744759

@@ -792,9 +807,12 @@ private Cursor productQuery(String appendSql, String[] args, boolean distinct, C
792807
sql += ",p.saleStop";
793808
}
794809

795-
sql += " FROM products p " +
796-
"JOIN prices pr ON pr.sku = p.sku " +
797-
appendSql;
810+
if(schemaVersionMajor >= 1 && schemaVersionMinor >= 11) {
811+
sql += ",(SELECT group_concat(ifnull(s.transmissionCode, \"\")) FROM scannableCodes s WHERE s.sku = p.sku)";
812+
}
813+
814+
sql += " FROM products p JOIN prices pr ON pr.sku = p.sku ";
815+
sql += appendSql;
798816

799817
return rawQuery(sql, args, cancellationSignal);
800818
}
@@ -819,9 +837,6 @@ private Cursor productQuery(String appendSql, String[] args, boolean distinct) {
819837
}
820838

821839
/**
822-
* This function is deprecated and will be removed from the SDK in the future. There will be no
823-
* alternative function to find products by name.
824-
*
825840
* Find a product by its name. Matching is normalized, so "Apple" finds also "apple".
826841
*
827842
* @param name The name of the product.

ui/src/main/java/io/snabble/sdk/ui/checkout/CheckoutEncodedCodesView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ private void addCodes() {
192192
}
193193
} else {
194194
for (int j = 0; j < shoppingCart.getQuantity(i); j++) {
195-
addScannableCode(shoppingCart.getScannedCode(i));
195+
addScannableCode(product.getTransmissionCode(shoppingCart.getScannedCode(i)));
196196
}
197197
}
198198
}

0 commit comments

Comments
 (0)