KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > business > hibernate > HibernatePropertiesManagerImpl


1 /*
2  * HibernatePropertiesManagerImpl.java
3  *
4  * Created on April 21, 2005, 10:40 AM
5  */

6
7 package org.roller.business.hibernate;
8
9 import java.util.HashMap JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12 import java.util.Map JavaDoc;
13 import net.sf.hibernate.Criteria;
14 import net.sf.hibernate.HibernateException;
15 import net.sf.hibernate.Session;
16 import net.sf.hibernate.expression.Expression;
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19 import org.roller.RollerException;
20 import org.roller.business.PersistenceStrategy;
21 import org.roller.business.PropertiesManagerImpl;
22 import org.roller.pojos.RollerPropertyData;
23
24 /**
25  * A hibernate specific implementation of the properties manager.
26  *
27  * @author Allen Gilliland
28  */

29 public class HibernatePropertiesManagerImpl extends PropertiesManagerImpl {
30     
31     static final long serialVersionUID = -4326713177137796936L;
32         
33     private static Log mLogger =
34         LogFactory.getFactory().getInstance(HibernatePropertiesManagerImpl.class);
35     
36     
37     /** Creates a new instance of HibernatePropertiesManagerImpl */
38     public HibernatePropertiesManagerImpl(PersistenceStrategy strategy) {
39         super(strategy);
40         mLogger.debug("Instantiating Hibernate Properties Manager");
41     }
42     
43     
44     /** Retrieve a single property by name */
45     public RollerPropertyData getProperty(String JavaDoc name) throws RollerException {
46         try
47         {
48             Session session = ((HibernateStrategy) mStrategy).getSession();
49             Criteria criteria = session.createCriteria(RollerPropertyData.class);
50             criteria.add(Expression.eq("name", name));
51             criteria.setMaxResults(1);
52             List JavaDoc list = criteria.list();
53             return (list.size()!= 0) ? (RollerPropertyData)list.get(0) : null;
54         }
55         catch (HibernateException e)
56         {
57             throw new RollerException(e);
58         }
59     }
60     
61     
62     /** Retrieve all properties */
63     public Map JavaDoc getProperties() throws RollerException {
64         
65         HashMap JavaDoc props = new HashMap JavaDoc();
66         
67         try
68         {
69             Session session = ((HibernateStrategy) mStrategy).getSession();
70             Criteria criteria = session.createCriteria(RollerPropertyData.class);
71             List JavaDoc list = criteria.list();
72             
73             // for convenience sake we are going to put the list of props
74
// into a map for users to access it. The value element of the
75
// hash still needs to be the RollerPropertyData object so that
76
// we can save the elements again after they have been updated
77
RollerPropertyData prop = null;
78             Iterator JavaDoc it = list.iterator();
79             while(it.hasNext()) {
80                 prop = (RollerPropertyData) it.next();
81                 props.put(prop.getName(), prop);
82             }
83         }
84         catch (HibernateException e)
85         {
86             throw new RollerException(e);
87         }
88         
89         return props;
90     }
91     
92 }
93
Popular Tags