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