From 25ae0a23d15383df7f84ad51ee8f078c519ed963 Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Sun, 22 Nov 2015 17:02:23 +0100 Subject: Get it to run on newest versions --- src/META-INF/persistence.xml | 13 ++- src/beans/CategoryManager.java | 8 +- src/beans/ContactManager.java | 5 ++ src/beans/ProductManager.java | 8 +- src/beans/UserManager.java | 176 +++++++++++++++++++++++------------------ src/jpa/Category.java | 153 +++++++++++++---------------------- src/jpa/Product.java | 94 ++++++++++------------ src/jpa/User.java | 17 ++-- 8 files changed, 231 insertions(+), 243 deletions(-) (limited to 'src') diff --git a/src/META-INF/persistence.xml b/src/META-INF/persistence.xml index 039a45e..fef05c4 100644 --- a/src/META-INF/persistence.xml +++ b/src/META-INF/persistence.xml @@ -1,15 +1,12 @@ -jpa.User -jpa.Category -jpa.Product +org.eclipse.persistence.jpa.PersistenceProvider +false - - - - - + + + diff --git a/src/beans/CategoryManager.java b/src/beans/CategoryManager.java index 7b96d2f..fc3d438 100644 --- a/src/beans/CategoryManager.java +++ b/src/beans/CategoryManager.java @@ -13,6 +13,8 @@ import java.io.IOException; import java.util.Collection; 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.EntityManagerFactory; @@ -21,6 +23,8 @@ import javax.persistence.Persistence; import jpa.Category; +@ManagedBean(name = "CategoryManager") +@SessionScoped public class CategoryManager { private Category current; @@ -48,7 +52,7 @@ public class CategoryManager FacesContext facesContext = FacesContext.getCurrentInstance(); Map params = facesContext.getExternalContext().getRequestParameterMap(); Integer selectedId = Integer.valueOf((String) params.get("selectedId")); -// System.out.println(selectedId); + // System.out.println(selectedId); EntityManagerFactory factory = Persistence .createEntityManagerFactory("catalog"); @@ -59,7 +63,7 @@ public class CategoryManager try { current = manager.find(Category.class, selectedId); -// System.out.println(current.getName()); + // System.out.println(current.getName()); tx.commit(); } catch (Exception ex) diff --git a/src/beans/ContactManager.java b/src/beans/ContactManager.java index 43c96e0..4625848 100644 --- a/src/beans/ContactManager.java +++ b/src/beans/ContactManager.java @@ -9,6 +9,11 @@ */ package beans; +import javax.faces.bean.ManagedBean; +import javax.faces.bean.SessionScoped; + +@ManagedBean(name = "ContactManager") +@SessionScoped public class ContactManager { // nothing implemented yet diff --git a/src/beans/ProductManager.java b/src/beans/ProductManager.java index cc0dcaa..d499e3c 100644 --- a/src/beans/ProductManager.java +++ b/src/beans/ProductManager.java @@ -12,6 +12,8 @@ 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.EntityManagerFactory; @@ -20,10 +22,12 @@ import javax.persistence.Persistence; import jpa.Product; +@ManagedBean(name = "ProductManager") +@SessionScoped public class ProductManager { private Product current; - + public Product getCurrent() { return current; @@ -34,7 +38,7 @@ public class ProductManager FacesContext facesContext = FacesContext.getCurrentInstance(); Map params = facesContext.getExternalContext().getRequestParameterMap(); String selectedId = (String) params.get("selectedId"); -// System.out.println(selectedId); + // System.out.println(selectedId); EntityManagerFactory factory = Persistence .createEntityManagerFactory("catalog"); diff --git a/src/beans/UserManager.java b/src/beans/UserManager.java index 9e08543..190926b 100644 --- a/src/beans/UserManager.java +++ b/src/beans/UserManager.java @@ -9,8 +9,12 @@ */ package beans; +import static util.DigestUtils.md5; + import java.util.List; +import javax.faces.bean.ManagedBean; +import javax.faces.bean.SessionScoped; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; @@ -18,83 +22,99 @@ import javax.persistence.Persistence; import javax.persistence.Query; import jpa.User; -import static util.DigestUtils.md5; -public class UserManager { - private User current; - - private boolean loggedIn; - - public UserManager() { - current = new User(); - } - - public String login() { - String outcome = "failure"; - if (current.getUsername() != null && current.getUsername().length() > 0 - && current.getPassword() != null - && current.getPassword().length() > 0) { - EntityManagerFactory factory = Persistence - .createEntityManagerFactory("catalog"); - EntityManager manager = factory.createEntityManager(); - Query query = manager - .createQuery("SELECT u FROM User u where u.username = :username and u.password = :password"); - query.setParameter("username", current.getUsername()); - query.setParameter("password", md5(current.getPassword())); - List results = query.getResultList(); - - if (!results.isEmpty()) { - loggedIn = true; - current = (User) results.get(0); - outcome = "success"; - } - } - // System.out.println(outcome); - return outcome; - } - - public String logout() { - loggedIn = false; - current = new User(); - return "home"; - } - - public void setUsername(String username) { - current.setUsername(username); - } - - public String getUsername() { - return current.getUsername(); - } - - public void setPassword(String password) { - current.setPassword(password); - } - - public String getPassword() { - return current.getPassword(); - } - - public boolean isLoggedIn() { - return loggedIn; - } - - public User getCurrent() { - EntityManagerFactory factory = Persistence - .createEntityManagerFactory("catalog"); - EntityManager manager = factory.createEntityManager(); - - EntityTransaction tx = manager.getTransaction(); - tx.begin(); - try { - current = manager.find(User.class, getUsername()); - - tx.commit(); - } catch (Exception ex) { - ex.printStackTrace(System.err); - tx.rollback(); - } - - return current; - } +@ManagedBean(name = "UserManager") +@SessionScoped +public class UserManager +{ + private User current; + + private boolean loggedIn; + + public UserManager() + { + current = new User(); + } + + public String login() + { + String outcome = "failure"; + if (current.getUsername() != null && current.getUsername().length() > 0 + && current.getPassword() != null + && current.getPassword().length() > 0) + { + EntityManagerFactory factory = Persistence + .createEntityManagerFactory("catalog"); + EntityManager manager = factory.createEntityManager(); + Query query = manager.createQuery( + "SELECT u FROM User u where u.username = :username and u.password = :password"); + query.setParameter("username", current.getUsername()); + query.setParameter("password", md5(current.getPassword())); + List results = query.getResultList(); + + if (!results.isEmpty()) + { + loggedIn = true; + current = (User) results.get(0); + outcome = "success"; + } + } + // System.out.println(outcome); + return outcome; + } + + public String logout() + { + loggedIn = false; + current = new User(); + return "home"; + } + + public void setUsername(String username) + { + current.setUsername(username); + } + + public String getUsername() + { + return current.getUsername(); + } + + public void setPassword(String password) + { + current.setPassword(password); + } + + public String getPassword() + { + return current.getPassword(); + } + + public boolean isLoggedIn() + { + return loggedIn; + } + + public User getCurrent() + { + EntityManagerFactory factory = Persistence + .createEntityManagerFactory("catalog"); + EntityManager manager = factory.createEntityManager(); + + EntityTransaction tx = manager.getTransaction(); + tx.begin(); + try + { + current = manager.find(User.class, getUsername()); + + tx.commit(); + } + catch (Exception ex) + { + ex.printStackTrace(System.err); + tx.rollback(); + } + + return current; + } } diff --git a/src/jpa/Category.java b/src/jpa/Category.java index c29c1f7..195b6e8 100644 --- a/src/jpa/Category.java +++ b/src/jpa/Category.java @@ -1,103 +1,64 @@ package jpa; import java.io.Serializable; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; +import javax.persistence.*; import java.util.Set; -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.ManyToMany; +/** + * The persistent class for the category database table. + * + */ @Entity -public class Category implements Serializable -{ - @Id - private int id; - - private String name; - - private String description; - - @ManyToMany(mappedBy = "categoryCollection") - private Set productCollection; - - private static final long serialVersionUID = 1L; - - public Category() - { - super(); - } - - public int getId() - { - return this.id; - } - - public void setId(int id) - { - this.id = id; - } - - public String getName() - { - return this.name; - } - - public void setName(String name) - { - this.name = name; - } - - public String getDescription() - { - return this.description; - } - - public void setDescription(String description) - { - this.description = description; - } - - /** - * The method getProductCollection() returns a - * Collection object that is incompatible with JSF. - * - * @return collection of product objects. - */ - public Collection getProducts() - { - Collection c = new ArrayList(); - - for (Iterator iter = getProductCollection().iterator(); iter.hasNext();) - { - Product product = (Product) iter.next(); - c.add(product); -// System.out.println(product.getName()); - } - - return c; - } - - public Set getProductCollection() - { - return this.productCollection; - } - - public void setProductCollection(Set productCollection) - { - this.productCollection = productCollection; - } - - public void addProduct(Product product) - { - Set products = getProductCollection(); - if (!products.contains(product)) - { - products.add(product); - product.addCategory(this); - } - } - -} +@Table(name="category") +@NamedQuery(name="Category.findAll", query="SELECT c FROM Category c") +public class Category implements Serializable { + private static final long serialVersionUID = 1L; + + @Id + private int id; + + private String description; + + private String name; + + //bi-directional many-to-many association to Product + @ManyToMany(mappedBy="categories") + private Set products; + + public Category() { + } + + public int getId() { + return this.id; + } + + public void setId(int id) { + this.id = id; + } + + public String getDescription() { + return this.description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + + public Set getProducts() { + return this.products; + } + + public void setProducts(Set products) { + this.products = products; + } + +} \ No newline at end of file diff --git a/src/jpa/Product.java b/src/jpa/Product.java index dc0b3f8..baf29ca 100644 --- a/src/jpa/Product.java +++ b/src/jpa/Product.java @@ -1,51 +1,56 @@ package jpa; import java.io.Serializable; +import javax.persistence.*; import java.math.BigDecimal; -import java.util.Collection; import java.util.Set; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.JoinTable; -import javax.persistence.ManyToMany; +/** + * The persistent class for the product database table. + * + */ @Entity +@Table(name="product") +@NamedQuery(name="Product.findAll", query="SELECT p FROM Product p") public class Product implements Serializable { + private static final long serialVersionUID = 1L; + @Id - @GeneratedValue(strategy=GenerationType.SEQUENCE) - private String id; + private int id; + @Lob private String description; - private BigDecimal price; + private BigDecimal height; private String name; - private BigDecimal width; + private BigDecimal price; - private BigDecimal height; + private BigDecimal width; + //bi-directional many-to-many association to Category @ManyToMany @JoinTable( - joinColumns=@JoinColumn(name="product_id"), - inverseJoinColumns=@JoinColumn(name="category_id")) - private Set categoryCollection; - - private static final long serialVersionUID = 1L; + name="product_category" + , joinColumns={ + @JoinColumn(name="product_id") + } + , inverseJoinColumns={ + @JoinColumn(name="category_id") + } + ) + private Set categories; public Product() { - super(); } - public String getId() { + public int getId() { return this.id; } - public void setId(String id) { + public void setId(int id) { this.id = id; } @@ -57,12 +62,12 @@ public class Product implements Serializable { this.description = description; } - public BigDecimal getPrice() { - return this.price; + public BigDecimal getHeight() { + return this.height; } - public void setPrice(BigDecimal price) { - this.price = price; + public void setHeight(BigDecimal height) { + this.height = height; } public String getName() { @@ -73,41 +78,28 @@ public class Product implements Serializable { this.name = name; } - public BigDecimal getWidth() { - return this.width; + public BigDecimal getPrice() { + return this.price; } - public void setWidth(BigDecimal width) { - this.width = width; + public void setPrice(BigDecimal price) { + this.price = price; } - public BigDecimal getHeight() { - return this.height; + public BigDecimal getWidth() { + return this.width; } - public void setHeight(BigDecimal height) { - this.height = height; + public void setWidth(BigDecimal width) { + this.width = width; } - public Collection getCategories() { - return this.getCategoryCollection(); - } - - public Set getCategoryCollection() { - return this.categoryCollection; + public Set getCategories() { + return this.categories; } - public void setCategoryCollection(Set categoryCollection) { - this.categoryCollection = categoryCollection; + public void setCategories(Set categories) { + this.categories = categories; } - public void addCategory(Category category) - { - Set categories = getCategoryCollection(); - if (!categories.contains(category)) - { - categories.add(category); - category.addProduct(this); - } - } -} +} \ No newline at end of file diff --git a/src/jpa/User.java b/src/jpa/User.java index 32d2af8..d96d27e 100644 --- a/src/jpa/User.java +++ b/src/jpa/User.java @@ -1,20 +1,25 @@ package jpa; import java.io.Serializable; -import javax.persistence.Entity; -import javax.persistence.Id; +import javax.persistence.*; + +/** + * The persistent class for the user database table. + * + */ @Entity +@Table(name="user") +@NamedQuery(name="User.findAll", query="SELECT u FROM User u") public class User implements Serializable { + private static final long serialVersionUID = 1L; + @Id private String username; private String password; - private static final long serialVersionUID = 1L; - public User() { - super(); } public String getUsername() { @@ -33,4 +38,4 @@ public class User implements Serializable { this.password = password; } -} +} \ No newline at end of file -- cgit v1.2.3-70-g09d2