1 package org.wcb.model.service.impl;
2
3 import org.wcb.model.service.ILogbookService;
4 import org.wcb.model.service.IExportService;
5 import org.wcb.model.vo.hibernate.Logbook;
6 import org.wcb.model.vo.LogExportVO;
7 import org.wcb.exception.ServiceException;
8
9 import java.io.*;
10 import java.util.List;
11 import java.util.Date;
12 import java.util.logging.Level;
13 import java.util.logging.Logger;
14 import java.text.SimpleDateFormat;
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 public class ExportService implements IExportService {
38
39 private ILogbookService service;
40 private Logger LOG = Logger.getLogger(ExportService.class.getName());
41
42 private static final String COMMA_SEP = ",";
43 private static final String TAB_SEP = "\t";
44 private static final String PIPE_SEP = "|";
45 private static final String SEMICOLON_SEP = ";";
46 private static final String QUOTE = "\"";
47
48 public static int COMMA = 1;
49 public static int TAB = 2;
50 public static int PIPE = 3;
51 public static int SEMICOLON = 4;
52 private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-M-dd");
53
54 private int userSelectedSeparator;
55
56 public ExportService() {
57 }
58
59 public void setLogService(ILogbookService svc) {
60 this.service = svc;
61 }
62
63 public ILogbookService getLogService() {
64 return this.service;
65 }
66
67
68
69
70
71
72
73
74
75 public boolean processFile(File outFile, LogExportVO exportVO, int separatorType) throws ServiceException {
76 this.userSelectedSeparator = separatorType;
77 BufferedWriter out = null;
78 try {
79 out = new BufferedWriter(new FileWriter(outFile.getAbsolutePath()));
80 process(out, exportVO);
81 return true;
82 } catch (IOException ioe) {
83 LOG.log(Level.WARNING, "Unable write file " + outFile.getAbsolutePath(), ioe);
84 return false;
85 }
86 finally {
87 try {
88 assert out != null;
89 out.close();
90 } catch (IOException ioe) {
91
92 }
93 }
94 }
95
96 private void process(BufferedWriter out, LogExportVO voState) throws IOException, ServiceException {
97 out.write(header(voState));
98 out.newLine();
99 List<Logbook> entries= service.getAllLogbookEntries();
100 for(Logbook entry:entries) {
101 out.write(marshall(entry, voState));
102 out.newLine();
103 }
104 }
105
106
107
108
109
110
111
112 private String header(LogExportVO voState) {
113 StringBuilder builder = new StringBuilder("#The format for this CSV file is as follows: \n#");
114 if(voState.isEntryDate()) {
115 builder.append("Date");
116 builder.append(getSeparatorType());
117 }
118 if(voState.isRegistration()) {
119 builder.append("AircraftBO Registration");
120 builder.append(getSeparatorType());
121 }
122 if(voState.isFAAFrom())
123 {
124 builder.append("Route of Flight FROM");
125 builder.append(getSeparatorType());
126 }
127 if(voState.isVia())
128 {
129 builder.append("Route of Flight VIA");
130 builder.append(getSeparatorType());
131 }
132 if(voState.isFAATo())
133 {
134 builder.append("Route of Flight TO");
135 builder.append(getSeparatorType());
136 }
137 if(voState.isFlightDuration())
138 {
139 builder.append("Duration of Flight");
140 builder.append(getSeparatorType());
141 }
142 if(voState.isPIC()) {
143 builder.append("PIC");
144 builder.append(getSeparatorType());
145 }
146 if(voState.isSIC())
147 {
148 builder.append("SIC");
149 builder.append(getSeparatorType());
150 }
151 if(voState.isCrossCountry()) {
152 builder.append("Cross Country");
153 builder.append(getSeparatorType());
154 }
155 if(voState.isFlightInstructing())
156 {
157 builder.append("as Flight Instructor");
158 builder.append(getSeparatorType());
159 }
160 if(voState.isSafetyPilot()) {
161 builder.append("as Safety Pilot");
162 builder.append(getSeparatorType());
163 }
164 if(voState.isDualReceived()) {
165 builder.append("Dual Recieved");
166 builder.append(getSeparatorType());
167 }
168 if(voState.isSolo()) {
169 builder.append("Solo");
170 builder.append(getSeparatorType());
171 }
172 if(voState.isDayTakeoff()) {
173 builder.append("Day T-O");
174 builder.append(getSeparatorType());
175 }
176 if(voState.isDayLanding()) {
177 builder.append("Day Landing");
178 builder.append(getSeparatorType());
179 }
180 if(voState.isNightTakeoff()) {
181 builder.append("Night T-O");
182 builder.append(getSeparatorType());
183 }
184 if(voState.isNightLanding()) {
185 builder.append("Night Landing");
186 builder.append(getSeparatorType());
187 }
188 if (voState.isInstrumentApproaches()) {
189 builder.append("Instrument Approaches");
190 builder.append(getSeparatorType());
191 }
192 if (voState.isConditionNight()) {
193 builder.append("Night");
194 builder.append(getSeparatorType());
195 }
196 if (voState.isActualIMC()) {
197 builder.append("Actual IMC");
198 builder.append(getSeparatorType());
199 }
200 if (voState.isSimulatedIMC()) {
201 builder.append("Simulated IMC");
202 builder.append(getSeparatorType());
203 }
204 if (voState.isFlightSim()) {
205 builder.append("Flight Simulator");
206 }
207 return builder.toString();
208 }
209
210 private String marshall(Logbook entry, LogExportVO voState) {
211 StringBuilder buffer = new StringBuilder();
212 if(voState.isEntryDate()) {
213 buffer.append(entry.getEntryDate());
214 buffer.append(getSeparatorType());
215 }
216 if(voState.isRegistration()) {
217 buffer.append(entry.getRegistration());
218 buffer.append(getSeparatorType());
219 }
220 if(voState.isFAAFrom())
221 {
222 buffer.append(entry.getFaaFrom());
223 buffer.append(getSeparatorType());
224 }
225 if(voState.isVia())
226 {
227 buffer.append(QUOTE);
228 buffer.append(entry.getFaaVia());
229 buffer.append(QUOTE);
230 buffer.append(getSeparatorType());
231 }
232 if(voState.isFAATo())
233 {
234 buffer.append(entry.getFaaTo());
235 buffer.append(getSeparatorType());
236 }
237 if(voState.isFlightDuration())
238 {
239 buffer.append(entry.getFlightDuration());
240 buffer.append(getSeparatorType());
241 }
242 if(voState.isPIC()) {
243 buffer.append(entry.getPic());
244 buffer.append(getSeparatorType());
245 }
246 if(voState.isSIC())
247 {
248 buffer.append(entry.getSic());
249 buffer.append(getSeparatorType());
250 }
251 if(voState.isCrossCountry()) {
252 buffer.append(entry.getCrossCountry());
253 buffer.append(getSeparatorType());
254 }
255 if(voState.isFlightInstructing())
256 {
257 buffer.append(entry.getFlightInstructing());
258 buffer.append(getSeparatorType());
259 }
260 if(voState.isSafetyPilot()) {
261 buffer.append(entry.getSafetyPilot());
262 buffer.append(getSeparatorType());
263 }
264 if(voState.isDualReceived()) {
265 buffer.append(entry.getDualReceived());
266 buffer.append(getSeparatorType());
267 }
268 if(voState.isSolo()) {
269 buffer.append(entry.getSolo());
270 buffer.append(getSeparatorType());
271 }
272 if(voState.isDayTakeoff()) {
273 buffer.append(entry.getDayTakeoffs());
274 buffer.append(getSeparatorType());
275 }
276 if(voState.isDayLanding()) {
277 buffer.append(entry.getDayLandings());
278 buffer.append(getSeparatorType());
279 }
280 if(voState.isNightTakeoff()) {
281 buffer.append(entry.getNightTakeoffs());
282 buffer.append(getSeparatorType());
283 }
284 if(voState.isNightLanding()) {
285 buffer.append(entry.getNightLandings());
286 buffer.append(getSeparatorType());
287 }
288 if (voState.isInstrumentApproaches()) {
289 buffer.append(entry.getInstrumentApproaches());
290 buffer.append(getSeparatorType());
291 }
292 if (voState.isConditionNight()) {
293 buffer.append(entry.getConditionNight());
294 buffer.append(getSeparatorType());
295 }
296 if (voState.isActualIMC()) {
297 buffer.append(entry.getConditionActualImc());
298 buffer.append(getSeparatorType());
299 }
300 if (voState.isSimulatedIMC()) {
301 buffer.append(entry.getConditionSimulatedImc());
302 buffer.append(getSeparatorType());
303 }
304 if (voState.isFlightSim()) {
305 buffer.append(entry.getConditionFlightSim());
306 }
307 return buffer.toString();
308 }
309
310
311
312
313
314
315 private String getSeparatorType() {
316 switch (userSelectedSeparator) {
317 case 1:
318 return COMMA_SEP;
319 case 2:
320 return TAB_SEP;
321 case 3:
322 return PIPE_SEP;
323 case 4:
324 return SEMICOLON_SEP;
325 default:
326 return COMMA_SEP;
327 }
328 }
329
330
331
332
333
334
335 private String formatDate(Date userDate) {
336 return formatter.format(userDate);
337 }
338 }