4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
SugEntityAdditionalAttribute.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: SugEntityAdditionalAttribute.java
5  * Description: Class representing additional attribute of entity in suggestion attribute
6  */
7 
8 /**
9  * @file SugEntityAdditionalAttribute.java
10  *
11  * @brief Class representing additional attribute of entity in suggestion attribute
12  * @author Martin Petr
13  */
14 
15 package cz.vutbr.fit.knot.annotations.modules.suggestionManager.attributes;
16 
19 import java.io.Serializable;
20 import java.util.logging.Level;
21 import java.util.logging.Logger;
22 import javax.persistence.Basic;
23 import javax.persistence.Column;
24 import javax.persistence.Entity;
25 import javax.persistence.GeneratedValue;
26 import javax.persistence.GenerationType;
27 import javax.persistence.Id;
28 import javax.persistence.JoinColumn;
29 import javax.persistence.ManyToOne;
30 import javax.persistence.NamedQueries;
31 import javax.persistence.NamedQuery;
32 import javax.persistence.Table;
33 import javax.validation.constraints.NotNull;
34 import javax.validation.constraints.Size;
35 import javax.xml.bind.annotation.XmlRootElement;
36 
37 /**
38  * Additional attribute of entity in suggestion attribute
39  *
40  * @brief Additional attribute of entity in suggestion attribute
41  * @author Martin Petr
42  */
43 @Entity
44 @Table(name = "sugEntityAdditionalAttribute")
45 @XmlRootElement
46 @NamedQueries({
47  @NamedQuery(name = "SugEntityAdditionalAttribute.findAll", query = "SELECT s FROM SugEntityAdditionalAttribute s"),
48  @NamedQuery(name = "SugEntityAdditionalAttribute.findById", query = "SELECT s FROM SugEntityAdditionalAttribute s WHERE s.id = :id"),
49  @NamedQuery(name = "SugEntityAdditionalAttribute.findByName", query = "SELECT s FROM SugEntityAdditionalAttribute s WHERE s.name = :name"),
50  @NamedQuery(name = "SugEntityAdditionalAttribute.findByStringValue", query = "SELECT s FROM SugEntityAdditionalAttribute s WHERE s.stringValue = :stringValue"),
51  @NamedQuery(name = "SugEntityAdditionalAttribute.findByEntityId", query = "SELECT s FROM SugEntityAdditionalAttribute s WHERE s.entityId = :entityId")})
52 public class SugEntityAdditionalAttribute implements Serializable, Comparable<Object> {
53  private static final long serialVersionUID = 1L;
54 
55  /** Id of attribute */
56  @Id
57  @GeneratedValue(strategy = GenerationType.IDENTITY)
58  @Basic(optional = false)
59  @Column(name = "id")
60  private Integer id;
61 
62  /** Name of attribute */
63  @Basic(optional = false)
64  @NotNull
65  @Size(min = 1, max = 255)
66  @Column(name = "name")
67  private String name;
68 
69  /** Type of attribute */
70  @Basic(optional = false)
71  @NotNull
72  @Column(name = "type")
73  private String type;
74 
75  /** Value of attribute */
76  @Basic(optional = true)
77  @Size(min = 0, max = 255)
78  @Column(name = "stringValue")
79  private String stringValue;
80 
81  /** Attribute priority (for order) */
82  @Column(name = "priority")
83  protected Integer priority;
84 
85  /** Id of entity attribute of suggestion to which this additional attribute belongs */
86  @Basic(optional = false)
87  @Column(name = "entityId", nullable = false, insertable = false, updatable = false)
88  private int entityId;
89 
90  /** Entity attribute of suggestion to which this additional attribute belongs */
91  @ManyToOne(optional = false)
92  @JoinColumn(name = "entityId", referencedColumnName = "id")
93  private SugEntityAttribute refEntityAttribute;
94 
95  /**
96  * Constructor
97  */
99  }
100 
101  /**
102  * Constructor
103  *
104  * @param id Id of attribute
105  */
106  public SugEntityAdditionalAttribute(Integer id) {
107  this.id = id;
108  }
109 
110  /**
111  * Constructor
112  *
113  * @param name Name of attribute
114  * @param stringValue Value of attribute
115  * @param refEntityAttribute Entity attribute of suggestion to which this additional attribute belongs
116  */
117  public SugEntityAdditionalAttribute(String name, String stringValue, SugEntityAttribute refEntityAttribute) {
118  this.name = name;
119  this.stringValue = stringValue;
120  this.refEntityAttribute = refEntityAttribute;
121  }
122 
123  /**
124  * Constructor
125  *
126  * @param id Id of attribute
127  * @param name Name of attribute
128  * @param stringValue Value of attribute
129  * @param refEntityAttribute Entity attribute of suggestion to which this additional attribute belongs
130  */
131  public SugEntityAdditionalAttribute(Integer id, String name, String stringValue, SugEntityAttribute refEntityAttribute) {
132  this.id = id;
133  this.name = name;
134  this.stringValue = stringValue;
135  this.refEntityAttribute = refEntityAttribute;
136  }
137 
138  /**
139  * Constructor
140  *
141  * @param name Name of attribute
142  * @param type Type of attribute
143  * @param stringValue Value of attribute
144  * @param refEntityAttribute Entity attribute of suggestion to which this additional attribute belongs
145  */
146  public SugEntityAdditionalAttribute(String name, String type, String stringValue, SugEntityAttribute refEntityAttribute) {
147  this.name = name;
148  this.type = type;
149  this.stringValue = stringValue;
150  this.refEntityAttribute = refEntityAttribute;
151  }
152 
153  /**
154  * Gets id of attribute
155  *
156  * @return Returns id of attribute
157  */
158  public Integer getId() {
159  return id;
160  }
161 
162  /**
163  * Sets id of attribute
164  *
165  * @param id Id of attribute
166  */
167  public void setId(Integer id) {
168  this.id = id;
169  }
170 
171  /**
172  * Gets name of attribute
173  *
174  * @return Returns name of attribute
175  */
176  public String getName() {
177  return name;
178  }
179 
180  /**
181  * Sets name of attribute
182  *
183  * @param name Name of attribute
184  */
185  public void setName(String name) {
186  this.name = name;
187  }
188 
189  /**
190  * Gets type of attribute
191  *
192  * @return Returns type of attribute
193  */
194  public String getType() {
195  return type;
196  }
197 
198  /**
199  * Sets type of attribute
200  *
201  * @param type New type of attribute
202  */
203  public void setType(String type) {
204  this.type = type;
205  }
206 
207  /**
208  * Gets value of attribute
209  *
210  * @return Returns value of attribute
211  */
212  public String getStringValue() {
213  return stringValue;
214  }
215 
216  /**
217  * Sets value of attribute
218  *
219  * @param stringValue Value of attribute
220  */
221  public void setStringValue(String stringValue) {
222  this.stringValue = stringValue;
223  }
224 
225  /**
226  * Gets id of entity attribute of suggestion to which this additional attribute belongs
227  *
228  * @return Returns id of entity attribute of suggestion to which this additional attribute belongs
229  */
230  public int getEntityId() {
231  return entityId;
232  }
233 
234  /**
235  * Sets id of entity attribute of suggestion to which this additional attribute belongs
236  *
237  * @param entityId Id of entity attribute of suggestion to which this additional attribute belongs
238  */
239  public void setEntityId(int entityId) {
240  this.entityId = entityId;
241  }
242 
243  /**
244  * Gets entity attribute of suggestion to which this additional attribute belongs
245  *
246  * @return Returns entity attribute of suggestion to which this additional attribute belongs
247  */
249  return refEntityAttribute;
250  }
251 
252  /**
253  * Sets entity attribute of suggestion to which this additional attribute belongs
254  *
255  * @param refEntityAttribute Entity attribute of suggestion to which this additional attribute belongs
256  */
257  public void setRefEntityAttribute(SugEntityAttribute refEntityAttribute) {
258  this.refEntityAttribute = refEntityAttribute;
259  }
260 
261  /**
262  * Gets attribute priority
263  *
264  * @return Returns attribute priority
265  */
266  public Integer getPriority() {
267  return priority;
268  }
269 
270  /**
271  * Sets attribute priority
272  *
273  * @param priority Attribute priority
274  */
275  public void setPriority(Integer priority) {
276  this.priority = priority;
277  }
278 
279  @Override
280  public int hashCode() {
281  int hash = 0;
282  hash += (id != null ? id.hashCode() : 0);
283  return hash;
284  }
285 
286  /**
287  * Returns serialized informations about attribute in XML for protocol V2
288  * (in Trix for suggestion)
289  *
290  * @return Returns serialized informations about attribute in XML
291  */
292  public String toXmlStringV2(){
293  String result;
294 
295  if (type != null) {
296  int typeIndex = Constants.SIMPLE_TYPES_FOR_ENT_AD_AT.indexOf(type);
297  if (typeIndex >= 0 && !type.equals(Constants.DEFAULT_SIMPLE_TYPE_FOR_ENT_AD_AT)) {
298  // if type is allowed and it is not default one
299  result = "<" + name + " type=\"" + Constants.SIMPLE_TYPES_URIS_FOR_ENT_AD_AT.get(typeIndex)
300  + "\">" + Util.toHTMLString(stringValue) + "</" + name + ">";
301  } else {
302  result = "<" + name + ">" + Util.toHTMLString(stringValue) + "</" + name + ">";
303  }
304  } else {
305  result = "<" + name + ">" + Util.toHTMLString(stringValue) + "</" + name + ">";
306  }
307  return result;
308  }
309 
310  /**
311  * Returns serialized informations about attribute in XML for protocol V2
312  * (in element for getEntities)
313  *
314  * @return Returns serialized informations about attribute in XML for protocol V2
315  */
316  public String toXMLResponseStringV2() {
317  int typeIndex = Constants.SIMPLE_TYPES_FOR_ENT_AD_AT.indexOf(type);
318  if (typeIndex < 0) {
319  typeIndex = Constants.SIMPLE_TYPES_FOR_ENT_AD_AT.indexOf("String");
320  if (typeIndex < 0) {
322  String msg = "URI for string was not found!";
323  Logger.getLogger(SugEntityAdditionalAttribute.class.getName()).log(Level.SEVERE, msg);
324  }
325  }
326  }
327  String typeURI = "";
328  if (typeIndex > 0) {
329  typeURI = Constants.SIMPLE_TYPES_URIS_FOR_ENT_AD_AT.get(typeIndex);
330  }
331  StringBuilder result = new StringBuilder();
332  result.append("<trix:triple><trix:uri>");
333  result.append(Util.toHTMLString(this.refEntityAttribute.getUri()));
334  result.append("</trix:uri><trix:name>");
335  result.append(Util.toHTMLString(name));
336  result.append("</trix:name><trix:typedLiteral datatype=\"").append(typeURI).append("\">");
337  result.append(Util.toHTMLString(stringValue));
338  result.append("</trix:typedLiteral></trix:triple>");
339  return result.toString();
340  }
341 
342  /**
343  * Compares this with other object and returns, whether objects are same type
344  * and have same id.
345  *
346  * @param object Object to compare with
347  * @return If object is same type and have same id, returns true, false otherwise
348  */
349  @Override
350  public boolean equals(Object object) {
351  if (!(object instanceof SugEntityAdditionalAttribute)) {
352  return false;
353  }
354  SugEntityAdditionalAttribute other = (SugEntityAdditionalAttribute) object;
355  if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
356  return false;
357  }
358  return true;
359  }
360 
361  @Override
362  public String toString() {
363  return "cz.vutbr.fit.knot.annotations.modules.suggestionManager.attributes.SugEntityAdditionalAttribute[ id=" + id + " ]";
364  }
365 
366  /**
367  * Compares attributes according to priority
368  *
369  * @param o Object to compare with
370  * @return Returns a negative integer, zero, or a positive integer as this
371  * object is less than, equal to, or greater than the specified object.
372  */
373  @Override
374  public int compareTo(Object o) {
375  if (o == null) {
376  return 1;
377  }
378  if (!(o instanceof SugEntityAdditionalAttribute)) {
379  throw new UnsupportedOperationException("Not supported yet.");
380  }
381  SugEntityAdditionalAttribute other = (SugEntityAdditionalAttribute) o;
382  if (this.priority == null && other.getPriority() == null) {
383  return 0;
384  }
385  if (this.priority == null && other.getPriority() != null) {
386  return -1;
387  }
388  if (this.priority != null && other.getPriority() == null) {
389  return 1;
390  }
391  return this.priority.compareTo(other.getPriority());
392  }
393 
394 } // public class SugEntityAdditionalAttribute
Class representing vocabulary entity attribute for prupose of suggestion.
static final String DEFAULT_SIMPLE_TYPE_FOR_ENT_AD_AT
Definition: Constants.java:225
SugEntityAdditionalAttribute(String name, String stringValue, SugEntityAttribute refEntityAttribute)
static String toHTMLString(String source)
Definition: Util.java:460
SugEntityAdditionalAttribute(String name, String type, String stringValue, SugEntityAttribute refEntityAttribute)
Utility class (manipulates RFC 3339 dates)
Definition: Util.java:29
SugEntityAdditionalAttribute(Integer id, String name, String stringValue, SugEntityAttribute refEntityAttribute)