Skip to content

Commit

Permalink
fix: do not save null values in DB.
Browse files Browse the repository at this point in the history
Closes #3842
  • Loading branch information
VaiTon committed Mar 2, 2021
1 parent 97624bd commit 5b22cc2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
Expand Up @@ -3,6 +3,8 @@
import android.util.Base64;
import android.util.Log;

import androidx.annotation.NonNull;

import org.greenrobot.greendao.converter.PropertyConverter;

import java.io.ByteArrayInputStream;
Expand All @@ -13,42 +15,42 @@
import java.util.HashMap;
import java.util.Map;

import static android.util.Base64.DEFAULT;

class MapOfStringsToStringConverter implements PropertyConverter<Map<String, String>, String> {
private static final String LOG_TAG;

@Override
@NonNull
public Map<String, String> convertToEntityProperty(String databaseValue) {
if (databaseValue == null) {
return null;
return new HashMap<>();
}
try {
ByteArrayInputStream bis=new ByteArrayInputStream(Base64.decode(databaseValue,Base64.DEFAULT));
ObjectInputStream objectInputStream=new ObjectInputStream(bis);
ByteArrayInputStream bis = new ByteArrayInputStream(Base64.decode(databaseValue, DEFAULT));
ObjectInputStream objectInputStream = new ObjectInputStream(bis);
objectInputStream.readObject();

} catch (IOException e) {
Log.e(LOG_TAG, "Cannot serialize map to database.", e);
} catch (ClassNotFoundException e) {
} catch (IOException | ClassNotFoundException e) {
Log.e(LOG_TAG, "Cannot serialize map to database.", e);
}
return null;
return new HashMap<>();
}

@Override
public String convertToDatabaseValue(Map<String, String> entityProperty) {
if (entityProperty == null) {
return null;
}
@NonNull
public String convertToDatabaseValue(Map<String, String> entity) {
HashMap<String, String> map = entity != null ? new HashMap<>(entity) : new HashMap<>();

try {
ByteArrayOutputStream bos=new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream=new ObjectOutputStream(bos);
objectOutputStream.writeObject(new HashMap<>(entityProperty));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(bos);
objectOutputStream.writeObject(map);
objectOutputStream.flush();
return Base64.encodeToString(bos.toByteArray(),Base64.DEFAULT);
return Base64.encodeToString(bos.toByteArray(), DEFAULT);
} catch (IOException e) {
Log.e(LOG_TAG, "Cannot serialize map to database.", e);
}
return null;
return "";
}

static {
Expand Down
Expand Up @@ -23,13 +23,15 @@
@Entity
public class OfflineSavedProduct implements Serializable {
private static final long serialVersionUID = 1L;
@SuppressWarnings("NotNullFieldNotInitialized")
@Index(unique = true)
@NonNull
private String barcode;
@Id
private Long id;
@Index
private boolean isDataUploaded;
@SuppressWarnings("NotNullFieldNotInitialized")
@NonNull
@Convert(converter = MapOfStringsToStringConverter.class, columnType = String.class)
private Map<String, String> productDetails;
Expand Down Expand Up @@ -70,16 +72,20 @@ public String getLanguage() {

@Nullable
public String getName() {
final Map<String, String> map = productDetails;
final String language = firstNotEmpty(map.get(ApiFields.Keys.LANG), "en");
return firstNotEmpty(map.get(ApiFields.Keys.lcProductNameKey(language)), map.get(ApiFields.Keys.lcProductNameKey("en")));
final String language = firstNotEmpty(productDetails.get(ApiFields.Keys.LANG), "en");
return firstNotEmpty(
productDetails.get(ApiFields.Keys.lcProductNameKey(language)),
productDetails.get(ApiFields.Keys.lcProductNameKey("en"))
);
}

@Nullable
public String getIngredients() {
final Map<String, String> map = productDetails;
final String language = firstNotEmpty(map.get(ApiFields.Keys.LANG), "en");
return firstNotEmpty(map.get(ApiFields.Keys.lcIngredientsKey(language)), map.get(ApiFields.Keys.lcIngredientsKey("en")));
final String language = firstNotEmpty(productDetails.get(ApiFields.Keys.LANG), "en");
return firstNotEmpty(
productDetails.get(ApiFields.Keys.lcIngredientsKey(language)),
productDetails.get(ApiFields.Keys.lcIngredientsKey("en"))
);
}

@Nullable
Expand Down

0 comments on commit 5b22cc2

Please sign in to comment.