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  package com.gisgraphy.importer;
24  
25  import java.io.File;
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  import org.hibernate.FlushMode;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  import org.springframework.beans.factory.annotation.Required;
33  
34  import com.gisgraphy.domain.geoloc.entity.Country;
35  import com.gisgraphy.domain.geoloc.entity.Language;
36  import com.gisgraphy.domain.repository.ICountryDao;
37  import com.gisgraphy.domain.repository.ILanguageDao;
38  import com.gisgraphy.domain.valueobject.GISSource;
39  import com.gisgraphy.domain.valueobject.NameValueDTO;
40  import com.gisgraphy.helper.GeolocHelper;
41  
42  /**
43   * Import the Countries.
44   * 
45   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
46   */
47  public class GeonamesCountryImporter extends AbstractSimpleImporterProcessor {
48  	
49  	protected static final Logger logger = LoggerFactory.getLogger(GeonamesCountryImporter.class);
50  
51      private ICountryDao countryDao;
52  
53      private ILanguageDao languageDao;
54  
55      /**
56       * Default constructor
57       */
58      public GeonamesCountryImporter() {
59  	super();
60      }
61  
62      /*
63       * (non-Javadoc)
64       * 
65       * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#processData(java.lang.String)
66       */
67      @Override
68      protected void processData(String line) {
69  	String[] fields = line.split("\t");
70  
71  	/*
72  	 * line table has the following fields :
73  	 * --------------------------------------------------- 0 ISO 1 ISO3 2
74  	 * ISO-Numeric 3 fips 4 Country name 5 Capital 6 Area(in sq km) 7
75  	 * Population 8 Continent 9 tld 10 CurrencyCode 11 CurrencyName 12 Phone
76  	 * 13 Postal Code Format 14 Postal Code Regex 15 Languages 16 geonameid
77  	 * 17 neighbours 18 EquivalentFipsCode
78  	 */
79  	// checkNumberOfColumn(fields);
80  	Country country = null;
81  	if (!isEmptyField(fields, 0, true) && !isEmptyField(fields, 1, true)
82  		&& !isEmptyField(fields, 2, true)) {
83  	    country = new Country(fields[0], fields[1], new Integer(fields[2])
84  		    .intValue());
85  	    country.setLocation(GeolocHelper.createPoint(0F, 0F));
86  	    country.setSource(GISSource.GEONAMES);
87  	} else {
88  	    logger.warn("country " + fields[4] + " requires iso3166 values");
89  	}
90  	
91  	country.setCountryCode(fields[0]);
92  
93  	if (!isEmptyField(fields, 16, false) && !fields[16].equals("0")) {
94  	    country.setFeatureId(new Long(fields[16]));
95  	} else {
96  	    logger.warn("country " + fields[4] + " has no featureid");
97  	    country.setFeatureId((++nbGisInserted) * -1);
98  	}
99  
100 	if (!isEmptyField(fields, 4, true)) {
101 	    country.setName(fields[4].trim());
102 	}
103 
104 	if (!isEmptyField(fields, 3, false)) {
105 	    country.setFipsCode(fields[3]);
106 	}
107 	if (!isEmptyField(fields, 5, false)) {
108 	    country.setCapitalName(fields[5].trim());
109 	}
110 	if (!isEmptyField(fields, 6, false)) {
111 	    country.setArea(Double.parseDouble(fields[6].replace(",", "")
112 		    .trim()));
113 	}
114 	if (!isEmptyField(fields, 7, false)) {
115 	    country
116 		    .setPopulation(new Integer(fields[7].replace(",", "")
117 			    .trim()));
118 	}
119 	if (!isEmptyField(fields, 8, false)) {
120 	    country.setContinent(fields[8]);
121 	}
122 	if (!isEmptyField(fields, 9, false)) {
123 	    country.setTld(fields[9].toLowerCase());
124 	}
125 
126 	if (!isEmptyField(fields, 10, false)) {
127 	    country.setCurrencyCode(fields[10]);
128 	}
129 
130 	if (!isEmptyField(fields, 11, false)) {
131 	    country.setCurrencyName(fields[11]);
132 	}
133 	if (!isEmptyField(fields, 12, false)) {
134 	    country.setPhonePrefix(fields[12]);
135 	}
136 	if (!isEmptyField(fields, 13, false)) {
137 	    country.setPostalCodeMask(fields[13]);
138 	}
139 	if (!isEmptyField(fields, 14, false)) {
140 	    country.setPostalCodeRegex(fields[14]);
141 	}
142 	if (!isEmptyField(fields, 15, false)) {
143 	    String[] languages = fields[15].split(",");
144 	    for (String element : languages) {
145 		String lang = element.toUpperCase();
146 		if (lang.indexOf("-") != -1) {
147 		    lang = lang.substring(0, lang.indexOf("-"));
148 		}
149 		Language language = this.languageDao.getByIso639Code(lang);
150 		if (language == null) {
151 		    throw new RuntimeException(
152 			    "could not retrieve language with code " + lang);
153 		}
154 		country.addSpokenLanguage(language);
155 	    }
156 	}
157 
158 	if (!isEmptyField(fields, 18, false)) {
159 	    country.setEquivalentFipsCode(fields[18]);
160 	}
161 
162 	this.countryDao.save(country);
163 
164     }
165     
166     @Override
167     protected void onFileProcessed(File file) {
168     	//we don't want to rename files so we overide the method
169     }
170     
171     /* (non-Javadoc)
172      * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#shouldBeSkiped()
173      */
174     @Override
175     public boolean shouldBeSkipped() {
176     	//should never be skiped
177     	return false;
178     }
179 
180     /*
181      * (non-Javadoc)
182      * 
183      * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#shouldIgnoreFirstLine()
184      */
185     @Override
186     protected boolean shouldIgnoreFirstLine() {
187 	return false;
188     }
189 
190     /*
191      * (non-Javadoc)
192      * 
193      * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#shouldIgnoreComments()
194      */
195     @Override
196     protected boolean shouldIgnoreComments() {
197 	return true;
198     }
199 
200     /*
201      * (non-Javadoc)
202      * 
203      * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#setCommitFlushMode()
204      */
205     @Override
206     protected void setCommitFlushMode() {
207 	this.languageDao.setFlushMode(FlushMode.COMMIT);
208 	this.countryDao.setFlushMode(FlushMode.COMMIT);
209 
210     }
211 
212     /*
213      * (non-Javadoc)
214      * 
215      * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#flushAndClear()
216      */
217     @Override
218     protected void flushAndClear() {
219 	this.languageDao.flushAndClear();
220 	this.countryDao.flushAndClear();
221     }
222 
223     /*
224      * (non-Javadoc)
225      * 
226      * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#getNumberOfColumns()
227      */
228     @Override
229     protected int getNumberOfColumns() {
230 	return 4;
231     }
232 
233     /**
234      * @param countryDao
235      *                The countryDao to set
236      */
237     @Required
238     public void setCountryDao(ICountryDao countryDao) {
239 	this.countryDao = countryDao;
240     }
241 
242     /**
243      * @param languageDao
244      *                The languageDao to set
245      */
246     @Required
247     public void setLanguageDao(ILanguageDao languageDao) {
248 	this.languageDao = languageDao;
249     }
250 
251     /*
252      * (non-Javadoc)
253      * 
254      * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#getFiles()
255      */
256     @Override
257     protected File[] getFiles() {
258 	File[] files = new File[1];
259 	files[0] = new File(importerConfig.getGeonamesDir()
260 		+ importerConfig.getCountriesFileName());
261 	return files;
262     }
263 
264     /*
265      * (non-Javadoc)
266      * 
267      * @see com.gisgraphy.domain.geoloc.importer.IGeonamesProcessor#rollback()
268      */
269     public List<NameValueDTO<Integer>> rollback() {
270 	List<NameValueDTO<Integer>> deletedObjectInfo = new ArrayList<NameValueDTO<Integer>>();
271 	logger.info("deleting countries...");
272 	int deletedCountries = countryDao.deleteAll();
273 	if (deletedCountries != 0) {
274 	    deletedObjectInfo.add(new NameValueDTO<Integer>(Country.class
275 		    .getSimpleName(), deletedCountries));
276 	}
277 	logger.info(deletedCountries + " countries have been deleted");
278 	resetStatus();
279 	return deletedObjectInfo;
280     }
281 
282 }