4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
EntityAttribute.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: EntityAttribute.java
5  * Description: Class representing vocabulary entity attribute.
6  */
7 
8 package cz.vutbr.fit.knot.annotations.entity.attribute;
9 
12 import java.util.ArrayList;
13 import java.util.Arrays;
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 EntityAttribute.java
25  *
26  * @brief Class representing vocabulary entity attribute.
27  */
28 
29 /**
30  * Class representing vocabulary entity attribute.
31  *
32  * @brief Class representing vocabulary entity attribute.
33  */
34 @Entity
35 @DiscriminatorValue("Entity")
36 @NamedQueries({
37  @NamedQuery(name = "Attribute.findByEntityName", query = "SELECT a FROM Attribute a WHERE a.stringValue = :name"),
38  @NamedQuery(name = "Attribute.findByEntityType", query = "SELECT a FROM Attribute a WHERE a.entityType = :type"),
39  @NamedQuery(name = "Attribute.findByEntityUri", query = "SELECT a FROM Attribute a WHERE a.uri = :uri")
40 })
41 public class EntityAttribute extends StructuredAttribute{
42 
43  /**
44  * Constructor.
45  */
46  public EntityAttribute(){
47  this.simpleType = "Entity";
48  }
49 
50  /**
51  * Constructor.
52  *
53  * @param name name of vocabulary entity
54  * @param type type of vocabulary entity
55  * @param URI uri of vocabulary entity
56  * @param visualRepresentation uri of visual representation of vocabulary entity
57  * @param description description of vocabulary entity
58  */
59  public EntityAttribute(String name, String type, String URI, String visualRepresentation, String description){
60  this.entityType = type;
61  this.stringValue = name;
62  this.simpleType = "Entity";
63  this.uri = URI;
64  this.entityVisualURI = visualRepresentation;
65  this.textValue = description;
66  }
67 
68  /**
69  * Gets additional attributes of this entity
70  *
71  * @return Returns additional attributes of this entity
72  */
73  @Override
74  public List<EntityAdditionalAttribute> getEntityAdditionalAttributes() {
75  return this.entityAdditionalAttributes;
76  }
77 
78  /**
79  * Sets additional attributes of this entity
80  *
81  * @param EntityAdditionalAttributes Additional attributes of this entity
82  */
83  public void setEntityAdditionalAttributes(ArrayList<EntityAdditionalAttribute> EntityAdditionalAttributes) {
84  this.entityAdditionalAttributes = EntityAdditionalAttributes;
85  }
86 
87  /**
88  * Sets entity description
89  *
90  * @param textValue New description
91  */
92  public void setDescription(String textValue) {
93  this.textValue = textValue;
94  }
95 
96  /**
97  * Gets entity description
98  *
99  * @return Returns entity description
100  */
101  public String getDescription() {
102  return textValue;
103  }
104 
105  /**
106  * Sets entity name
107  *
108  * @param name New entity name
109  */
110  public void setEntityName(String name) {
111  this.stringValue = name;
112  }
113 
114  /**
115  * Gets value of attribute in XML (serailized informationa about entity)
116  *
117  * @return Returns value of attribute in XML
118  */
119  @Override
120  protected String getXmlBody() {
121  StringBuilder result = new StringBuilder("<entity");
122 
123  if(this.stringValue != null && !this.stringValue.isEmpty()){
124  result.append(" name=\"");
125  result.append(escapeHTML(this.stringValue));
126  result.append("\"");
127  }
128 
129  if(this.uri != null && !this.uri.isEmpty()){
130  result.append(" uri=\"");
131  result.append(escapeHTML(this.uri));
132  result.append("\"");
133  }
134 
135  if(this.entityType != null && !this.entityType.isEmpty()){
136  result.append(" type=\"");
137  result.append(this.entityType);
138  result.append("\"");
139  }
140 
141  if(this.entityVisualURI != null && !this.entityVisualURI.isEmpty()){
142  result.append(" visualRepresentation=\"");
143  if (this.entityVisualURI != null && !this.entityVisualURI.startsWith("http")) {
144  result.append(AppBean.getKBImagePrefix());
145  }
146  result.append(escapeHTML(this.entityVisualURI));
147  result.append("\"");
148  }
149 
150  if(this.textValue != null && !this.textValue.isEmpty()){
151  result.append("><![CDATA[");
152  result.append(this.textValue);
153  result.append("]]></entity>");
154  }else{
155  result.append("/>");
156  }
157 
158  return result.toString();
159  }
160 
161  /**
162  * Method converts the object into an XML string (for protocol 1.x).
163  *
164  * @return XML string that represents this object
165  */
166  public String toXmlString(){
167 
168  StringBuilder result = new StringBuilder("<entity");
169 
170  if (this.stringValue != null && !this.stringValue.isEmpty()) {
171  result.append(" name=\"");
172  result.append(escapeHTML(this.stringValue));
173  result.append("\"");
174  }
175 
176  if (this.uri != null && !this.uri.isEmpty()) {
177  result.append(" uri=\"");
178  result.append(escapeHTML(this.uri));
179  result.append("\"");
180  }
181 
182  if (this.entityType != null && !this.entityType.isEmpty()) {
183  result.append(" type=\"");
184  result.append(this.entityType);
185  result.append("\"");
186  }
187 
188  if (this.entityVisualURI != null && !this.entityVisualURI.isEmpty()) {
189  result.append(" visualRepresentation=\"");
190  if (this.entityVisualURI != null && !this.entityVisualURI.startsWith("http")) {
191  result.append(AppBean.getKBImagePrefix());
192  }
193  result.append(escapeHTML(this.entityVisualURI));
194  result.append("\"");
195  }
196 
197  if (this.textValue != null && !this.textValue.isEmpty()) {
198  result.append("><![CDATA[");
199  result.append(this.textValue);
200  result.append("]]></entity>");
201  } else {
202  result.append("/>");
203  }
204 
205  return result.toString();
206  } // toXmlString()
207 
208  /**
209  * Returns serialized informations about attribute of annotation in XML
210  * for protocol v. 2.0 without header (target and attribute specification
211  * triples)
212  *
213  * @return Returns serialized informations about attribute of annotation in XML
214  */
215  public String toXMLStringWHV2(){
216  StringBuilder result = new StringBuilder();
217 
218  if(this.uri == null){
219  // empty entity
220  result.append("<trix:uri>");
221  result.append("http://knot.fit.vutbr.cz/annotations/knotOAExtension#anyEntity");
222  result.append("</trix:uri></trix:triple>");
223  return result.toString();
224  }
225 
226  result.append("<trix:uri>");
227  result.append(escapeHTML(this.uri));
228  result.append("</trix:uri></trix:triple>");
229 
230  // trix with entity type
231  result.append("<trix:triple><trix:uri>");
232  result.append(escapeHTML(this.uri));
233  result.append("</trix:uri><trix:uri>rdf:type</trix:uri><trix:name>");
234  result.append(entityType);
235  result.append("</trix:name></trix:triple>");
236 
237  // trix with name
238  if(stringValue != null && !stringValue.isEmpty()){
239  result.append("<trix:triple><trix:uri>");
240  result.append(escapeHTML(this.uri));
241  result.append("</trix:uri><trix:name>name</trix:name><trix:typedLiteral datatype=\"http://www.w3.org/2001/XMLSchema#string\">");
242  result.append(escapeHTML(stringValue));
243  result.append("</trix:typedLiteral></trix:triple>");
244  }
245 
246  // trix with description
247  if(textValue != null && !textValue.isEmpty()){
248  result.append("<trix:triple><trix:uri>");
249  result.append(escapeHTML(this.uri));
250  result.append("</trix:uri><trix:name>description</trix:name><trix:typedLiteral datatype=\"http://www.w3.org/2001/XMLSchema#string\">");
251  result.append(escapeHTML(textValue));
252  result.append("</trix:typedLiteral></trix:triple>");
253  }
254 
255  // trix with image
256  if(entityVisualURI != null && !entityVisualURI.isEmpty()){
257  result.append("<trix:triple><trix:uri>");
258  result.append(escapeHTML(this.uri));
259  result.append("</trix:uri><trix:name>image</trix:name><trix:typedLiteral datatype=\"http://knot.fit.vutbr.cz/annotations/knotOAExtension#imageUri\">");
260  result.append(escapeHTML(entityVisualURI));
261  result.append("</trix:typedLiteral></trix:triple>");
262  }
263 
264  if(entityAdditionalAttributes != null && !entityAdditionalAttributes.isEmpty()){
265  Collections.sort(entityAdditionalAttributes);
266  Iterator additionalAttrIt = entityAdditionalAttributes.iterator();
267  while(additionalAttrIt.hasNext()){
268  EntityAdditionalAttribute actualAttr = (EntityAdditionalAttribute) additionalAttrIt.next();
269  result.append(actualAttr.toXmlStringV2());
270  }
271  }
272 
273  return result.toString();
274  }
275 
276  /**
277  * Method converts the object into an XML string for prupose of protocol version 2.
278  *
279  * @return XML string that represents this object
280  */
281  public String toXMLResponseStringV2(){
282  StringBuilder result = new StringBuilder("<entity");
283  boolean hasContent = false;
284  //Name
285  result.append(" name=\"");
286  if (this.stringValue != null && !this.stringValue.isEmpty()) {
287  result.append(escapeHTML(this.stringValue));
288  }
289  result.append("\"");
290 
291  // Uri
292  result.append(" uri=\"");
293  if (this.uri != null && !this.uri.isEmpty()) {
294  result.append(escapeHTML(this.uri));
295  }
296  result.append("\"");
297 
298  // Type
299  result.append(" type=\"");
300  if (this.entityType != null && !this.entityType.isEmpty()) {
301  result.append(this.entityType);
302  }
303  result.append("\"");
304 
305  //Image
306  result.append(" image=\"");
307  if (this.entityVisualURI != null && !this.entityVisualURI.isEmpty()) {
308  if (!this.entityVisualURI.startsWith("http")) {
309  result.append(AppBean.getKBImagePrefix());
310  }
311  result.append(escapeHTML(this.entityVisualURI));
312  }
313 
314  if (this.entityAdditionalAttributes != null && !this.entityAdditionalAttributes.isEmpty()) {
315  result.append("\">");
316  Collections.sort(entityAdditionalAttributes);
317  Iterator<EntityAdditionalAttribute> addtitionalAttrsIt = entityAdditionalAttributes.iterator();
318  while (addtitionalAttrsIt.hasNext()) {
319  EntityAdditionalAttribute a = addtitionalAttrsIt.next();
320  if (a.getName() != null && a.getName().equals("disambiguation")) {
321  a.setName("Disambiguation"); // small fix of special attribute for case sensitive clients
322  }
323  result.append(a.toXMLResponseStringV2());
324  }
325 
326  hasContent = true;
327  }
328 
329  // Description
330  if (this.textValue != null && !this.textValue.isEmpty()) {
331  if (!hasContent) {
332  result.append("\">");
333  }
334  result.append("<description><![CDATA[").append(this.textValue).append("]]></description>");
335  hasContent = true;
336  }
337 
338  if (hasContent) {
339  result.append("</entity>");
340  } else {
341  result.append("\"/>");
342  }
343 
344  return result.toString();
345  }
346 
347  /**
348  * Sets value of attribute using given parameters from raw input
349  *
350  * @param values Parameters from raw input
351  * @throws IllegalArgumentException
352  */
353  @Override
354  public void setRawValues(Object values) throws IllegalArgumentException {
355  @SuppressWarnings("unchecked") ArrayList<Object> valuesArray = (ArrayList<Object>)values;
356  @SuppressWarnings("unchecked") Map<String,String> parameters = (Map<String,String>)valuesArray.get(1);
357  String value = (String)valuesArray.get(2);
358 
359  if(parameters.get("name") != null){
360  this.stringValue = parameters.get("name");
361  }
362 
363  if(parameters.get("type") != null){
364  this.entityType = parameters.get("type");
365  }
366 
367  if(parameters.get("uri") != null){
368  this.uri = parameters.get("uri");
369  }
370 
371  if(parameters.get("visualRepresentation") != null){
372  this.entityVisualURI = parameters.get("visualRepresentation");
373  }
374 
375  if(value != null){
376  this.textValue = value;
377  }
378  }
379 
380 
381  /**
382  * The method converts HTML forbidden symbols to their safe version.
383  *
384  * @param s string that contains forbiden symbols
385  * @return HTML safe string
386  */
387  private String escapeHTML(String s){
388  StringBuilder sb = new StringBuilder();
389  int n = s.length();
390  for (int i = 0; i < n; i++) {
391  char c = s.charAt(i);
392  switch (c) {
393  case '<': sb.append("&lt;"); break;
394  case '>': sb.append("&gt;"); break;
395  case '&': sb.append("&amp;"); break;
396  case '"': sb.append("&quot;"); break;
397  case '\'': sb.append("&apos;");break;
398  default: sb.append(c); break;
399  }
400  }
401  return sb.toString();
402  }
403 
404  /**
405  * Gets name of this attribute to XML
406  *
407  * @return Name of this attribute to XML
408  */
409  @Override
410  public String getXmlAttributeName() {
411  return "entity";
412  }
413 
414  /**
415  * Gets attribute value
416  *
417  * @return Returns attribute value
418  */
419  @Override
420  public Object getValue() {
421  return new Entity(this.entityType,this.stringValue,this.uri,this.entityVisualURI, this.textValue);
422  }
423 
424  /**
425  * Sets attribute value
426  *
427  * @param value Attribute value (Entity)
428  */
429  @Override
430  public void setValue(Object value) {
431  this.entityType = ((Entity)value).getType();
432  this.stringValue = ((Entity)value).getName();
433  this.uri = ((Entity)value).getURI();
434  this.entityVisualURI = ((Entity)value).getVisualRepresentation();
435  this.textValue = ((Entity)value).getDescription();
436  }
437 
438  /**
439  * The method sets the parameters from a string in JSON.
440  *
441  * @param value String parsed from JSON
442  */
443  public void setFromJSONStringValue(String value){
444  Entity ent = new Entity();
445  ent.fromJSONString(value);
446  setValue(ent);
447  }
448 
449  /**
450  * Sets URI of visual representation of entity
451  *
452  * @param entityVisualURI URI of visual representation of entity
453  */
454  public void setEntityVisualURI(String entityVisualURI) {
455  this.entityVisualURI = entityVisualURI;
456  }
457 
458  /**
459  * Sets type of entity
460  *
461  * @param entityType Type of entity
462  */
463  public void setEntityType(String entityType) {
464  this.entityType = entityType;
465  }
466 
467  /**
468  * Sets string value of attribute
469  *
470  * @param stringValue Gets string value of attribute
471  */
472  public void setStringValue(String stringValue) {
473  this.stringValue = stringValue;
474  }
475 
476  /**
477  * Encode values of the attribute to one string
478  *
479  * This method can be used for generating value for JSON object
480  *
481  * @return Encoded values of the attribute in one string
482  */
483  @Override
484  public String toString() {
485  return ((Entity)getValue()).toJSONString();
486  }
487 
488  /**
489  * Gets URI addres in ontology for this type of attribute
490  *
491  * @return Return URI addres in ontology for this type of attribute.
492  */
493  @Override
494  public String getTypeOntologyUri(){
495  return Constants.ENTITY_URI;
496  }
497 
498  /**
499  * Class holds values of dictionary entity.
500  *
501  * @brief Class holds values of dictionary entity.
502  */
503  public static class Entity {
504 
505  /** type of vocabulary entity */
506  private String type;
507  /** name of vocalbulary entity */
508  private String name;
509  /** uri of vocabulary entity */
510  private String URI;
511  /** uri of image that entity represents */
512  private String visualRepresentation;
513  /** description of vocabulary entity */
514  private String description;
515 
516  /** Array of forbidden JSON characters */
517  public static final ArrayList<String> UNESACPED_CHARS = new ArrayList<String>(Arrays.asList(
518  "\\",
519  "\"",
520  "/" ,
521  "=" ,
522  ","
523  ));
524 
525  /** Array escaped characters of forbidden JSON characters */
526  public static final ArrayList<String> ESCAPED_CHARS = new ArrayList<String>(Arrays.asList(
527  "\\\\",
528  "\\\"",
529  "\\/" ,
530  "\\=" ,
531  "\\,"
532  ));
533 
534  /**
535  * Constructor
536  */
537  public Entity() {
538  }
539 
540  /**
541  * Creates new Entity
542  *
543  * @param type type of vocabulary entity
544  * @param name name of vocalbulary entity
545  * @param URI uri of vocabulary entity
546  * @param visualRepresentation URI address of image of object that entity represents
547  * @param description description of vocabulary entity
548  */
549  public Entity(String type, String name, String URI, String visualRepresentation, String description) {
550  this.type = type;
551  this.name = name;
552  this.URI = URI;
553  this.visualRepresentation = visualRepresentation;
554  this.description = description;
555  }
556 
557  /**
558  * Gets type of vocabulary entity.
559  *
560  * @return type of vocabulary entity
561  */
562  public String getType() {
563  return type;
564  }
565 
566  /**
567  * Sets type of vocabulary entity.
568  *
569  * @param type type of vocabulary entity
570  */
571  public void setType(String type) {
572  this.type = type;
573  }
574 
575  /**
576  * Gets name of vocalbulary entity.
577  *
578  * @return name of vocalbulary entity
579  */
580  public String getName() {
581  return name;
582  }
583 
584  /**
585  * Sets name of vocalbulary entity.
586  *
587  * @param name name of vocalbulary entity
588  */
589  public void setName(String name) {
590  this.name = name;
591  }
592 
593  /**
594  * Gets uri of vocabulary entity.
595  *
596  * @return uri of vocabulary entity
597  */
598  public String getURI() {
599  return URI;
600  }
601 
602  /**
603  * Sets uri of vocabulary entity.
604  *
605  * @param URI uri of vocabulary entity
606  */
607  public void setURI(String URI) {
608  this.URI = URI;
609  }
610 
611  /**
612  * Gets URI address of image of object that entity represents.
613  *
614  * @return URI address of image of object that entity represents
615  */
616  public String getVisualRepresentation() {
617  return visualRepresentation;
618  }
619 
620  /**
621  * Sets URI address of image of object that entity represents.
622  *
623  * @param visualRepresentation URI address of image of object that entity represents
624  */
625  public void setVisualRepresentation(String visualRepresentation) {
626  this.visualRepresentation = visualRepresentation;
627  }
628 
629  /**
630  * Gets description of vocabulary entity.
631  *
632  * @return description of vocabulary entity
633  */
634  public String getDescription() {
635  return description;
636  }
637 
638  /**
639  * Sets description of vocabulary entity.
640  *
641  * @param description description of vocabulary entity
642  */
643  public void setDescription(String description) {
644  this.description = description;
645  }
646 
647  @Override
648  public String toString() {
649  return "Entity{" + "type=" + type
650  + ", name=" + name
651  + ", URI=" + URI
652  + ", visualRepresentation=" + visualRepresentation
653  + ", description=" + description + '}';
654  }
655 
656  /**
657  * The method converts the string to a JSON safe string.
658  *
659  * @param text string to convert
660  * @return string without forbidden JSON characters
661  */
662  private String escapeJSON(String text){
663  String result = text;
664  for(int i = 0; i < UNESACPED_CHARS.size(); i++){
665  result = result.replace(UNESACPED_CHARS.get(i), ESCAPED_CHARS.get(i));
666  }
667 
668  return result;
669  }
670 
671  /**
672  * Method converts the JSON safe string to normal string.
673  *
674  * @param text JSON safe string to convert
675  * @return converted normal string
676  */
677  private String unescapeJSON(String text){
678  String result = text;
679  for(int i = 0; i < UNESACPED_CHARS.size(); i++){
680  result = result.replace(ESCAPED_CHARS.get(i),UNESACPED_CHARS.get(i));
681  }
682 
683  return result;
684  }
685 
686  /**
687  * The method converts the object to a JSON string.
688  *
689  * @return JSON string
690  */
691  public String toJSONString(){
692  StringBuilder result = new StringBuilder();
693  if(this.name != null && !this.name.isEmpty()){
694  result.append("name = ");
695  result.append(escapeJSON(this.name));
696  result.append(" ,");
697  }
698 
699  if(this.type != null && !this.type.isEmpty()){
700  result.append("type = ");
701  result.append(escapeJSON(this.type));
702  result.append(" ,");
703  }
704 
705  if(this.URI != null && !this.URI.isEmpty()){
706  result.append("uri = ");
707  result.append(escapeJSON(this.URI));
708  result.append(" ,");
709  }
710 
711  if(this.visualRepresentation != null && !this.visualRepresentation.isEmpty()){
712  result.append("visualRepresentation = ");
713  result.append(escapeJSON(this.visualRepresentation));
714  result.append(" ,");
715  }
716 
717  if(this.description != null && !this.description.isEmpty()){
718  result.append("description = ");
719  result.append(escapeJSON(this.description));
720  result.append(" ,");
721  }
722 
723  if(result.length() > 2){
724  result.deleteCharAt(result.length()-1);
725  }
726 
727  return result.toString();
728  }
729 
730  /**
731  * Method sets the object parameters from JSON string.
732  *
733  * @param text JSON string
734  */
735  public void fromJSONString(String text) {
736  if (text != null && !text.isEmpty()) {
737  String[] splited = text.split(" ,");
738  for (int i = 0; i < splited.length; i++) {
739  String[] pair = splited[i].split(" = ");
740  if (pair.length == 2) {
741  if (pair[0].equals("name")) {
742  this.name = unescapeJSON(pair[1]);
743  }
744 
745  if (pair[0].equals("type")) {
746  this.name = unescapeJSON(pair[1]);
747  }
748 
749  if (pair[0].equals("uri")) {
750  this.URI = unescapeJSON(pair[1]);
751  }
752 
753  if (pair[0].equals("visualRepresentation")) {
754  this.visualRepresentation = unescapeJSON(pair[1]);
755  }
756 
757  if (pair[0].equals("description")) {
758  this.description = unescapeJSON(pair[1]);
759  }
760  }
761  }
762  }
763  } // fromJSONString()
764  } // public static class Entity
765 } // public class EntityAttribute
Abstract class representing more complex attribute.
Singleton for storing global variables.
Definition: AppBean.java:47
Class representing vocabulary entity attribute.
Entity(String type, String name, String URI, String visualRepresentation, String description)
EntityAttribute(String name, String type, String URI, String visualRepresentation, String description)
List< EntityAdditionalAttribute > getEntityAdditionalAttributes()
void setEntityAdditionalAttributes(ArrayList< EntityAdditionalAttribute > EntityAdditionalAttributes)