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.entity;
24
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import javax.persistence.CascadeType;
29 import javax.persistence.Column;
30 import javax.persistence.Entity;
31 import javax.persistence.FetchType;
32 import javax.persistence.JoinColumn;
33 import javax.persistence.ManyToOne;
34 import javax.persistence.OneToMany;
35 import javax.persistence.Transient;
36
37 import org.hibernate.annotations.Cache;
38 import org.hibernate.annotations.CacheConcurrencyStrategy;
39 import org.hibernate.annotations.Fetch;
40 import org.hibernate.annotations.FetchMode;
41 import org.hibernate.annotations.Index;
42
43 import com.gisgraphy.helper.FeatureClassCodeHelper;
44 import com.gisgraphy.helper.IntrospectionIgnoredField;
45
46
47
48
49
50
51
52
53
54 @Entity
55 @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
56 public class Adm extends GisFeature {
57
58
59
60
61
62
63
64
65
66
67
68
69
70 public Adm(GisFeature gisFeature, Integer level) {
71 super(gisFeature);
72 setFeatureClass("A");
73 setLevel(level);
74 setFeatureCode("ADM" + getLevel());
75 }
76
77
78
79
80
81
82
83
84
85 public Adm(Integer level) {
86 setLevel(level);
87 setFeatureClass("A");
88 setFeatureCode("ADM" + getLevel());
89 }
90
91
92
93
94 protected Adm() {
95 super();
96 }
97
98
99
100
101
102 @Transient
103 public boolean isConsistentForLevel() {
104 if (getCountryCode() == null) {
105 return false;
106 }
107 if (this.getLevel() == 1 && this.getAdm1Code() == null) {
108 return false;
109 } else if (this.getLevel() == 2
110 && (this.getAdm1Code() == null || this.getAdm2Code() == null)) {
111 return false;
112 } else if (this.getLevel() == 3
113 && (this.getAdm1Code() == null || this.getAdm2Code() == null || this
114 .getAdm3Code() == null)) {
115 return false;
116 } else if (this.getLevel() == 4
117 && (this.getAdm1Code() == null || this.getAdm2Code() == null
118 || this.getAdm3Code() == null || this.getAdm4Code() == null)) {
119 return false;
120 }
121 return true;
122 }
123
124 private Integer level;
125
126 @IntrospectionIgnoredField
127 private Adm parent;
128
129 private List<Adm> children;
130
131
132
133
134 @Column(nullable = false)
135 @Index(name = "admLevel")
136 public Integer getLevel() {
137 return level;
138 }
139
140
141
142
143
144
145
146
147
148
149 public void setLevel(Integer level) {
150 if (level < 1 || level > 4) {
151 throw new IllegalArgumentException(
152 "The level of an Adm can not be " + level
153 + ". it must be beetween 1 and 4");
154 }
155 this.level = level;
156 }
157
158
159
160
161
162
163
164
165 public void addChild(Adm child) {
166 if (child == null) {
167 logger.info("Could not add a null child");
168 throw new IllegalArgumentException("Could not add a null child to "
169 + this);
170 }
171 if (child.getLevel() != getLevel() + 1) {
172 throw new IllegalArgumentException("a child of level "
173 + child.getLevel() + " : " + child
174 + " should not be added to an Adm of level " + getLevel()
175 + " : " + this.toString() + " but will be added");
176 }
177 List<Adm> currentChilds = getChildren();
178 if (currentChilds == null) {
179 currentChilds = new ArrayList<Adm>();
180 }
181 currentChilds.add(child);
182 this.setChildren(currentChilds);
183 child.setParent(this);
184
185 }
186
187
188
189
190
191
192
193
194
195 public void addChildren(List<Adm> children) {
196 if (children != null) {
197 for (Adm child : children) {
198 addChild(child);
199 }
200 }
201 ;
202 }
203
204
205
206
207
208
209
210
211 @OneToMany(cascade = { CascadeType.ALL }, mappedBy = "parent")
212 @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
213 @Fetch(FetchMode.SELECT)
214 public List<Adm> getChildren() {
215 return children;
216 }
217
218
219
220
221
222
223
224
225
226 public void setChildren(List<Adm> children) {
227
228
229
230
231
232
233
234 this.children = children;
235
236 }
237
238
239
240
241
242
243 @ManyToOne(fetch = FetchType.EAGER)
244 @JoinColumn(nullable = true, name = "parent")
245 @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
246 public Adm getParent() {
247 return parent;
248 }
249
250
251
252
253
254
255
256
257
258 public void setParent(Adm parent) {
259
260
261
262
263
264 this.parent = parent;
265 }
266
267 private static boolean isAdmCodeEmpty(String admCode) {
268 if (admCode == null || admCode.trim().length() == 0) {
269 return true;
270 }
271 return false;
272 }
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288 public static int getProcessedLevelFromCodes(String adm1Code,
289 String adm2Code, String adm3Code, String adm4Code) {
290 if (!isAdmCodeEmpty(adm1Code)) {
291 if (!isAdmCodeEmpty(adm2Code)) {
292 if (!isAdmCodeEmpty(adm3Code)) {
293 if (!isAdmCodeEmpty(adm4Code)) {
294
295 return 4;
296 } else {
297
298 return 3;
299 }
300 } else {
301
302 return 2;
303 }
304 } else {
305
306 return 1;
307 }
308 } else {
309
310 return 0;
311 }
312
313 }
314
315
316
317
318
319
320
321
322
323
324
325
326
327 public static int getProcessedLevelFromFeatureClassCode(
328 String featureClass, String featureCode) {
329 if (FeatureClassCodeHelper.is_Adm(featureClass, featureCode)) {
330 int level = 0;
331 try {
332 level = new Integer(featureCode.substring(3)).intValue();
333 } catch (NumberFormatException e) {
334 }
335 return level;
336
337 } else {
338 return 0;
339 }
340
341 }
342
343
344
345
346
347
348 @Override
349 public int hashCode() {
350 final int PRIME = 31;
351 int result = super.hashCode();
352 result = PRIME * result
353 + ((getAdm1Code() == null) ? 0 : getAdm1Code().hashCode());
354 result = PRIME * result
355 + ((getAdm2Code() == null) ? 0 : getAdm2Code().hashCode());
356 result = PRIME * result
357 + ((getAdm3Code() == null) ? 0 : getAdm3Code().hashCode());
358 result = PRIME * result
359 + ((getAdm4Code() == null) ? 0 : getAdm4Code().hashCode());
360 result = PRIME * result + ((level == null) ? 0 : level.hashCode());
361 return result;
362 }
363
364
365
366
367 @Override
368 public boolean equals(Object obj) {
369 if (this == obj) {
370 return true;
371 }
372 if (!super.equals(obj)) {
373 return false;
374 }
375 if (getClass() != obj.getClass()) {
376 return false;
377 }
378 final Adm other = (Adm) obj;
379 if (level == null) {
380 if (other.level != null) {
381 return false;
382 }
383 } else if (!level.equals(other.level)) {
384 return false;
385 }
386 if (getCountryCode() == null) {
387 if (other.getCountryCode() != null) {
388 return false;
389 }
390 } else if (!getCountryCode().equals(other.getCountryCode())) {
391 return false;
392 }
393 if (getAdm1Code() == null) {
394 if (other.getAdm1Code() != null) {
395 return false;
396 }
397 } else if (!getAdm1Code().equals(other.getAdm1Code())) {
398 return false;
399 }
400 if (getAdm2Code() == null) {
401 if (other.getAdm2Code() != null) {
402 return false;
403 }
404 } else if (!getAdm2Code().equals(other.getAdm2Code())) {
405 return false;
406 }
407 if (getAdm3Code() == null) {
408 if (other.getAdm3Code() != null) {
409 return false;
410 }
411 } else if (!getAdm3Code().equals(other.getAdm3Code())) {
412 return false;
413 }
414 if (getAdm4Code() == null) {
415 if (other.getAdm4Code() != null) {
416 return false;
417 }
418 } else if (!getAdm4Code().equals(other.getAdm4Code())) {
419 return false;
420 }
421 return true;
422 }
423
424
425
426
427
428
429 @Override
430 public String toString() {
431 return "Adm[" + getCountryCode() + "." + getAdm1Code() + "."
432 + getAdm2Code() + "." + getAdm3Code() + "." + getAdm4Code()
433 + "][level=" + getLevel() + "][" + getFeatureId() + "]["
434 + getName() + "]";
435 }
436
437 }