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 files from a server
37   * 
38   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
39   */
40  public class GeonamesFileRetriever extends AbstractFileRetriever {
41  	
42  	protected static final Logger logger = LoggerFactory.getLogger(GeonamesFileRetriever.class);
43  
44      /*
45       * (non-Javadoc)
46       * 
47       * @seecom.gisgraphy.domain.geoloc.importer.AbstractFileRetriever#
48       * getDownloadDirectory()
49       */
50      public String getDownloadDirectory() {
51  	return importerConfig.getGeonamesDir();
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.getGeonamesDownloadURL();
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  	    try {
78  			ImporterHelper.unzipFile(filesToUnZip[i]);
79  		} catch (Exception e) {
80  			logger.error(filesToUnZip[i].getAbsolutePath()+" is not a valid zip file");
81  		}
82  	}
83  
84  	// for log purpose
85  	File[] filesToImport = ImporterHelper.listCountryFilesToImport(getDownloadDirectory());
86  
87  	for (int i = 0; i < filesToImport.length; i++) {
88  	    logger.info("the files " + filesToImport[i].getName() + " will be imported");
89  	}
90      }
91  
92      /*
93       * (non-Javadoc)
94       * 
95       * @see
96       * com.gisgraphy.domain.geoloc.importer.AbstractFileRetriever#shouldBeSkipped
97       * ()
98       */
99      @Override
100     public boolean shouldBeSkipped() {
101 	return !importerConfig.isGeonamesImporterEnabled();
102     }
103 
104     /* (non-Javadoc)
105      * @see com.gisgraphy.domain.geoloc.importer.AbstractFileRetriever#getFilesToDownload()
106      */
107     @Override
108     List<String> getFilesToDownload() {
109 	return importerConfig.getGeonamesDownloadFilesListFromOption();
110     }
111 
112     @Override
113     public File[] getFilesToDecompress() throws IOException {
114 	return ImporterHelper.listZipFiles(getDownloadDirectory());
115     }
116 
117     @Override
118     public boolean isFileNotFoundTolerant() {
119 	return false;
120     }
121 
122 }