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.valueobject;
24  
25  import javax.persistence.Transient;
26  import javax.xml.bind.annotation.XmlAccessType;
27  import javax.xml.bind.annotation.XmlAccessorType;
28  import javax.xml.bind.annotation.XmlRootElement;
29  import javax.xml.bind.annotation.XmlTransient;
30  
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  import com.gisgraphy.domain.geoloc.entity.Adm;
35  import com.gisgraphy.domain.geoloc.entity.Country;
36  import com.gisgraphy.domain.geoloc.entity.GisFeature;
37  import com.gisgraphy.domain.geoloc.entity.ZipCodeAware;
38  import com.gisgraphy.helper.URLUtils;
39  import com.vividsolutions.jts.geom.Point;
40  
41  /**
42   * Value object that represents a gisFeature with a distance The JAXB node name
43   * is {@link Constants#GISFEATUREDISTANCE_JAXB_NAME}
44   * 
45   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
46   */
47  @XmlRootElement(name = Constants.GISFEATUREDISTANCE_JAXB_NAME)
48  @XmlAccessorType(XmlAccessType.FIELD)
49  public class GisFeatureDistance {
50  
51      public static class GisFeatureDistanceBuilder {
52  
53  	public static GisFeatureDistanceBuilder gisFeatureDistance() {
54  	    return new GisFeatureDistanceBuilder();
55  	}
56  
57  	private final GisFeatureDistance gisFeatureDistance;
58  
59  	private GisFeatureDistanceBuilder() {
60  	    gisFeatureDistance = new GisFeatureDistance();
61  	}
62  
63  	public GisFeatureDistance build() {
64  	    gisFeatureDistance.updateFields();
65  	    return gisFeatureDistance;
66  	}
67  
68  	public GisFeatureDistanceBuilder withName(String name) {
69  	    gisFeatureDistance.name = name;
70  	    return this;
71  	}
72  
73  	public GisFeatureDistanceBuilder withLocation(Point location) {
74  	    gisFeatureDistance.location = location;
75  	    return this;
76  	}
77  
78  	public GisFeatureDistanceBuilder withDistance(Double distance) {
79  	    gisFeatureDistance.distance = distance;
80  	    return this;
81  	}
82  
83  	public GisFeatureDistanceBuilder withAdm1Code(String adm1Code) {
84  	    gisFeatureDistance.adm1Code = adm1Code;
85  	    return this;
86  	}
87  
88  	public GisFeatureDistanceBuilder withAdm2Code(String adm2Code) {
89  	    gisFeatureDistance.adm2Code = adm2Code;
90  	    return this;
91  	}
92  
93  	public GisFeatureDistanceBuilder withAdm3Code(String adm3Code) {
94  	    gisFeatureDistance.adm3Code = adm3Code;
95  	    return this;
96  	}
97  
98  	public GisFeatureDistanceBuilder withAdm4Code(String adm4Code) {
99  	    gisFeatureDistance.adm4Code = adm4Code;
100 	    return this;
101 	}
102 
103 	public GisFeatureDistanceBuilder withAdm1Name(String adm1Name) {
104 	    gisFeatureDistance.adm1Name = adm1Name;
105 	    return this;
106 	}
107 
108 	public GisFeatureDistanceBuilder withAdm2Name(String adm2Name) {
109 	    gisFeatureDistance.adm2Name = adm2Name;
110 	    return this;
111 	}
112 
113 	public GisFeatureDistanceBuilder withAdm3Name(String adm3Name) {
114 	    gisFeatureDistance.adm3Name = adm3Name;
115 	    return this;
116 	}
117 
118 	public GisFeatureDistanceBuilder withAdm4Name(String adm4Name) {
119 	    gisFeatureDistance.adm4Name = adm4Name;
120 	    return this;
121 	}
122 
123 	public GisFeatureDistanceBuilder withAsciiName(String asciiName) {
124 	    gisFeatureDistance.asciiName = asciiName;
125 	    return this;
126 	}
127 
128 	public GisFeatureDistanceBuilder withCountryCode(String countryCode) {
129 	    if (countryCode != null) {
130 		gisFeatureDistance.countryCode = countryCode.toUpperCase();
131 	    }
132 	    return this;
133 	}
134 
135 	public GisFeatureDistanceBuilder withElevation(Integer elevation) {
136 	    gisFeatureDistance.elevation = elevation;
137 	    return this;
138 	}
139 
140 	public GisFeatureDistanceBuilder withFeatureClass(String featureClass) {
141 	    gisFeatureDistance.featureClass = featureClass;
142 	    return this;
143 	}
144 
145 	public GisFeatureDistanceBuilder withFeatureCode(String featureCode) {
146 	    gisFeatureDistance.featureCode = featureCode;
147 	    return this;
148 	}
149 
150 	public GisFeatureDistanceBuilder withFeatureId(Long featureId) {
151 	    gisFeatureDistance.featureId = featureId;
152 	    return this;
153 	}
154 
155 	public GisFeatureDistanceBuilder withGtopo30(Integer gtopo30) {
156 	    gisFeatureDistance.gtopo30 = gtopo30;
157 	    return this;
158 	}
159 
160 	public GisFeatureDistanceBuilder withPopulation(Integer population) {
161 	    gisFeatureDistance.population = population;
162 	    return this;
163 	}
164 
165 	public GisFeatureDistanceBuilder withTimeZone(String timezone) {
166 	    gisFeatureDistance.timezone = timezone;
167 	    return this;
168 	}
169 
170 	public GisFeatureDistanceBuilder withZipCode(String zipCode) {
171 	    gisFeatureDistance.zipCode = zipCode;
172 	    return this;
173 	}
174 	
175 	public GisFeatureDistanceBuilder withArea(Double area) {
176 	    gisFeatureDistance.area = area;
177 	    return this;
178 	}
179 	
180 	public GisFeatureDistanceBuilder withLevel(Integer level) {
181 	    gisFeatureDistance.level = level;
182 	    return this;
183 	}
184 	
185 	public GisFeatureDistanceBuilder withTld(String tld) {
186 	    gisFeatureDistance.tld = tld;
187 	    return this;
188 	}
189 	
190 	public GisFeatureDistanceBuilder withCapitalName(String capitalName) {
191 	    gisFeatureDistance.capitalName = capitalName;
192 	    return this;
193 	}
194 	
195 	public GisFeatureDistanceBuilder withContinent(String continent) {
196 	    gisFeatureDistance.continent = continent;
197 	    return this;
198 	}
199 	
200 	public GisFeatureDistanceBuilder withPostalCodeRegex(String postalCodeRegex) {
201 	    gisFeatureDistance.postalCodeRegex = postalCodeRegex;
202 	    return this;
203 	}
204 	
205 	    
206 	public GisFeatureDistanceBuilder withCurrencyCode(String currencyCode) {
207 	    gisFeatureDistance.currencyCode = currencyCode;
208 	    return this;
209 	}
210 	    
211 	public GisFeatureDistanceBuilder withCurrencyName(String currencyName) {
212 	    gisFeatureDistance.currencyName = currencyName;
213 	    return this;
214 	}
215 	
216 	public GisFeatureDistanceBuilder withEquivalentFipsCode(String equivalentFipsCode) {
217 	    gisFeatureDistance.equivalentFipsCode = equivalentFipsCode;
218 	    return this;
219 	}
220 	  
221 	public GisFeatureDistanceBuilder withFipsCode(String fipsCode) {
222 	    gisFeatureDistance.fipsCode = fipsCode;
223 	    return this;
224 	}
225 
226 	public GisFeatureDistanceBuilder withIso3166Alpha2Code(String iso3166Alpha2Code) {
227 	    gisFeatureDistance.iso3166Alpha2Code = iso3166Alpha2Code;
228 	    return this;
229 	}
230 	
231 	public GisFeatureDistanceBuilder withIso3166Alpha3Code(String iso3166Alpha3Code) {
232 	    gisFeatureDistance.iso3166Alpha3Code = iso3166Alpha3Code;
233 	    return this;
234 	}
235 
236 	public GisFeatureDistanceBuilder withIso3166NumericCode(Integer iso3166NumericCode) {
237 	    gisFeatureDistance.iso3166NumericCode = iso3166NumericCode;
238 	    return this;
239 	}
240 
241 	public GisFeatureDistanceBuilder withPhonePrefix(String phonePrefix) {
242 	    gisFeatureDistance.phonePrefix = phonePrefix;
243 	    return this;
244 	}
245 	    
246 	public GisFeatureDistanceBuilder withPostalCodeMask(String postalCodeMask) {
247 	    gisFeatureDistance.postalCodeMask = postalCodeMask;
248 	    return this;
249 	}
250 
251 
252 	public GisFeatureDistanceBuilder withPlaceType(
253 		Class<? extends GisFeature> placeType) {
254 	    if (placeType != null) {
255 		gisFeatureDistance.placeType = placeType.getSimpleName();
256 	    }
257 	    return this;
258 	}
259 
260     }
261 
262     @XmlTransient
263     @Transient
264     protected static final Logger logger = LoggerFactory
265 	    .getLogger(GisFeatureDistance.class);
266 
267     @XmlTransient
268     @Transient
269     private GisFeature gisFeature;
270 
271     @XmlTransient
272     @Transient
273     private Point location;
274 
275     private Double distance;
276 
277     private String name;
278 
279     private String adm1Code;
280 
281     private String adm2Code;
282 
283     private String adm3Code;
284 
285     private String adm4Code;
286 
287     private String adm1Name;
288 
289     private String adm2Name;
290 
291     private String adm3Name;
292 
293     private String adm4Name;
294 
295     private String asciiName;
296 
297     private String countryCode;
298 
299     private Integer elevation;
300 
301     private String featureClass;
302 
303     private String featureCode;
304 
305     private Long featureId;
306 
307     private Integer gtopo30;
308 
309     private Integer population;
310 
311     private String timezone;
312 
313     private Double lat;
314 
315     private Double lng;
316 
317     private String placeType;
318 
319     private String zipCode;
320 
321     private String google_map_url;
322 
323     private String yahoo_map_url;
324 
325     private String country_flag_url;
326     
327     private Integer level;
328     
329     private Double area;
330 
331     private String tld;
332 
333     private String capitalName;
334 
335     private String continent;
336 
337     private String postalCodeRegex;
338     
339     private String currencyCode;
340 
341     private String currencyName;
342 
343     private String equivalentFipsCode;
344 
345     private String fipsCode;
346 
347     private String iso3166Alpha2Code;
348 
349     private String iso3166Alpha3Code;
350 
351     private Integer iso3166NumericCode;
352     
353     private String phonePrefix;
354 
355     private String postalCodeMask;
356 
357     public GisFeatureDistance() {
358 	super();
359     }
360 
361     /**
362      * @param gisFeature
363      *                The gisFeature
364      * @param distance
365      *                The distance
366      */
367     public GisFeatureDistance(GisFeature gisFeature, Double distance) {
368 	super();
369 	this.distance = distance;
370 	this.gisFeature = gisFeature;
371 	if (gisFeature != null) {
372 
373 	    this.adm1Code = gisFeature.getAdm1Code();
374 	    this.adm2Code = gisFeature.getAdm2Code();
375 	    this.adm3Code = gisFeature.getAdm3Code();
376 	    this.adm4Code = gisFeature.getAdm4Code();
377 
378 	    this.adm1Name = gisFeature.getAdm1Name();
379 	    this.adm2Name = gisFeature.getAdm2Name();
380 	    this.adm3Name = gisFeature.getAdm3Name();
381 	    this.adm4Name = gisFeature.getAdm4Name();
382 
383 	    if (gisFeature.getAsciiName() != null) {
384 		this.asciiName = gisFeature.getAsciiName().trim();
385 	    }
386 	    if (gisFeature.getCountryCode() != null) {
387 		this.countryCode = gisFeature.getCountryCode().toUpperCase();
388 	    }
389 	    this.elevation = gisFeature.getElevation();
390 	    this.featureClass = gisFeature.getFeatureClass();
391 	    this.featureCode = gisFeature.getFeatureCode();
392 	    this.featureId = gisFeature.getFeatureId();
393 	    this.gtopo30 = gisFeature.getGtopo30();
394 	    this.location = gisFeature.getLocation();
395 	    this.name = gisFeature.getName().trim();
396 	    this.population = gisFeature.getPopulation();
397 	    this.timezone = gisFeature.getTimezone();
398 	    if (gisFeature instanceof ZipCodeAware) {
399 		this.zipCode = ((ZipCodeAware) gisFeature).getZipCode();
400 	    }
401 	    this.placeType = gisFeature.getClass().getSimpleName()
402 		    .toLowerCase();
403 	    updateFields();
404 	}
405     }
406     
407     
408     
409     /**
410      * @param adm
411      *                The adm
412      * @param distance
413      *                The distance
414      */
415     public GisFeatureDistance(Adm adm, Double distance) {
416 	this((GisFeature)adm,distance);
417 	this.level = adm.getLevel();
418     }
419     
420     /**
421      * @param country
422      *                The country
423      * @param distance
424      *                The distance
425      */
426     public GisFeatureDistance(Country country, Double distance) {
427 	this((GisFeature)country,distance);
428 	    this.elevation = country.getElevation();
429 	    this.featureClass = country.getFeatureClass();
430 	    this.featureCode = country.getFeatureCode();
431 	    this.featureId = country.getFeatureId();
432 	    this.gtopo30 = country.getGtopo30();
433 	    this.location = country.getLocation();
434 	    this.name = country.getName().trim();
435 	    this.population = country.getPopulation();
436 	    this.timezone = country.getTimezone();
437 	    this.area=country.getArea();
438 	    this.tld=country.getTld();
439 	    this.capitalName=country.getCapitalName();
440 	    this.continent=country.getContinent();
441 	    this.postalCodeMask=country.getPostalCodeMask();
442 	    this.postalCodeRegex=country.getPostalCodeRegex();
443 	    this.currencyCode=country.getCurrencyCode();
444 	    this.currencyName=country.getCurrencyName();
445 	    this.equivalentFipsCode = country.getEquivalentFipsCode();
446 	    this.fipsCode=country.getFipsCode();
447 	    this.iso3166Alpha2Code= country.getIso3166Alpha2Code();
448 	    this.iso3166Alpha3Code= country.getIso3166Alpha3Code();
449 	    this.iso3166NumericCode= country.getIso3166NumericCode();
450 	    this.phonePrefix=country.getPhonePrefix();
451     }
452 
453     /**
454      * update the calculated fields (GoogleMapUrl,YahooMapURL,CountryFlag,...)
455      * 
456      */
457     public void updateFields() {
458 	this.google_map_url = URLUtils.createGoogleMapUrl(this.location,
459 		this.name);
460 	this.yahoo_map_url = URLUtils.createYahooMapUrl(this.location);
461 	this.country_flag_url = URLUtils.createCountryFlagUrl(this.countryCode);
462 	if (this.location != null) {
463 	    this.lat = location.getY();
464 	    this.lng = location.getX();
465 	}
466 	if (featureClass != null && featureCode != null) {
467 	    try {
468 		this.placeType = FeatureCode.valueOf(
469 			featureClass + "_" + featureCode).getObject()
470 			.getClass().getSimpleName();
471 	    } catch (RuntimeException e) {
472 	    }
473 	}
474     }
475 
476     /**
477      * @return The distance
478      */
479     public Double getDistance() {
480 	return distance;
481     }
482 
483     /**
484      * @return The gisfeature
485      */
486     @Transient
487     @XmlTransient
488     public GisFeature getGisFeature() {
489 	return this.gisFeature;
490     }
491 
492     /**
493      * @return the name
494      */
495     public String getName() {
496 	return this.name;
497     }
498 
499     /**
500      * @return the location
501      */
502     @Transient
503     @XmlTransient
504     public Point getLocation() {
505 	return this.location;
506     }
507 
508     /**
509      * @return the adm1Code
510      */
511     public String getAdm1Code() {
512 	return this.adm1Code;
513     }
514 
515     /**
516      * @return the adm2Code
517      */
518     public String getAdm2Code() {
519 	return this.adm2Code;
520     }
521 
522     /**
523      * @return the adm3Code
524      */
525     public String getAdm3Code() {
526 	return this.adm3Code;
527     }
528 
529     /**
530      * @return the adm4Code
531      */
532     public String getAdm4Code() {
533 	return this.adm4Code;
534     }
535 
536     /**
537      * @return the adm1Name
538      */
539     public String getAdm1Name() {
540 	return this.adm1Name;
541     }
542 
543     /**
544      * @return the adm2Name
545      */
546     public String getAdm2Name() {
547 	return this.adm2Name;
548     }
549 
550     /**
551      * @return the adm3Name
552      */
553     public String getAdm3Name() {
554 	return this.adm3Name;
555     }
556 
557     /**
558      * @return the adm4Name
559      */
560     public String getAdm4Name() {
561 	return this.adm4Name;
562     }
563 
564     /**
565      * @return the asciiName
566      */
567     public String getAsciiName() {
568 	return this.asciiName;
569     }
570 
571     /**
572      * @return the countryCode
573      */
574     public String getCountryCode() {
575 	return this.countryCode;
576     }
577 
578     /**
579      * @return the elevation
580      */
581     public Integer getElevation() {
582 	return this.elevation;
583     }
584 
585     /**
586      * @return the featureClass
587      */
588     public String getFeatureClass() {
589 	return this.featureClass;
590     }
591 
592     /**
593      * @return the featureCode
594      */
595     public String getFeatureCode() {
596 	return this.featureCode;
597     }
598 
599     /**
600      * @return the featureId
601      */
602     public Long getFeatureId() {
603 	return this.featureId;
604     }
605 
606     /**
607      * @return the gtopo30
608      */
609     public Integer getGtopo30() {
610 	return this.gtopo30;
611     }
612 
613     /**
614      * @return the population
615      */
616     public Integer getPopulation() {
617 	return this.population;
618     }
619 
620     /**
621      * @return the timezone
622      */
623     public String getTimezone() {
624 	return this.timezone;
625     }
626 
627     /**
628      * @return the lat
629      */
630     public Double getLat() {
631 	return this.lat;
632     }
633 
634     /**
635      * @return the lng
636      */
637     public Double getLng() {
638 	return this.lng;
639     }
640 
641     /**
642      * @return the zipCode
643      */
644     public String getZipCode() {
645 	return this.zipCode;
646     }
647 
648     /**
649      * @return the placeType
650      */
651     public String getPlaceType() {
652 	return placeType;
653     }
654 
655     /**
656      * @return the google_map_url
657      */
658     public String getGoogle_map_url() {
659 	return this.google_map_url;
660     }
661 
662     /**
663      * @return the yahoo_map_url
664      */
665     public String getYahoo_map_url() {
666 	return this.yahoo_map_url;
667     }
668 
669     /**
670      * @return the country_flag_url
671      */
672     public String getCountry_flag_url() {
673 	return this.country_flag_url;
674     }
675 
676     /**
677      * @return the level
678      */
679     public Integer getLevel() {
680         return level;
681     }
682 
683     /**
684      * @return the area
685      */
686     public Double getArea() {
687         return area;
688     }
689 
690     /**
691      * @return the tld
692      */
693     public String getTld() {
694         return tld;
695     }
696 
697     /**
698      * @return the capitalName
699      */
700     public String getCapitalName() {
701         return capitalName;
702     }
703 
704     /**
705      * @return the continent
706      */
707     public String getContinent() {
708         return continent;
709     }
710 
711     /**
712      * @return the postalCodeRegex
713      */
714     public String getPostalCodeRegex() {
715         return postalCodeRegex;
716     }
717 
718     /**
719      * @return the currencyCode
720      */
721     public String getCurrencyCode() {
722         return currencyCode;
723     }
724 
725     /**
726      * @return the currencyName
727      */
728     public String getCurrencyName() {
729         return currencyName;
730     }
731 
732     /**
733      * @return the equivalentFipsCode
734      */
735     public String getEquivalentFipsCode() {
736         return equivalentFipsCode;
737     }
738 
739     /**
740      * @return the fipsCode
741      */
742     public String getFipsCode() {
743         return fipsCode;
744     }
745 
746     /**
747      * @return the iso3166Alpha2Code
748      */
749     public String getIso3166Alpha2Code() {
750         return iso3166Alpha2Code;
751     }
752 
753     /**
754      * @return the iso3166Alpha3Code
755      */
756     public String getIso3166Alpha3Code() {
757         return iso3166Alpha3Code;
758     }
759 
760     /**
761      * @return the iso3166NumericCode
762      */
763     public Integer getIso3166NumericCode() {
764         return iso3166NumericCode;
765     }
766 
767     /**
768      * @return the phonePrefix
769      */
770     public String getPhonePrefix() {
771         return phonePrefix;
772     }
773 
774     /**
775      * @return the postalCodeMask
776      */
777     public String getPostalCodeMask() {
778         return postalCodeMask;
779     }
780 
781 }