66import com .google .gson .Gson ;
77import com .google .gson .GsonBuilder ;
88import com .google .gson .JsonParseException ;
9- import com .google .gson .annotations .SerializedName ;
109
1110import org .apache .commons .io .IOUtils ;
11+ import org .apache .commons .lang3 .StringUtils ;
1212
1313import java .io .IOException ;
1414import java .io .InputStream ;
1515import java .nio .charset .Charset ;
16- import java .util .Map ;
1716import java .util .concurrent .CountDownLatch ;
1817
1918import io .snabble .sdk .utils .Logger ;
@@ -104,7 +103,7 @@ public void findBySku(String sku, final OnProductAvailableListener productAvaila
104103 get (url , productAvailableListener );
105104 }
106105
107- public void findByCode (String code , final OnProductAvailableListener productAvailableListener ) {
106+ public void findByCode (final String code , final OnProductAvailableListener productAvailableListener ) {
108107 if (productAvailableListener == null ) {
109108 return ;
110109 }
@@ -123,7 +122,28 @@ public void findByCode(String code, final OnProductAvailableListener productAvai
123122
124123 url = url .replace ("{code}" , code );
125124
126- get (url , productAvailableListener );
125+ // TODO remove when backend has implemented ean8->ean13 lookups
126+ get (url , new OnProductAvailableListener () {
127+ @ Override
128+ public void onProductAvailable (Product product , boolean wasOnlineProduct ) {
129+ success (productAvailableListener , product );
130+ }
131+
132+ @ Override
133+ public void onProductNotFound () {
134+ if (code .length () >= 8 && code .length () < 13 ) {
135+ String newCode = StringUtils .repeat ('0' , 13 - code .length ()) + code ;
136+ findByCode (newCode , productAvailableListener );
137+ } else {
138+ notFound (productAvailableListener );
139+ }
140+ }
141+
142+ @ Override
143+ public void onError () {
144+ error (productAvailableListener );
145+ }
146+ });
127147 }
128148
129149 public void findByWeighItemId (String weighItemId , final OnProductAvailableListener productAvailableListener ) {
0 commit comments