package com.mousetech.gourmetj; import java.io.Serializable; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import javax.annotation.PostConstruct; import javax.faces.event.AjaxBehaviorEvent; import javax.faces.view.ViewScoped; import javax.inject.Inject; import javax.inject.Named; import org.apache.commons.lang3.StringUtils; import org.primefaces.event.ReorderEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.mousetech.gourmetj.persistence.dao.ShopcatRepository; import com.mousetech.gourmetj.persistence.model.Ingredient; import com.mousetech.gourmetj.persistence.model.Recipe; import com.mousetech.gourmetj.persistence.model.Shopcat; @Named @ViewScoped public class ShoppingListBean implements Serializable { /** * Serial version for session save/restore */ private static final long serialVersionUID = 7449440266704831598L; /* Logger */ @SuppressWarnings("unused") private static final Logger log = LoggerFactory.getLogger(ShoppingListBean.class); @Inject private UserSession userSession; private List siList; // private List ingredientList; @PostConstruct public void init() { // Load up details on recipes this.siList = new ArrayList(30); buildMaps(); } public List getRecipeList() { return this.userSession.getShoppingList(); } public List getIngredientList() { return this.siList; } private void buildMaps() { for (Recipe r : this.getRecipeList()) { buildMapsFor(r); } // Now consolidate amounts and sort by // shopcat/item/ingkey optimizeIngredients(this.siList); this.siList.sort(new ShopclassComparator()); } /** * Run the ingredient list for the selected recipe and add * them to siList * * @param r * * @see #buildMaps() */ private void buildMapsFor(Recipe r) { for (Ingredient ing : r.getIngredientHash()) { String ingkey = ing.getIngkey(); if (StringUtils.isBlank(ingkey)) { continue; } String shopCatName = ing.getShopCat() != null ? ing.getShopCat().getShopcategory() : null; ShopIngredient sing = new ShopIngredient( ing.getAmount(), ing.getUnit(), ing.getItem(), ing.getIngkey(), shopCatName); siList.add(sing); } } /** * Sort ShopIngredient list, then optimize it by * consolidating amounts where possible. * * @param victim List to optimize * * #TestedBy @see ShoppingListBeanTest */ static void optimizeIngredients( List victim) { victim.sort(null); for (int i = 0; i < (victim.size() - 1); i++) { ShopIngredient si = victim.get(i); ShopIngredient si2 = victim.get(i + 1); if (si.compareTo(si2) == 0) { si.setAmount(si.getAmount() + si2.getAmount()); victim.remove(si2); // reduces size() } } } class ShopclassComparator implements Comparator { @Override public int compare(ShopIngredient ing1, ShopIngredient ing2) { int i = 0; i = relate(ing1.getShopCat(), ing2.getShopCat()); if (i != 0) { return i; } i = relate(ing1.getItem(), ing2.getItem()); if (i != 0) { return i; } i = relate(ing1.getIngkey(), ing2.getIngkey()); if (i != 0) { return i; } return 0; } private int relate(String item2, String item3) { if ((item2 == null) && (item3 == null)) { return 0; } if (item2 == null) { return -1; } if (item3 == null) { return 1; } return item2.compareTo(item3); } } private List shopcatList; public List getShopcatList() { if (shopcatList == null) { shopcatList = loadShopcatList(); } return shopcatList; } @Inject ShopcatRepository shopcatRepository; private List loadShopcatList() { return shopcatRepository .findAllByOrderByShopcategoryAsc(); } private Shopcat xeditShopcat = new Shopcat(); private String oldShopcategoryName; /** * @return the editShopcat */ public Shopcat getEditShopcat() { return xeditShopcat; } public void doEditShopcat(int scId) { xeditShopcat = null; final List scl = getShopcatList(); for (Shopcat sc : scl) { if (sc.getId() == scId) { xeditShopcat = sc; this.oldShopcategoryName = sc.getShopcategory(); if (sc.getPosition() == null) { sc.setPosition(0); } return; } } log.error("SHOPCAT " + scId + " NOT FOUND"); } public void ajaxOnClickShopcatIngkey() { // Saves 1 shopcat/ingkey this.shopcatRepository.save(xeditShopcat); this.shopcatList = null; } /** * Updates all ingredient keys for shopcat name-change. * Note that once done, this cannot be undone! */ public void ajaxOnClickShopcat() { this.shopcatRepository.UpdateShopcats( this.oldShopcategoryName, xeditShopcat.getShopcategory()); this.shopcatList = null; } }