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.webapp.action;
24  
25  import java.util.Arrays;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.ResourceBundle;
29  import java.util.Set;
30  
31  import org.reflections.Reflections;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  import org.springframework.beans.factory.annotation.Required;
35  
36  import com.gisgraphy.domain.geoloc.entity.GisFeature;
37  import com.gisgraphy.domain.valueobject.GisgraphyConfig;
38  import com.gisgraphy.fulltext.IFullTextSearchEngine;
39  import com.gisgraphy.helper.PropertiesHelper;
40  import com.gisgraphy.importer.IImporterManager;
41  import com.gisgraphy.importer.ImporterConfig;
42  import com.gisgraphy.importer.ImporterManager;
43  import com.gisgraphy.importer.ImporterMetaDataException;
44  import com.opensymphony.xwork2.ActionSupport;
45  
46  /**
47   * Action that retrieve the configuration and
48   * 
49   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
50   * @see ImporterManager
51   */
52  public class ImportConfirmAction extends ActionSupport {
53  
54  	protected static final String IMPORT_VIEW_NAME = "import";
55  	protected static final String CHECK_CONFIG_VIEW_NAME = "checkconfig";
56  	protected static final String ERRORCONFIG = "errorconfig";
57  
58  	private static Logger logger = LoggerFactory.getLogger(ImportConfirmAction.class);
59  
60  	/**
61  	 * Default serial ID
62  	 */
63  	private static final long serialVersionUID = 2387732133217655558L;
64  
65  	private ImporterConfig importerConfig;
66  
67  	private IImporterManager importerManager;
68  
69  	private String errorMessage = "";
70  
71  	public static final String STATUS = "status";
72  
73  	public static final String STEP_BASE_VIEW_NAME = "importWizardStep";
74  
75  	private IFullTextSearchEngine fullTextSearchEngine;
76  
77  	private boolean importallcountries = true;
78  
79  	private boolean importallplacetype = true;
80  
81  	private List<String> countryCodes;
82  
83  	private List<String> placetypes;
84  
85  	private int step = 1;
86  	
87  	private boolean configGotProblems= false;
88  	
89  
90  	/*
91  	 * (non-Javadoc)
92  	 * 
93  	 * @see com.opensymphony.xwork2.ActionSupport#execute()
94  	 */
95  	@Override
96  	public String execute() throws Exception {
97  		boolean alreadyDone;
98  		try {
99  			alreadyDone = importerManager.isAlreadyDone();
100 			if (importerManager.isInProgress() || alreadyDone) {
101 				return STATUS;
102 			}
103 		} catch (ImporterMetaDataException e) {
104 			errorMessage = e.getMessage();
105 			return ERRORCONFIG;
106 		}
107 		return super.execute();
108 	}
109 	
110 	public String checkConfig(){
111 		return CHECK_CONFIG_VIEW_NAME;
112 		
113 	}
114 
115 	public String[] getPlacetypesList() {
116 		Reflections reflections = new Reflections("com.gisgraphy.domain.geoloc.entity");
117 
118 		Set<Class<? extends GisFeature>> allClasses = 
119 				reflections.getSubTypesOf(GisFeature.class);
120 		String[] placeTypes;
121 		if (allClasses!=null){
122 			placeTypes = new String[allClasses.size()];
123 			int i=0;
124 			for (Class clazz: allClasses){
125 				placeTypes[i]=clazz.getSimpleName();
126 				i++;
127 			}
128 		} else {
129 			placeTypes = new String[0];
130 		}
131 		 Arrays.sort(placeTypes);
132 		 return placeTypes;
133 	}
134 
135 	public String doImport() throws Exception {
136 		setConfig();
137 		if (isConfigOk()){
138 			return IMPORT_VIEW_NAME;
139 		} else {
140 			step=8;
141 			return super.execute();
142 		}
143 	}
144 
145 	protected void setConfig() {
146 		if (importallcountries) {
147 			importerConfig.setOpenStreetMapFilesToDownload(ImporterConfig.OPENSTREETMAP_DEFAULT_FILES_TO_DOWNLOAD);
148 			importerConfig.setOpenStreetMapHouseNumberFilesToDownload(ImporterConfig.OPENSTREETMAP_DEFAULT_FILES_TO_DOWNLOAD);
149 			importerConfig.setOpenStreetMapCitiesFilesToDownload(ImporterConfig.OPENSTREETMAP_DEFAULT_FILES_TO_DOWNLOAD);
150 			importerConfig.setOpenStreetMapPoisFilesToDownload(ImporterConfig.OPENSTREETMAP_DEFAULT_FILES_TO_DOWNLOAD);
151 			importerConfig.setGeonamesFilesToDownload(ImporterConfig.GEONAMES_DEFAULT_FILES_TO_DOWNLOAD);
152 			
153 		} else {
154 			if (getCountryCodes() != null && getCountryCodes().size() > 0) {
155 				StringBuffer geonamesCountryFileList = new StringBuffer();
156 				StringBuffer openstreetmapCountryFileList = new StringBuffer();
157 				for (String country : countryCodes) {
158 					if (country != null && country.length() == 2) {
159 						geonamesCountryFileList.append(country.toUpperCase()).append(ImporterConfig.GEONAMES_COMPRESSED_FILE_EXTENSION).append(ImporterConfig.OPTION_SEPARATOR);
160 						openstreetmapCountryFileList.append(country.toUpperCase()).append(ImporterConfig.OPENSTREETAMP_COMPRESSED_FILE_EXTENSION).append(ImporterConfig.OPTION_SEPARATOR);
161 					}
162 				}
163 				geonamesCountryFileList.append(ImporterConfig.GEONAMES_ALTERNATENAME_ZIP_FILE);
164 				String openstreetmapCountryFileListAsString = openstreetmapCountryFileList.toString();
165 				if (openstreetmapCountryFileListAsString.endsWith(ImporterConfig.OPTION_SEPARATOR)){
166 					openstreetmapCountryFileListAsString = openstreetmapCountryFileListAsString.substring(0, openstreetmapCountryFileListAsString.length() - 1);
167 				}
168 				String geonamesFileList = geonamesCountryFileList.toString();
169 				importerConfig.setOpenStreetMapFilesToDownload(openstreetmapCountryFileListAsString);
170 				importerConfig.setOpenStreetMapHouseNumberFilesToDownload(openstreetmapCountryFileListAsString);
171 				importerConfig.setOpenStreetMapCitiesFilesToDownload(openstreetmapCountryFileListAsString);
172 				importerConfig.setOpenStreetMapPoisFilesToDownload(openstreetmapCountryFileListAsString);
173 				importerConfig.setGeonamesFilesToDownload(geonamesFileList);
174 				// TODO +log
175 			} else {
176 				logger.info("Import all countries is false but no country list recieved,set list to default");
177 				importerConfig.setOpenStreetMapFilesToDownload(ImporterConfig.OPENSTREETMAP_DEFAULT_FILES_TO_DOWNLOAD);
178 				importerConfig.setOpenStreetMapHouseNumberFilesToDownload(ImporterConfig.OPENSTREETMAP_DEFAULT_FILES_TO_DOWNLOAD);
179 				importerConfig.setOpenStreetMapCitiesFilesToDownload(ImporterConfig.OPENSTREETMAP_DEFAULT_FILES_TO_DOWNLOAD);
180 				importerConfig.setOpenStreetMapPoisFilesToDownload(ImporterConfig.OPENSTREETMAP_DEFAULT_FILES_TO_DOWNLOAD);
181 				importerConfig.setGeonamesFilesToDownload(ImporterConfig.GEONAMES_DEFAULT_FILES_TO_DOWNLOAD);
182 			}
183 		}
184 		logger.info("openstreetmap files to download wizard : "+importerConfig.getOpenStreetMapFilesToDownload());
185 		logger.info("geonames files to download wizard : "+importerConfig.getGeonamesFilesToDownload());
186 		//placetype section
187 		if (importallplacetype) {
188 			importerConfig.setAcceptRegExString(ImporterConfig.ACCEPT_ALL_REGEX_OPTION);
189 		} else {
190 			if (getPlacetypes() != null && getPlacetypes().size() > 0) {
191 				StringBuffer placetypeOption = new StringBuffer();
192 				for(String placetype:getPlacetypes()){
193 					placetypeOption.append(placetype.toUpperCase()).append(ImporterConfig.REGEXP_SEPARATOR);
194 				}
195 				String optionAsString = placetypeOption.toString();
196 				if (optionAsString.endsWith(ImporterConfig.REGEXP_SEPARATOR)){
197 					optionAsString = optionAsString.substring(0, optionAsString.length() - 1);
198 				}
199 				importerConfig.setAcceptRegExString(optionAsString);
200 			} else {
201 				logger.info("Import all placetype is false but no placetype list recieved,set list to default");
202 				importerConfig.setAcceptRegExString(ImporterConfig.ACCEPT_ALL_REGEX_OPTION);
203 			}
204 		}
205 		logger.info("placetypes wizard :  "+importerConfig.getAcceptRegExString());
206 	}
207 
208 	public int getNumberOfProcessors() {
209 		int numberOfProcessors = Runtime.getRuntime().availableProcessors();
210 		logger.info(numberOfProcessors + " processor(s) has been detected");
211 		return numberOfProcessors;
212 	}
213 
214 	/**
215 	 * @return the importerConfig
216 	 */
217 	public ImporterConfig getImporterConfig() {
218 		return importerConfig;
219 	}
220 
221 	/**
222 	 * @return true if the directory with the file to import exists and is
223 	 *         accessible
224 	 */
225 	public boolean isDownloadDirectoryAccessible() {
226 		return importerConfig.isGeonamesDownloadDirectoryAccessible();
227 	}
228 
229 	/**
230 	 * @return true if the directory with the file to import exists and is
231 	 *         accessible
232 	 */
233 	public boolean isOpenStreetMapDownloadDirectoryAccessible() {
234 		return importerConfig.isOpenStreetMapDownloadDirectoryAccessible();
235 	}
236 
237 	/**
238 	 * @return true if the regexp of the feature class/ code are correct
239 	 */
240 	public boolean isRegexpCorrects() {
241 		return importerConfig.isRegexpCorrects();
242 	}
243 
244 	/**
245 	 * @return true if he fulltext search engine is alive
246 	 */
247 	public boolean isFulltextSearchEngineAlive() {
248 		return fullTextSearchEngine.isAlive();
249 	}
250 
251 	/**
252 	 * @return true if he fulltext search engine is alive
253 	 */
254 	public String getFulltextSearchEngineURL() {
255 		return fullTextSearchEngine.getURL();
256 	}
257 
258 	/**
259 	 * @return true if the Geonames importer is enabled
260 	 */
261 	public boolean isGeonamesImporterEnabled() {
262 		return importerConfig.isGeonamesImporterEnabled();
263 	}
264 	
265 
266 	/**
267 	 * Enable / Disable Geonames importer
268 	 */
269 	public void setGeonamesImporterEnabled(boolean geonamesImporterEnabled) {
270 		importerConfig.setGeonamesImporterEnabled(geonamesImporterEnabled);
271 	}
272 
273 	/**
274 	 * @return true if he openStreetMap importer is enabled
275 	 */
276 	public boolean isOpenStreetMapImporterEnabled() {
277 		return importerConfig.isOpenstreetmapImporterEnabled();
278 	}
279 
280 	/**
281 	 * Enable / Disable OpenStreetMap importer
282 	 */
283 	public void setOpenStreetMapImporterEnabled(boolean openStreetMapImporter) {
284 		importerConfig.setOpenstreetmapImporterEnabled(openStreetMapImporter);
285 	}
286 	
287 
288 	/**
289 	 * @return true if the house number importer is enabled
290 	 */
291 	public boolean isHousenumberImporterEnabled() {
292 		return importerConfig.isOpenstreetmapHouseNumberImporterEnabled();
293 	}
294 	
295 	/**
296 	 * Enable / Disable housenumber importer
297 	 */
298 	public void setHousenumberImporterEnabled(boolean housenumberImporterEnabled) {
299 		importerConfig.setOpenstreetmapHouseNumberImporterEnabled(housenumberImporterEnabled);
300 	}
301 	
302 	/**
303 	 * @return true if the quattroshapes importer is enabled
304 	 */
305 	public boolean isQuattroshapesImporterEnabled() {
306 		return importerConfig.isQuattroshapesImporterEnabled();
307 	}
308 	
309 	/**
310 	 * Enable / Disable quattroshpaes importer
311 	 */
312 	public void setQuattroshapesImporterEnabled(boolean quattroshapesImporterEnabled) {
313 		importerConfig.setQuattroshapesImporterEnabled(quattroshapesImporterEnabled);
314 	}
315 
316 	/**
317 	 * @param importerManager
318 	 *            the importerManager to set
319 	 */
320 	@Required
321 	public void setImporterManager(IImporterManager importerManager) {
322 		this.importerManager = importerManager;
323 	}
324 
325 	/**
326 	 * @param fullTextSearchEngine
327 	 *            the fullTextSearchEngine to set
328 	 */
329 	@Required
330 	public void setFullTextSearchEngine(IFullTextSearchEngine fullTextSearchEngine) {
331 		this.fullTextSearchEngine = fullTextSearchEngine;
332 	}
333 
334 	@Required
335 	public void setImporterConfig(ImporterConfig importerConfig) {
336 		this.importerConfig = importerConfig;
337 	}
338 
339 	public Map<String, String> getConfigValuesMap() {
340 		return PropertiesHelper.convertBundleToMap(ResourceBundle.getBundle(GisgraphyConfig.ENVIRONEMENT_PROPERTIES_FILE));
341 	}
342 
343 	public String getErrorMessage() {
344 		return errorMessage;
345 	}
346 
347 	public boolean isImportallcountries() {
348 		return importallcountries;
349 	}
350 
351 	public void setImportallcountries(boolean importallcountries) {
352 		this.importallcountries = importallcountries;
353 	}
354 
355 	public boolean isImportallplacetype() {
356 		return importallplacetype;
357 	}
358 
359 	public void setImportallplacetype(boolean importallplacetype) {
360 		this.importallplacetype = importallplacetype;
361 	}
362 
363 	public void setPlacetypes(List<String> placetypes) {
364 		this.placetypes = placetypes;
365 	}
366 
367 	public List<String> getPlacetypes() {
368 		return placetypes;
369 	}
370 
371 	public List<String> getCountryCodes() {
372 		return countryCodes;
373 	}
374 
375 	public void setCountryCodes(List<String> countryCodes) {
376 		this.countryCodes = countryCodes;
377 	}
378 
379 	public int getStep() {
380 		return step;
381 	}
382 
383 	public void setStep(int step) {
384 		this.step = step;
385 	}
386 
387 	public boolean isImportEmbededAlternateNames() {
388 		return importerConfig.isImportGisFeatureEmbededAlternateNames();
389 	}
390 
391 	public void setImportEmbededAlternateNames(boolean importEmbededAlternateNames) {
392 		importerConfig.setImportGisFeatureEmbededAlternateNames(importEmbededAlternateNames);
393 	}
394 
395 	public boolean isFillIsInEnabled() {
396 		return importerConfig.isOpenStreetMapFillIsIn();
397 	}
398 
399 	public void setFillIsInEnabled(boolean fillIsInEnabled) {
400 		importerConfig.setOpenStreetMapFillIsIn(fillIsInEnabled);
401 	}
402 
403 	public boolean isRetrieveFileEnable() {
404 		return importerConfig.isRetrieveFiles();
405 	}
406 
407 	public void setRetrieveFileEnable(boolean isRetrieveFileEnable) {
408 		importerConfig.setRetrieveFiles(isRetrieveFileEnable);
409 	}
410 	
411 	
412 	public boolean isConfigOk(){
413 		boolean configOK =(importerConfig.isConfigCorrectForImport() && isFulltextSearchEngineAlive());
414 		configGotProblems = ! configOK;
415 		return configOK;
416 		
417 	}
418 
419 	public boolean isConfigGotProblems() {
420 		return configGotProblems;
421 	}
422 
423 	public void setConfigGotProblems(boolean configGotProblems) {
424 		this.configGotProblems = configGotProblems;
425 	}
426 
427 	
428 
429 }