4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
SugAttributeManager.java
Go to the documentation of this file.
1 /*
2  * Project: Server for annotations sharing
3  * Author: Jan Planer xplane01@stud.fit.vutbr.cz
4  * File: SugAttributeManager.java
5  * Description: Attribute manager provides a way how to create new attributes.
6  * This is handful for example at xml parsing for prupose of suggestion
7  */
8 
9 /**
10  * @file SugAttributeManager.java
11  *
12  * @brief Suggestion attribute manager provides a way how to create new attributes.
13  */
14 
15 package cz.vutbr.fit.knot.annotations.modules.suggestionManager.attributes;
16 
19 import java.util.HashMap;
20 
21 /**
22  * Attribute manager provides a way how to create new attributes for prupose of
23  * suggestion.
24  *
25  * @brief Attribute manager provides a way how to create new attributes for
26  * prupose of suggestion.
27  */
28 public class SugAttributeManager {
29  /**
30  * Constructor
31  */
32  public static SugBaseAttribute createAttribute() { return null; }
33 
34  /**
35  * Mapping of types of attributes to classes
36  */
37  private static final HashMap<String,Class> mapping = new HashMap<String,Class>();
38  static {
39  mapping.put("AnyAnnotation", SugAnyAttribute.class);
40  mapping.put("SuggestionLink", SugLinkedAttribute.class);
41  mapping.put("suggestionLink", SugLinkedAttribute.class);
42  mapping.put("AnnotationLink", SugLinkedAttribute.class);
43  mapping.put("annotationLink", SugLinkedAttribute.class);
44  mapping.put("LinkedAnnotation", SugLinkedAttribute.class);
45  mapping.put("linkedAnnotation", SugLinkedAttribute.class);
46  mapping.put("Boolean", SugBooleanAttribute.class);
47  mapping.put("Date", SugDateAttribute.class);
48  mapping.put("DateTime", SugDateTimeAttribute.class);
49  mapping.put("Decimal", SugDecimalAttribute.class);
50  mapping.put("GeoPoint", SugGeoPointAttribute.class);
51  mapping.put("geoPoint", SugGeoPointAttribute.class);
52  mapping.put("Image", SugImageAttribute.class);
53  mapping.put("Integer", SugIntegerAttribute.class);
54  mapping.put("NestedAnnotation", SugNestedAttribute.class);
55  mapping.put("nestedAnnotation", SugNestedAttribute.class);
56  mapping.put("NestedSuggestion", SugNestedAttribute.class);
57  mapping.put("nestedSuggestion", SugNestedAttribute.class);
58  mapping.put("Person", SugPersonAttribute.class);
59  mapping.put("person", SugPersonAttribute.class);
60  mapping.put("String", SugStringAttribute.class);
61  mapping.put("Time", SugTimeAttribute.class);
62  mapping.put("URI", SugUriAttribute.class);
63  mapping.put("Duration", SugDurationAttribute.class);
64  mapping.put("Binary", SugBinaryAttribute.class);
65  mapping.put("Text", SugTextAttribute.class);
66  mapping.put("Entity", SugEntityAttribute.class);
67  mapping.put("entity", SugEntityAttribute.class);
68  }
69 
70 
71  /**
72  * Creates a new instance according to type
73  *
74  * @param type Type of new instance
75  * @return Returns a new instance of appropriate class
76  */
77  private static SugBaseAttribute createObject(String type) throws ClassNotFoundException {
78  if(type.equalsIgnoreCase("NestedAnnotation")){
79  type = "NestedSuggestion";
80  }
81  if(type.equalsIgnoreCase("AnnotationLink")){
82  type = "SuggestionLink";
83  }
84  type = type.substring(0, 1).toUpperCase() + type.substring(1);
85  try {
86  SugBaseAttribute ret = (SugBaseAttribute) mapping.get(type).newInstance();
87  ret.setSimpleType(type);
88  return ret;
89  } catch (Exception e) {
90  throw new ClassNotFoundException("Class representing attribute " + type + " not found");
91  }
92  }
93 
94  /**
95  * Creates new empty attribute
96  *
97  * @param name Name of attribute
98  * @param simpleType Name of simple type of attribute
99  * @param refSuggestion Suggestion to which this attribute belongs
100  * @return Returns new empty attribute
101  */
102  public static SugBaseAttribute createAttribute(String name, String simpleType, Suggestion refSuggestion) throws ClassNotFoundException {
103  SugBaseAttribute ret = createObject(simpleType);
104  ret.setName(name);
105  ret.setRefSuggestion(refSuggestion);
106  return ret;
107  }
108 
109  /**
110  * Creates new attribute
111  *
112  * @param name Name of attribute
113  * @param simpleType Name of simple type of attribute
114  * @param value Value of attribute according to its type. For example, if
115  * type is Date then value is expected to be a date.
116  * @param refSuggestion Suggestion to which this attribute belongs
117  * @return Returns new attribute
118  */
119  public static SugBaseAttribute createAttribute(String name, String simpleType, Object value, Suggestion refSuggestion) throws ClassNotFoundException {
120  SugBaseAttribute ret = createAttribute(name, simpleType, refSuggestion);
121  ret.setValue(value);
122  ret.setRefSuggestion(refSuggestion);
123  return ret;
124  }
125 
126 
127  /**
128  * Creates a new Attribute with structured type. (For example with nested
129  * annotation)
130  *
131  * @param name Name of attribute
132  * @param simpleType Name of simple type of attribute
133  * @param attributeType Structured type of attribute (type of annotation)
134  * @param value Nested value. Usually nested annotation
135  * @param refSuggestion Suggestion to which this attribute belongs
136  * @return Returns new attribute of structured type
137  */
138  public static SugBaseAttribute createAttribute(String name, String simpleType, AnnotType attributeType, Object value, Suggestion refSuggestion) throws ClassNotFoundException {
139  SugBaseAttribute ret = SugAttributeManager.createAttribute(name, simpleType, value, refSuggestion);
140  ret.setAttributeType(attributeType);
141  return ret;
142  }
143 
144  /**
145  * Returns true if String type is correct type of attribute
146  *
147  * @param type Type of attribute
148  * @return True if it can be created, false if not
149  */
150  public static boolean containsType(String type){
151  return mapping.containsKey(type);
152  }
153 
154  /**
155  * Returns true if attr has the right class instance of it's simpletype
156  *
157  * @param attr Attribute for check
158  * @return True if simpletype matches the class instance, false otherwise
159  */
160  public static boolean hasAttributeRightInstance(SugBaseAttribute attr){
161  if(mapping.get(attr.getSimpleType()).isInstance(attr)){
162  return true;
163  }
164  else{
165  return false;
166  }
167  }
168 
169  /**
170  * Cast the right Attribute class
171  * Attention! If simpletype is some bad string, then method returns the same attribute as
172  * inserted in parameters!
173  *
174  * @param attr querried nested attribute
175  * @return Right class instance
176  */
178  try{
180  ret.updateFromBaseAttributeAll(attr);
181  return ret;
182  }
183  catch(ClassNotFoundException ex){
184  return attr;
185  }
186  }
187 
188 } // public class SugAttributeManager
Attribute manager provides a way how to create new attributes for prupose of suggestion.
static SugBaseAttribute createAttribute(String name, String simpleType, Suggestion refSuggestion)
static SugBaseAttribute createAttribute(String name, String simpleType, Object value, Suggestion refSuggestion)
static SugBaseAttribute createAttribute(String name, String simpleType, AnnotType attributeType, Object value, Suggestion refSuggestion)
Class representing type of annotation.
Definition: AnnotType.java:58
Class representing suggestion of annotation.
Definition: Suggestion.java:87