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.Required;
31  
32  import com.gisgraphy.domain.repository.IOpenStreetMapDao;
33  import com.gisgraphy.domain.repository.IdGenerator;
34  import com.gisgraphy.domain.valueobject.GisgraphyConfig;
35  import com.gisgraphy.domain.valueobject.ImporterStatus;
36  import com.gisgraphy.domain.valueobject.NameValueDTO;
37  import com.gisgraphy.service.IInternationalisationService;
38  
39  /**
40   * build the fulltext engine in order to use the street fulltext search
41   * 
42   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
43   */
44  public class OpenStreetMapFulltextBuilder implements IImporterProcessor {
45  
46     /**
47      * The logger
48      */
49     	protected static final Logger logger = LoggerFactory.getLogger(OpenStreetMapFulltextBuilder.class);
50      	
51  	private ImporterConfig importerConfig;
52  
53  	private IInternationalisationService internationalisationService;
54  
55  	private IOpenStreetMapDao openStreetMapDao;
56  
57  	/**
58  	 * The paginate step
59  	 */
60  	protected int increment = 10000;
61  	
62  	protected ImporterStatus status = ImporterStatus.WAITING;
63  
64  	private long lineProcessed = 0;
65  
66  	private long numberOfLinesToProcess = 0;
67  
68  
69  	private String statusMessage;
70  
71  	/* (non-Javadoc)
72  	 * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#flushAndClear()
73  	 */
74  	protected void flushAndClear() {
75  		openStreetMapDao.flushAndClear();
76  
77  	}
78  
79  	/* (non-Javadoc)
80  	 * @see com.gisgraphy.domain.geoloc.importer.IGeonamesProcessor#rollback()
81  	 */
82  	public List<NameValueDTO<Integer>> rollback() {
83  		List<NameValueDTO<Integer>> deletedObjectInfo = new ArrayList<NameValueDTO<Integer>>();
84  		logger.info("rollback OpenStreetMapFulltextBuilder ");
85  		resetStatus();
86  		return deletedObjectInfo;
87  	}
88  
89  	/**
90  	 * @param openStreetMapDao the openStreetMapDao to set
91  	 */
92  	@Required
93  	public void setOpenStreetMapDao(IOpenStreetMapDao openStreetMapDao) {
94  		this.openStreetMapDao = openStreetMapDao;
95  	}
96  
97  
98  
99  	public String getCurrentFileName() {
100 		return this.getClass().getSimpleName();
101 	}
102 
103 	public long getNumberOfLinesToProcess() {
104 		return numberOfLinesToProcess;
105 	}
106 
107 	public long getReadFileLine() {
108 		return lineProcessed;
109 	}
110 
111 	public ImporterStatus getStatus() {
112 		return status;
113 	}
114 
115 	public String getStatusMessage() {
116 		return statusMessage;
117 	}
118 
119 	public long getTotalReadLine() {
120 		return lineProcessed;
121 	}
122 
123 	public void process() {
124 		status = ImporterStatus.PROCESSING;
125 		try {
126 			if (!shouldBeSkipped()) {
127 				setup();
128 			    statusMessage = internationalisationService.getString("import.build.openstreetmap.fulltext.searchEngine.count");
129 				numberOfLinesToProcess = new Long(openStreetMapDao.countEstimate()).intValue();
130 				logger.info(numberOfLinesToProcess+" streets will be build for the fulltext engine");
131 				statusMessage = internationalisationService.getString("import.build.openstreetmap.fulltext.searchEngine");
132 				for (long start=IdGenerator.OPENSTREETMAP_GID_NO_CONFLICT;start<=(numberOfLinesToProcess+IdGenerator.OPENSTREETMAP_GID_NO_CONFLICT);start = start+increment){
133 				    long from = start;
134 				    long to = start+increment-1;
135 				    logger.info("paginate build of openstreetmap fulltext engine for streets from "+start+" to "+to);
136 					openStreetMapDao.updateTS_vectorColumnForStreetNameSearchPaginate(from, to);
137 					lineProcessed = Math.min(to-IdGenerator.OPENSTREETMAP_GID_NO_CONFLICT, numberOfLinesToProcess);
138 				}
139 				
140 				this.status = ImporterStatus.PROCESSED;
141 			} else {
142 				logger.info("OpenStreetMapFulltextBuilder will be skiped");
143 				this.status = ImporterStatus.SKIPPED;
144 			}
145 			statusMessage = "";
146 		} catch (Exception e) {
147 			this.statusMessage = "Error during the construction of the openstreetmap fulltext engine : " + e.getMessage();
148 			logger.error(statusMessage,e);
149 			status = ImporterStatus.ERROR;
150 			throw new ImporterException(statusMessage, e);
151 		} 
152 
153 	}
154 
155 	protected void setup() {
156 		statusMessage = internationalisationService.getString("import.message.createIndex");
157 		openStreetMapDao.createFulltextIndexes();
158 	}
159 
160 	public void resetStatus() {
161 		this.lineProcessed = 0;
162 		this.numberOfLinesToProcess = 0;
163 		this.status = ImporterStatus.WAITING;
164 		this.statusMessage = "";
165 	}
166 
167 	public boolean shouldBeSkipped() {
168 		return !importerConfig.isOpenstreetmapImporterEnabled() || !GisgraphyConfig.STREET_SEARCH_FULLTEXT_MODE;
169 	}
170 
171 	public ImporterConfig getImporterConfig() {
172 	    return importerConfig;
173 	}
174 
175 	@Required
176 	public void setImporterConfig(ImporterConfig importerConfig) {
177 	    this.importerConfig = importerConfig;
178 	}
179 
180 	@Required
181 	public void setInternationalisationService(IInternationalisationService internationalisationService) {
182 	    this.internationalisationService = internationalisationService;
183 	}
184 
185 	public IOpenStreetMapDao getOpenStreetMapDao() {
186 	    return openStreetMapDao;
187 	}
188 	
189 }