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 | |
| parent | a14734c965fd7aaf6485bb2bb2a72a68bdb5d14d (diff) | |
| download | JCatalog-5f30a827fc84af1e54f0716a49995e98a044ebd6.tar.gz JCatalog-5f30a827fc84af1e54f0716a49995e98a044ebd6.zip | |
Add editing capabilities
| -rw-r--r-- | WebContent/pages/categories.xhtml | 22 | ||||
| -rw-r--r-- | WebContent/pages/category.xhtml | 20 | ||||
| -rw-r--r-- | WebContent/pages/product.xhtml | 7 | ||||
| -rw-r--r-- | WebContent/pages/products.xhtml | 16 | ||||
| -rw-r--r-- | src/beans/CategoryManager.java | 187 | ||||
| -rw-r--r-- | src/beans/ProductManager.java | 162 |
6 files changed, 307 insertions, 107 deletions
diff --git a/WebContent/pages/categories.xhtml b/WebContent/pages/categories.xhtml index cb6ef27..7c3ecac 100644 --- a/WebContent/pages/categories.xhtml +++ b/WebContent/pages/categories.xhtml @@ -43,17 +43,19 @@ <f:param name="selectedId" value="#{category.id}" /> </h:commandLink> </h:column> - <c:if test="#{UserManager.loggedIn}"> - <h:column> - <f:facet name="header"> - <h:outputText value="" /> - </f:facet> - <h:commandLink value="Edit Details ..." immediate="true" action="category"> - <f:param name="selectedId" value="#{category.id}" /> - </h:commandLink> - </h:column> - </c:if> + <h:column rendered="#{UserManager.loggedIn}"> + <f:facet name="header"> + <h:outputText value="" /> + </f:facet> + <h:commandLink value="Edit" immediate="true" + actionListener="#{CategoryManager.edit}" action="edit"> + <f:param name="selectedId" value="#{category.id}" /> + </h:commandLink> + </h:column> </h:dataTable> + <h:commandLink rendered="#{UserManager.loggedIn}" disabled="#{not UserManager.loggedIn}" value="New Category" immediate="true" + actionListener="#{CategoryManager.createCategory}" action="createCategory"> + </h:commandLink> </h:form> </ui:define> diff --git a/WebContent/pages/category.xhtml b/WebContent/pages/category.xhtml index 4a5ea10..7d6f43a 100644 --- a/WebContent/pages/category.xhtml +++ b/WebContent/pages/category.xhtml @@ -13,7 +13,25 @@ <ui:define name="pageHeader">JCatalog Application</ui:define> <ui:define name="body"> - <h2>Welcome to the JCatalog Demo Application <h:outputText id="category" value="#{CategoryManager.current.name}" /></h2> + <div style="width: 500px; padding: 5px; margin: 0px auto; float: left;"> + <h:form> + <div class="row"> + <label for="name">Name:</label> + <span class="formw"><h:inputText id="name" disabled="#{not UserManager.loggedIn}" value="#{CategoryManager.current.name}" accesskey="n" size="40" /></span> + </div> + <div class="row"> + <label for="description">Description:</label> + <span class="formw"><h:inputTextarea rows="20" cols="40" id="description" disabled="#{not UserManager.loggedIn}" value="#{CategoryManager.current.description}" accesskey="d" /></span> + </div> + <div class="row"> + <span class="formw"> + <h:commandButton action="categories" id="back" value="Back" type="submit" accesskey="b" /> + <h:commandButton action="#{CategoryManager.saveCategory}" rendered="#{UserManager.loggedIn}" disabled="#{not UserManager.loggedIn}" id="save" value="Save" type="submit" accesskey="s" /> + </span> + </div> + <div class="spacer" /> + </h:form> + </div> </ui:define> <ui:define name="pageFooter">JCatalog Footer</ui:define> diff --git a/WebContent/pages/product.xhtml b/WebContent/pages/product.xhtml index 84b49f6..d5f3f41 100644 --- a/WebContent/pages/product.xhtml +++ b/WebContent/pages/product.xhtml @@ -17,19 +17,20 @@ <h:form> <div class="row"> <label for="name">Name:</label> - <span class="formw"><h:inputText id="name" disabled="true" value="#{ProductManager.current.name}" accesskey="n" size="40" /></span> + <span class="formw"><h:inputText id="name" disabled="#{not UserManager.loggedIn}" value="#{ProductManager.current.name}" accesskey="n" size="40" /></span> </div> <div class="row"> <label for="price">Price:</label> - <span class="formw"><h:inputText id="price" disabled="true" value="#{ProductManager.current.price}" accesskey="p" size="40" /></span> + <span class="formw"><h:inputText id="price" disabled="#{not UserManager.loggedIn}" value="#{ProductManager.current.price}" accesskey="p" size="40" /></span> </div> <div class="row"> <label for="description">Description:</label> - <span class="formw"><h:inputTextarea rows="20" cols="40" id="description" disabled="true" value="#{ProductManager.current.description}" accesskey="d" /></span> + <span class="formw"><h:inputTextarea rows="20" cols="40" id="description" disabled="#{not UserManager.loggedIn}" value="#{ProductManager.current.description}" accesskey="d" /></span> </div> <div class="row"> <span class="formw"> <h:commandButton action="products" id="back" value="Back" type="submit" accesskey="b" /> + <h:commandButton action="#{ProductManager.saveCurrentProduct}" rendered="#{UserManager.loggedIn}" disabled="#{not UserManager.loggedIn}" id="save" value="Save" type="submit" accesskey="s" /> </span> </div> <div class="spacer" /> diff --git a/WebContent/pages/products.xhtml b/WebContent/pages/products.xhtml index 1b5e5e4..8050c30 100644 --- a/WebContent/pages/products.xhtml +++ b/WebContent/pages/products.xhtml @@ -50,8 +50,22 @@ <f:param name="selectedId" value="#{product.id}" /> </h:commandLink> </h:column> + <h:column rendered="#{UserManager.loggedIn}"> + <f:facet name="header"> + <h:outputText value="" /> + </f:facet> + <h:commandLink rendered="#{UserManager.loggedIn}" disabled="#{not UserManager.loggedIn}" + value="remove" immediate="true" actionListener="#{ProductManager.deleteProduct}" action="deleteProduct"> + <f:param name="selectedId" value="#{product.id}" /> + </h:commandLink> + </h:column> </h:dataTable> - + <h:commandLink rendered="#{UserManager.loggedIn}" value="New Product" immediate="true" + actionListener="#{ProductManager.newProduct}" action="newProduct"> + </h:commandLink> + <h:commandLink rendered="#{UserManager.loggedIn}" disabled="#{!UserManager.loggedIn}" value="Delete Category" immediate="true" + actionListener="#{CategoryManager.deleteCurrentCategory}" action="deleteCurrentCategory"> + </h:commandLink> </h:form> </ui:define> 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(); + } } |
