View Javadoc

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   * Object representing an observing site.
36   * @author Jean-Philippe MINETTI
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  	 * Serial number.
47  	 */
48  	private static final long serialVersionUID = 8737783233977803826L;
49  
50  	/**
51  	 * Identifier given to observing site.
52  	 */
53  	@Id
54  	@GeneratedValue
55  	@Column(name = "ID", nullable = false)
56  	private Long id = null;
57  
58  	/**
59  	 * Name given to observing site.
60  	 */
61  	@Column(name = "NAME", length = 40, nullable = false)
62  	private String name = null;
63  
64  	/**
65  	 * Latitude φ of the geographic position in degrees (°).
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  	 * Longitude λ of the geographic position in degrees (°).
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  	 * Elevation of the geographic position in meters (m).
84  	 */
85  	@Column(name = "ELEVATION", nullable = false)
86  	private short elevation;
87  
88  	/**
89  	 * All points of the celestial limit used to delimit the visible portion of the sky.
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  	 * Algorithm that determine how use the points list to calculate the celestial limits.
99  	 */
100 	@Column(name = "LIMIT_ALGORITHM", length = 20, nullable = false)
101 	@Enumerated(EnumType.STRING)
102 	private CelestialLimitAlgorithm celestialLimitAlgorithm;
103 
104 	/**
105 	 * Constructor.
106 	 */
107 	public ObservingSiteImpl () {
108 		super();
109 	}
110 
111 	/**
112 	 * Constructor.
113 	 * @param observingSite Observing site.
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 	 * (non-Javadoc)
131 	 * @see org.minetti.astrodevice.server.core.object.GeographicCoordinates#getId()
132 	 */
133 	public Long getId () {
134 		return this.id;
135 	}
136 
137 	/*
138 	 * (non-Javadoc)
139 	 * @see org.minetti.astrodevice.server.core.object.GeographicCoordinates#getName()
140 	 */
141 	public String getName () {
142 		return this.name;
143 	}
144 
145 	/*
146 	 * (non-Javadoc)
147 	 * @see org.minetti.astrodevice.server.core.object.ObservingSite#setName(java.lang.String)
148 	 */
149 	public void setName (final String name) {
150 		this.name = name;
151 	}
152 
153 	/*
154 	 * (non-Javadoc)
155 	 * @see org.minetti.astrodevice.server.core.object.GeographicCoordinates#getLatitude()
156 	 */
157 	public Coordinate getLatitude () {
158 		return (this.embeddedLatitude != null ? this.embeddedLatitude.toCoordinate(false) : null);
159 	}
160 
161 	/*
162 	 * (non-Javadoc)
163 	 * @see
164 	 * org.minetti.astrodevice.server.core.object.ObservingSite#setLatitude(org.minetti.astrodevice
165 	 * .common.coordinate .Coordinate)
166 	 */
167 	public void setLatitude (final Coordinate latitude) {
168 		this.embeddedLatitude = (latitude != null ? new EmbeddedSexagesimalCoordinate(false, latitude) : null);
169 	}
170 
171 	/*
172 	 * (non-Javadoc)
173 	 * @see org.minetti.astrodevice.server.core.object.GeographicCoordinates#getLongitude()
174 	 */
175 	public Coordinate getLongitude () {
176 		return (this.embeddedLongitude != null ? this.embeddedLongitude.toCoordinate(false) : null);
177 	}
178 
179 	/*
180 	 * (non-Javadoc)
181 	 * @see
182 	 * org.minetti.astrodevice.server.core.object.ObservingSite#setLongitude(org.minetti.astrodevice
183 	 * .common.coordinate .Coordinate)
184 	 */
185 	public void setLongitude (final Coordinate longitude) {
186 		this.embeddedLongitude = (longitude != null ? new EmbeddedSexagesimalCoordinate(false, longitude) : null);
187 	}
188 
189 	/*
190 	 * (non-Javadoc)
191 	 * @see org.minetti.astrodevice.server.core.object.GeographicCoordinates#getElevation()
192 	 */
193 	public short getElevation () {
194 		return this.elevation;
195 	}
196 
197 	/*
198 	 * (non-Javadoc)
199 	 * @see org.minetti.astrodevice.server.core.object.ObservingSite#setElevation(short)
200 	 */
201 	public void setElevation (final short elevation) {
202 		this.elevation = elevation;
203 	}
204 
205 	/*
206 	 * (non-Javadoc)
207 	 * @see org.minetti.astrodevice.server.core.object.ObservingSite#getCelestialLimitPointSet()
208 	 */
209 	public SortedSet<CelestialLimitPoint> getCelestialLimitPointSet () {
210 		return this.celestialLimitPointSet;
211 	}
212 
213 	/*
214 	 * (non-Javadoc)
215 	 * @see org.minetti.astrodevice.server.core.object.ObservingSite#getCelestialLimitAlgorithm()
216 	 */
217 	public CelestialLimitAlgorithm getCelestialLimitAlgorithm () {
218 		return this.celestialLimitAlgorithm;
219 	}
220 
221 	/*
222 	 * (non-Javadoc)
223 	 * @see
224 	 * org.minetti.astrodevice.server.core.object.ObservingSite#setCelestialLimitAlgorithm(org.minetti
225 	 * .astrodevice.server.core.type.CelestialLimitAlgorithm)
226 	 */
227 	public void setCelestialLimitAlgorithm (final CelestialLimitAlgorithm celestialLimitAlgorithm) {
228 		this.celestialLimitAlgorithm = celestialLimitAlgorithm;
229 	}
230 
231 	/*
232 	 * (non-Javadoc)
233 	 * @see java.lang.Object#hashCode()
234 	 */
235 	@Override
236 	public int hashCode () {
237 		return (this.name != null ? this.name.hashCode() : 0);
238 	}
239 
240 	/*
241 	 * (non-Javadoc)
242 	 * @see java.lang.Object#equals(java.lang.Object)
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 	 * (non-Javadoc)
258 	 * @see java.lang.Object#toString()
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 }