View Javadoc
1   package org.itracker.persistence.dao;
2   
3   import org.hibernate.Criteria;
4   import org.hibernate.HibernateException;
5   import org.hibernate.criterion.Expression;
6   import org.itracker.model.Configuration;
7   
8   import java.util.List;
9   
10  /**
11   *
12   */
13  public class ConfigurationDAOImpl extends BaseHibernateDAOImpl<Configuration>
14          implements ConfigurationDAO {
15  
16      public Configuration findByPrimaryKey(Integer configId) {
17          try {
18              return (Configuration) getSession().get(Configuration.class, configId);
19          } catch (HibernateException e) {
20              throw convertHibernateAccessException(e);
21          }
22      }
23  
24      @Deprecated
25      @SuppressWarnings("unchecked")
26      public List<Configuration> findByType(int type) {
27          return _findByTypeAndValue(Configuration.Type.valueOf(type), null);
28      }
29  
30      @Deprecated
31      @SuppressWarnings("unchecked")
32      public List<Configuration> findByTypeAndValue(int type, String value) {
33          return findByTypeAndValue(Configuration.Type.valueOf(type), value);
34      }
35  
36  
37      public Configuration findByTypeValueKey(Configuration.Type type, String value) {
38          List<Configuration> res = _findByTypeAndValue(type, value);
39          if (res.isEmpty()) {
40              return null;
41          }
42          return res.get(0);
43      }
44  
45      public List<Configuration> findByType(Configuration.Type type) {
46          return _findByTypeAndValue(type, null);
47      }
48  
49      public List<Configuration> findByTypeAndValue(Configuration.Type type, final String value) {
50          String _val = value;
51          if (null == _val) {
52              _val = "null";
53          }
54          return _findByTypeAndValue(type, _val);
55      }
56      private List<Configuration> _findByTypeAndValue(Configuration.Type type, String value) {
57          Criteria criteria = getSession().createCriteria(Configuration.class);
58          criteria.add(Expression.eq("type", type));
59          if (null != value) {
60              criteria.add(Expression.eq("value", value));
61              criteria.setMaxResults(1);
62          }
63  
64          try {
65              return criteria.list();
66          } catch (HibernateException e) {
67              throw convertHibernateAccessException(e);
68          }
69      }
70  
71  
72  }