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  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
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  
66  
67  
68  
69  
70  
71  
72      public boolean validate(Logbook entry) {
73          IValidatorService validator = getValidatorService();
74          validator.setObject(entry);
75          return validator.validate();
76      }
77  
78      
79  
80  
81  
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  
96  
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 
123 
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 
144 
145 
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 
161 
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             
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 }