From 5f30a827fc84af1e54f0716a49995e98a044ebd6 Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Sun, 13 Dec 2015 22:07:46 +0100 Subject: Add editing capabilities --- src/beans/CategoryManager.java | 205 ++++++++++++++++++++++++++++------------- src/beans/ProductManager.java | 174 +++++++++++++++++++++++++--------- 2 files changed, 272 insertions(+), 107 deletions(-) (limited to 'src') diff --git a/src/beans/CategoryManager.java b/src/beans/CategoryManager.java index 005b399..cfa8bba 100644 --- a/src/beans/CategoryManager.java +++ b/src/beans/CategoryManager.java @@ -24,66 +24,147 @@ import jpa.EntityManagerFactorySingleton; @ManagedBean(name = "CategoryManager") @SessionScoped -public class CategoryManager -{ - private Category current; - - public Category getCurrent() - { - return current; - } - - public void setCurrent(Category current) - { - this.current = current; - } - - public Collection getCategories() - { - EntityManager manager = EntityManagerFactorySingleton.getInstance() - .getEntityManagerFactory().createEntityManager(); - - Collection categories = manager.createQuery("SELECT c FROM Category c ") - .getResultList(); - manager.close(); - - return categories; - } - - public void select(javax.faces.event.ActionEvent actionEvent) - { - FacesContext facesContext = FacesContext.getCurrentInstance(); - Map params = facesContext.getExternalContext().getRequestParameterMap(); - Integer selectedId = Integer.valueOf((String) params.get("selectedId")); - // System.out.println(selectedId); - - EntityManager manager = EntityManagerFactorySingleton.getInstance() - .getEntityManagerFactory().createEntityManager(); - - EntityTransaction tx = manager.getTransaction(); - tx.begin(); - try - { - current = manager.find(Category.class, selectedId); - // System.out.println(current.getName()); - tx.commit(); - } - catch (Exception ex) - { - ex.printStackTrace(System.err); - tx.rollback(); - } - - FacesContext context = FacesContext.getCurrentInstance(); - try - { - context.getExternalContext().redirect("products.jsf"); - } - catch (IOException e) - { - e.printStackTrace(); - } - context.responseComplete(); - manager.close(); - } +public class CategoryManager { + private Category current; + + public Category getCurrent() { + return current; + } + + public void setCurrent(Category current) { + this.current = current; + } + + public Collection getCategories() { + EntityManager manager = EntityManagerFactorySingleton.getInstance().getEntityManagerFactory() + .createEntityManager(); + + Collection categories = manager.createQuery("SELECT c FROM Category c ").getResultList(); + manager.close(); + + return categories; + } + + public void select(javax.faces.event.ActionEvent actionEvent) { + FacesContext facesContext = FacesContext.getCurrentInstance(); + Map params = facesContext.getExternalContext().getRequestParameterMap(); + Integer selectedId = Integer.valueOf((String) params.get("selectedId")); + // System.out.println(selectedId); + + EntityManager manager = EntityManagerFactorySingleton.getInstance().getEntityManagerFactory() + .createEntityManager(); + + EntityTransaction tx = manager.getTransaction(); + tx.begin(); + try { + current = manager.find(Category.class, selectedId); + // System.out.println(current.getName()); + tx.commit(); + } catch (Exception ex) { + ex.printStackTrace(System.err); + tx.rollback(); + } + + FacesContext context = FacesContext.getCurrentInstance(); + try { + context.getExternalContext().redirect("products.jsf"); + } catch (IOException e) { + e.printStackTrace(); + } + context.responseComplete(); + manager.close(); + } + + public void edit(javax.faces.event.ActionEvent actionEvent) { + FacesContext context = FacesContext.getCurrentInstance(); + Map params = context.getExternalContext().getRequestParameterMap(); + Integer selectedId = Integer.valueOf((String) params.get("selectedId")); + // System.out.println(selectedId); + + EntityManager manager = EntityManagerFactorySingleton.getInstance().getEntityManagerFactory() + .createEntityManager(); + + EntityTransaction tx = manager.getTransaction(); + tx.begin(); + try { + current = manager.find(Category.class, selectedId); + // System.out.println(current.getName()); + tx.commit(); + } catch (Exception ex) { + ex.printStackTrace(System.err); + tx.rollback(); + } + + try { + context.getExternalContext().redirect("category.jsf"); + } catch (IOException e) { + e.printStackTrace(); + } + context.responseComplete(); + } + + public void deleteCurrentCategory(javax.faces.event.ActionEvent actionEvent) { + FacesContext context = FacesContext.getCurrentInstance(); + + EntityManager manager = EntityManagerFactorySingleton.getInstance().getEntityManagerFactory() + .createEntityManager(); + EntityTransaction tx = manager.getTransaction(); + tx.begin(); + try { + current = manager.find(Category.class, current.getId()); + manager.remove(current); + current = null; + + tx.commit(); + } catch (Exception ex) { + ex.printStackTrace(System.err); + if (tx.isActive()) + tx.rollback(); + } + + try { + context.getExternalContext().redirect("categories.jsf"); + } catch (IOException e) { + e.printStackTrace(); + } + context.responseComplete(); + } + + public void createCategory() { + current = new Category(); + + FacesContext context = FacesContext.getCurrentInstance(); + + try { + context.getExternalContext().redirect("category.jsf"); + } catch (IOException e) { + e.printStackTrace(); + } + context.responseComplete(); + } + + public void saveCategory() { + EntityManager manager = EntityManagerFactorySingleton.getInstance().getEntityManagerFactory() + .createEntityManager(); + + EntityTransaction tx = manager.getTransaction(); + try { + tx.begin(); + current = manager.merge(current); + + tx.commit(); + } catch (Exception ex) { + ex.printStackTrace(System.err); + tx.rollback(); + } + + FacesContext context = FacesContext.getCurrentInstance(); + + try { + context.getExternalContext().redirect("categories.jsf"); + } catch (IOException e) { + e.printStackTrace(); + } + context.responseComplete(); + } } diff --git a/src/beans/ProductManager.java b/src/beans/ProductManager.java index 4910fff..b9e5bfb 100644 --- a/src/beans/ProductManager.java +++ b/src/beans/ProductManager.java @@ -17,56 +17,140 @@ 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 facesContext = FacesContext.getCurrentInstance(); - Map params = facesContext.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(); - } - - FacesContext context = FacesContext.getCurrentInstance(); - try - { - context.getExternalContext().redirect("product.jsf"); - } - catch (IOException e) - { - e.printStackTrace(); - } - context.responseComplete(); - manager.close(); - } +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(); + } } -- cgit v1.2.3-70-g09d2