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 GeonamesAdm3Importer extends AbstractImporterProcessor {
46
47 private IAdmDao admDao;
48
49
50
51
52 public GeonamesAdm3Importer() {
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 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
80 if (!isEmptyField(fields, 0, true)) {
81 String[] fullAdm3Code = fields[0].split("\\.");
82
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
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
120 Adm adm2 = null;
121
122 adm2 = this.admDao.getAdm2(countryCode, adm1Code, adm2Code);
123
124
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
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
147
148
149 @Override
150 protected boolean shouldBeSkipped() {
151 return !importerConfig.isGeonamesImporterEnabled();
152 }
153
154
155
156
157
158
159 @Override
160 protected boolean shouldIgnoreFirstLine() {
161 return false;
162 }
163
164
165
166
167
168
169 @Override
170 protected boolean shouldIgnoreComments() {
171 return true;
172 }
173
174
175
176
177
178
179 @Override
180 protected void setCommitFlushMode() {
181 this.admDao.setFlushMode(FlushMode.COMMIT);
182 }
183
184
185
186
187
188
189 @Override
190 protected void flushAndClear() {
191 this.admDao.flushAndClear();
192 }
193
194
195
196
197
198
199 @Override
200 protected int getNumberOfColumns() {
201 return 2;
202 }
203
204
205
206
207
208 @Required
209 public void setAdmDao(IAdmDao admDao) {
210 this.admDao = admDao;
211 }
212
213
214
215
216
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
228
229
230
231 @Override
232 protected int getMaxInsertsBeforeFlush() {
233
234
235 return 1;
236 }
237
238
239
240
241
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 }