/* * $RCSFile$ * * Created on 11.06.2007 * for Project: * by steins * * (C) 2005-2006 by */ package beans; import java.io.IOException; import java.util.Map; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import javax.faces.context.FacesContext; import javax.persistence.EntityManager; import javax.persistence.EntityTransaction; import javax.servlet.http.HttpSession; import jpa.Category; import jpa.EntityManagerFactorySingleton; import jpa.Product; @ManagedBean(name = "ProductManager") @SessionScoped public class ProductManager { private Product current; public Product getCurrent() { return current; } public void select(javax.faces.event.ActionEvent actionEvent) { FacesContext context = FacesContext.getCurrentInstance(); Map params = context.getExternalContext().getRequestParameterMap(); int selectedId = Integer.valueOf((String) params.get("selectedId"), 10); // System.out.println(selectedId); EntityManager manager = EntityManagerFactorySingleton.getInstance().getEntityManagerFactory() .createEntityManager(); EntityTransaction tx = manager.getTransaction(); tx.begin(); try { current = manager.find(Product.class, selectedId); tx.commit(); } catch (Exception ex) { ex.printStackTrace(System.err); tx.rollback(); } try { context.getExternalContext().redirect("product.jsf"); } catch (IOException e) { e.printStackTrace(); } context.responseComplete(); manager.close(); } public void saveCurrentProduct() { FacesContext context = FacesContext.getCurrentInstance(); EntityManager manager = EntityManagerFactorySingleton.getInstance().getEntityManagerFactory() .createEntityManager(); EntityTransaction tx = manager.getTransaction(); HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false); CategoryManager cm = (CategoryManager) session.getAttribute("CategoryManager"); try { tx.begin(); current = manager.merge(current); cm.setCurrent(manager.find(Category.class, cm.getCurrent().getId())); Category cat = manager.find(Category.class, cm.getCurrent().getId()); cat.getProducts().add(current); manager.merge(cat); cm.setCurrent(manager.find(Category.class, cm.getCurrent().getId())); tx.commit(); } catch (Exception ex) { ex.printStackTrace(System.err); if (tx.isActive()) { tx.rollback(); } } try { context.getExternalContext().redirect("products.jsf"); } catch (IOException e) { e.printStackTrace(); } context.responseComplete(); manager.close(); } public void newProduct() { current = new Product(); FacesContext context = FacesContext.getCurrentInstance(); try { context.getExternalContext().redirect("product.jsf"); } catch (IOException e) { e.printStackTrace(); } context.responseComplete(); } public void deleteProduct(javax.faces.event.ActionEvent actionEvent) { FacesContext context = FacesContext.getCurrentInstance(); Map params = context.getExternalContext().getRequestParameterMap(); int selectedId = Integer.valueOf((String) params.get("selectedId"), 10); // System.out.println(selectedId); EntityManager manager = EntityManagerFactorySingleton.getInstance().getEntityManagerFactory() .createEntityManager(); HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false); CategoryManager cm = (CategoryManager) session.getAttribute("CategoryManager"); EntityTransaction tx = manager.getTransaction(); tx.begin(); try { current = manager.find(Product.class, selectedId); Category ca = manager.find(Category.class, cm.getCurrent().getId()); ca.getProducts().remove(current); manager.merge(ca); if (current.getCategories().isEmpty()) { manager.remove(current); current = null; } cm.setCurrent(manager.find(Category.class, cm.getCurrent().getId())); tx.commit(); } catch (Exception ex) { ex.printStackTrace(System.err); tx.rollback(); } try { context.getExternalContext().redirect("products.jsf"); } catch (IOException e) { e.printStackTrace(); } context.responseComplete(); manager.close(); } }