1   package org.wcb.model.bd;
2   
3   import org.wcb.model.service.ILogbookService;
4   import org.wcb.model.service.IServicesConstants;
5   import org.wcb.model.service.IValidatorService;
6   import org.wcb.model.vo.hibernate.Logbook;
7   import org.wcb.model.vo.hibernate.AircraftBO;
8   import org.wcb.model.vo.hibernate.AircraftTypeBO;
9   import org.wcb.model.util.SpringUtil;
10  import org.wcb.exception.ServiceException;
11  
12  import javax.swing.*;
13  import java.util.List;
14  import java.util.ArrayList;
15  
16  /**
17   * <small>
18   * Copyright (c)  2006  wbogaardt.
19   * Permission is granted to copy, distribute and/or modify this document
20   * under the terms of the GNU Free Documentation License, Version 1.2
21   * or any later version published by the Free Software Foundation;
22   * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover
23   * Texts.  A copy of the license is included in the section entitled "GNU
24   * Free Documentation License".
25   * <p/>
26   * $File:  $ <br>
27   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Mar 24, 2006 1:17:29 PM $ <br>
28   * </small>
29   *
30   * @author wbogaardt
31   * @version 1
32   *          Date: Mar 24, 2006
33   *          Time: 1:17:29 PM
34   */
35  
36  public class LogbookDelegate {
37  
38      private ILogbookService logbookService;
39      private IValidatorService logbookValidator;
40  
41      public ILogbookService getLogbookService() {
42          if(logbookService == null)
43          {
44              logbookService = (ILogbookService) SpringUtil.getApplicationContext().getBean(IServicesConstants.LOGBOOK_SERVICE);
45          }
46          return logbookService;
47      }
48  
49      public void setLogbookService(ILogbookService logbookService) {
50          this.logbookService = logbookService;
51      }
52  
53      public IValidatorService getValidatorService() {
54          if( logbookValidator == null) {
55              logbookValidator = (IValidatorService) SpringUtil.getApplicationContext().getBean(IServicesConstants.LOGBOOK_VALIDATION_SERVICE);
56          }
57          return logbookValidator;
58      }
59  
60      public void setValidatorService(IValidatorService svc) {
61          logbookValidator = svc;
62      }
63  
64      /**
65       * This takes a log book entry and uses the LogbookValidatorService
66       * to validate that all data required and is entered correctly.
67       * This should be run before saving the data to the database. It can also
68       * be used to validate information back to the user.
69       * @param entry Logbook entry to validate
70       * @return True everything is correct based on business rules otherwise it returns false.
71       */
72      public boolean validate(Logbook entry) {
73          IValidatorService validator = getValidatorService();
74          validator.setObject(entry);
75          return validator.validate();
76      }
77  
78      /**
79       * This gives access to save a log book entry or even updated an existing one
80       * due to the underlying hibernate framework.
81       * @param entry The log book entry.
82       */
83      public void saveLogEntry(Logbook entry) {
84          try
85          {
86              getLogbookService().saveLogBookEntry(entry);
87          }
88          catch (ServiceException se)
89          {
90              se.printStackTrace();
91          }
92      }
93  
94      /**
95       * Gets a list of all the logbook entries in the database.
96       * @return List of Logbook wrapped data objects.
97       */
98      public List<Logbook> getAllLogbookEntries() {
99          try
100         {
101             return getLogbookService().getAllLogbookEntries();
102         }
103         catch (ServiceException se)
104         {
105             if(se.getCause().getMessage().equals("No access to database")) {
106                int answer = JOptionPane.showConfirmDialog(null, "There appears to be more than one  \n" +
107                         "instance of this application running.  \n" +
108                         "Press ok to cancel running a multiple instance.", "Multiple Application Error", JOptionPane.DEFAULT_OPTION);
109                 if(answer == JOptionPane.YES_OPTION) {
110                     System.exit(0);
111                 }
112             }
113             Logbook value = new Logbook();
114             List<Logbook> returnVal = new ArrayList<Logbook>();
115             returnVal.add(value);
116             return returnVal;
117         }
118 
119     }
120 
121      /**
122      * Gets a list of all the logbook entries in the database.
123      * @return List of Logbook wrapped data objects.
124      */
125     public List<Logbook> findByRegistration(String registration) {
126         try
127         {
128             return getLogbookService().findByRegistration(registration);
129         }
130         catch (ServiceException se)
131         {
132             se.printStackTrace();
133         }
134         return null;
135     }
136 
137     public boolean isAirplaneInLogbook(String registration) {
138         List<Logbook> log = this.findByRegistration(registration);
139         return log != null && log.size() > 0;
140     }
141 
142     /**
143      * Allows finding a log book entry based on the entry's primary key
144      * @param pkId  The primary key to look up the entry on.
145      * @return  The row data of the logbook entry
146      */
147     public Logbook findLogEntry(Integer pkId) {
148         try
149         {
150             return getLogbookService().findLogBookEntry(pkId);
151         }
152         catch (ServiceException se)
153         {
154             se.printStackTrace();
155         }
156         return null;
157     }
158 
159     /**
160      * Deletes a logbook entry
161      * @param entry The entry to delete
162      */
163     public boolean deleteLogEntry(Logbook entry) {
164         try
165         {
166             getLogbookService().deleteLogBookEntry(entry);
167         }
168         catch (ServiceException se)
169         {
170             se.printStackTrace();
171             return false;
172         }
173         return true;
174     }
175 
176     public AircraftBO getAircraftForLogEntry(Logbook lineitem) {
177         AircraftBO returnValue = new AircraftBO();
178         try
179         {
180             String registry = lineitem.getRegistration();
181             returnValue.setRegistrationNumber(registry);
182             return getLogbookService().getAircraftByRegistration(registry);
183         }
184         catch (ServiceException se)
185         {
186             //should log the exception but will return a default airplane with the requested registration number.
187             returnValue = new AircraftBO(lineitem.getRegistration(), 1);
188         }
189         return returnValue;
190     }
191 
192     public AircraftTypeBO getAircraftType(AircraftBO aircraft) {
193         try
194         {
195             return getLogbookService().getAircraftTypeById(aircraft.getTypeId());
196         }
197         catch (ServiceException se)
198         {
199             return new AircraftTypeBO("Cessna", "150M", "C-150M",1);
200         }
201     }
202 }