summaryrefslogtreecommitdiffstats
path: root/src/beans
diff options
context:
space:
mode:
authorStefan Suhren <suhren.stefan@fh-swf.de>2015-12-13 22:07:46 +0100
committerStefan Suhren <suhren.stefan@fh-swf.de>2015-12-13 23:35:23 +0100
commit5f30a827fc84af1e54f0716a49995e98a044ebd6 (patch)
tree27a68fbd5640c445f549e696af8141aeba6e9769 /src/beans
parenta14734c965fd7aaf6485bb2bb2a72a68bdb5d14d (diff)
downloadJCatalog-5f30a827fc84af1e54f0716a49995e98a044ebd6.tar.gz
JCatalog-5f30a827fc84af1e54f0716a49995e98a044ebd6.zip
Add editing capabilities
Diffstat (limited to 'src/beans')
-rw-r--r--src/beans/CategoryManager.java187
-rw-r--r--src/beans/ProductManager.java162
2 files changed, 257 insertions, 92 deletions
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 class CategoryManager {
+ private Category current;
- public Category getCurrent()
- {
- return current;
- }
+ public Category getCurrent() {
+ return current;
+ }
- public void setCurrent(Category current)
- {
- this.current = current;
- }
+ public void setCurrent(Category current) {
+ this.current = current;
+ }
- public Collection getCategories()
- {
- EntityManager manager = EntityManagerFactorySingleton.getInstance()
- .getEntityManagerFactory().createEntityManager();
+ public Collection getCategories() {
+ EntityManager manager = EntityManagerFactorySingleton.getInstance().getEntityManagerFactory()
+ .createEntityManager();
- Collection categories = manager.createQuery("SELECT c FROM Category c ")
- .getResultList();
- manager.close();
+ Collection categories = manager.createQuery("SELECT c FROM Category c ").getResultList();
+ manager.close();
- return categories;
- }
+ 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);
+ 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();
+ 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();
- }
+ 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();
- }
+ 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<String, String> 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 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();
+ }
}