package com.din.din.webapp.beans.interfaces;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import com.din.din.model.dao.RecipeDAO;
import com.din.din.model.entities.KeyEntity;
import com.din.din.model.entities.Project;
import com.din.din.model.entities.Recipe;
import com.din.din.model.entities.RecipeItem;
import com.din.din.model.util.EntityCachingManager;
import com.din.din.model.util.EntityCachingManager.CacheLoader;
import com.din.din.model.webapp.CategorizedManager;
import com.din.din.webapp.beans.util.BeanUtil;
import com.din.din.webapp.listeners.EMFListener;
public class RecipeManager extends CategorizedManager<Recipe> {
private RecipeWizard wizard = null;
public RecipeManager(Project project, EntityCachingManager cache) {
super(project, cache);
}
protected Map<Object, List<KeyEntity>> loadCategorizedItems() {
Map<Object, List<KeyEntity>> recipes = null;
try {
recipes = getCache().getMapped(Recipe.class, "category", "project", getProject(), new CacheLoader<Recipe>() {
public List<Recipe> onCacheLoad() {
return RecipeDAO.getRecipesByProject(getProject());
}
});
} catch (Exception e) {
BeanUtil.addMessageFmt(FacesMessage.SEVERITY_ERROR, "error.recipe.load", "error.recipe.load.description");
e.printStackTrace();
}
return recipes;
}
@Override
public void setSelected(Recipe selected) {
super.setSelected(selected);
final Recipe selectedSet = getSelected();
if(selectedSet != null) {
selectedSet.setProject(getProject());
if(selectedSet.getRecipeItems() == null || selectedSet.getRecipeItems().size() == 0) {
try {
RecipeDAO.loadRecipeItems(selectedSet, getCache());
} catch (Exception e) {
Logger.getLogger(RecipeManager.class.getName()).log(Level.WARNING, "Unable to load RecipeItem list", e);
e.printStackTrace();
}
}
}
}
public synchronized String removeRecipe() {
boolean success = true;
Recipe selected = getSelected();
if(selected != null) {
EntityManager entityManager = EMFListener.get();
EntityTransaction transaction = entityManager.getTransaction();
EntityCachingManager cache = getCache();
try {
transaction.begin();
for(RecipeItem item : selected.getRecipeItems()) {
//RecipeItemDAO.delete(entityManager, item, cache);
selected = entityManager.merge(selected);
item.setRecipe(selected);
}
RecipeDAO.delete(entityManager, selected, cache);
transaction.commit();
cache.commit();
setSelected(null);
refresh();
} catch (Exception e) {
success = false;
Logger.getLogger(RecipeManager.class.getSimpleName()).log(Level.WARNING, "Error deleting recipe!", e);
BeanUtil.addMessage(FacesMessage.SEVERITY_ERROR, "delete.error", e.getMessage());
} finally {
if(transaction.isActive()) {
transaction.rollback();
cache.rollback();
}
entityManager.close();
}
}
if(success) {
selected = null;
BeanUtil.addMessageFmt(FacesMessage.SEVERITY_INFO, "delete.success", "delete.success.message", BeanUtil.getMessage(Recipe.class.getSimpleName()));
}
return null;
}
@Override
public synchronized String editStart() {
String returnStr = super.editStart();
Recipe selected = getSelected();
if(selected == null) {
Recipe recipe = new Recipe();
selected = recipe;
recipe.setProject(getProject());
recipe.setCookingTime(BigDecimal.ZERO);
}
getWizard().setEntity(selected);
return returnStr;
}
@Override
public synchronized String editFinish() {
String returnStr = super.editFinish();
// Update our recipe list to reflect changes
if(wizard != null && wizard.isFinished()) {
refresh();
}
wizard = null;
return returnStr;
}
public RecipeWizard getWizard() {
if(wizard == null) {
wizard = new RecipeWizard(getProject(), getCache());
}
return wizard;
}
}