diff options
| author | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-12-13 22:07:46 +0100 |
|---|---|---|
| committer | Stefan Suhren <suhren.stefan@fh-swf.de> | 2015-12-13 23:35:23 +0100 |
| commit | 5f30a827fc84af1e54f0716a49995e98a044ebd6 (patch) | |
| tree | 27a68fbd5640c445f549e696af8141aeba6e9769 /src/beans/ProductManager.java | |
| parent | a14734c965fd7aaf6485bb2bb2a72a68bdb5d14d (diff) | |
| download | JCatalog-5f30a827fc84af1e54f0716a49995e98a044ebd6.tar.gz JCatalog-5f30a827fc84af1e54f0716a49995e98a044ebd6.zip | |
Add editing capabilities
Diffstat (limited to 'src/beans/ProductManager.java')
| -rw-r--r-- | src/beans/ProductManager.java | 162 |
1 files changed, 123 insertions, 39 deletions
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 class ProductManager { + private Product current; - public Product getCurrent() - { - return 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); + 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(); + EntityManager manager = EntityManagerFactorySingleton.getInstance().getEntityManagerFactory() + .createEntityManager(); - EntityTransaction tx = manager.getTransaction(); - tx.begin(); - try - { - current = manager.find(Product.class, selectedId); + EntityTransaction tx = manager.getTransaction(); + tx.begin(); + try { + current = manager.find(Product.class, selectedId); - tx.commit(); - } - catch (Exception ex) - { - ex.printStackTrace(System.err); - tx.rollback(); - } + 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(); - } + 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(); + } } |
