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.Column;
26  import javax.persistence.Entity;
27  import javax.persistence.EnumType;
28  import javax.persistence.Enumerated;
29  import javax.persistence.FetchType;
30  import javax.persistence.GeneratedValue;
31  import javax.persistence.GenerationType;
32  import javax.persistence.Id;
33  import javax.persistence.JoinColumn;
34  import javax.persistence.ManyToOne;
35  import javax.persistence.SequenceGenerator;
36  import javax.persistence.Transient;
37  
38  import org.hibernate.annotations.Cache;
39  import org.hibernate.annotations.CacheConcurrencyStrategy;
40  import org.hibernate.annotations.Index;
41  import org.hibernate.annotations.Type;
42  
43  import com.gisgraphy.domain.valueobject.HouseNumberType;
44  import com.gisgraphy.domain.valueobject.SRID;
45  import com.gisgraphy.helper.IntrospectionIgnoredField;
46  import com.gisgraphy.street.HouseNumberComparator;
47  import com.vividsolutions.jts.geom.Point;
48  
49  /**
50   * Represents a housenumber (typically link to an {@link OpenStreetMap}.
51   * 
52   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
53   */
54  @Entity
55  @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
56  @SequenceGenerator(name = "housenumbersequence", sequenceName = "housenumber_sequence")
57  public class HouseNumber implements Comparable<HouseNumber>{
58  	
59  
60      public static final String LOCATION_COLUMN_NAME = "location";
61  
62      /**
63       * Needed by CGLib
64       */
65      public HouseNumber() {
66      }
67  
68      public HouseNumber(String number, Point location) {
69      	if (number==null || "".equals(number.trim())){
70      		throw new IllegalArgumentException("wrong number given for housenumber");
71      	}
72      	if (location==null){
73      		throw new IllegalArgumentException("wrong location for housenumber");
74      	}
75      	this.number = number;
76  		this.location = location;
77  	}
78  
79  	@IntrospectionIgnoredField
80      private Long id;
81  
82      private Long openstreetmapId;
83      
84      private String number;
85      
86      @IntrospectionIgnoredField
87      private HouseNumberType type;
88      
89      @IntrospectionIgnoredField
90      private static final HouseNumberComparator comparator = new HouseNumberComparator();
91  
92      private String name;
93  
94      private Point location;
95      
96      private OpenStreetMap street;
97  
98      
99      /**
100      * @return the id (technical one)
101      */
102     @Id
103     @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "housenumbersequence")
104     public Long getId() {
105 	return id;
106     }
107 
108     /**
109      * @param id
110      *            the id to set
111      */
112     public void setId(Long id) {
113 	this.id = id;
114     }
115 
116        
117     /**
118      * @return the openstreetmap internal id
119      */
120     @Column(unique = false, nullable = true)
121     public Long getOpenstreetmapId() {
122         return openstreetmapId;
123     }
124 
125     /**
126      * @param openstreetmapId the openstreetmap internal id
127      */
128     public void setOpenstreetmapId(Long openstreetmapId) {
129         this.openstreetmapId = openstreetmapId;
130     }
131 
132     /**
133      * @return the name of the house. 
134      * It can be the name of the shop if present or of the house.
135      * associated to the house number, it gives more informations.
136      *  
137      */
138     @Column(length = 255)
139     public String getName() {
140 	return name;
141     }
142 
143     /**
144      * @param name
145      *            the name to set
146      */
147     public void setName(String name) {
148 	this.name = name;
149     }
150 
151 
152     /**
153      * Returns The JTS location point of the current house : The Geometry
154      * representation for the latitude, longitude. The Return type is a JTS
155      * point. The Location is calculate from the 4326 {@link SRID}
156      * 
157      * @see SRID
158      * @return The JTS Point
159      */
160     @Type(type = "org.hibernatespatial.GeometryUserType")
161     @Column(name = HouseNumber.LOCATION_COLUMN_NAME,nullable=false)
162     public Point getLocation() {
163 	return location;
164     }
165     
166     /**
167      * @return Returns the latitude (north-south) from the Location
168      *         {@link #getLocation()}.
169      * @see #getLongitude()
170      * @see #getLocation()
171      */
172     @Transient
173     public Double getLatitude() {
174 	Double latitude = null;
175 	if (this.location != null) {
176 	    latitude = this.location.getY();
177 	}
178 	return latitude;
179     }
180     
181     /**
182      * @return Returns the longitude (east-west) from the Location
183      *         {@link #getLocation()}.
184      * @see #getLongitude()
185      * @see #getLocation()
186      */
187     @Transient
188     public Double getLongitude() {
189 	Double longitude = null;
190 	if (this.location != null) {
191 	    longitude = this.location.getX();
192 	}
193 	return longitude;
194     }
195 
196     /**
197      * @param location
198      *            the location to set
199      */
200     public void setLocation(Point location) {
201 	this.location = location;
202     }
203 
204 	/**
205 	 * @return the number of the house.
206 	 * it can be null if the house has only a name. but it is typically filled
207 	 */
208     @Column(nullable=true)
209 	public String getNumber() {
210 		return number;
211 	}
212 
213 	/**
214 	 * @param number the number to set
215 	 */
216 	public void setNumber(String number) {
217 		this.number = number;
218 	}
219 
220 	/**
221 	 * @return the type of node. 
222 	 * @see HouseNumberType for details
223 	 */
224 	@Enumerated(EnumType.STRING)
225    // @Column(nullable = false)
226 	public HouseNumberType getType() {
227 		return type;
228 	}
229 
230 	/**
231 	 * @param type the type to set
232 	 */
233 	public void setType(HouseNumberType type) {
234 		this.type = type;
235 	}
236 
237 	/**
238 	 * @return the street associated to this house number
239 	 */
240 	 @ManyToOne(fetch = FetchType.LAZY)//TODO HN
241 	 @JoinColumn(nullable = false, name = "street")
242 	 @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
243 	 @Index(name = "housenumberstreetindex")
244 	public OpenStreetMap getStreet() {
245 		return street;
246 	}
247 
248 	/**
249 	 * @param street the street to set
250 	 */
251 	public void setStreet(OpenStreetMap street) {
252 		this.street = street;
253 	}
254 
255 	@Override
256 	public int hashCode() {
257 		final int prime = 31;
258 		int result = 1;
259 		result = prime * result + ((id == null) ? 0 : id.hashCode());
260 		return result;
261 	}
262 
263 	/*@Override
264 	public boolean equals(Object obj) {
265 		if (this == obj){
266 			return true;
267 		}
268 		if (obj == null){
269 			return false;
270 		}
271 		if (getClass() != obj.getClass()){
272 			return false;
273 		}
274 		HouseNumber other = (HouseNumber) obj;
275 		if (openstreetmapId == null) {
276 			if (other.openstreetmapId != null){
277 				return false;
278 			} else {
279 				return other.is_same(this);
280 			}
281 		} else if (!openstreetmapId.equals(other.openstreetmapId)){
282 			return other.is_same(this);
283 		}
284 		return true;
285 	}
286 */
287 	
288 	 @Override
289 	public boolean equals(Object obj) {
290 		if (this == obj)
291 			return true;
292 		if (obj == null)
293 			return false;
294 		if (getClass() != obj.getClass())
295 			return false;
296 		HouseNumber other = (HouseNumber) obj;
297 		if (id == null) {
298 			if (other.id != null)
299 				return false;
300 		} else if (!id.equals(other.id))
301 			return false;
302 		return true;
303 	}
304 	 
305 	@Override
306 	public String toString() {
307 		return "HouseNumber [id=" + id + ", openstreetmapId=" + openstreetmapId
308 				+ ", number=" + number + ", type=" + type + ", name=" + name
309 				+ ", location=" + location + ", street=" + street
310 				+ ", getId()=" + getId() + ", getOpenstreetmapId()="
311 				+ getOpenstreetmapId() + ", getName()=" + getName()
312 				+ ", getLocation()=" + getLocation() + ", getLatitude()="
313 				+ getLatitude() + ", getLongitude()=" + getLongitude()
314 				+ ", getNumber()=" + getNumber() + ", getType()=" + getType()
315 				+ ", getStreet()=" + getStreet() + ", hashCode()=" + hashCode()
316 				+ ", getClass()=" + getClass() + ", toString()="
317 				+ super.toString() + "]";
318 	}
319 
320 	/**
321 	 * check if address are content identical
322 	 * If number are equals and street (if number is null, we look at the name)
323 	 */
324 	public boolean is_same(Object obj) {
325 		if (this == obj)
326 			return true;
327 		if (obj == null)
328 			return false;
329 		if (getClass() != obj.getClass())
330 			return false;
331 		HouseNumber other = (HouseNumber) obj;
332 		if (number == null) {
333 			if (name == null) {
334 				if (other.name != null)
335 					return false;
336 			} else if (!name.equals(other.name))
337 				return false;
338 		} else if (!number.equals(other.number))
339 			return false;
340 		if (street == null) {
341 			if (other.street != null)
342 				return false;
343 		} else if (!street.equals(other.street))
344 			return false;
345 		return true;
346 	}
347 
348 	public int compareTo(HouseNumber o) {
349 		
350 		return comparator.compare(this, o);
351 	}
352 
353    
354 
355 
356 
357 }