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.domain.geoloc.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.springframework.beans.factory.annotation.Required;
31  
32  import com.gisgraphy.domain.geoloc.entity.Adm;
33  import com.gisgraphy.domain.repository.IAdmDao;
34  import com.gisgraphy.domain.valueobject.GISSource;
35  import com.gisgraphy.domain.valueobject.NameValueDTO;
36  import com.gisgraphy.helper.GeolocHelper;
37  
38  /**
39   * Import the Adm of level 3 file. It is the first step of the adm3 import
40   * process, the import will be complete when all the datastore object will be
41   * updated by the {@link GeonamesFeatureImporter}
42   * 
43   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
44   */
45  public class GeonamesAdm3Importer extends AbstractImporterProcessor {
46  
47      private IAdmDao admDao;
48  
49      /**
50       * Default constructor
51       */
52      public GeonamesAdm3Importer() {
53  	super();
54      }
55  
56      /*
57       * (non-Javadoc)
58       * 
59       * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#processData(java.lang.String)
60       */
61      @Override
62      protected void processData(String line) {
63  	String[] fields = line.split("\t");
64  
65  	/*
66  	 * line table has the following fields :
67  	 * --------------------------------------------------- 0 : code ; 1 name
68  	 */
69  	checkNumberOfColumn(fields);
70  	Adm adm3 = new Adm(3);
71  	adm3.setLocation(GeolocHelper.createPoint(0F, 0F));
72  	adm3.setFeatureId((++AbstractImporterProcessor.nbGisInserted) * -1);
73  	adm3.setSource(GISSource.GEONAMES);
74  
75  	if (!isEmptyField(fields, 1, true)) {
76  	    adm3.setName(fields[1].trim());
77  	}
78  
79  	// process code
80  	if (!isEmptyField(fields, 0, true)) {
81  	    String[] fullAdm3Code = fields[0].split("\\.");
82  	    // check the format
83  	    if (fullAdm3Code.length != 4) {
84  		logger.warn("adm3 importer needs code with 4 fields : "
85  			+ dumpFields(fullAdm3Code) + " is not correct");
86  		return;
87  	    }
88  
89  	    String countryCode = fullAdm3Code[0].toUpperCase();
90  	    String adm1Code = fullAdm3Code[1];
91  	    String adm2Code = fullAdm3Code[2];
92  	    String adm3Code = fullAdm3Code[3];
93  
94  	    // set the code Value
95  	    if (!isEmptyField(fullAdm3Code, 0, true)) {
96  		adm3.setCountryCode(countryCode);
97  	    }
98  	    if (!isEmptyField(fullAdm3Code, 1, true)) {
99  		adm3.setAdm1Code(adm1Code);
100 	    }
101 	    if (!isEmptyField(fullAdm3Code, 2, true)) {
102 		adm3.setAdm2Code(adm2Code);
103 	    }
104 	    if (!isEmptyField(fullAdm3Code, 3, true)) {
105 		adm3.setAdm3Code(adm3Code);
106 	    }
107 
108 	    Adm duplicate = this.admDao.getAdm3(countryCode, adm1Code,
109 		    adm2Code, adm3Code);
110 
111 	    if (duplicate != null) {
112 		logger
113 			.warn(adm3
114 				+ " will not be saved because it is duplicate (same codes) with "
115 				+ duplicate);
116 		return;
117 	    }
118 
119 	    // try to get the parent Adm2
120 	    Adm adm2 = null;
121 
122 	    adm2 = this.admDao.getAdm2(countryCode, adm1Code, adm2Code);
123 
124 	    // if found add to Adm2Childs
125 	    if (adm2 == null) {
126 		logger.warn("could not find adm2 for " + countryCode + "."
127 			+ adm1Code + "." + adm2Code + " for the adm3 " + adm3);
128 		return;
129 	    } else {
130 		adm2.addChild(adm3);
131 	    }
132 	}
133 
134 	if (!isEmptyField(fields, 2, false)) {
135 	    adm3.setAsciiName(fields[2].trim());
136 	}
137 
138 	// fallback if asciiName is not null and name is null
139 	if (isEmptyField(fields, 1, false) && !isEmptyField(fields, 2, false)) {
140 	    adm3.setName(fields[2].trim());
141 	}
142 
143 	this.admDao.save(adm3);
144     }
145     
146     /* (non-Javadoc)
147      * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#shouldBeSkiped()
148      */
149     @Override
150     protected boolean shouldBeSkipped() {
151 	return !importerConfig.isGeonamesImporterEnabled();
152     }
153 
154     /*
155      * (non-Javadoc)
156      * 
157      * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#shouldIgnoreFirstLine()
158      */
159     @Override
160     protected boolean shouldIgnoreFirstLine() {
161 	return false;
162     }
163 
164     /*
165      * (non-Javadoc)
166      * 
167      * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#shouldIgnoreComments()
168      */
169     @Override
170     protected boolean shouldIgnoreComments() {
171 	return true;
172     }
173 
174     /*
175      * (non-Javadoc)
176      * 
177      * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#setCommitFlushMode()
178      */
179     @Override
180     protected void setCommitFlushMode() {
181 	this.admDao.setFlushMode(FlushMode.COMMIT);
182     }
183 
184     /*
185      * (non-Javadoc)
186      * 
187      * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#flushAndClear()
188      */
189     @Override
190     protected void flushAndClear() {
191 	this.admDao.flushAndClear();
192     }
193 
194     /*
195      * (non-Javadoc)
196      * 
197      * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#getNumberOfColumns()
198      */
199     @Override
200     protected int getNumberOfColumns() {
201 	return 2;
202     }
203 
204     /**
205      * @param admDao
206      *                The admDao to set
207      */
208     @Required
209     public void setAdmDao(IAdmDao admDao) {
210 	this.admDao = admDao;
211     }
212 
213     /*
214      * (non-Javadoc)
215      * 
216      * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#getFiles()
217      */
218     @Override
219     protected File[] getFiles() {
220 	File[] files = new File[1];
221 	files[0] = new File(importerConfig.getGeonamesDir()
222 		+ importerConfig.getAdm3FileName());
223 	return files;
224     }
225 
226     /*
227      * (non-Javadoc)
228      * 
229      * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#getMaxInsertsBeforeFlush()
230      */
231     @Override
232     protected int getMaxInsertsBeforeFlush() {
233 	// we commit each times because we don't want duplicate adm and the
234 	// cache is NONSTRICT_READ_WRITE (assynchronous)
235 	return 1;
236     }
237 
238     /*
239      * (non-Javadoc)
240      * 
241      * @see com.gisgraphy.domain.geoloc.importer.IGeonamesProcessor#rollback()
242      */
243     public List<NameValueDTO<Integer>> rollback() {
244 	List<NameValueDTO<Integer>> deletedObjectInfo = new ArrayList<NameValueDTO<Integer>>();
245 	logger.info("deleting adm3...");
246 	int deletedadm = admDao.deleteAllByLevel(3);
247 	if (deletedadm != 0) {
248 	    deletedObjectInfo
249 		    .add(new NameValueDTO<Integer>("ADM3", deletedadm));
250 	}
251 	logger.info(deletedadm + " adm3s have been deleted");
252 	resetStatusFields();
253 	return deletedObjectInfo;
254     }
255 
256 }