summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/META-INF/persistence.xml15
-rw-r--r--src/beans/CategoryManager.java83
-rw-r--r--src/beans/ContactManager.java15
-rw-r--r--src/beans/ProductManager.java70
-rw-r--r--src/beans/UserManager.java100
-rw-r--r--src/jpa/Category.java103
-rw-r--r--src/jpa/JPATest.java62
-rw-r--r--src/jpa/Product.java113
-rw-r--r--src/jpa/User.java36
-rw-r--r--src/resources.properties2
-rw-r--r--src/util/DigestUtils.java58
11 files changed, 657 insertions, 0 deletions
diff --git a/src/META-INF/persistence.xml b/src/META-INF/persistence.xml
new file mode 100644
index 0000000..039a45e
--- /dev/null
+++ b/src/META-INF/persistence.xml
@@ -0,0 +1,15 @@
+<?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>
+<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"/>
+</properties>
+</persistence-unit>
+</persistence>
diff --git a/src/beans/CategoryManager.java b/src/beans/CategoryManager.java
new file mode 100644
index 0000000..7b96d2f
--- /dev/null
+++ b/src/beans/CategoryManager.java
@@ -0,0 +1,83 @@
+/*
+ * $RCSFile$
+ *
+ * Created on 08.03.2007
+ * for Project:
+ * by steins
+ *
+ * (C) 2005-2006 by
+ */
+package beans;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Map;
+
+import javax.faces.context.FacesContext;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.EntityTransaction;
+import javax.persistence.Persistence;
+
+import jpa.Category;
+
+public class CategoryManager
+{
+ private Category current;
+
+ public Category getCurrent()
+ {
+ return current;
+ }
+
+ public void setCurrent(Category current)
+ {
+ this.current = current;
+ }
+
+ public Collection getCategories()
+ {
+ EntityManagerFactory factory = Persistence
+ .createEntityManagerFactory("catalog");
+ EntityManager manager = factory.createEntityManager();
+ return manager.createQuery("SELECT c FROM Category c ").getResultList();
+ }
+
+ 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);
+
+ EntityManagerFactory factory = Persistence
+ .createEntityManagerFactory("catalog");
+ EntityManager manager = factory.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();
+ }
+
+ FacesContext context = FacesContext.getCurrentInstance();
+ try
+ {
+ context.getExternalContext().redirect("products.jsf");
+ }
+ catch (IOException e)
+ {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ context.responseComplete();
+ }
+}
diff --git a/src/beans/ContactManager.java b/src/beans/ContactManager.java
new file mode 100644
index 0000000..43c96e0
--- /dev/null
+++ b/src/beans/ContactManager.java
@@ -0,0 +1,15 @@
+/*
+ * $RCSFile$
+ *
+ * Created on 12.03.2007
+ * for Project:
+ * by steins
+ *
+ * (C) 2005-2006 by
+ */
+package beans;
+
+public class ContactManager
+{
+ // nothing implemented yet
+}
diff --git a/src/beans/ProductManager.java b/src/beans/ProductManager.java
new file mode 100644
index 0000000..cc0dcaa
--- /dev/null
+++ b/src/beans/ProductManager.java
@@ -0,0 +1,70 @@
+/*
+ * $RCSFile$
+ *
+ * Created on 11.06.2007
+ * for Project:
+ * by steins
+ *
+ * (C) 2005-2006 by
+ */
+package beans;
+
+import java.io.IOException;
+import java.util.Map;
+
+import javax.faces.context.FacesContext;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.EntityTransaction;
+import javax.persistence.Persistence;
+
+import jpa.Product;
+
+public class ProductManager
+{
+ private Product current;
+
+ public Product getCurrent()
+ {
+ return current;
+ }
+
+ public void select(javax.faces.event.ActionEvent actionEvent)
+ {
+ FacesContext facesContext = FacesContext.getCurrentInstance();
+ Map params = facesContext.getExternalContext().getRequestParameterMap();
+ String selectedId = (String) params.get("selectedId");
+// System.out.println(selectedId);
+
+ EntityManagerFactory factory = Persistence
+ .createEntityManagerFactory("catalog");
+ EntityManager manager = factory.createEntityManager();
+
+ EntityTransaction tx = manager.getTransaction();
+ tx.begin();
+ try
+ {
+ current = manager.find(Product.class, selectedId);
+
+ tx.commit();
+ }
+ catch (Exception ex)
+ {
+ ex.printStackTrace(System.err);
+ tx.rollback();
+ }
+
+ FacesContext context = FacesContext.getCurrentInstance();
+ try
+ {
+ context.getExternalContext().redirect("product.jsf");
+ }
+ catch (IOException e)
+ {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ context.responseComplete();
+ }
+
+}
diff --git a/src/beans/UserManager.java b/src/beans/UserManager.java
new file mode 100644
index 0000000..9e08543
--- /dev/null
+++ b/src/beans/UserManager.java
@@ -0,0 +1,100 @@
+/*
+ * $RCSFile$
+ *
+ * Created on 06.12.2006
+ * for Project:
+ * by steins
+ *
+ * (C) 2005-2006 by
+ */
+package beans;
+
+import java.util.List;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.EntityTransaction;
+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;
+ }
+}
diff --git a/src/jpa/Category.java b/src/jpa/Category.java
new file mode 100644
index 0000000..c29c1f7
--- /dev/null
+++ b/src/jpa/Category.java
@@ -0,0 +1,103 @@
+package jpa;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Set;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.ManyToMany;
+
+@Entity
+public class Category implements Serializable
+{
+ @Id
+ private int id;
+
+ private String name;
+
+ private String description;
+
+ @ManyToMany(mappedBy = "categoryCollection")
+ private Set<Product> 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 <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> getProductCollection()
+ {
+ return this.productCollection;
+ }
+
+ 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);
+ }
+ }
+
+}
diff --git a/src/jpa/JPATest.java b/src/jpa/JPATest.java
new file mode 100644
index 0000000..5a4749e
--- /dev/null
+++ b/src/jpa/JPATest.java
@@ -0,0 +1,62 @@
+package jpa;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.EntityTransaction;
+import javax.persistence.Persistence;
+
+public class JPATest
+{
+ /**
+ * @param args
+ */
+ public static void main(String[] args)
+ {
+ EntityManagerFactory factory = Persistence
+ .createEntityManagerFactory("catalog");
+ EntityManager manager = factory.createEntityManager();
+ EntityTransaction tx = manager.getTransaction();
+ tx.begin();
+ try
+ {
+// Product product = manager.find(Product.class, "7");
+// Category newCategory = manager.find(Category.class, 4);
+// product.addCategory(newCategory);
+// manager.persist(product);
+// Collection<Category> categories = product.getCategoryCollection();
+// for (Category category : categories)
+// {
+// System.out.println(category.getName());
+// }
+ Product product = new Product();
+ product.setDescription("Noch ein Testprodukt");
+ product.setName("Produkt");
+ manager.persist(product);
+// User user = new User();
+// user.setUsername("bestertester2");
+// System.out.println(DigestUtils.md5("masterkey"));
+// user.setPassword(DigestUtils.md5("masterkey"));
+// manager.persist(user);
+
+ tx.commit();
+ }
+ catch (Exception ex)
+ {
+ ex.printStackTrace(System.err);
+ tx.rollback();
+ }
+
+// CategoryManager cm = new CategoryManager();
+// Collection<Category> categories = cm.getCategories();
+// for (Category category : categories)
+// {
+// System.out.println(category.getName());
+// Set<Product> products = category.getProductCollection();
+// for (Product product : products)
+// {
+// System.out.println("\t" + product.getName());
+// }
+//
+// }
+ }
+}
diff --git a/src/jpa/Product.java b/src/jpa/Product.java
new file mode 100644
index 0000000..dc0b3f8
--- /dev/null
+++ b/src/jpa/Product.java
@@ -0,0 +1,113 @@
+package jpa;
+
+import java.io.Serializable;
+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;
+
+@Entity
+public class Product implements Serializable {
+ @Id
+ @GeneratedValue(strategy=GenerationType.SEQUENCE)
+ private String id;
+
+ private String description;
+
+ private BigDecimal price;
+
+ private String name;
+
+ private BigDecimal width;
+
+ private BigDecimal height;
+
+ @ManyToMany
+ @JoinTable(
+ joinColumns=@JoinColumn(name="product_id"),
+ inverseJoinColumns=@JoinColumn(name="category_id"))
+ private Set<Category> categoryCollection;
+
+ private static final long serialVersionUID = 1L;
+
+ public Product() {
+ super();
+ }
+
+ public String getId() {
+ return this.id;
+ }
+
+ public void setId(String id) {
+ this.id = id;
+ }
+
+ public String getDescription() {
+ return this.description;
+ }
+
+ public void setDescription(String description) {
+ this.description = description;
+ }
+
+ public BigDecimal getPrice() {
+ return this.price;
+ }
+
+ public void setPrice(BigDecimal price) {
+ this.price = price;
+ }
+
+ public String getName() {
+ return this.name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public BigDecimal getWidth() {
+ return this.width;
+ }
+
+ public void setWidth(BigDecimal width) {
+ this.width = width;
+ }
+
+ public BigDecimal getHeight() {
+ return this.height;
+ }
+
+ public void setHeight(BigDecimal height) {
+ this.height = height;
+ }
+
+ public Collection getCategories() {
+ return this.getCategoryCollection();
+ }
+
+ public Set<Category> getCategoryCollection() {
+ return this.categoryCollection;
+ }
+
+ public void setCategoryCollection(Set<Category> categoryCollection) {
+ this.categoryCollection = categoryCollection;
+ }
+
+ public void addCategory(Category category)
+ {
+ Set<Category> categories = getCategoryCollection();
+ if (!categories.contains(category))
+ {
+ categories.add(category);
+ category.addProduct(this);
+ }
+ }
+}
diff --git a/src/jpa/User.java b/src/jpa/User.java
new file mode 100644
index 0000000..32d2af8
--- /dev/null
+++ b/src/jpa/User.java
@@ -0,0 +1,36 @@
+package jpa;
+
+import java.io.Serializable;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+@Entity
+public class User implements Serializable {
+ @Id
+ private String username;
+
+ private String password;
+
+ private static final long serialVersionUID = 1L;
+
+ public User() {
+ super();
+ }
+
+ public String getUsername() {
+ return this.username;
+ }
+
+ public void setUsername(String username) {
+ this.username = username;
+ }
+
+ public String getPassword() {
+ return this.password;
+ }
+
+ public void setPassword(String password) {
+ this.password = password;
+ }
+
+}
diff --git a/src/resources.properties b/src/resources.properties
new file mode 100644
index 0000000..8e1cc07
--- /dev/null
+++ b/src/resources.properties
@@ -0,0 +1,2 @@
+prompt=Your Name\:
+greeting=Hello
diff --git a/src/util/DigestUtils.java b/src/util/DigestUtils.java
new file mode 100644
index 0000000..4633715
--- /dev/null
+++ b/src/util/DigestUtils.java
@@ -0,0 +1,58 @@
+/*
+ * $RCSFile$
+ *
+ * Created on 06.12.2006
+ * for Project:
+ * by steins
+ *
+ * (C) 2005-2006 by
+ */
+package util;
+
+import java.io.UnsupportedEncodingException;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+
+public class DigestUtils
+{
+ private static final String HEX_DIGITS = "0123456789abcdef";
+
+ private DigestUtils()
+ {
+ }
+
+ public static String bin2hex(byte[] bin)
+ {
+ StringBuilder sb = new StringBuilder(32);
+ for (int i = 0; i < bin.length; ++i)
+ {
+ byte b = bin[i];
+ int h = (b & 0xf0) >> 4;
+ sb.append(HEX_DIGITS.charAt(h));
+ h = b & 0x0f;
+ sb.append(HEX_DIGITS.charAt(h));
+ }
+
+ return sb.substring(0);
+ }
+
+ public static String md5(String s)
+ {
+ MessageDigest md = null;
+ try
+ {
+ md = MessageDigest.getInstance("MD5");
+ md.update(s.getBytes("ISO-8859-1"));
+ }
+ catch (NoSuchAlgorithmException e)
+ {
+ e.printStackTrace();
+ }
+ catch (UnsupportedEncodingException e)
+ {
+ e.printStackTrace();
+ }
+
+ return bin2hex(md.digest());
+ }
+}