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   * 
20   *   David Masclet <davidmasclet@gisgraphy.com>
21   ******************************************************************************/
22  package com.gisgraphy.helper;
23  
24  import javax.persistence.Transient;
25  
26  import org.springframework.beans.BeansException;
27  import org.springframework.beans.factory.annotation.Autowired;
28  import org.springframework.context.ApplicationContext;
29  import org.springframework.context.ApplicationContextAware;
30  import org.springframework.stereotype.Service;
31  
32  import com.gisgraphy.domain.geoloc.entity.Country;
33  import com.gisgraphy.domain.geoloc.entity.GisFeature;
34  import com.gisgraphy.domain.repository.ICountryDao;
35  
36  @Service
37  public class GisFeatureHelper implements ApplicationContextAware{
38      
39      /**
40       * this method avoid the necessary injection in gisfeature
41       */
42      public static GisFeatureHelper getInstance(){
43  	if (applicationContext!=null){
44  	return (GisFeatureHelper) applicationContext.getBean("gisFeatureHelper");
45  	} else throw new RuntimeException("the application context is not injected");
46      }
47      
48      @Autowired
49      public ICountryDao countryDao;
50      //it is not a good idea to have a static property here but i need the get instance to be static to avoid 
51      //injection in gisFeature
52      private static ApplicationContext applicationContext;
53      
54      /**
55       * Returns a name of the form : (adm1Name et adm2Name are printed) Paris,
56       * D├ępartement de Ville-De-Paris, Ile-De-France, (FR)
57       * 
58       * @param withCountry
59       *                Whether the country information should be added
60       * @return a name with the Administrative division and Country
61       */
62      @Transient
63      public String getFullyQualifiedName(GisFeature gisFeature ,boolean withCountry) {
64  	StringBuilder completeCityName = new StringBuilder();
65  	completeCityName.append(gisFeature.getName());
66  	String adm2Name = gisFeature.getAdm2Name();
67  	if (adm2Name != null && !adm2Name.trim().equals("")) {
68  	    completeCityName.append(", " + adm2Name);
69  	}
70  	String adm1Name = gisFeature.getAdm1Name();
71  	if (adm1Name != null && !adm1Name.trim().equals("")) {
72  	    completeCityName.append(", " + adm1Name);
73  	}
74  
75  	if (withCountry) {
76  	    Country countryObj = getCountry(gisFeature.getCountryCode());
77  	    if (countryObj != null && countryObj.getName() != null
78  		    && !countryObj.getName().trim().equals("")) {
79  		completeCityName.append(" , " + countryObj.getName() + "");
80  	    }
81  	}
82  
83  	return completeCityName.toString();
84      }
85      
86      /**
87       * @return a name with the Administrative division (but without Country)
88       */
89      @Transient
90      public String getFullyQualifiedName(GisFeature gisFeature) {
91  	return getFullyQualifiedName(gisFeature,false);
92      }
93  
94      
95      /**
96       * @return the country from the country code. Return null if the country Code
97       *         is null or if no country is found
98       */
99      @Transient
100     public Country getCountry(String countryCode) {
101 	Country country = null;
102 	if (countryCode != null) {
103 	    country = countryDao.getByIso3166Alpha2Code(countryCode);
104 	}
105 	return country;
106     }
107 
108     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
109 	GisFeatureHelper.applicationContext = applicationContext;
110 	
111     }
112 
113 }