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  
37  import org.hibernate.annotations.Cache;
38  import org.hibernate.annotations.CacheConcurrencyStrategy;
39  import org.hibernate.annotations.Index;
40  
41  import com.gisgraphy.domain.valueobject.AlternateNameSource;
42  
43  /**
44   * Represents an Alternate Name of a feature
45   * 
46   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
47   */
48  @Entity
49  @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
50  @SequenceGenerator(name = "Alternateosmname", sequenceName = "Alternateosmname_sequence")
51  public class AlternateOsmName {
52  
53      private Long id;
54  
55      private Integer alternateNameId;
56  
57      private String name;
58  
59      private OpenStreetMap street;
60  
61      private AlternateNameSource source;
62  
63      private String language;
64  
65  
66      /**
67       * @param name
68       *                The name of the alternate name
69       * @param source
70       *                what is the source the alternate name 
71       * @see AlternateNameSource
72       * 
73       **/                
74      public AlternateOsmName(String name, AlternateNameSource source) {
75  	super();
76  	this.name = name;
77  	this.source = source;
78      }
79  
80      /**
81       * Default constructor (Needed by CGLib)
82       */
83      public AlternateOsmName() {
84  	super();
85      }
86  
87      /**
88       * The datastore id
89       * 
90       * @return The datastore id, it is not a domain value, just a technical One
91       */
92      @Id
93      @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "Alternateosmname")
94      public Long getId() {
95  	return this.id;
96      }
97  
98      /**
99       * Set the datastore id. You should never call this method. It is the
100      * responsability of the dataStore
101      * 
102      * @param id
103      *                The datastore id
104      */
105     public void setId(Long id) {
106 	this.id = id;
107     }
108 
109     /**
110      * @return The name of the current alternate name
111      */
112     //@Index(name = "Alternatenameindex")
113     @Column(nullable = false, length = 200)
114     public String getName() {
115 	return name;
116     }
117 
118     /**
119      * Set the name of the current alternate name
120      * 
121      * @param name
122      *                The name to set
123      */
124     public void setName(String name) {
125 	this.name = name;
126     }
127 
128     /**
129      * The GisFeature, the Alternate name refers to
130      * 
131      * @return the GisFeature, the AlternateName refers to
132      */
133     @ManyToOne(fetch = FetchType.LAZY)
134     @JoinColumn(nullable = false, name = "street")
135     @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
136     @Index(name = "Alternatenameosmnameindex")
137     public OpenStreetMap getStreet() {
138 	return street;
139     }
140 
141     /**
142      * Set the gisFeature, the alternate name refers to
143      * 
144      * @param street
145      *                The street, the alternate name refers to
146      */
147     public void setStreet(OpenStreetMap street) {
148 	this.street = street;
149     }
150 
151     /**
152      * It tells from which files / gazetteers it has been imported
153      * 
154      * @return The source from which the Alternate name come From (from the
155      *         alternate name file, from the dump of the country file, a personal
156      *         add,...)
157      * @see AlternateNameSource
158      */
159     @Enumerated(EnumType.STRING)
160     @Column(nullable = false)
161     public AlternateNameSource getSource() {
162 	return source;
163     }
164 
165     /**
166      * Set the source from which the alternate name come from
167      */
168     public void setSource(AlternateNameSource source) {
169 	this.source = source;
170     }
171 
172     /**
173      * Returns the alternate name id. The id could be null if the
174      * alternate name come from the country dump file.
175      * 
176      * @return The alternateNameId
177      */
178     public Integer getAlternateNameId() {
179 	return alternateNameId;
180     }
181 
182     /**
183      * Set the alternateName id
184      * 
185      * @param alternateNameId
186      *                The id to set
187      */
188     public void setAlternateNameId(Integer alternateNameId) {
189 	this.alternateNameId = alternateNameId;
190     }
191 
192    
193     /**
194      * @return The iso639 Alpha2 or alpha 3 LanguageCode of the
195      *         {@link AlternateOsmName}
196      */
197     @Column(length = 7)
198     public String getLanguage() {
199 	return language;
200     }
201 
202     /**
203      * Set the the iso 639 alpha2 or alpha 3 languageCode for the current
204      * alternate name
205      * 
206      * @param language
207      *                The language to set, it will be automaticaly upercased
208      */
209     public void setLanguage(String language) {
210 	this.language = language == null ? language : language.toUpperCase();
211     }
212 
213     /*
214      * (non-Javadoc)
215      * 
216      * @see java.lang.Object#hashCode()
217      */
218     @Override
219     public int hashCode() {
220 	final int PRIME = 31;
221 	int result = super.hashCode();
222 	result = PRIME * result
223 		+ ((street == null) ? 0 : street.hashCode());
224 	result = PRIME * result + ((name == null) ? 0 : name.hashCode());
225 	return result;
226     }
227 
228     /*
229      * (non-Javadoc)
230      * 
231      * @see java.lang.Object#equals(java.lang.Object)
232      */
233     @Override
234     public boolean equals(Object obj) {
235 	if (this == obj) {
236 	    return true;
237 	}
238 	if (getClass() != obj.getClass()) {
239 	    return false;
240 	}
241 	final AlternateOsmName other = (AlternateOsmName) obj;
242 	if (street == null) {
243 	    if (other.street != null) {
244 		return false;
245 	    }
246 	} else if (!street.equals(other.street)) {
247 	    return false;
248 	}
249 	if (name == null) {
250 	    if (other.name != null) {
251 		return false;
252 	    }
253 	} else if (!name.equals(other.name)) {
254 	    return false;
255 	}
256 	return true;
257     }
258 
259     /*
260      * (non-Javadoc)
261      * 
262      * @see java.lang.Object#toString()
263      */
264     @Override
265     public String toString() {
266 	return name + "[" + alternateNameId + "][linked to "
267 		+ street.getId() + "[lang=" + language + "]";
268     }
269 }