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.geoloc.entity;
24  
25  import java.util.Set;
26  
27  import javax.persistence.Entity;
28  import javax.persistence.Transient;
29  
30  import org.hibernate.annotations.Cache;
31  import org.hibernate.annotations.CacheConcurrencyStrategy;
32  import org.hibernate.annotations.Index;
33  
34  import com.gisgraphy.helper.IntrospectionIgnoredField;
35  
36  /**
37   * Represents a city Object
38   * 
39   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
40   */
41  @Entity
42  @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
43  public class City extends GisFeature implements ZipCodesAware {
44  
45  	public final static String MUNICIPALITY_FIELD_NAME = "municipality";
46  	
47  	/**
48  	 * This fields indicates that the city has some properties(admCode, population that make the city a municipality. 
49  	 * This is necessary because some place in geonames are marked as popular places and can be quater or common place.
50  	 * by identifing city we can restrict the search to city.
51  	 */
52  	@IntrospectionIgnoredField
53  	private boolean municipality=false;
54  	
55  	/**
56  	 * Constructor that populate the {@link City} with the gisFeature fields<br>
57  	 * 
58  	 * @param gisFeature
59  	 *                The gisFeature from which we want to populate the
60  	 *                {@linkplain City}
61  	 */
62  	public City(GisFeature gisFeature) {
63  		super(gisFeature);
64  	}
65  
66  	/**
67  	 * Override the gisFeature value.<br>
68  	 * Default to true;<br>
69  	 * If this field is set to false, then the object won't be synchronized with
70  	 * the fullText search engine
71  	 */
72  	@Override
73  	@Transient
74  	public boolean isFullTextSearchable() {
75  		return true;
76  	}
77  
78  	/**
79  	 * Default constructor (Needed by CGLib)
80  	 */
81  	public City() {
82  		super();
83  	}
84  
85  	/*
86  	 * (non-Javadoc)
87  	 * 
88  	 * @see com.gisgraphy.domain.geoloc.entity.GisFeature#hashCode()
89  	 */
90  	@Override
91  	public int hashCode() {
92  		final int PRIME = 31;
93  		int result = super.hashCode();
94  		result = PRIME * result + ((getFeatureId() == null) ? 0 : getFeatureId().hashCode());
95  		return result;
96  	}
97  
98  	
99  	/**
100 	 * Returns a name with adm1Name and adm2Name added (if not null).
101 	 * Paris(Zipcode), D├ępartement de Ville-De-Paris, Ile-De-France, (FR)
102 	 * 
103 	 * @param withCountry
104 	 *                Whether the country information should be added
105 	 * @return a name with the Administrative division and Country
106 	 */
107 	@Transient
108 	@Override
109 	public String getFullyQualifiedName(boolean withCountry) {
110 		StringBuilder completeCityName = new StringBuilder();
111 		completeCityName.append(getName());
112 		Set<ZipCode> zipCodes = getZipCodes();
113 		if (zipCodes != null && zipCodes.size() == 1) {
114 			completeCityName.append(" (");
115 			completeCityName.append(zipCodes.iterator().next());
116 			completeCityName.append(")");
117 		}
118 		if (getAdm2Name() != null && !getAdm2Name().trim().equals("")) {
119 			completeCityName.append(", " + getAdm2Name());
120 		}
121 		if (getAdm1Name() != null && !getAdm1Name().trim().equals("")) {
122 			completeCityName.append(", " + getAdm1Name());
123 		}
124 
125 		if (withCountry) {
126 			Country countryObj = getCountry();
127 			if (countryObj != null && countryObj.getName() != null && !countryObj.getName().trim().equals("")) {
128 				completeCityName.append(" , " + countryObj.getName() + "");
129 			}
130 		}
131 
132 		return completeCityName.toString();
133 	}
134 
135 	@Index(name = "cityMunicipalityIndex")
136 	public boolean isMunicipality() {
137 		return municipality;
138 	}
139 
140 	public void setMunicipality(boolean isMunicipality) {
141 		this.municipality = isMunicipality;
142 	}
143 
144 	
145 
146 }