1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
40
41
42
43
44
45 public class GeonamesAdm4Importer extends AbstractImporterProcessor {
46
47 private IAdmDao admDao;
48
49
50
51
52 public GeonamesAdm4Importer() {
53 super();
54 }
55
56
57
58
59
60
61 @Override
62 protected void processData(String line) {
63 String[] fields = line.split("\t");
64
65
66
67
68
69
70 checkNumberOfColumn(fields);
71 Adm adm4 = new Adm(4);
72 adm4.setLocation(GeolocHelper.createPoint(0F, 0F));
73 adm4.setFeatureId((++AbstractImporterProcessor.nbGisInserted) * -1);
74 adm4.setSource(GISSource.GEONAMES);
75
76
77 if (!isEmptyField(fields, 0, true)) {
78 String[] fullAdm4Code = fields[0].split("\\.");
79
80 if (fullAdm4Code.length != 5) {
81 logger.warn("adm4 importer needs code with 5 fields : "
82 + dumpFields(fullAdm4Code) + " is not correct");
83 return;
84 }
85
86 if (!isEmptyField(fields, 1, true)) {
87 adm4.setName(fields[1].trim());
88 }
89
90 String countryCode = fullAdm4Code[0].toUpperCase();
91 String adm1Code = fullAdm4Code[1];
92 String adm2Code = fullAdm4Code[2];
93 String adm3Code = fullAdm4Code[3];
94 String adm4Code = fullAdm4Code[4];
95
96
97 if (!isEmptyField(fullAdm4Code, 0, true)) {
98 adm4.setCountryCode(countryCode);
99 }
100 if (!isEmptyField(fullAdm4Code, 1, true)) {
101 adm4.setAdm1Code(adm1Code);
102 }
103 if (!isEmptyField(fullAdm4Code, 2, true)) {
104 adm4.setAdm2Code(adm2Code);
105 }
106 if (!isEmptyField(fullAdm4Code, 3, true)) {
107 adm4.setAdm3Code(adm3Code);
108 }
109 if (!isEmptyField(fullAdm4Code, 4, true)) {
110 adm4.setAdm4Code(adm4Code);
111 }
112
113 Adm duplicate = this.admDao.getAdm4(countryCode, adm1Code,
114 adm2Code, adm3Code, adm4Code);
115 if (duplicate != null) {
116 logger
117 .warn(adm4
118 + " will not be saved because it is duplicate (same codes) with "
119 + duplicate);
120 return;
121 }
122
123
124 Adm adm3 = null;
125
126 adm3 = this.admDao.getAdm3(countryCode, adm1Code, adm2Code,
127 adm3Code);
128
129
130 if (adm3 == null) {
131 logger.warn("could not find adm3 for " + countryCode + "."
132 + adm1Code + "." + adm2Code + "." + adm3Code
133 + " for the adm4 " + adm4);
134 return;
135 } else {
136 adm3.addChild(adm4);
137 }
138 }
139
140 if (!isEmptyField(fields, 2, false)) {
141 adm4.setAsciiName(fields[2].trim());
142 }
143
144
145 if (isEmptyField(fields, 1, false) && !isEmptyField(fields, 2, false)) {
146 adm4.setName(fields[2].trim());
147 }
148
149 this.admDao.save(adm4);
150
151 }
152
153
154
155
156 @Override
157 protected boolean shouldBeSkipped() {
158 return !importerConfig.isGeonamesImporterEnabled();
159 }
160
161
162
163
164
165
166 @Override
167 protected boolean shouldIgnoreFirstLine() {
168 return false;
169 }
170
171
172
173
174
175
176 @Override
177 protected boolean shouldIgnoreComments() {
178 return true;
179 }
180
181
182
183
184
185
186 @Override
187 protected void setCommitFlushMode() {
188 this.admDao.setFlushMode(FlushMode.COMMIT);
189 }
190
191
192
193
194
195
196 @Override
197 protected void flushAndClear() {
198 this.admDao.flushAndClear();
199 }
200
201
202
203
204
205
206 @Override
207 protected int getNumberOfColumns() {
208 return 2;
209 }
210
211
212
213
214
215 @Required
216 public void setAdmDao(IAdmDao admDao) {
217 this.admDao = admDao;
218 }
219
220
221
222
223
224
225 @Override
226 protected File[] getFiles() {
227 File[] files = new File[1];
228 files[0] = new File(importerConfig.getGeonamesDir()
229 + importerConfig.getAdm4FileName());
230 return files;
231 }
232
233
234
235
236
237
238 @Override
239 protected int getMaxInsertsBeforeFlush() {
240
241
242 return 1;
243 }
244
245
246
247
248
249
250 public List<NameValueDTO<Integer>> rollback() {
251 List<NameValueDTO<Integer>> deletedObjectInfo = new ArrayList<NameValueDTO<Integer>>();
252 logger.info("deleting adm4...");
253 int deletedadm = admDao.deleteAllByLevel(4);
254 if (deletedadm != 0) {
255 deletedObjectInfo
256 .add(new NameValueDTO<Integer>("ADM4", deletedadm));
257 }
258 logger.info(deletedadm + " adm4s have been deleted");
259 resetStatusFields();
260 return deletedObjectInfo;
261 }
262
263 }