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   
6   package com.hack23.cia.service.dao;
7   
8   import java.util.List;
9   
10  import org.hibernate.HibernateException;
11  import org.hibernate.criterion.Order;
12  import org.hibernate.criterion.Restrictions;
13  
14  import com.hack23.cia.model.sweden.impl.Ballot;
15  import com.hack23.cia.model.sweden.impl.Vote;
16  import com.hack23.cia.model.sweden.impl.Ballot.ImportStatus;
17  
18  /***
19   * The Class BallotDAOImpl.
20   */
21  public class BallotDAOImpl extends GenericHibernateDAO<Ballot, Long> implements
22          BallotDAO {
23  
24      /***
25       * Instantiates a new ballot dao impl.
26       */
27      public BallotDAOImpl() {
28          super(Ballot.class);
29      }
30  
31      /*
32       * (non-Javadoc)
33       * 
34       * @see com.hack23.cia.service.dao.BallotDAO#getAllCompleted()
35       */
36      @SuppressWarnings("unchecked")
37      @Override
38      public final List<Ballot> getAllCompleted() {
39          try {
40              return getSession().createCriteria(Ballot.class).add(
41                      Restrictions.eq("importStatus", ImportStatus.Completed)) //$NON-NLS-1$
42                      .setCacheable(true).list();
43          } catch (final HibernateException hibernateException) {
44              throw getHibernateTemplate().convertHibernateAccessException(
45                      hibernateException);
46          }
47      }
48  
49      /*
50       * (non-Javadoc)
51       * 
52       * @see com.hack23.cia.service.dao.CommiteeReportDAO#getAllCreated()
53       */
54      @Override
55  	@SuppressWarnings("unchecked")
56      public final List<Ballot> getAllCreated() {
57          try {
58              return getSession().createCriteria(Ballot.class).add(
59                      Restrictions.eq("importStatus", ImportStatus.Created)) //$NON-NLS-1$
60                      .setCacheable(true).list();
61          } catch (final HibernateException hibernateException) {
62              throw getHibernateTemplate().convertHibernateAccessException(
63                      hibernateException);
64          }
65      }
66  
67      /*
68       * (non-Javadoc)
69       * 
70       * @see com.hack23.cia.service.dao.BallotDAO#getAllOrderedByWeek()
71       */
72      @Override
73  	@SuppressWarnings("unchecked")
74      public final List<Ballot> getAllOrderedByWeek() {
75          return this
76                  .getHibernateTemplate()
77                  .find(
78                          "select week(ballot.datum),ballot from Ballot ballot order by week(ballot.datum)"); //$NON-NLS-1$
79      }
80  
81      /*
82       * (non-Javadoc)
83       * 
84       * @see com.hack23.cia.service.dao.BallotDAO#getLast()
85       */
86      @Override
87      public final Ballot getLast() {
88          try {
89              return ((Vote) getSession().createCriteria(Vote.class)
90                      .setMaxResults(1).addOrder((Order.desc("datum"))) //$NON-NLS-1$
91                      .setCacheable(true).setMaxResults(1).uniqueResult())
92                      .getBallot();
93          } catch (final HibernateException hibernateException) {
94              throw getHibernateTemplate().convertHibernateAccessException(
95                      hibernateException);
96          }
97      }
98  
99  }