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 1 file. It is the first step of the Adm1 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 GeonamesAdm1Importer extends AbstractImporterProcessor {
46  
47      private IAdmDao admDao;
48  
49      /**
50       * Default constructor
51       */
52      public GeonamesAdm1Importer() {
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 :
68  	// name
69  	//
70  	checkNumberOfColumn(fields);
71  	Adm adm = new Adm(1);
72  	String[] code = {};
73  	if (!isEmptyField(fields, 0, true)) {
74  	    code = fields[0].split("\\.");
75  	    if (!isEmptyField(code, 0, true)) {
76  		adm.setCountryCode(code[0].toUpperCase());
77  	    }
78  	    if (!isEmptyField(code, 1, true)) {
79  		adm.setAdm1Code(code[1]);
80  	    }
81  	}
82  
83  	if (!isEmptyField(fields, 1, true)) {
84  	    adm.setName(fields[1].trim());
85  	}
86  
87  	adm.setLocation(GeolocHelper.createPoint(0F, 0F));
88  	adm.setFeatureId((++AbstractImporterProcessor.nbGisInserted) * -1);
89  	adm.setSource(GISSource.GEONAMES);
90  	adm.setFeatureClass("A");
91  	adm.setFeatureCode("ADM1");
92  
93  	Adm duplicate = this.admDao.getAdm1(code[0], code[1]);
94  	if (duplicate == null) {
95  	    // no adm1 with the same Adm1Code exist
96  	    this.admDao.save(adm);
97  	} else {
98  	    logger
99  		    .warn(adm
100 			    + " will not be saved because it is duplicate (same codes) with "
101 			    + duplicate);
102 	}
103 
104     }
105     
106     /* (non-Javadoc)
107      * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#shouldBeSkiped()
108      */
109     @Override
110     protected boolean shouldBeSkipped() {
111 	return !importerConfig.isGeonamesImporterEnabled();
112     }
113 
114     /*
115      * (non-Javadoc)
116      * 
117      * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#shouldIgnoreFirstLine()
118      */
119     @Override
120     protected boolean shouldIgnoreFirstLine() {
121 	return false;
122     }
123 
124     /*
125      * (non-Javadoc)
126      * 
127      * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#shouldIgnoreComments()
128      */
129     @Override
130     protected boolean shouldIgnoreComments() {
131 	return true;
132     }
133 
134     /*
135      * (non-Javadoc)
136      * 
137      * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#setCommitFlushMode()
138      */
139     @Override
140     protected void setCommitFlushMode() {
141 	this.admDao.setFlushMode(FlushMode.COMMIT);
142 
143     }
144 
145     /*
146      * (non-Javadoc)
147      * 
148      * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#flushAndClear()
149      */
150     @Override
151     protected void flushAndClear() {
152 	this.admDao.flushAndClear();
153 
154     }
155 
156     /*
157      * (non-Javadoc)
158      * 
159      * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#getNumberOfColumns()
160      */
161     @Override
162     protected int getNumberOfColumns() {
163 	return 2;
164     }
165 
166     /**
167      * @param admDao
168      *                the admDao to set
169      */
170     @Required
171     public void setAdmDao(IAdmDao admDao) {
172 	this.admDao = admDao;
173     }
174 
175     /*
176      * (non-Javadoc)
177      * 
178      * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#getFiles()
179      */
180     @Override
181     protected File[] getFiles() {
182 	File[] files = new File[1];
183 	files[0] = new File(importerConfig.getGeonamesDir()
184 		+ importerConfig.getAdm1FileName());
185 	return files;
186     }
187 
188     /*
189      * (non-Javadoc)
190      * 
191      * @see com.gisgraphy.domain.geoloc.importer.AbstractImporterProcessor#getMaxInsertsBeforeFlush()
192      */
193     @Override
194     protected int getMaxInsertsBeforeFlush() {
195 	// we commit each times because we don't want duplicate adm and the
196 	// cache is NONSTRICT_READ_WRITE (assynchronous)
197 	return 1;
198     }
199 
200     /*
201      * (non-Javadoc)
202      * 
203      * @see com.gisgraphy.domain.geoloc.importer.IGeonamesProcessor#rollback()
204      */
205     public List<NameValueDTO<Integer>> rollback() {
206 	List<NameValueDTO<Integer>> deletedObjectInfo = new ArrayList<NameValueDTO<Integer>>();
207 	logger.info("deleting adm1...");
208 	int deletedadm = admDao.deleteAllByLevel(1);
209 	if (deletedadm != 0) {
210 	    deletedObjectInfo
211 		    .add(new NameValueDTO<Integer>("ADM1", deletedadm));
212 	}
213 	logger.info(deletedadm + " adm1s have been deleted");
214 	resetStatusFields();
215 	return deletedObjectInfo;
216     }
217 
218 }