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 javax.persistence.Entity;
26  import javax.persistence.Transient;
27  
28  import org.hibernate.annotations.Cache;
29  import org.hibernate.annotations.CacheConcurrencyStrategy;
30  import org.hibernate.annotations.Index;
31  
32  /**
33   * Represents a city Object
34   * 
35   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
36   */
37  @Entity
38  @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
39  public class City extends GisFeature implements ZipCodeAware {
40  
41      private String zipCode;
42  
43      /**
44       * Constructor that populate the {@link City} with the gisFeature fields<br>
45       * 
46       * @param gisFeature
47       *                The gisFeature from which we want to populate the
48       *                {@linkplain City}
49       */
50      public City(GisFeature gisFeature) {
51  	super(gisFeature);
52      }
53  
54      /**
55       * Override the gisFeature value.<br>
56       * Default to true;<br>
57       * If this field is set to false, then the object won't be synchronized with
58       * the fullText search engine
59       */
60      @Override
61      @Transient
62      public boolean isFullTextSearchable() {
63  	return true;
64      }
65  
66      /*
67       * (non-Javadoc)
68       * 
69       * @see com.gisgraphy.domain.geoloc.entity.ZipCodeAware#setZipCode(java.lang.Integer)
70       */
71      public void setZipCode(String zipCode) {
72  	this.zipCode = zipCode;
73      }
74  
75      /*
76       * (non-Javadoc)
77       * 
78       * @see com.gisgraphy.domain.geoloc.entity.ZipCodeAware#getZipCode()
79       */
80      @Index(name = "cityZipCode")
81      public String getZipCode() {
82  	return zipCode;
83      }
84  
85      /**
86       * Default constructor (Needed by CGLib)
87       */
88      public City() {
89  	super();
90      }
91  
92      /*
93       * (non-Javadoc)
94       * 
95       * @see com.gisgraphy.domain.geoloc.entity.GisFeature#hashCode()
96       */
97      @Override
98      public int hashCode() {
99  	final int PRIME = 31;
100 	int result = super.hashCode();
101 	result = PRIME * result
102 		+ ((getFeatureId() == null) ? 0 : getFeatureId().hashCode());
103 	return result;
104     }
105 
106     /*
107      * (non-Javadoc)
108      * 
109      * @see com.gisgraphy.domain.geoloc.entity.GisFeature#equals(java.lang.Object)
110      */
111     @Override
112     public boolean equals(Object obj) {
113 	if (this == obj) {
114 	    return true;
115 	}
116 	if (!super.equals(obj)) {
117 	    return false;
118 	}
119 	if (getClass() != obj.getClass()) {
120 	    return false;
121 	}
122 	final City other = (City) obj;
123 	if (getFeatureId() == null) {
124 	    if (other.getFeatureId() != null) {
125 		return false;
126 	    }
127 	} else if (!getFeatureId().equals(other.getFeatureId())) {
128 	    return false;
129 	}
130 	return true;
131     }
132 
133     /*
134      * (non-Javadoc)
135      * 
136      * @see com.gisgraphy.domain.geoloc.entity.GisFeature#populate(com.gisgraphy.domain.geoloc.entity.GisFeature)
137      */
138     @Override
139     public void populate(GisFeature gisFeature) {
140 	super.populate(gisFeature);
141 	if (gisFeature instanceof ZipCodeAware) {
142 	    this.setZipCode(((ZipCodeAware) gisFeature).getZipCode());
143 	}
144     }
145 
146     /**
147      * Returns a name of the form : (adm1Name et adm2Name are printed)
148      * Paris(Zipcode), Département de Ville-De-Paris, Ile-De-France, (FR)
149      * 
150      * @param withCountry
151      *                Whether the country information should be added
152      * @return a name with the Administrative division and Country
153      */
154     @Transient
155     @Override
156     public String getFullyQualifiedName(boolean withCountry) {
157 	StringBuilder completeCityName = new StringBuilder();
158 	completeCityName.append(getName());
159 	if (zipCode != null) {
160 	    completeCityName.append(" (");
161 	    completeCityName.append(getZipCode());
162 	    completeCityName.append(")");
163 	}
164 	if (getAdm2Name() != null && !getAdm2Name().trim().equals("")) {
165 	    completeCityName.append(", " + getAdm2Name());
166 	}
167 	if (getAdm1Name() != null && !getAdm1Name().trim().equals("")) {
168 	    completeCityName.append(", " + getAdm1Name());
169 	}
170 
171 	if (withCountry) {
172 	    Country countryObj = getCountry();
173 	    if (countryObj != null && countryObj.getName() != null
174 		    && !countryObj.getName().trim().equals("")) {
175 		completeCityName.append(" , " + countryObj.getName() + "");
176 	    }
177 	}
178 
179 	return completeCityName.toString();
180     }
181 
182 }