summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/META-INF/persistence.xml13
-rw-r--r--src/beans/CategoryManager.java8
-rw-r--r--src/beans/ContactManager.java5
-rw-r--r--src/beans/ProductManager.java8
-rw-r--r--src/beans/UserManager.java148
-rw-r--r--src/jpa/Category.java125
-rw-r--r--src/jpa/Product.java94
-rw-r--r--src/jpa/User.java17
8 files changed, 203 insertions, 215 deletions
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 @@
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="catalog">
-<class>jpa.User</class>
-<class>jpa.Category</class>
-<class>jpa.Product</class>
+<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
+<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
- <property name="toplink.logging.level" value="INFO"/>
- <property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
- <property name="toplink.jdbc.url" value="jdbc:mysql://localhost/catalog"/>
- <property name="toplink.jdbc.user" value="root"/>
- <property name="toplink.jdbc.password" value="masterkey"/>
+ <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
+ <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/jcatalog"/>
+ <property name="javax.persistence.jdbc.user" value="root"/>
</properties>
</persistence-unit>
</persistence>
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;
+@ManagedBean(name = "UserManager")
+@SessionScoped
+public class UserManager
+{
+ private User current;
- private boolean loggedIn;
+ private boolean loggedIn;
- public UserManager() {
- current = new User();
- }
+ 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();
+ 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;
- }
+ 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 String logout()
+ {
+ loggedIn = false;
+ current = new User();
+ return "home";
+ }
- public void setUsername(String username) {
- current.setUsername(username);
- }
+ public void setUsername(String username)
+ {
+ current.setUsername(username);
+ }
- public String getUsername() {
- return current.getUsername();
- }
+ public String getUsername()
+ {
+ return current.getUsername();
+ }
- public void setPassword(String password) {
- current.setPassword(password);
- }
+ public void setPassword(String password)
+ {
+ current.setPassword(password);
+ }
- public String getPassword() {
- return current.getPassword();
- }
+ public String getPassword()
+ {
+ return current.getPassword();
+ }
- public boolean isLoggedIn() {
- return loggedIn;
- }
+ public boolean isLoggedIn()
+ {
+ return loggedIn;
+ }
- public User getCurrent() {
- EntityManagerFactory factory = Persistence
- .createEntityManagerFactory("catalog");
- EntityManager manager = factory.createEntityManager();
+ 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());
+ EntityTransaction tx = manager.getTransaction();
+ tx.begin();
+ try
+ {
+ current = manager.find(User.class, getUsername());
- tx.commit();
- } catch (Exception ex) {
- ex.printStackTrace(System.err);
- tx.rollback();
- }
+ tx.commit();
+ }
+ catch (Exception ex)
+ {
+ ex.printStackTrace(System.err);
+ tx.rollback();
+ }
- return current;
- }
+ 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;
+@Table(name="category")
+@NamedQuery(name="Category.findAll", query="SELECT c FROM Category c")
+public class Category implements Serializable {
+ private static final long serialVersionUID = 1L;
- private String name;
+ @Id
+ private int id;
- private String description;
+ private String description;
- @ManyToMany(mappedBy = "categoryCollection")
- private Set<Product> productCollection;
+ private String name;
- private static final long serialVersionUID = 1L;
+ //bi-directional many-to-many association to Product
+ @ManyToMany(mappedBy="categories")
+ private Set<Product> products;
- public Category()
- {
- super();
- }
+ public Category() {
+ }
- public int getId()
- {
- return this.id;
- }
+ public int getId() {
+ return this.id;
+ }
- public void setId(int id)
- {
- this.id = id;
- }
+ public void setId(int id) {
+ this.id = id;
+ }
- public String getName()
- {
- return this.name;
- }
+ public String getDescription() {
+ return this.description;
+ }
- public void setName(String name)
- {
- this.name = name;
- }
+ public void setDescription(String description) {
+ this.description = description;
+ }
- public String getDescription()
- {
- return this.description;
- }
+ public String getName() {
+ return this.name;
+ }
- public void setDescription(String description)
- {
- this.description = description;
- }
+ public void setName(String name) {
+ this.name = name;
+ }
- /**
- * The method <code>getProductCollection()</code> returns a
- * Collection object that is incompatible with JSF.
- *
- * @return collection of product objects.
- */
- public Collection<Product> getProducts()
- {
- Collection<Product> c = new ArrayList<Product>();
-
- for (Iterator iter = getProductCollection().iterator(); iter.hasNext();)
- {
- Product product = (Product) iter.next();
- c.add(product);
-// System.out.println(product.getName());
- }
-
- return c;
- }
+ public Set<Product> getProducts() {
+ return this.products;
+ }
- public Set<Product> getProductCollection()
- {
- return this.productCollection;
- }
+ public void setProducts(Set<Product> products) {
+ this.products = products;
+ }
- public void setProductCollection(Set<Product> productCollection)
- {
- this.productCollection = productCollection;
- }
-
- public void addProduct(Product product)
- {
- Set<Product> products = getProductCollection();
- if (!products.contains(product))
- {
- products.add(product);
- product.addCategory(this);
- }
- }
-
-}
+} \ 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<Category> categoryCollection;
-
- private static final long serialVersionUID = 1L;
+ name="product_category"
+ , joinColumns={
+ @JoinColumn(name="product_id")
+ }
+ , inverseJoinColumns={
+ @JoinColumn(name="category_id")
+ }
+ )
+ private Set<Category> 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<Category> getCategoryCollection() {
- return this.categoryCollection;
+ public Set<Category> getCategories() {
+ return this.categories;
}
- public void setCategoryCollection(Set<Category> categoryCollection) {
- this.categoryCollection = categoryCollection;
+ public void setCategories(Set<Category> categories) {
+ this.categories = categories;
}
- public void addCategory(Category category)
- {
- Set<Category> 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