View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  /**
24   *
25   */
26  package com.gisgraphy.importer;
27  
28  import java.io.File;
29  import java.io.IOException;
30  import java.util.List;
31  
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  /**
36   * Retrieve The Geonames zip code files from a server
37   * 
38   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
39   */
40  public class GeonamesZipCodeFileRetriever extends AbstractFileRetriever {
41  	
42  	protected static final Logger logger = LoggerFactory.getLogger(GeonamesZipCodeFileRetriever.class);
43  
44      /*
45       * (non-Javadoc)
46       * 
47       * @seecom.gisgraphy.domain.geoloc.importer.AbstractFileRetriever#
48       * getDownloadDirectory()
49       */
50      public String getDownloadDirectory() {
51  	return importerConfig.getGeonamesZipCodeDir();
52      }
53  
54      /*
55       * (non-Javadoc)
56       * 
57       * @see
58       * com.gisgraphy.domain.geoloc.importer.AbstractFileRetriever#getDownloadBaseUrl
59       * ()
60       */
61      public String getDownloadBaseUrl() {
62  	return importerConfig.getGeonamesZipCodeDownloadURL();
63      }
64  
65  
66      
67      /*
68       * (non-Javadoc)
69       * 
70       * @see
71       * com.gisgraphy.domain.geoloc.importer.AbstractFileRetriever#decompressFiles
72       * ()
73       */
74      public void decompressFiles() throws IOException  {
75  	File[] filesToUnZip = getFilesToDecompress();
76  	for (int i = 0; i < filesToUnZip.length; i++) {
77  	    ImporterHelper.unzipFile(filesToUnZip[i]);
78  	}
79  
80  	// for log purpose
81  	File[] filesToImport = ImporterHelper.listCountryFilesToImport(getDownloadDirectory());
82  
83  	for (int i = 0; i < filesToImport.length; i++) {
84  	    logger.info("the files " + filesToImport[i].getName() + " will be imported");
85  	}
86      }
87  
88      /*
89       * (non-Javadoc)
90       * 
91       * @see
92       * com.gisgraphy.domain.geoloc.importer.AbstractFileRetriever#shouldBeSkipped
93       * ()
94       */
95      @Override
96      public boolean shouldBeSkipped() {
97  	return !importerConfig.isGeonamesImporterEnabled();
98      }
99  
100     /* (non-Javadoc)
101      * @see com.gisgraphy.domain.geoloc.importer.AbstractFileRetriever#getFilesToDownload()
102      */
103     @Override
104     List<String> getFilesToDownload() {
105 	return importerConfig.getGeonamesDownloadFilesListFromOption();
106     }
107 
108     @Override
109     public File[] getFilesToDecompress() throws IOException {
110 	return ImporterHelper.listZipFiles(getDownloadDirectory());
111     }
112 
113     @Override
114     public boolean isFileNotFoundTolerant() {
115 	return true;
116     }
117 
118 }