1   package org.wcb.model.service.impl;
2   
3   import com.Ostermiller.util.CSVParser;
4   
5   import java.io.*;
6   import java.util.List;
7   import java.util.logging.Level;
8   import java.util.logging.Logger;
9   
10  import org.wcb.model.vo.hibernate.Logbook;
11  import org.wcb.model.vo.hibernate.AirportBO;
12  import org.wcb.model.vo.LogImportVO;
13  import org.wcb.model.service.ILogbookService;
14  import org.wcb.model.service.IImportService;
15  import org.wcb.exception.ServiceException;
16  import org.wcb.resources.ImportAirportDataFile;
17  import org.wcb.resources.ImportLogbookDataFile;
18  
19  /**
20   * <small>
21   * Copyright (c)  2006  wbogaardt.
22   * Permission is granted to copy, distribute and/or modify this document
23   * under the terms of the GNU Free Documentation License, Version 1.2
24   * or any later version published by the Free Software Foundation;
25   * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover
26   * Texts.  A copy of the license is included in the section entitled "GNU
27   * Free Documentation License".
28   * <p/>
29   * $File:  $ <br>
30   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Apr 3, 2006 10:12:33 AM $ <br>
31   * </small>
32   *
33   * @author wbogaardt
34   * @version 1
35   *          Date: Apr 3, 2006
36   *          Time: 10:12:33 AM
37   */
38  
39  public class ImportService implements IImportService {
40  
41      private Logger LOG = Logger.getLogger(ImportService.class.getName());
42  
43      private ILogbookService service;
44  
45      public ImportService() {
46      }
47  
48      public void setLogService(ILogbookService svc) {
49          this.service = svc;
50      }
51  
52      public ILogbookService getLogService() {
53          return this.service;
54      }
55  
56      /**
57       * This imports a file from a comma delimited format. Look at the export service
58       * to see what that file format is.
59       * @param inFile File object, which the user selected from the ImportFileDialog box
60       * @param vo The logb import object helps determine order in the csv file.
61       * @return true file was imported successfully false it was not.
62       * @throws ServiceException
63       */
64      public boolean processFile(File inFile, LogImportVO vo) throws ServiceException{
65          try {
66              BufferedReader in = new BufferedReader(new FileReader(inFile.getAbsolutePath()));
67              ImportLogbookDataFile utilityParser = new ImportLogbookDataFile();
68              List<Logbook> entries = utilityParser.process(in, vo);
69              service.saveLogBookEntry(entries);
70              return true;
71          }
72          catch (IOException ioe) {
73              LOG.log(Level.WARNING, "Unable to parse and read file " + inFile.getAbsolutePath(), ioe);
74              return false;
75          }
76      }
77  
78      /**
79       * This process a file from http://www.transtats.bts.gov/DL_SelectFields.asp?Table_ID=288&DB_Short_Name=Aviation%20Support%20Tables
80       * the Bureau of Transportation Statistics.
81       * @param inFile http://www.transtats.bts.gov/DL_SelectFields.asp?Table_ID=288&DB_Short_Name=Aviation%20Support%20Tables
82       * @return  true that all data was processed
83       * @throws ServiceException
84       */
85      public boolean processAirportFile(File inFile) throws ServiceException {
86          try {
87              BufferedReader in = new BufferedReader(new FileReader(inFile.getAbsolutePath()));
88              String[][] result = CSVParser.parse(in, ',');
89              ImportAirportDataFile utilityParser = new ImportAirportDataFile();
90              List<AirportBO> airports = utilityParser.marshall(result);
91              service.saveAirports(airports);
92              return true;
93          }
94          catch (IOException ioe) {
95              LOG.log(Level.WARNING, "Unable to parse and read file " + inFile.getAbsolutePath(), ioe);
96              return false;
97          }
98      }
99  }