View Javadoc

1   package org.minetti.astrodevice.server.hibernate.object;
2   
3   import java.io.Serializable;
4   import java.util.HashMap;
5   import java.util.Map;
6   import javax.persistence.Column;
7   import javax.persistence.Entity;
8   import javax.persistence.GeneratedValue;
9   import javax.persistence.Id;
10  import javax.persistence.Lob;
11  import javax.persistence.NamedQueries;
12  import javax.persistence.NamedQuery;
13  import javax.persistence.Table;
14  import javax.persistence.Transient;
15  import org.hibernate.annotations.Index;
16  import org.minetti.astrodevice.common.device.DeviceSignature;
17  import org.minetti.astrodevice.server.core.object.DeviceSpecification;
18  import org.minetti.astrodevice.server.plugin.device.BaseDeviceSignature;
19  
20  /**
21   * Object representing the specifications on a device.
22   * @author Jean-Philippe MINETTI
23   */
24  @Table(name = "DEVICE")
25  @org.hibernate.annotations.Table(appliesTo = "DEVICE", indexes = { @Index(name = "K_DEVICE_SIGNATURE", columnNames = { "BUS", "PRODUCT_ID", "PRODUCT_DESCRIPTION",
26  		"PRODUCT_VERSION", "SERIAL_NUMBER" }) })
27  @NamedQueries({
28  		@NamedQuery(name = "allDeviceSpecificationQuery", query = "FROM DeviceSpecificationImpl t ORDER BY t.name"),
29  		@NamedQuery(name = "pluginDeviceSpecificationQuery", query = "FROM DeviceSpecificationImpl t WHERE (t.pluginClassName=:pluginClassName) ORDER BY t.name"),
30  		@NamedQuery(name = "sameProductDeviceSpecificationQuery", query = "FROM DeviceSpecificationImpl t WHERE (t.bus=:bus) and (t.productId=:productId) and (t.productDescription=:productDescription) and (t.productVersion=:productVersion) ORDER BY t.name"),
31  		@NamedQuery(name = "sameProductDeviceSpecificationQueryWithNullVersion", query = "FROM DeviceSpecificationImpl t WHERE (t.bus=:bus) and (t.productId=:productId) and (t.productDescription=:productDescription) and (t.productVersion is null) ORDER BY t.name"),
32  		@NamedQuery(name = "signatureDeviceSpecificationQuery", query = "FROM DeviceSpecificationImpl t WHERE (t.bus=:bus) and (t.productId=:productId) and (t.productDescription=:productDescription) and (t.productVersion=:productVersion) and (t.serialNumber=:serialNumber) ORDER BY t.name"),
33  		@NamedQuery(name = "signatureDeviceSpecificationQueryWithNullVersion", query = "FROM DeviceSpecificationImpl t WHERE (t.bus=:bus) and (t.productId=:productId) and (t.productDescription=:productDescription) and (t.productVersion is null) and (t.serialNumber=:serialNumber) ORDER BY t.name"),
34  		@NamedQuery(name = "signatureDeviceSpecificationQueryWithNullSerialNumber", query = "FROM DeviceSpecificationImpl t WHERE (t.bus=:bus) and (t.productId=:productId) and (t.productDescription=:productDescription) and (t.productVersion=:productVersion) and (t.serialNumber is null) ORDER BY t.name"),
35  		@NamedQuery(name = "signatureDeviceSpecificationQueryWithNullVersionAndSerialNumber", query = "FROM DeviceSpecificationImpl t WHERE (t.bus=:bus) and (t.productId=:productId) and (t.productDescription=:productDescription) and (t.productVersion is null) and (t.serialNumber is null) ORDER BY t.name") })
36  @Entity
37  public final class DeviceSpecificationImpl
38  		implements DeviceSpecification, Serializable {
39  
40  	/**
41  	 * Serial number.
42  	 */
43  	private static final long serialVersionUID = -7709095111542974139L;
44  
45  	/**
46  	 * Signature used for recognize the device.
47  	 */
48  	protected final class DeviceSignatureImpl
49  			extends BaseDeviceSignature {
50  
51  		/**
52  		 * Constructor.
53  		 */
54  		protected DeviceSignatureImpl () {
55  			super();
56  		}
57  
58  		/*
59  		 * (non-Javadoc)
60  		 * @see org.minetti.astrodevice.common.device.DeviceSignature#getBus()
61  		 */
62  		public String getBus () {
63  			return DeviceSpecificationImpl.this.bus;
64  		}
65  
66  		/*
67  		 * (non-Javadoc)
68  		 * @see org.minetti.astrodevice.common.device.DeviceSignature#getProductId()
69  		 */
70  		public String getProductId () {
71  			return DeviceSpecificationImpl.this.productId;
72  		}
73  
74  		/*
75  		 * (non-Javadoc)
76  		 * @see org.minetti.astrodevice.common.device.DeviceSignature#getProductDescription()
77  		 */
78  		public String getProductDescription () {
79  			return DeviceSpecificationImpl.this.productDescription;
80  		}
81  
82  		/*
83  		 * (non-Javadoc)
84  		 * @see org.minetti.astrodevice.common.device.DeviceSignature#getProductVersion()
85  		 */
86  		public String getProductVersion () {
87  			return DeviceSpecificationImpl.this.productVersion;
88  		}
89  
90  		/*
91  		 * (non-Javadoc)
92  		 * @see org.minetti.astrodevice.common.device.DeviceSignature#getSerialNumber()
93  		 */
94  		public String getSerialNumber () {
95  			return DeviceSpecificationImpl.this.serialNumber;
96  		}
97  
98  		/*
99  		 * (non-Javadoc)
100 		 * @see org.minetti.astrodevice.common.device.DeviceSignature#getProperties()
101 		 */
102 		public Map<String, Object> getProperties () {
103 			return DeviceSpecificationImpl.this.properties;
104 		}
105 
106 	}
107 
108 	/**
109 	 * Identifier given to device.
110 	 */
111 	@Id
112 	@GeneratedValue
113 	@Column(name = "ID", nullable = false)
114 	private Long id = null;
115 
116 	/**
117 	 * Name given to device.
118 	 */
119 	@Column(name = "NAME", length = 40, nullable = false, unique = true)
120 	private String name = null;
121 
122 	/**
123 	 * Class name of plug-in related to device.
124 	 */
125 	@Column(name = "PLUGIN", nullable = false)
126 	@Index(name = "K_DEVICE_PLUGIN")
127 	private String pluginClassName = null;
128 
129 	/**
130 	 * Bus name where the device is connected (never NULL).
131 	 */
132 	@Column(name = "BUS", length = 15, nullable = false)
133 	protected String bus = null;
134 
135 	/**
136 	 * Unique identifier identifying the device product (never NULL).
137 	 */
138 	@Column(name = "PRODUCT_ID", length = 15, nullable = false)
139 	protected String productId = null;
140 
141 	/**
142 	 * Product description of device (never NULL).
143 	 */
144 	@Column(name = "PRODUCT_DESCRIPTION", length = 255, nullable = false)
145 	protected String productDescription = null;
146 
147 	/**
148 	 * Product version of device (can be NULL).
149 	 */
150 	@Column(name = "PRODUCT_VERSION", length = 8)
151 	protected String productVersion = null;
152 
153 	/**
154 	 * Serial number of device or other data identifying the devices of the same product (can be
155 	 * NULL).
156 	 */
157 	@Column(name = "SERIAL_NUMBER", length = 255)
158 	protected String serialNumber = null;
159 
160 	/**
161 	 * Map containing all properties of the device (never NULL).
162 	 */
163 	@Column(name = "PROPERTIES", nullable = false)
164 	@Lob
165 	protected HashMap<String, Object> properties = null;
166 
167 	/**
168 	 * Signature used for recognize the device.
169 	 */
170 	@Transient
171 	private final DeviceSignature signature = new DeviceSignatureImpl();
172 
173 	/**
174 	 * Constructor.
175 	 */
176 	public DeviceSpecificationImpl () {
177 		super();
178 	}
179 
180 	/**
181 	 * Constructor.
182 	 * @param deviceSpecification Object representing the specifications on a device.
183 	 */
184 	public DeviceSpecificationImpl (final DeviceSpecification deviceSpecification) {
185 		super();
186 		this.name = deviceSpecification.getName();
187 		this.pluginClassName = deviceSpecification.getPluginClassName();
188 		this.bus = deviceSpecification.getSignature().getBus();
189 		this.productId = deviceSpecification.getSignature().getProductId();
190 		this.productDescription = deviceSpecification.getSignature().getProductDescription();
191 		this.productVersion = deviceSpecification.getSignature().getProductVersion();
192 		this.serialNumber = deviceSpecification.getSignature().getSerialNumber();
193 		this.properties = new HashMap<String, Object>(deviceSpecification.getSignature().getProperties());
194 	}
195 
196 	/*
197 	 * (non-Javadoc)
198 	 * @see org.minetti.astrodevice.server.core.object.DeviceSpecification#getId()
199 	 */
200 	public final Long getId () {
201 		return this.id;
202 	}
203 
204 	/*
205 	 * (non-Javadoc)
206 	 * @see org.minetti.astrodevice.server.core.object.DeviceSpecification#getName()
207 	 */
208 	public final String getName () {
209 		return this.name;
210 	}
211 
212 	/*
213 	 * (non-Javadoc)
214 	 * @see org.minetti.astrodevice.server.core.object.DeviceSpecification#setName(java.lang.String)
215 	 */
216 	public final void setName (final String name) {
217 		this.name = name;
218 	}
219 
220 	/*
221 	 * (non-Javadoc)
222 	 * @see org.minetti.astrodevice.server.core.object.DeviceSpecification#getPluginClassName()
223 	 */
224 	public final String getPluginClassName () {
225 		return this.pluginClassName;
226 	}
227 
228 	/*
229 	 * (non-Javadoc)
230 	 * @see
231 	 * org.minetti.astrodevice.server.core.object.DeviceSpecification#setPluginClassName(java.lang
232 	 * .String)
233 	 */
234 	public final void setPluginClassName (final String pluginClassName) {
235 		this.pluginClassName = pluginClassName;
236 	}
237 
238 	/*
239 	 * (non-Javadoc)
240 	 * @see org.minetti.astrodevice.server.core.object.DeviceSpecification#getSignature()
241 	 */
242 	public final DeviceSignature getSignature () {
243 		return this.signature;
244 	}
245 
246 	/*
247 	 * (non-Javadoc)
248 	 * @see java.lang.Object#hashCode()
249 	 */
250 	@Override
251 	public final int hashCode () {
252 		return (this.name != null ? this.name.hashCode() : 0);
253 	}
254 
255 	/*
256 	 * (non-Javadoc)
257 	 * @see java.lang.Object#equals(java.lang.Object)
258 	 */
259 	@Override
260 	public final boolean equals (final Object obj) {
261 		boolean result = false;
262 		if (obj == this) {
263 			result = true;
264 		}
265 		else if ((obj instanceof DeviceSpecification) && (this.name != null)) {
266 			result = this.name.equals(((DeviceSpecification) obj).getName());
267 		}
268 		return result;
269 	}
270 
271 	/*
272 	 * (non-Javadoc)
273 	 * @see java.lang.Object#toString()
274 	 */
275 	@Override
276 	public final String toString () {
277 		return this.name + " - " + this.signature;
278 	}
279 
280 }