1 package org.minetti.astrodevice.server.hibernate.object;
2
3 import java.io.Serializable;
4 import java.util.SortedSet;
5 import java.util.TreeSet;
6 import javax.persistence.AttributeOverride;
7 import javax.persistence.AttributeOverrides;
8 import javax.persistence.CascadeType;
9 import javax.persistence.Column;
10 import javax.persistence.Embedded;
11 import javax.persistence.Entity;
12 import javax.persistence.EnumType;
13 import javax.persistence.Enumerated;
14 import javax.persistence.FetchType;
15 import javax.persistence.GeneratedValue;
16 import javax.persistence.Id;
17 import javax.persistence.JoinColumn;
18 import javax.persistence.NamedQueries;
19 import javax.persistence.NamedQuery;
20 import javax.persistence.OneToMany;
21 import javax.persistence.Table;
22 import javax.persistence.UniqueConstraint;
23 import org.hibernate.annotations.ForeignKey;
24 import org.hibernate.annotations.Sort;
25 import org.hibernate.annotations.SortType;
26 import org.minetti.astrodevice.common.coordinate.Coordinate;
27 import org.minetti.astrodevice.common.coordinate.format.CoordinateFormat;
28 import org.minetti.astrodevice.server.core.object.CelestialLimitPoint;
29 import org.minetti.astrodevice.server.core.object.ObservingSite;
30 import org.minetti.astrodevice.server.core.object.comparator.CelestialLimitPointComparator;
31 import org.minetti.astrodevice.server.core.type.CelestialLimitAlgorithm;
32 import org.minetti.astrodevice.server.hibernate.object.embedded.EmbeddedSexagesimalCoordinate;
33
34
35
36
37
38 @Table(name = "OBSERVING_SITE", uniqueConstraints = { @UniqueConstraint(name = "K_OBSERVING_SITE_NAME", columnNames = { "NAME" }),
39 @UniqueConstraint(columnNames = { "LATITUDE_DEG", "LATITUDE_MIN", "LATITUDE_SEC", "LONGITUDE_DEG", "LONGITUDE_MIN", "LONGITUDE_SEC" }) })
40 @NamedQueries({ @NamedQuery(name = "allObservingSiteQuery", query = "FROM ObservingSiteImpl t ORDER BY t.name") })
41 @Entity
42 public final class ObservingSiteImpl
43 implements ObservingSite, Serializable {
44
45
46
47
48 private static final long serialVersionUID = 8737783233977803826L;
49
50
51
52
53 @Id
54 @GeneratedValue
55 @Column(name = "ID", nullable = false)
56 private Long id = null;
57
58
59
60
61 @Column(name = "NAME", length = 40, nullable = false)
62 private String name = null;
63
64
65
66
67 @Embedded
68 @AttributeOverrides({ @AttributeOverride(name = "angle", column = @Column(name = "LATITUDE_DEG", nullable = false)),
69 @AttributeOverride(name = "minutes", column = @Column(name = "LATITUDE_MIN", nullable = false)),
70 @AttributeOverride(name = "seconds", column = @Column(name = "LATITUDE_SEC", nullable = false)) })
71 private EmbeddedSexagesimalCoordinate embeddedLatitude;
72
73
74
75
76 @Embedded
77 @AttributeOverrides({ @AttributeOverride(name = "angle", column = @Column(name = "LONGITUDE_DEG", nullable = false)),
78 @AttributeOverride(name = "minutes", column = @Column(name = "LONGITUDE_MIN", nullable = false)),
79 @AttributeOverride(name = "seconds", column = @Column(name = "LONGITUDE_SEC", nullable = false)) })
80 private EmbeddedSexagesimalCoordinate embeddedLongitude;
81
82
83
84
85 @Column(name = "ELEVATION", nullable = false)
86 private short elevation;
87
88
89
90
91 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, targetEntity = CelestialLimitPointImpl.class)
92 @JoinColumn(name = "OBSERVING_SITE_ID", nullable = false)
93 @Sort(type = SortType.COMPARATOR, comparator = CelestialLimitPointComparator.class)
94 @ForeignKey(name = "FK_LIMIT_POINT_OBSERVING_SITE")
95 private SortedSet<CelestialLimitPoint> celestialLimitPointSet;
96
97
98
99
100 @Column(name = "LIMIT_ALGORITHM", length = 20, nullable = false)
101 @Enumerated(EnumType.STRING)
102 private CelestialLimitAlgorithm celestialLimitAlgorithm;
103
104
105
106
107 public ObservingSiteImpl () {
108 super();
109 }
110
111
112
113
114
115 public ObservingSiteImpl (final ObservingSite observingSite) {
116 super();
117 this.id = observingSite.getId();
118 this.name = observingSite.getName();
119 setLatitude(observingSite.getLatitude());
120 setLongitude(observingSite.getLongitude());
121 this.elevation = observingSite.getElevation();
122 this.celestialLimitPointSet = new TreeSet<CelestialLimitPoint>(new CelestialLimitPointComparator());
123 for (final CelestialLimitPoint celestialLimitPoint : observingSite.getCelestialLimitPointSet()) {
124 this.celestialLimitPointSet.add(new CelestialLimitPointImpl(celestialLimitPoint));
125 }
126 this.celestialLimitAlgorithm = observingSite.getCelestialLimitAlgorithm();
127 }
128
129
130
131
132
133 public Long getId () {
134 return this.id;
135 }
136
137
138
139
140
141 public String getName () {
142 return this.name;
143 }
144
145
146
147
148
149 public void setName (final String name) {
150 this.name = name;
151 }
152
153
154
155
156
157 public Coordinate getLatitude () {
158 return (this.embeddedLatitude != null ? this.embeddedLatitude.toCoordinate(false) : null);
159 }
160
161
162
163
164
165
166
167 public void setLatitude (final Coordinate latitude) {
168 this.embeddedLatitude = (latitude != null ? new EmbeddedSexagesimalCoordinate(false, latitude) : null);
169 }
170
171
172
173
174
175 public Coordinate getLongitude () {
176 return (this.embeddedLongitude != null ? this.embeddedLongitude.toCoordinate(false) : null);
177 }
178
179
180
181
182
183
184
185 public void setLongitude (final Coordinate longitude) {
186 this.embeddedLongitude = (longitude != null ? new EmbeddedSexagesimalCoordinate(false, longitude) : null);
187 }
188
189
190
191
192
193 public short getElevation () {
194 return this.elevation;
195 }
196
197
198
199
200
201 public void setElevation (final short elevation) {
202 this.elevation = elevation;
203 }
204
205
206
207
208
209 public SortedSet<CelestialLimitPoint> getCelestialLimitPointSet () {
210 return this.celestialLimitPointSet;
211 }
212
213
214
215
216
217 public CelestialLimitAlgorithm getCelestialLimitAlgorithm () {
218 return this.celestialLimitAlgorithm;
219 }
220
221
222
223
224
225
226
227 public void setCelestialLimitAlgorithm (final CelestialLimitAlgorithm celestialLimitAlgorithm) {
228 this.celestialLimitAlgorithm = celestialLimitAlgorithm;
229 }
230
231
232
233
234
235 @Override
236 public int hashCode () {
237 return (this.name != null ? this.name.hashCode() : 0);
238 }
239
240
241
242
243
244 @Override
245 public boolean equals (final Object obj) {
246 boolean result = false;
247 if (obj == this) {
248 result = true;
249 }
250 else if ((obj instanceof ObservingSite) && (this.name != null)) {
251 result = this.name.equals(((ObservingSite) obj).getName());
252 }
253 return result;
254 }
255
256
257
258
259
260 @Override
261 public String toString () {
262 return this.name + " (" + CoordinateFormat.getSexagesimalLatitudeInstance(0.1).format(getLatitude()) + ", "
263 + CoordinateFormat.getSexagesimalLongitudeInstance(0.1).format(getLongitude()) + ", " + this.elevation + "m)";
264 }
265
266 }