1   package org.wcb.model.manager.impl;
2   
3   import org.wcb.model.dao.ILogbookDAO;
4   import org.wcb.model.vo.hibernate.Logbook;
5   import org.wcb.model.manager.ILogbookManager;
6   import org.wcb.exception.ManagerException;
7   import org.wcb.exception.InfrastructureException;
8   import org.springframework.dao.DataAccessException;
9   
10  import java.util.List;
11  import java.util.Collection;
12  
13  /**
14   * <small>
15   * Copyright (c)  2006  wbogaardt.
16   * Permission is granted to copy, distribute and/or modify this document
17   * under the terms of the GNU Free Documentation License, Version 1.2
18   * or any later version published by the Free Software Foundation;
19   * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover
20   * Texts.  A copy of the license is included in the section entitled "GNU
21   * Free Documentation License".
22   * <p/>
23   * $File:  $ <br>
24   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Mar 24, 2006 11:14:01 AM $ <br>
25   * </small>
26   *
27   * @author wbogaardt
28   * @version 1
29   *          Date: Mar 24, 2006
30   *          Time: 11:14:01 AM
31   */
32  
33  public class LogbookManager extends BaseManager implements ILogbookManager {
34  
35      private ILogbookDAO iDAO;
36  
37      /**
38       * This is injected by Spring framework normally.
39       *
40       * @param oDAO LogbookDAO object injected by spring.
41       */
42      public void setDao(ILogbookDAO oDAO) {
43          this.iDAO = oDAO;
44      }
45  
46      /**
47       * This is a bean enabled manager so we can get
48       * a reference to the DAO object this way.
49       * @return The interface to the LogbookDAO.
50       */
51      public ILogbookDAO getDao() {
52          return this.iDAO;
53      }
54  
55      /**
56       * Find all of the logbook entries.
57       * @return  list of all entries of Logbook object type
58       * @throws ManagerException
59       */
60      public List<Logbook> findAll() throws ManagerException {
61          List<Logbook> returnValue;
62          try
63          {
64              returnValue = iDAO.findAll();
65              if (returnValue == null)
66              {
67                  throw new ManagerException("Failed to find logbook entries: ");
68              }
69          }
70          catch (InfrastructureException ie)
71          {
72              throw new ManagerException("Failed to find logbook entries due to InfrastructureException", ie);
73          }
74          catch (DataAccessException dae) {
75              throw new ManagerException("No access to database", dae);
76          }
77          return returnValue;
78      }
79  
80      /**
81       * Find all of the logbook entries.
82       * @return  list of all entries of Logbook object type
83       * @throws ManagerException
84       */
85      public List<Logbook> findByRegistration(String sReg) throws ManagerException {
86          List<Logbook> returnValue;
87          try
88          {
89              returnValue = iDAO.findByRegistration(sReg);
90              if (returnValue == null)
91              {
92                  throw new ManagerException("Failed to find logbook entries by registration: ");
93              }
94          }
95          catch (InfrastructureException ie)
96          {
97              throw new ManagerException("Failed to find logbook entries due to InfrastructureException", ie);
98          }
99          return returnValue;
100     }
101 
102     /**
103      * Find all of the logbook entries.
104      * @return  list of all entries of Logbook object type
105      * @throws ManagerException
106      */
107     public List<Logbook> findCrossCountry() throws ManagerException {
108         List<Logbook> returnValue;
109         try
110         {
111             returnValue = iDAO.findAllCrossCountry();
112             if (returnValue == null)
113             {
114                 throw new ManagerException("Failed to find logbook entries by cross country: ");
115             }
116         }
117         catch (InfrastructureException ie)
118         {
119             throw new ManagerException("Failed to find logbook entries due to InfrastructureException", ie);
120         }
121         return returnValue;
122     }
123 
124     /**
125      * Allows finding of a specified entry
126      * @param i The item number in the log book database
127      * @return The logbook entry
128      * @throws ManagerException
129      */
130     public Logbook findEntry(Integer i) throws ManagerException {
131         Logbook returnValue;
132         try
133         {
134             returnValue = iDAO.findEntry(i);
135             if (returnValue == null)
136             {
137                 throw new ManagerException("Failed to find logbook entry: " + i.intValue());
138             }
139         }
140         catch (InfrastructureException ie)
141         {
142             throw new ManagerException("Failed to find logbook entry due to InfrastructureException " + i.intValue(), ie);
143         }
144         return returnValue;
145     }
146 
147     /**
148      * Allows an entry to be saved
149      * @param entry log book entry object
150      * @throws ManagerException
151      */
152     public void saveEntry(Logbook entry) throws ManagerException {
153         try
154         {
155             iDAO.saveOrUpdateObject(entry);
156         }
157         catch (Exception ie) {
158             throw new ManagerException("Failed to save logbook entry due to InfrastructureException ", ie);
159         }
160     }
161 
162      /**
163      * This saves a collection of logbook data. Used primarily by import functionality.
164      * @param entries logbook data to save into the database.
165      * @throws org.wcb.exception.ManagerException
166      */
167     public void saveOrUpdateLogbook(Collection<Logbook> entries) throws ManagerException {
168         try
169         {
170             iDAO.saveOrUpdateAllObjects(entries);
171         }
172         catch (Exception ie) {
173             throw new ManagerException("Failed to save all logbook entries due to InfrastructureException ", ie);
174         }
175     }
176 
177     /**
178      * Allows deletion of a log book entry
179      * @param entry the entry to delete.
180      * @throws ManagerException
181      */
182     public void deleteEntry(Logbook entry) throws ManagerException {
183         try
184         {
185             iDAO.deleteEntry(entry);
186         }
187         catch (InfrastructureException ie) {
188             throw new ManagerException("Failed to delete logbook entry due to InfrastructureException ", ie);
189         }
190     }
191 }