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