4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
GeoPointAttribute.java
Go to the documentation of this file.
1 /*
2  * Project: Server for annotations sharing
3  * Author: Ing. Jaroslav Dytrych idytrych@fit.vutbr.cz, Jan Planer xplane01@stud.fit.vutbr.cz
4  * File: GeoPointAttribute.java
5  * Description: Class representing attribute of type GeoPoint and class representing GeoPoint
6  */
7 
8 package cz.vutbr.fit.knot.annotations.entity.attribute;
9 
12 import java.math.BigDecimal;
13 import java.util.ArrayList;
14 import java.util.Map;
15 import javax.persistence.DiscriminatorValue;
16 import javax.persistence.Entity;
17 import javax.persistence.NamedQueries;
18 import javax.persistence.NamedQuery;
19 
20 
21 /**
22  * @file GeoPointAttribute.java
23  *
24  * @brief Class representing attribute of type GeoPoint and class representing GeoPoint
25  */
26 
27 /**
28  * Class representing attribute of type GeoPoint
29  *
30  * @brief Class representing attribute of type GeoPoint
31  */
32 @Entity
33 @DiscriminatorValue("GeoPoint")
34 @NamedQueries({
35  @NamedQuery(name = "Attribute.findByGeoLat", query = "SELECT a FROM Attribute a WHERE a.geoLat = :geoLat"),
36  @NamedQuery(name = "Attribute.findByGeoLong", query = "SELECT a FROM Attribute a WHERE a.geoLong = :geoLong")
37 })
38 public class GeoPointAttribute extends StructuredAttribute {
39 
40  /**
41  * Constructor
42  */
43  public GeoPointAttribute() {
44  }
45 
46  /**
47  * Constructor for GeoPointAttribute
48  *
49  * @param name Name of attribute
50  * @param geoLat Latitude
51  * @param geoLong Longitude
52  * @param refAnnotation Annotation to which this attribute belongs
53  */
54  public GeoPointAttribute(String name, BigDecimal geoLat, BigDecimal geoLong, Annotation refAnnotation) {
55  this.name = name;
56  this.simpleType = "GeoPoint";
57  this.geoLat = geoLat;
58  this.geoLong = geoLong;
59  this.refAnnotation = refAnnotation;
60  }
61 
62  /**
63  * Gets value of the attribute
64  *
65  * @return value of the attribute
66  */
67  @Override
68  public Object getValue() {
69  return new GeoPoint(geoLat, geoLong);
70  }
71 
72  /**
73  * Sets value of the attribute. Value should already be a native java value.
74  *
75  * @param value New value of the attribute
76  */
77  @Override
78  public void setValue(Object value) {
79  this.geoLat = ((GeoPoint) value).getLatitude();
80  this.geoLong = ((GeoPoint) value).getLongitude();
81  }
82 
83  /**
84  * XML of the structured attribute looks like this:
85  * @verbatim
86  * <a:attribute ...... >
87  * <nested>
88  * <value1>...</value1>
89  * <value2>...</value2>
90  * </nested>
91  * </a:attribute>
92  * @endverbatim
93  *
94  * This method returns the content of the a:attribute element with all nested
95  * elements.
96  *
97  * @return Returns the content of the a:attribute element with all nested elements.
98  */
99  @Override
100  protected String getXmlBody() {
101  if (geoLat == null || geoLong == null) {
102  return "";
103  }
104  return "<geo:Point>"
105  + "<geo:lat>" + geoLat.toPlainString() + "</geo:lat> "
106  + "<geo:long>" + geoLong.toPlainString() + "</geo:long>"
107  + "</geo:Point>";
108  }
109 
110  /**
111  * Parses provided values and sets that values as a values of the attribute
112  *
113  * @param values New values of the attribute in raw forms from XML
114  */
115  @Override
116  public void setRawValues(Object values) throws IllegalArgumentException {
117  @SuppressWarnings("unchecked") ArrayList<Object> valuesArray = (ArrayList<Object>)values;
118  @SuppressWarnings("unchecked") Map<String,String> tag = (Map<String,String>)valuesArray.get(0);
119 
120  String latitude = tag.get("geo:lat");
121  String longitude = tag.get("geo:long");
122 
123  try {
124  this.geoLat = new BigDecimal(latitude);
125  this.geoLong = new BigDecimal(longitude);
126  } catch (NumberFormatException e) {
127  throw new IllegalArgumentException("Latitude or Longitude is not valid Decimal value.");
128  }
129  }
130 
131  /**
132  * XML of the structured attribute looks like this:
133  * @verbatim
134  * <a:attribute ...... >
135  * <nested>
136  * <value1>...</value1>
137  * <value2>...</value2>
138  * </nested>
139  * </a:attribute>
140  * @endverbatim
141  *
142  * @return the name of the "nested" attribute , which is "geo:Point" for this type
143  */
144  @Override
145  public String getXmlAttributeName() {
146  return "geo:Point";
147  }
148 
149  /**
150  * Parses provided string value and sets that value as a values of the attribute
151  *
152  * This method can be used for parse value from string received in JSON object
153  *
154  * @param GPointEncoded New values of the attribute in String
155  */
156  public void setStringValue(String GPointEncoded) throws IllegalArgumentException {
157  // empty value of the attribute
158  if (GPointEncoded.equals("null")) {
159  this.geoLat = null;
160  this.geoLong = null;
161  } else {
162  // points value not null
163  String[] Points = GPointEncoded.split(" ");
164  if (Points.length == 2) {
165  try {
166  this.geoLat = new BigDecimal(Points[0]);
167  this.geoLong = new BigDecimal(Points[1]);
168  } catch (NumberFormatException e) {
169  throw new IllegalArgumentException("Latitude or Longitude is not valid Decimal value.");
170  }
171  } else {
172  throw new IllegalArgumentException("Too many words in String.");
173  }
174  }
175  }
176 
177  public void setStringValue(String latitude, String longitude) throws IllegalArgumentException{
178  if(latitude == null && longitude == null){
179  return;
180  }
181 
182  if(latitude == null || longitude == null){
183  throw new IllegalArgumentException("Latitude or Longitude value is null");
184  }
185 
186  try{
187  this.geoLat = new BigDecimal(latitude);
188  this.geoLong = new BigDecimal(longitude);
189  } catch (NumberFormatException e) {
190  throw new IllegalArgumentException("Latitude or Longitude is not valid Decimal value.");
191  }
192  }
193 
194  /**
195  * Encode values of the attribute to one string
196  *
197  * This method can be used for generating value for JSON object
198  *
199  * @return Encoded values of the attribute in one string
200  */
201  @Override
202  public String toString() {
203  StringBuilder sb = new StringBuilder();
204  if (this.geoLat != null) {
205  sb.append(this.geoLat.toString());
206  }
207  sb.append(' ');
208  if (this.geoLong != null) {
209  sb.append(this.geoLong.toString());
210  }
211  return sb.toString();
212  }
213 
214  /**
215  * Gets URI addres in ontology for this type of attribute
216  *
217  * @return Return URI addres in ontology for this type of attribute.
218  */
219  @Override
220  public String getTypeOntologyUri(){
222  }
223 
224  /**
225  * This class represents GeoPoint class. Each GeoPoint is determined by two
226  * values: latitude and longitude
227  *
228  * @brief This class represents GeoPoint value.
229  * @author Jan Planer
230  */
231  public static class GeoPoint {
232  /** Longitude */
233  BigDecimal longitude;
234  /** Lattitude */
235  BigDecimal latitude;
236 
237  /**
238  * Constructor
239  */
240  public GeoPoint() {
241  }
242 
243  /**
244  * Creates new GeoPoint
245  *
246  * @param latitude latitude of the new GeoPoint
247  * @param longitude longitude of the new GeoPoint
248  */
249  public GeoPoint(BigDecimal latitude, BigDecimal longitude) {
250  this.latitude = latitude;
251  this.longitude = longitude;
252  }
253 
254  /**
255  * Gets longitude
256  *
257  * @return longitude
258  */
259  public BigDecimal getLongitude() {
260  return longitude;
261  }
262 
263  /**
264  * Gets latitude
265  *
266  * @return latitude
267  */
268  public BigDecimal getLatitude() {
269  return latitude;
270  }
271 
272  /**
273  * Sets latitude
274  *
275  * @param value new latitude
276  */
277  public void setLatitude(BigDecimal value) {
278  this.latitude = value;
279  }
280 
281  /**
282  * Sets longitude
283  *
284  * @param value new longitude
285  */
286  public void setLongitude(BigDecimal value) {
287  this.longitude = value;
288  }
289  } // public static class GeoPoint
290 } // public class GeoPointAttribute
291 
292 
Abstract class representing more complex attribute.
GeoPointAttribute(String name, BigDecimal geoLat, BigDecimal geoLong, Annotation refAnnotation)