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.application.GovernmentPortal;
10  import org.directdemocracyportal.democracy.model.application.Portal;
11  import org.directdemocracyportal.democracy.model.world.Country;
12  import org.directdemocracyportal.democracy.model.world.Government;
13  import org.directdemocracyportal.democracy.model.world.OrganisationType;
14  import org.directdemocracyportal.democracy.model.world.Region;
15  import org.directdemocracyportal.democracy.service.dao.PortalDAO;
16  import org.hibernate.criterion.DetachedCriteria;
17  import org.hibernate.criterion.Restrictions;
18  import org.springframework.dao.DataAccessException;
19  import org.springframework.transaction.annotation.Propagation;
20  import org.springframework.transaction.annotation.Transactional;
21  
22  /***
23   * The Class HibernatePortalDAO.
24   */
25  @Transactional(propagation = Propagation.MANDATORY)
26  public class HibernatePortalDAO extends GenericHibernateDAO<Portal, Long>
27          implements PortalDAO
28  {
29  
30      /***
31       * Instantiates a new hibernate portal dao.
32       */
33      public HibernatePortalDAO() {
34          super(Portal.class);
35      }
36  
37      /*
38       * (non-Javadoc)
39       *
40       * @see org.directdemocracyportal.democracy.service.dao.PortalDAO#findByName(java.lang.String)
41       */
42      @Transactional(readOnly = true)
43      public Portal findByName(String name) throws DataAccessException {
44          DetachedCriteria findByName = DetachedCriteria.forClass(Portal.class);
45          findByName.add(Restrictions.eq("name", name));
46  
47          @SuppressWarnings("unchecked")
48          List<Portal> result = this.getHibernateTemplate().findByCriteria(
49                  findByName);
50  
51          return result.isEmpty() ? null : result.get(0);
52      }
53  
54      /*
55       * (non-Javadoc)
56       *
57       * @see org.directdemocracyportal.democracy.service.dao.PortalDAO#findGovernmentPortal(org.directdemocracyportal.democracy.model.world.Government)
58       */
59      @Override
60      public GovernmentPortal findGovernmentPortal(Government government) {
61          DetachedCriteria findByGovernment = DetachedCriteria
62                  .forClass(GovernmentPortal.class);
63          findByGovernment.add(Restrictions.eq("government.id", government
64                  .getId()));
65  
66          @SuppressWarnings("unchecked")
67          List<GovernmentPortal> result = this.getHibernateTemplate()
68                  .findByCriteria(findByGovernment);
69  
70          return result.isEmpty() ? null : result.get(0);
71      }
72  
73      /*
74       * (non-Javadoc)
75       *
76       * @see org.directdemocracyportal.democracy.service.dao.PortalDAO#findNationalByCountry(org.directdemocracyportal.democracy.model.world.Country)
77       */
78      @Override
79      public Portal findNationalByCountry(Country country) {
80          DetachedCriteria findByCountry = DetachedCriteria
81                  .forClass(GovernmentPortal.class);
82          findByCountry.add(Restrictions.eq("country.id", country.getId()));
83          findByCountry.add(Restrictions.eq("organisationType",
84                  OrganisationType.National));
85  
86          @SuppressWarnings("unchecked")
87          List<GovernmentPortal> result = this.getHibernateTemplate()
88                  .findByCriteria(findByCountry);
89  
90          return result.isEmpty() ? null : result.get(0);
91      }
92  
93      /*
94       * (non-Javadoc)
95       *
96       * @see org.directdemocracyportal.democracy.service.dao.PortalDAO#findRegionalByRegion(org.directdemocracyportal.democracy.model.world.Region)
97       */
98      @Override
99      public Portal findRegionalByRegion(Region region) {
100         DetachedCriteria findByRegion = DetachedCriteria
101                 .forClass(GovernmentPortal.class);
102         findByRegion.add(Restrictions.eq("region.id", region.getId()));
103         findByRegion.add(Restrictions.eq("organisationType",
104                 OrganisationType.Regional));
105 
106         @SuppressWarnings("unchecked")
107         List<GovernmentPortal> result = this.getHibernateTemplate()
108                 .findByCriteria(findByRegion);
109 
110         return result.isEmpty() ? null : result.get(0);
111     }
112 }