4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
SugEntityAttribute.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: SugEntityAttribute.java
5  * Description: Class representing vocabulary entity attribute for prupose of suggestion
6  */
7 
8 package cz.vutbr.fit.knot.annotations.modules.suggestionManager.attributes;
9 
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.Map;
18 import javax.persistence.DiscriminatorValue;
19 import javax.persistence.Entity;
20 import javax.persistence.NamedQueries;
21 import javax.persistence.NamedQuery;
22 
23 /**
24  * @file SugEntityAttribute.java
25  *
26  * @brief Class representing vocabulary entity attribute for prupose of suggestion
27  */
28 
29 /**
30  * Class representing vocabulary entity attribute for prupose of suggestion
31  *
32  * @brief Class representing vocabulary entity attribute for prupose of suggestion
33  */
34 @Entity
35 @DiscriminatorValue("Entity")
36 @NamedQueries({
37  @NamedQuery(name = "SuggestionAttribute.findByEntityName", query = "SELECT a FROM SuggestionAttribute a WHERE a.stringValue = :name"),
38  @NamedQuery(name = "SuggestionAttribute.findByEntityType", query = "SELECT a FROM SuggestionAttribute a WHERE a.entityType = :type"),
39  @NamedQuery(name = "SuggestionAttribute.findByEntityUri", query = "SELECT a FROM SuggestionAttribute a WHERE a.uri = :uri")
40 })
42  /**
43  * Constructor.
44  */
46 
47  }
48 
49  /**
50  * Constructor.
51  *
52  * @param name name of vocabulary entity
53  * @param type type of vocabulary entity
54  * @param URI uri of vocabulary entity
55  * @param visualRepresentation uri of visual representation of vocabulary entity
56  * @param description description of vocabulary entity
57  */
58  public SugEntityAttribute(String name, String type, String URI, String visualRepresentation, String description){
59  this.entityType = type;
60  this.stringValue = name;
61  this.simpleType = "Entity";
62  this.uri = URI;
63  this.entityVisualURI = visualRepresentation;
64  this.textValue = description;
65  }
66 
67  /**
68  * Gets additional attributes of this entity
69  *
70  * @return Returns additional attributes of this entity
71  */
72  @Override
73  public List<SugEntityAdditionalAttribute> getEntityAdditionalAttributes() {
74  return this.entityAdditionalAttributes;
75  }
76 
77  /**
78  * Sets additional attributes of this entity
79  *
80  * @param EntityAdditionalAttributes Additional attributes of this entity
81  */
82  public void setEntityAdditionalAttributes(ArrayList<SugEntityAdditionalAttribute> EntityAdditionalAttributes) {
83  this.entityAdditionalAttributes = EntityAdditionalAttributes;
84  }
85 
86  /**
87  * Sets entity description
88  *
89  * @param textValue New description
90  */
91  public void setDescription(String textValue) {
92  this.textValue = textValue;
93  }
94 
95  /**
96  * Gets entity description
97  *
98  * @return Returns entity description
99  */
100  public String getDescription() {
101  return textValue;
102  }
103 
104  /**
105  * Sets entity name
106  *
107  * @param name New entity name
108  */
109  public void setEntityName(String name) {
110  this.stringValue = name;
111  }
112 
113  /**
114  * Gets value of attribute in XML (serailized informationa about entity)
115  *
116  * @return Returns value of attribute in XML
117  */
118  @Override
119  protected String getXmlBody() {
120  StringBuilder result = new StringBuilder("<entity");
121 
122  if(this.stringValue != null && !this.stringValue.isEmpty()){
123  result.append(" name=\"");
124  result.append(escapeHTML(this.stringValue));
125  result.append("\"");
126  }
127 
128  if(this.uri != null && !this.uri.isEmpty()){
129  result.append(" uri=\"");
130  result.append(escapeHTML(this.uri));
131  result.append("\"");
132  }
133 
134  if(this.entityType != null && !this.entityType.isEmpty()){
135  result.append(" type=\"");
136  result.append(this.entityType);
137  result.append("\"");
138  }
139 
140  if(this.entityVisualURI != null && !this.entityVisualURI.isEmpty()){
141  result.append(" visualRepresentation=\"");
142  result.append(escapeHTML(this.entityVisualURI));
143  result.append("\"");
144  }
145 
146  if(this.textValue != null && !this.textValue.isEmpty()){
147  result.append("><![CDATA[");
148  result.append(this.textValue);
149  result.append("]]></entity>");
150  }else{
151  result.append("/>");
152  }
153 
154  return result.toString();
155  }
156  /**
157  * Method converts the object into an XML string (for protocol 1.x).
158  *
159  * @return XML string that represents this object
160  */
161  public String toXmlString(){
162 
163  StringBuilder result = new StringBuilder("<entity");
164 
165  if(this.stringValue != null && !this.stringValue.isEmpty()){
166  result.append(" name=\"");
167  result.append(escapeHTML(this.stringValue));
168  result.append("\"");
169  }
170 
171  if(this.uri != null && !this.uri.isEmpty()){
172  result.append(" uri=\"");
173  result.append(escapeHTML(this.uri));
174  result.append("\"");
175  }
176 
177  if(this.entityType != null && !this.entityType.isEmpty()){
178  result.append(" type=\"");
179  result.append(this.entityType);
180  result.append("\"");
181  }
182 
183  if(this.entityVisualURI != null && !this.entityVisualURI.isEmpty()){
184  result.append(" visualRepresentation=\"");
185  if (this.entityVisualURI != null && !this.entityVisualURI.startsWith("http")) {
186  result.append(AppBean.getKBImagePrefix());
187  }
188  result.append(escapeHTML(this.entityVisualURI));
189  result.append("\"");
190  }
191 
192  if(this.textValue != null && !this.textValue.isEmpty()){
193  result.append("><![CDATA[");
194  result.append(this.textValue);
195  result.append("]]></entity>");
196  }else{
197  result.append("/>");
198  }
199 
200  return result.toString();
201  }
202 
203  /**
204  * Returns serialized informations about attribute of suggestion in XML
205  * for protocol v. 2.0 without header (target and attribute specification
206  * triples)
207  *
208  * @return Returns serialized informations about attribute of suggestion in XML
209  */
210  public String toXMLStringWHV2(){
211  StringBuilder result = new StringBuilder();
212 
213  if(this.uri == null){
214  // empty entity
215  result.append("<trix:uri>");
216  result.append("http://knot.fit.vutbr.cz/annotations/knotOAExtension#anyEntity");
217  result.append("</trix:uri></trix:triple>");
218  return result.toString();
219  }
220 
221  result.append("<trix:uri>");
222  result.append(escapeHTML(this.uri));
223  result.append("</trix:uri></trix:triple>");
224 
225  // trix with entity type
226  result.append("<trix:triple><trix:uri>");
227  result.append(escapeHTML(this.uri));
228  result.append("</trix:uri><trix:uri>rdf:type</trix:uri><trix:name>");
229  result.append(entityType);
230  result.append("</trix:name></trix:triple>");
231 
232  // trix with name
233  if(stringValue != null && !stringValue.isEmpty()){
234  result.append("<trix:triple><trix:uri>");
235  result.append(escapeHTML(this.uri));
236  result.append("</trix:uri><trix:name>name</trix:name><trix:typedLiteral datatype=\"http://www.w3.org/2001/XMLSchema#string\">");
237  result.append(escapeHTML(stringValue));
238  result.append("</trix:typedLiteral></trix:triple>");
239  }
240 
241  // trix with description
242  if(textValue != null && !textValue.isEmpty()){
243  result.append("<trix:triple><trix:uri>");
244  result.append(escapeHTML(this.uri));
245  result.append("</trix:uri><trix:name>description</trix:name><trix:typedLiteral datatype=\"http://www.w3.org/2001/XMLSchema#string\">");
246  result.append(escapeHTML(textValue));
247  result.append("</trix:typedLiteral></trix:triple>");
248  }
249 
250  // trix with image
251  if(entityVisualURI != null && !entityVisualURI.isEmpty()){
252  result.append("<trix:triple><trix:uri>");
253  result.append(escapeHTML(this.uri));
254  result.append("</trix:uri><trix:name>image</trix:name><trix:typedLiteral datatype=\"http://knot.fit.vutbr.cz/annotations/knotOAExtension#imageUri\">");
255  result.append(escapeHTML(entityVisualURI));
256  result.append("</trix:typedLiteral></trix:triple>");
257  }
258 
259  if(entityAdditionalAttributes != null && !entityAdditionalAttributes.isEmpty()){
260  Collections.sort(entityAdditionalAttributes);
261  Iterator<SugEntityAdditionalAttribute> additionalAttrIt = entityAdditionalAttributes.iterator();
262  while(additionalAttrIt.hasNext()){
263  SugEntityAdditionalAttribute actualAttr = additionalAttrIt.next();
264  result.append(actualAttr.toString());
265  }
266  }
267 
268  return result.toString();
269  }
270 
271  /**
272  * Sets value of attribute using given parameters from raw input
273  *
274  * @param values Parameters from raw input
275  * @throws IllegalArgumentException
276  */
277  @Override
278  public void setRawValues(Object values) throws IllegalArgumentException {
279  @SuppressWarnings("unchecked") ArrayList<Object> valuesArray = (ArrayList<Object>)values;
280  @SuppressWarnings("unchecked") Map<String,String> parameters = (Map<String,String>)valuesArray.get(1);
281  String value = (String)valuesArray.get(2);
282 
283  if(parameters.get("name") != null){
284  this.stringValue = parameters.get("name");
285  }
286 
287  if(parameters.get("type") != null){
288  this.entityType = parameters.get("type");
289  }
290 
291  if(parameters.get("uri") != null){
292  this.uri = parameters.get("uri");
293  }
294 
295  if(parameters.get("visualRepresentation") != null){
296  this.entityVisualURI = parameters.get("visualRepresentation");
297  }
298 
299  if(value != null){
300  this.textValue = value;
301  }
302  }
303 
304 
305  /**
306  * The method converts HTML forbidden symbols to their safe version.
307  *
308  * @param s string that contains forbiden symbols
309  * @return HTML safe string
310  */
311  private String escapeHTML(String s){
312  StringBuilder sb = new StringBuilder();
313  int n = s.length();
314  for (int i = 0; i < n; i++) {
315  char c = s.charAt(i);
316  switch (c) {
317  case '<': sb.append("&lt;"); break;
318  case '>': sb.append("&gt;"); break;
319  case '&': sb.append("&amp;"); break;
320  case '"': sb.append("&quot;"); break;
321  case '\'': sb.append("&apos;");break;
322  default: sb.append(c); break;
323  }
324  }
325  return sb.toString();
326  }
327 
328  /**
329  * Gets name of this attribute to XML
330  *
331  * @return Name of this attribute to XML
332  */
333  @Override
334  public String getXmlAttributeName() {
335  return "entity";
336  }
337 
338  /**
339  * Gets attribute value
340  *
341  * @return Returns attribute value
342  */
343  @Override
344  public Object getValue() {
345  return new EntityAttribute.Entity(this.entityType,this.stringValue,this.uri,this.entityVisualURI, this.textValue);
346  }
347 
348  /**
349  * Sets attribute value
350  *
351  * @param value Attribute value (Entity)
352  */
353  @Override
354  public void setValue(Object value) {
355  this.entityType = ((EntityAttribute.Entity)value).getType();
356  this.stringValue = ((EntityAttribute.Entity)value).getName();
357  this.uri = ((EntityAttribute.Entity)value).getURI();
358  this.entityVisualURI = ((EntityAttribute.Entity)value).getVisualRepresentation();
359  this.textValue = ((EntityAttribute.Entity)value).getDescription();
360  }
361 
362  /**
363  * The method sets the parameters from a string in JSON.
364  *
365  * @param value string parsed from JSON
366  */
367  public void setStringValue(String value){
368  EntityAttribute.Entity ent = new EntityAttribute.Entity();
369  ent.fromJSONString(value);
370  setValue(ent);
371  }
372 
373  /**
374  * Encode values of the attribute to one string
375  *
376  * This method can be used for generating value for JSON object
377  *
378  * @return Encoded values of the attribute in one string
379  */
380  @Override
381  public String toString() {
382  return ((EntityAttribute.Entity)getValue()).toJSONString();
383  }
384 
385  /**
386  * Gets URI addres in ontology for this type of attribute
387  *
388  * @return Return URI addres in ontology for this type of attribute.
389  */
390  @Override
391  public String getTypeOntologyUri(){
392  return Constants.ENTITY_URI;
393  }
394 } // public class SugEntityAttribute
SugEntityAttribute(String name, String type, String URI, String visualRepresentation, String description)
Singleton for storing global variables.
Definition: AppBean.java:47
Class representing vocabulary entity attribute for prupose of suggestion.
Class representing vocabulary entity attribute.
void setEntityAdditionalAttributes(ArrayList< SugEntityAdditionalAttribute > EntityAdditionalAttributes)