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.FetchType;
28  import javax.persistence.GeneratedValue;
29  import javax.persistence.GenerationType;
30  import javax.persistence.Id;
31  import javax.persistence.JoinColumn;
32  import javax.persistence.ManyToOne;
33  import javax.persistence.SequenceGenerator;
34  
35  import org.hibernate.annotations.Cache;
36  import org.hibernate.annotations.CacheConcurrencyStrategy;
37  import org.hibernate.annotations.Index;
38  
39  /**
40   * Represents a {@link ZipCode}.
41   * 
42   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
43   */
44  @Entity
45  @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
46  @SequenceGenerator(name = "zipcode", sequenceName = "zipcode_sequence")
47  public class ZipCode {
48  
49      private Long id;
50  
51      private String code;
52  
53      private GisFeature gisFeature;
54  
55      /**
56       * /** Default Constructor
57       */
58      public ZipCode() {
59      }
60  
61      /**
62       * @param code
63       *            the code of the zipcode (will be uppercased)
64       */
65      public ZipCode(String code) {
66  	if (code != null) {
67  	    this.code = code.toUpperCase();
68  	} else {
69  	    throw new IllegalArgumentException("Can not create a zip code with null code");
70  	}
71      }
72  
73      /**
74       * The datastore id
75       * 
76       * @return The datastore id, it is not a domain value, just a technical One
77       */
78      @Id
79      @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "zipcode")
80      public Long getId() {
81  	return id;
82      }
83  
84      /**
85       * @param id
86       *            the id to set
87       */
88      public void setId(Long id) {
89  	this.id = id;
90      }
91  
92      /**
93       * @return the code
94       */
95      @Column(nullable = false, length = 80)
96      @Index(name = "zipcode_code")
97      public String getCode() {
98  	return code;
99      }
100 
101     /**
102      * @param code
103      *            the code to set, (will be uppercased)
104      */
105     public void setCode(String code) {
106 	if (code!=null){
107 	    this.code = code.toUpperCase();
108 	} else {
109 	    this.code =null;
110 	}
111     }
112 
113     /**
114      * @param gisFeature
115      *            the gisfeature of the zipcode
116      */
117     public void setGisFeature(GisFeature gisFeature) {
118 	this.gisFeature = gisFeature;
119     }
120 
121     /**
122      * @return the gisFeature associated to this zip code
123      */
124     @ManyToOne(fetch = FetchType.LAZY)
125     @JoinColumn(nullable = true, name = "gisFeature")
126     @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
127     @Index(name = "zipcodefeatureidindex")
128     public GisFeature getGisFeature() {
129 	return this.gisFeature;
130     }
131 
132     @Override
133     public String toString() {
134 	return this.code;
135     }
136 
137     /*
138      * (non-Javadoc)
139      * 
140      * @see java.lang.Object#hashCode()
141      */
142     @Override
143     public int hashCode() {
144 	final int prime = 31;
145 	int result = 1;
146 	result = prime * result + ((code == null) ? 0 : code.hashCode());
147 	return result;
148     }
149 
150     /*
151      * (non-Javadoc)
152      * 
153      * @see java.lang.Object#equals(java.lang.Object)
154      */
155     @Override
156     public boolean equals(Object obj) {
157 	if (this == obj)
158 	    return true;
159 	if (obj == null)
160 	    return false;
161 	if (getClass() != obj.getClass())
162 	    return false;
163 	final ZipCode other = (ZipCode) obj;
164 	if (code == null) {
165 	    if (other.code != null)
166 		return false;
167 	} else if (!code.equals(other.code))
168 	    return false;
169 	return true;
170     }
171 
172 }