Skip to content

Commit

Permalink
#115: fixed a bug with not working ignore case on non-ascii symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
bugy committed Feb 2, 2018
1 parent aeb8abe commit 643dc4d
Showing 1 changed file with 56 additions and 37 deletions.
93 changes: 56 additions & 37 deletions SheLi/src/main/java/net/buggy/shoplist/data/SqlliteDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.activeandroid.query.Delete;
import com.activeandroid.query.From;
import com.activeandroid.query.Select;
import com.google.common.base.CharMatcher;
import com.google.common.base.Joiner;
import com.google.common.base.Objects;
import com.google.common.collect.ImmutableList;
Expand All @@ -28,6 +29,7 @@
import net.buggy.shoplist.model.ShopItem;
import net.buggy.shoplist.model.UnitOfMeasure;
import net.buggy.shoplist.utils.CollectionUtils;
import net.buggy.shoplist.utils.StringUtils;

import java.io.Serializable;
import java.math.BigDecimal;
Expand Down Expand Up @@ -151,11 +153,9 @@ public void addShopItem(ShopItem shopItem) {

@Override
public void addProduct(Product product) {
final List<Model> existingProducts = new Select()
.from(StoredProduct.class)
.where("name = ? COLLATE NOCASE", product.getName())
.execute();
if (!existingProducts.isEmpty()) {
final StoredProduct existingProduct = findEntityByName(
product.getName(), StoredProduct.class);
if (existingProduct != null) {
throw new IllegalStateException(
"Product with the name " + product.getName() + " already exists");
}
Expand Down Expand Up @@ -241,11 +241,9 @@ public void saveShopItem(ShopItem shopItem) {

@Override
public void addCategory(Category category) {
final List<Model> existingCategories = new Select()
.from(StoredCategory.class)
.where("name = ? COLLATE NOCASE", category.getName())
.execute();
if (!existingCategories.isEmpty()) {
final StoredCategory existingCategory = findEntityByName(
category.getName(), StoredCategory.class);
if (existingCategory != null) {
throw new IllegalStateException("Category with name " + category.getName() + " already exists");
}

Expand Down Expand Up @@ -483,23 +481,9 @@ private StoredSynchronizationRecord findSynchronizationRecord(
@Nullable
@Override
public Product findProductByName(String name) {
final List<StoredProduct> products = new Select()
.from(StoredProduct.class)
.where("name = ? COLLATE NOCASE", (name != null)
? name.trim().toLowerCase()
: null)
.execute();


if (products.size() == 0) {
return null;
}

if (products.size() > 1) {
Log.e("SqlliteDao", "findProductByName: more than 1 product found, name=" + name);
}
final StoredProduct storedProduct = findEntityByName(name, StoredProduct.class);

return products.get(0).toProduct();
return storedProduct != null ? storedProduct.toProduct() : null;
}

@Nullable
Expand Down Expand Up @@ -560,16 +544,36 @@ public ShopItem findShopItemByProductName(String productName) {
@Nullable
@Override
public Category findCategoryByName(String name) {
final StoredCategory category = new Select()
.from(StoredCategory.class)
.where("name = ? COLLATE NOCASE", name)
.executeSingle();
final StoredCategory storedCategory = findEntityByName(name, StoredCategory.class);

if (category == null) {
return null;
return storedCategory != null ? storedCategory.toModel() : null;
}

@Nullable
private <T extends Model & Named> T findEntityByName(String name, Class<T> clazz) {
boolean canSearchIgnoreCase = canSqlSearchIgnoreCase(name);

if (canSearchIgnoreCase) {
return new Select()
.from(clazz)
.where("name = ? COLLATE NOCASE", name)
.executeSingle();
}

return category.toModel();
final List<T> existingCategories = new Select()
.from(clazz)
.execute();
for (T entity : existingCategories) {
if (StringUtils.equalIgnoreCase(entity.getName(), name)) {
return entity;
}
}

return null;
}

private boolean canSqlSearchIgnoreCase(String value) {
return (value == null) || CharMatcher.ascii().matchesAllOf(value);
}

@Override
Expand Down Expand Up @@ -696,7 +700,7 @@ private <T extends Entity> void notifyEntityRemoved(T entity) {
}

@Table(name = "Categories")
public static class StoredCategory extends Model {
public static class StoredCategory extends Model implements Named {
@Column(name = "Name")
private String name;

Expand Down Expand Up @@ -726,6 +730,11 @@ public Category toModel() {
category.setColor(color);
return category;
}

@Override
public String getName() {
return name;
}
}

@Table(name = "ProductCategoryLinks")
Expand Down Expand Up @@ -758,7 +767,7 @@ public StoredCategory getCategory() {
}

@Table(name = "Products")
public static class StoredProduct extends Model {
public static class StoredProduct extends Model implements Named {
@Column(name = "Name")
private String name;

Expand Down Expand Up @@ -844,8 +853,8 @@ public Product toProduct(@Nullable Map<Long, Category> categoryMap) {
category = categoryMap.get(storedCategory.getId());
} else {
category = storedCategory.toModel();
}
}

productCategories.add(category);
}

Expand Down Expand Up @@ -878,6 +887,12 @@ public Set<StoredCategory> getCategories() {

return result;
}


@Override
public String getName() {
return name;
}
}

@NonNull
Expand All @@ -893,6 +908,10 @@ private static List<StoredCategory> findCategories(Collection<Long> ids) {
return storedCategories;
}

private interface Named {
String getName();
}

@Table(name = "ShopItems")
public static class StoredShopItem extends Model {
@Column(name = "Product")
Expand Down

0 comments on commit 643dc4d

Please sign in to comment.