01: package net.ar.webonswing.petstore.persistence;
02:
03: import java.util.*;
04:
05: import net.ar.webonswing.*;
06: import net.ar.webonswing.petstore.model.*;
07: import net.sf.hibernate.*;
08: import net.sf.hibernate.cfg.*;
09: import net.sf.hibernate.exception.*;
10: import net.sf.hibernate.expression.*;
11:
12: import org.springframework.orm.hibernate.*;
13:
14: public class Dao {
15: public static final Object SESSION_FACTORY_KEY = "session-factory";
16:
17: public SessionFactory getSessionFactory() {
18: try {
19: SessionFactory sessionFactory = (SessionFactory) WosFramework
20: .getApplicationContext().get(SESSION_FACTORY_KEY);
21:
22: if (sessionFactory == null) {
23: Configuration cfg = new Configuration()
24: .configure("/hibernate/config/hibernate.cfg.xml");
25: sessionFactory = cfg.buildSessionFactory();
26: WosFramework.getApplicationContext().put(
27: SESSION_FACTORY_KEY, sessionFactory);
28: }
29:
30: return sessionFactory;
31: } catch (HibernateException e) {
32: throw new NestableRuntimeException(e);
33: }
34: }
35:
36: public List getCustomers(String aUserName, String aPassword) {
37: return new HibernateTemplate(getSessionFactory())
38: .find(
39: "from customer in class net.ar.webonswing.petstore.model.Customer where customer.username = ? and customer.password = ?",
40: new Object[] { aUserName, aPassword });
41: }
42:
43: public List getCustomers(String aUserName) {
44: return new HibernateTemplate(getSessionFactory())
45: .find(
46: "from customer in class net.ar.webonswing.petstore.model.Customer where customer.username = ?",
47: aUserName);
48: }
49:
50: public void saveOrUpdate(Object anObject) {
51: new HibernateTemplate(getSessionFactory()).save(anObject);
52: }
53:
54: public List getCategories() {
55: return new HibernateTemplate(getSessionFactory())
56: .find("from net.ar.webonswing.petstore.model.Category");
57: }
58:
59: public List searchProducts(final String aKeyword) {
60: return new HibernateTemplate(getSessionFactory())
61: .executeFind(new HibernateCallback() {
62: public Object doInHibernate(Session session)
63: throws HibernateException {
64: String searchString = "%" + aKeyword + "%";
65: return session
66: .createCriteria(Product.class)
67: .add(
68: Expression
69: .disjunction()
70: .add(
71: Expression
72: .like(
73: "productId",
74: searchString))
75: .add(
76: Expression
77: .like(
78: "name",
79: searchString))
80: .add(
81: Expression
82: .like(
83: "description",
84: searchString)))
85: .list();
86: }
87: });
88: }
89: }
|