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.util.ArrayList;
26  import java.util.List;
27  
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  import org.springframework.beans.factory.annotation.Autowired;
31  
32  import com.gisgraphy.domain.geoloc.entity.GisFeature;
33  import com.gisgraphy.domain.repository.IGisDao;
34  import com.gisgraphy.domain.valueobject.ImporterStatus;
35  import com.gisgraphy.domain.valueobject.NameValueDTO;
36  import com.gisgraphy.service.IInternationalisationService;
37  
38  /**
39   * Create the required index for all the Geonames databases
40   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
41   *
42   */
43  public class GeonamesDatabaseIndexer implements IImporterProcessor {
44      
45      
46      public static final String DEFAULT_CURRENT_FILENAME = "?";
47  
48      /**
49       * The logger
50       */
51      protected static final Logger logger = LoggerFactory
52  	    .getLogger(GeonamesDatabaseIndexer.class);
53      
54      @Autowired
55      protected IGisDao<? extends GisFeature>[] daos;
56      
57      @Autowired
58      protected ImporterConfig importerConfig;
59      
60      @Autowired
61      protected IInternationalisationService internationalisationService;
62      
63      private IGisDao<? extends GisFeature> currentDao;
64      
65      private int numberOfDaoThatHaveBeenIndexed = 0;
66      
67      private ImporterStatus status = ImporterStatus.WAITING;
68      
69      private String statusMessage = "";
70      
71      
72  
73      public String getCurrentFileName() {
74  	return currentDao == null ? DEFAULT_CURRENT_FILENAME :this.currentDao.getPersistenceClass().getSimpleName();
75      }
76  
77      public long getNumberOfLinesToProcess() {
78  	return this.daos == null ? 0 :this.daos.length;
79      }
80  
81      public long getReadFileLine() {
82  	return numberOfDaoThatHaveBeenIndexed;
83      }
84  
85      public ImporterStatus getStatus() {
86  	return status;
87      }
88  
89      public String getStatusMessage() {
90  	return statusMessage;
91      }
92  
93      public long getTotalReadLine() {
94  	return numberOfDaoThatHaveBeenIndexed;
95      }
96  
97      public void process() {
98  	try {
99  	    if (shouldBeSkipped()){
100 		this.status = ImporterStatus.SKIPPED;
101 		return;
102 	    }
103 	    this.status = ImporterStatus.PROCESSING;
104 	    setup();
105 	   for (int i=0; i < daos.length;i++){
106 	       currentDao=daos[i];
107 	       statusMessage = internationalisationService.getString("import.message.createIndex",new String[]{daos[i].getPersistenceClass().getSimpleName()});
108 	       daos[i].createGISTIndexForLocationColumn();
109 	       numberOfDaoThatHaveBeenIndexed++;
110 	   }
111 	    
112 	this.status = ImporterStatus.PROCESSED;
113 	this.statusMessage="";
114     this.currentDao = null;
115 	} catch (Exception e) {
116 	    this.status = ImporterStatus.ERROR;
117 	    this.statusMessage = "The import is done but performance may not be optimal because an error occurred when creating spatial indexes for geonames in DAO "
118 		    + getCurrentFileName() + "(maybe you haven't the SQL rights or the indexes are already created) : " + e.getCause();
119 	    logger.error(statusMessage,e);
120 	   // throw new ImporterException(statusMessage, e.getCause());
121 	} finally {
122 	    try {
123 		tearDown();
124 	    } catch (Exception e) {
125 		this.status = ImporterStatus.ERROR;
126 		this.statusMessage = "An error occured on teardown :"+e;
127 		logger.error(statusMessage,e);
128 	    }
129 	}
130 
131     }
132 
133     /**
134      * Template method that can be override. This method is called before the
135      * process start. it is not called for each file processed.
136      */
137     protected void setup() {
138     }
139     /**
140      * Template method that can be override. This method is called after the end
141      * of the process. it is not called for each file processed.
142      * You should always call super.tearDown() when you override this method
143      */
144     protected void tearDown() {
145     }
146 
147     /* (non-Javadoc)
148      * @see com.gisgraphy.domain.geoloc.importer.IImporterProcessor#rollback()
149      */
150     public List<NameValueDTO<Integer>> rollback() {
151 	return new ArrayList<NameValueDTO<Integer>>();
152     }
153 
154     /* (non-Javadoc)
155      * @see com.gisgraphy.domain.geoloc.importer.IImporterProcessor#shouldBeSkipped()
156      */
157     public boolean shouldBeSkipped() {
158 	return !importerConfig.isGeonamesImporterEnabled();
159     }
160 
161     /**
162      * @param importerConfig the importerConfig to set
163      */
164     public void setImporterConfig(ImporterConfig importerConfig) {
165         this.importerConfig = importerConfig;
166     }
167 
168     /* (non-Javadoc)
169      * @see com.gisgraphy.domain.geoloc.importer.IImporterProcessor#resetStatus()
170      */
171     public void resetStatus() {
172 	numberOfDaoThatHaveBeenIndexed = 0;
173 	status = ImporterStatus.WAITING;
174 	statusMessage = "";
175 	currentDao = null;
176     }
177 
178   
179 
180 }