View Javadoc

1   /*
2   Copyright 2010 James Pether Sörling Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 
3   	$Id
4   */
5   package org.directdemocracyportal.democracy.service.dao.hibernate;
6   
7   import java.util.List;
8   
9   import org.directdemocracyportal.democracy.model.world.Country;
10  import org.directdemocracyportal.democracy.model.world.Region;
11  import org.directdemocracyportal.democracy.service.dao.CountryDAO;
12  import org.hibernate.criterion.DetachedCriteria;
13  import org.hibernate.criterion.Restrictions;
14  import org.springframework.dao.DataAccessException;
15  import org.springframework.transaction.annotation.Propagation;
16  import org.springframework.transaction.annotation.Transactional;
17  
18  /***
19   * The Class HibernateCountryDAO.
20   */
21  @Transactional(propagation = Propagation.MANDATORY)
22  public class HibernateCountryDAO extends GenericHibernateDAO<Country, Long>
23          implements CountryDAO
24  {
25  
26      /***
27       * Instantiates a new hibernate country dao.
28       */
29      public HibernateCountryDAO() {
30          super(Country.class);
31      }
32  
33      /*
34       * (non-Javadoc)
35       *
36       * @see org.directdemocracyportal.democracy.service.dao.CountryDAO#findByName(java.lang.String)
37       */
38      @Transactional(readOnly = true)
39      public Country findByName(String name) throws DataAccessException {
40          DetachedCriteria findByName = DetachedCriteria.forClass(Country.class);
41          findByName.add(Restrictions.eq("name", name));
42  
43          @SuppressWarnings("unchecked")
44          List<Country> result = this.getHibernateTemplate().findByCriteria(
45                  findByName);
46  
47          return result.isEmpty() ? null : result.get(0);
48      }
49  
50      /*
51       * (non-Javadoc)
52       *
53       * @see org.directdemocracyportal.democracy.service.dao.CountryDAO#findRegionByName(java.lang.String)
54       */
55      @Transactional(readOnly = true)
56      public Region findRegionByName(String name) throws DataAccessException {
57          DetachedCriteria findByName = DetachedCriteria.forClass(Region.class);
58          findByName.add(Restrictions.eq("name", name));
59  
60          @SuppressWarnings("unchecked")
61          List<Region> result = this.getHibernateTemplate().findByCriteria(
62                  findByName);
63  
64          return result.isEmpty() ? null : result.get(0);
65      }
66  
67      /*
68       * (non-Javadoc)
69       *
70       * @see org.directdemocracyportal.democracy.service.dao.CountryDAO#saveRegion(org.directdemocracyportal.democracy.model.world.Region)
71       */
72      @Override
73      public Region saveRegion(Region region) {
74          if (region.isNew()) {
75              this.getHibernateTemplate().save(region);
76          } else {
77              this.getHibernateTemplate().saveOrUpdate(region);
78          }
79          return region;
80      }
81  }