1   package org.wcb.model.dao.impl;
2   
3   import org.wcb.model.vo.hibernate.Logbook;
4   import org.wcb.model.dao.ILogbookDAO;
5   import org.wcb.exception.DAOException;
6   import org.wcb.exception.InfrastructureException;
7   import org.hibernate.criterion.DetachedCriteria;
8   import org.hibernate.criterion.Restrictions;
9   import org.hibernate.criterion.Order;
10  import org.springframework.dao.DataAccessException;
11  
12  import javax.persistence.OrderBy;
13  import java.util.List;
14  
15  /**
16   * <small>
17   * Copyright (c)  2006  wbogaardt.
18   * Permission is granted to copy, distribute and/or modify this document
19   * under the terms of the GNU Free Documentation License, Version 1.2
20   * or any later version published by the Free Software Foundation;
21   * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover
22   * Texts.  A copy of the license is included in the section entitled "GNU
23   * Free Documentation License".
24   * <p/>
25   * $File:  $ <br>
26   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Mar 24, 2006 10:26:59 AM $ <br>
27   * </small>
28   *
29   * @author wbogaardt
30   * @version 1
31   *          Date: Mar 24, 2006
32   *          Time: 10:26:59 AM
33   */
34  
35  public class LogbookDAO extends AbstractHibernateDAO implements ILogbookDAO {
36  
37      /**
38       * Returns all log book entries in the database.
39       * @return A list of all Logbook objects
40       * @throws InfrastructureException
41       */
42      public List<Logbook> findAll() throws InfrastructureException, DataAccessException {
43          DetachedCriteria criteria = DetachedCriteria.forClass(Logbook.class);
44          criteria.addOrder(Order.asc("entryDate"));
45          return getHibernateTemplate().findByCriteria(criteria);
46      }
47  
48  
49      /**
50       * Returns all log book entries in the database based on aircraft registration.
51       * @return A list of all Logbook objects
52       * @throws InfrastructureException
53       */
54      public List<Logbook> findByRegistration(String registration) throws InfrastructureException{
55      	try {
56      		return getObjectsBy(Logbook.class, "registration", registration);
57      	} catch (DAOException daoe) {
58      		throw new InfrastructureException(daoe);
59      	}
60      }
61  
62      /**
63       * Returns all log book entries in the database based on cross country.
64       * @return A list of all Logbook objects
65       * @throws InfrastructureException
66       */
67      public List<Logbook> findAllCrossCountry() throws InfrastructureException {
68      	DetachedCriteria criteria = DetachedCriteria.forClass(Logbook.class);
69      	criteria.add(Restrictions.gt("crossCountry", 0));
70      	return getHibernateTemplate().findByCriteria(criteria);
71          //return getHibernateTemplate().find("from Logbook entry where entry.crossCountry > 0");
72      }
73  
74      /**
75       * Find a log book entry by the primary key id of the entry.
76       * @param id Primary key id in the database
77       * @return The log book entry
78       * @throws InfrastructureException
79       */
80      public Logbook findEntry(Integer id) throws InfrastructureException {
81      	try {
82      		return (Logbook) getObjectsBy(Logbook.class, "id", id).get(0);
83      	} catch (DAOException daoe) {
84      		throw new InfrastructureException(daoe);
85      	} catch (IndexOutOfBoundsException ioobe) {
86              throw new InfrastructureException("No entry found");
87          }
88      	/*try
89          {
90          return (Logbook) getHibernateTemplate().find(
91                  "from Logbook entry where entry.id = ?", id).get(0);
92          } catch (IndexOutOfBoundsException ioobe) {
93              throw new InfrastructureException("No entry found");
94          }*/
95      }
96  
97      /**
98       * Allows an entry item to be deleted from the database
99       * @param oEntry Item to be deleted
100      * @throws InfrastructureException
101      */
102     public void deleteEntry(Logbook oEntry) throws InfrastructureException {
103         getHibernateTemplate().delete(oEntry);
104     }
105 }