4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
EntityAdditionalAttribute.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: EntityAdditionalAttribute.java
5  * Description: Class representing additional attribute of entity in annotation attribute
6  */
7 
8 /**
9  * @file EntityAdditionalAttribute.java
10  *
11  * @brief Class representing additional attribute of entity in annotation attribute
12  * @author Martin Petr
13  */
14 
15 package cz.vutbr.fit.knot.annotations.entity.attribute;
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 annotation attribute
39  *
40  * @brief Additional attribute of entity in annotation attribute
41  * @author Martin Petr
42  */
43 @Entity
44 @Table(name = "entityAdditionalAttribute")
45 @XmlRootElement
46 @NamedQueries({
47  @NamedQuery(name = "EntityAdditionalAttribute.findAll", query = "SELECT e FROM EntityAdditionalAttribute e"),
48  @NamedQuery(name = "EntityAdditionalAttribute.findById", query = "SELECT e FROM EntityAdditionalAttribute e WHERE e.id = :id"),
49  @NamedQuery(name = "EntityAdditionalAttribute.findByName", query = "SELECT e FROM EntityAdditionalAttribute e WHERE e.name = :name"),
50  @NamedQuery(name = "EntityAdditionalAttribute.findByStringValue", query = "SELECT e FROM EntityAdditionalAttribute e WHERE e.stringValue = :stringValue"),
51  @NamedQuery(name = "EntityAdditionalAttribute.findByEntity", query = "SELECT e FROM EntityAdditionalAttribute e WHERE e.entityId = :entity")})
52 public class EntityAdditionalAttribute 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 annotation 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 annotation to which this additional attribute belongs */
91  @ManyToOne(optional = false)
92  @JoinColumn(name = "entityId", referencedColumnName = "id")
93  private EntityAttribute refEntityAttribute;
94 
95  /**
96  * Constructor
97  */
99  }
100 
101  /**
102  * Constructor
103  *
104  * @param id Id of attribute
105  */
106  public EntityAdditionalAttribute(Integer id) {
107  this.id = id;
108  }
109 
110  /**
111  * Constructor
112  *
113  * @param id Id of attribute
114  * @param name Name of attribute
115  * @param stringValue Value of attribute
116  * @param refEntityAttribute Entity attribute of annotation to which this additional attribute belongs
117  */
118  public EntityAdditionalAttribute(Integer id, String name, String stringValue, EntityAttribute refEntityAttribute) {
119  this.id = id;
120  this.name = name;
121  this.stringValue = stringValue;
122  this.refEntityAttribute = refEntityAttribute;
123  }
124 
125  /**
126  * Constructor
127  *
128  * @param name Name of attribute
129  * @param stringValue Value of attribute
130  * @param refEntityAttribute Entity attribute of annotation to which this additional attribute belongs
131  */
132  public EntityAdditionalAttribute(String name, String stringValue, EntityAttribute refEntityAttribute) {
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 annotation to which this additional attribute belongs
145  */
146  public EntityAdditionalAttribute(String name, String type, String stringValue, EntityAttribute 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 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 attridbute
182  *
183  * @param name Returns 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 annotation to which this additional attribute belongs
227  *
228  * @return Returns id of entity attribute of annotation to which this additional attribute belongs
229  */
230  public int getEntityId() {
231  return entityId;
232  }
233 
234  /**
235  * Gets entity attribute of annotation to which this additional attribute belongs
236  *
237  * @return Returns entity attribute of annotation to which this additional attribute belongs
238  */
240  return refEntityAttribute;
241  }
242 
243  /**
244  * Sets entity attribute of annotation to which this additional attribute belongs
245  *
246  * @param refEntityAttribute Entity attribute of annotation to which this additional attribute belongs
247  */
248  public void setRefEntityAttribute(EntityAttribute refEntityAttribute) {
249  this.refEntityAttribute = refEntityAttribute;
250  }
251 
252  /**
253  * Gets attribute priority
254  *
255  * @return Returns attribute priority
256  */
257  public Integer getPriority() {
258  return priority;
259  }
260 
261  /**
262  * Sets attribute priority
263  *
264  * @param priority Attribute priority
265  */
266  public void setPriority(Integer priority) {
267  this.priority = priority;
268  }
269 
270  /**
271  * Returns serialized informations about attribute in XML for protocol V2
272  * (in Trix for annotation)
273  *
274  * @return Returns serialized informations about attribute in XML
275  */
276  public String toXmlStringV2(){
277  int typeIndex = Constants.SIMPLE_TYPES_FOR_ENT_AD_AT.indexOf(type);
278  if (typeIndex < 0) {
279  typeIndex = Constants.SIMPLE_TYPES_FOR_ENT_AD_AT.indexOf("String");
280  if (typeIndex < 0) {
282  String msg = "URI for string was not found!";
283  Logger.getLogger(EntityAdditionalAttribute.class.getName()).log(Level.SEVERE, msg);
284  }
285  }
286  }
287  String typeURI = "";
288  if (typeIndex > 0) {
289  typeURI = Constants.SIMPLE_TYPES_URIS_FOR_ENT_AD_AT.get(typeIndex);
290  }
291  StringBuilder result = new StringBuilder();
292  result.append("<trix:triple><trix:uri>");
293  result.append(Util.toHTMLString(this.refEntityAttribute.getUri()));
294  result.append("</trix:uri><trix:name>");
295  result.append(Util.toHTMLString(name));
296  result.append("</trix:name><trix:typedLiteral datatype=\"").append(typeURI).append("\">");
297  result.append(Util.toHTMLString(stringValue));
298  result.append("</trix:typedLiteral></trix:triple>");
299  return result.toString();
300  }
301 
302  /**
303  * Returns serialized informations about attribute in XML for protocol V2
304  * (in element for getEntities)
305  *
306  * @return Returns serialized informations about attribute in XML for protocol V2
307  */
308  public String toXMLResponseStringV2() {
309  String result;
310 
311  if (type != null) {
312  int typeIndex = Constants.SIMPLE_TYPES_FOR_ENT_AD_AT.indexOf(type);
313  if (typeIndex >= 0 && !type.equals(Constants.DEFAULT_SIMPLE_TYPE_FOR_ENT_AD_AT)) {
314  // if type is allowed and it is not default one
315  result = "<" + name + " type=\"" + Constants.SIMPLE_TYPES_URIS_FOR_ENT_AD_AT.get(typeIndex)
316  + "\">" + Util.toHTMLString(stringValue) + "</" + name + ">";
317  } else {
318  result = "<" + name + ">" + Util.toHTMLString(stringValue) + "</" + name + ">";
319  }
320  } else {
321  result = "<" + name + ">" + Util.toHTMLString(stringValue) + "</" + name + ">";
322  }
323  return result;
324  }
325 
326  @Override
327  public int hashCode() {
328  int hash = 0;
329  hash += (id != null ? id.hashCode() : 0);
330  return hash;
331  }
332 
333  /**
334  * Compares this with other object and returns, whether objects are same type
335  * and have same id.
336  *
337  * @param object Object to compare with
338  * @return If object is same type and have same id, returns true, false otherwise
339  */
340  @Override
341  public boolean equals(Object object) {
342  if (!(object instanceof EntityAdditionalAttribute)) {
343  return false;
344  }
345  EntityAdditionalAttribute other = (EntityAdditionalAttribute) object;
346  if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
347  return false;
348  }
349  return true;
350  }
351 
352  @Override
353  public String toString() {
354  return "cz.vutbr.fit.knot.annotations.entity.attribute.EntityAdditionalAttribute[ id=" + id + " ]";
355  }
356 
357  /**
358  * Compares attributes according to priority
359  *
360  * @param o Object to compare with
361  * @return Returns a negative integer, zero, or a positive integer as this
362  * object is less than, equal to, or greater than the specified object.
363  */
364  @Override
365  public int compareTo(Object o) {
366  if (o == null) {
367  return 1;
368  }
369  if (!(o instanceof EntityAdditionalAttribute)) {
370  throw new UnsupportedOperationException("Not supported yet.");
371  }
372  EntityAdditionalAttribute other = (EntityAdditionalAttribute) o;
373  if (this.priority == null && other.getPriority() == null) {
374  return 0;
375  }
376  if (this.priority == null && other.getPriority() != null) {
377  return -1;
378  }
379  if (this.priority != null && other.getPriority() == null) {
380  return 1;
381  }
382  return this.priority.compareTo(other.getPriority());
383  }
384 
385 } // public class EntityAdditionalAttribute
Class representing vocabulary entity attribute.
EntityAdditionalAttribute(String name, String stringValue, EntityAttribute refEntityAttribute)
static final String DEFAULT_SIMPLE_TYPE_FOR_ENT_AD_AT
Definition: Constants.java:225
EntityAdditionalAttribute(String name, String type, String stringValue, EntityAttribute refEntityAttribute)
static String toHTMLString(String source)
Definition: Util.java:460
EntityAdditionalAttribute(Integer id, String name, String stringValue, EntityAttribute refEntityAttribute)
Utility class (manipulates RFC 3339 dates)
Definition: Util.java:29