4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
SecApiReqTypeDef.java
Go to the documentation of this file.
1 /*
2  * Project: Server for annotations sharing
3  * Author: Jaroslav Dytrych idytrych@fit.vutbr.cz
4  * File: SecApiReqTypeDef.java
5  * Description: Auxiliary class for creating of requested type definition
6  * for SEC API
7  */
8 
9 package cz.vutbr.fit.knot.annotations.modules.suggestionManager;
10 
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.Iterator;
15 
16 /**
17  * Class for creating of requested type definition for SEC API
18  *
19  * @brief Class for creating of requested type definition for SEC API
20  */
21 public class SecApiReqTypeDef implements Cloneable {
22  /** Name of suggestion type */
23  private String typeName = null;
24  /** Available attributes (what we can request) */
25  private ArrayList<String> availableAttributes = null;
26  /** Attributes which will be requested */
27  private ArrayList<String> requestedAttributes = null;
28  /** Priorities of attributes */
29  private HashMap<String,Integer> priorities = null;
30  /** Definition of alternative description */
31  private String altDescDef = null;
32 
33  /**
34  * Constructor
35  *
36  * @param name Type name
37  */
38  public SecApiReqTypeDef(String name) {
39  typeName = name;
40  availableAttributes = new ArrayList<String>();
41  requestedAttributes = null;
42  priorities = new HashMap<String, Integer>();
43  }
44 
45 
46 
47  /**
48  * Gets part of request with given type, for example:
49  * "visual_artist": ["full_name", "wikipedia_url"]
50  *
51  * @return Returns part of request with given type
52  */
53  public String getRequestString() {
54  StringBuilder result = new StringBuilder("\"" + typeName + "\": [");
55 
56  if (requestedAttributes != null) {
57  Iterator<String> atIt = requestedAttributes.iterator();
58  if (atIt.hasNext()) { // first attribute
59  String at = atIt.next();
60  result.append("\"").append(at).append("\"");
61  }
62  while (atIt.hasNext()) {
63  String at = atIt.next();
64  result.append(", \"").append(at).append("\"");
65  }
66  } else {
67  Iterator<String> atIt = availableAttributes.iterator();
68  if (atIt.hasNext()) { // first attribute
69  String at = atIt.next();
70  result.append("\"").append(at).append("\"");
71  }
72  while (atIt.hasNext()) {
73  String at = atIt.next();
74  result.append(", \"").append(at).append("\"");
75  }
76  }
77 
78  result.append("]");
79  return result.toString();
80  } // getRequestString()
81 
82  /**
83  * Gets part of request with given type, for example:
84  * "visual_artist": ["full_name", "wikipedia_url"]
85  *
86  * @param filterAtt List of attributes which should be selected
87  * @return Returns part of request with given type
88  */
89  public String getRequestString(ArrayList<String> filterAtt) {
90  StringBuilder result = new StringBuilder("\"" + typeName + "\": [");
91 
92  Iterator<String> atIt = availableAttributes.iterator();
93  boolean firstFound = false;
94  while (atIt.hasNext() && !firstFound) { // first attribute
95  String at = atIt.next();
96  if (filterAtt.contains(at)) {
97  result.append("\"").append(at).append("\"");
98  firstFound = true;
99  }
100  }
101  while (atIt.hasNext()) {
102  String at = atIt.next();
103  if (filterAtt.contains(at)) {
104  result.append(", \"").append(at).append("\"");
105  }
106  }
107 
108  result.append("]");
109  return result.toString();
110  } // getRequestString()
111 
112  /**
113  * Prepares list of requested attributes
114  *
115  * If desired attributes are available, they will be used. If they are not
116  * available, unnecessary attributes will be sorted out.
117  *
118  * @param desiredAttributes Desired attributes (what do we want)
119  * @param unnecessaryAttributes Unnecessary attributes (what we do not want)
120  */
121  public void filterAttributes(ArrayList<String> desiredAttributes, ArrayList<String> unnecessaryAttributes) {
122  if (desiredAttributes == null && availableAttributes == null) {
123  requestedAttributes = new ArrayList<String>();
124  return;
125  }
126  if ((desiredAttributes == null || desiredAttributes.isEmpty()) && availableAttributes != null) {
127  if (unnecessaryAttributes != null && !unnecessaryAttributes.isEmpty()) {
128  requestedAttributes = new ArrayList<String>();
129  Iterator<String> avIt = availableAttributes.iterator();
130  while (avIt.hasNext()) {
131  String a = avIt.next();
132  if (!unnecessaryAttributes.contains(a)) {
133  requestedAttributes.add(a);
134  }
135  }
136  } else {
137  requestedAttributes = new ArrayList<String>(availableAttributes);
138  }
139  return;
140  }
141 
142  requestedAttributes = new ArrayList<String>();
143  Iterator<String> desIt = desiredAttributes.iterator();
144  while (desIt.hasNext()) {
145  String d = desIt.next();
146  if (availableAttributes.contains(d)) {
147  requestedAttributes.add(d);
148  }
149  }
150  } // filterAttributes()
151 
152  /**
153  * Prepares list of requested type definitions
154  *
155  * If desired attributes are available, they will be used. If they are not
156  * available, unnecessary attributes will be sorted out.
157  */
158  public static void filterTypes() {
159  ArrayList<SecApiReqTypeDef> availableTypes = AppBean.getAvTypeDefinitions();
160 
161  ArrayList<SecApiReqTypeDef> desiredTypes = AppBean.getDesTypeDefinitions();
162  ArrayList<SecApiReqTypeDef> unnecessaryTypes = AppBean.getUnTypeDefinitions();
163  ArrayList<SecApiReqTypeDef> unnecessaryAts = AppBean.getUnAtDefinitions();
164  ArrayList<SecApiReqTypeDef> requestedTypes = new ArrayList<SecApiReqTypeDef>();
165 
166  ArrayList<SecApiReqTypeDef> desiredTypesEA = AppBean.getDesTypeDefinitionsEA();
167  ArrayList<SecApiReqTypeDef> unnecessaryTypesEA = AppBean.getUnTypeDefinitionsEA();
168  ArrayList<SecApiReqTypeDef> unnecessaryAtsEA = AppBean.getUnAtDefinitionsEA();
169  ArrayList<SecApiReqTypeDef> requestedTypesEA = new ArrayList<SecApiReqTypeDef>();
170 
171  if (availableTypes == null) {
172  AppBean.setReqTypeDefinitions(requestedTypes);
173  AppBean.setReqTypeDefinitionsEA(requestedTypesEA);
174  return;
175  }
176 
177  if ((desiredTypes == null || desiredTypes.isEmpty())) {
178  if (unnecessaryTypes != null && !unnecessaryTypes.isEmpty()) {
179  if (unnecessaryAts == null) { // effectively fix NullPointerException
180  unnecessaryAts = new ArrayList<SecApiReqTypeDef>();
181  }
182  Iterator<SecApiReqTypeDef> avIt = availableTypes.iterator();
183  while (avIt.hasNext()) {
184  SecApiReqTypeDef a = avIt.next();
185  if (!unnecessaryTypes.contains(a)) {
186  SecApiReqTypeDef ca = a.clone();
187  requestedTypes.add(ca);
188  boolean uFound = false;
189  Iterator<SecApiReqTypeDef> unIt = unnecessaryAts.iterator();
190  while (unIt.hasNext() && !uFound) {
191  SecApiReqTypeDef u = unIt.next();
192  if (u.getTypeName().contentEquals(a.getTypeName())) {
193  uFound = true;
194  // sort out unnecessary attributes and initialise list of requested attributes
195  ca.filterAttributes(null, u.getAvailableAttributes());
196  }
197  }
198  if (!uFound) {
199  ca.filterAttributes(null, null); // initialise list of requested attributes
200  }
201  }
202  }
203  } else {
204  requestedTypes = new ArrayList<SecApiReqTypeDef>();
205  Iterator<SecApiReqTypeDef> avIt = availableTypes.iterator();
206  while (avIt.hasNext()) {
207  SecApiReqTypeDef a = avIt.next();
208  SecApiReqTypeDef ca = a.clone();
209  ca.filterAttributes(null, null);
210  requestedTypes.add(ca);
211  }
212 
213  }
214  } else {
215  Iterator<SecApiReqTypeDef> desIt = desiredTypes.iterator();
216  while (desIt.hasNext()) {
217  SecApiReqTypeDef d = desIt.next();
218  Iterator<SecApiReqTypeDef> avIt = availableTypes.iterator();
219  while (avIt.hasNext()) {
220  SecApiReqTypeDef a = avIt.next();
221  if (a.getTypeName().contentEquals(d.getTypeName())) {
222  SecApiReqTypeDef ca = a.clone();
223  requestedTypes.add(ca);
224  ca.filterAttributes(d.getAvailableAttributes(), null); // flter attributes
225  break;
226  }
227  }
228  }
229  }
230 
231  if ((desiredTypesEA == null || desiredTypesEA.isEmpty())) {
232  if (unnecessaryTypesEA != null && !unnecessaryTypesEA.isEmpty()) {
233  if (unnecessaryAtsEA == null) { // effectively fix NullPointerException
234  unnecessaryAtsEA = new ArrayList<SecApiReqTypeDef>();
235  }
236  Iterator<SecApiReqTypeDef> avIt = availableTypes.iterator();
237  while (avIt.hasNext()) {
238  SecApiReqTypeDef a = avIt.next();
239  if (!unnecessaryTypesEA.contains(a)) {
240  SecApiReqTypeDef ca = a.clone();
241  requestedTypesEA.add(ca);
242  boolean uFound = false;
243  Iterator<SecApiReqTypeDef> unIt = unnecessaryAtsEA.iterator();
244  while (unIt.hasNext() && !uFound) {
245  SecApiReqTypeDef u = unIt.next();
246  if (u.getTypeName().contentEquals(a.getTypeName())) {
247  uFound = true;
248  // sort out unnecessary attributes and initialise list of requested attributes
249  ca.filterAttributes(null, u.getAvailableAttributes());
250  }
251  }
252  if (!uFound) {
253  ca.filterAttributes(null, null); // initialise list of requested attributes
254  }
255  }
256  }
257  } else {
258  requestedTypesEA = new ArrayList<SecApiReqTypeDef>();
259  Iterator<SecApiReqTypeDef> avIt = availableTypes.iterator();
260  while (avIt.hasNext()) {
261  SecApiReqTypeDef a = avIt.next();
262  SecApiReqTypeDef ca = a.clone();
263  ca.filterAttributes(null, null);
264  }
265 
266  }
267  } else {
268  Iterator<SecApiReqTypeDef> desIt = desiredTypesEA.iterator();
269  while (desIt.hasNext()) {
270  SecApiReqTypeDef d = desIt.next();
271  Iterator<SecApiReqTypeDef> avIt = availableTypes.iterator();
272  while (avIt.hasNext()) {
273  SecApiReqTypeDef a = avIt.next();
274  if (a.getTypeName().contentEquals(d.getTypeName())) {
275  SecApiReqTypeDef ca = a.clone();
276  requestedTypesEA.add(ca);
277  ca.filterAttributes(d.getAvailableAttributes(), null); // flter attributes
278  break;
279  }
280  }
281  }
282  }
283 
284  AppBean.setReqTypeDefinitions(requestedTypes);
285  AppBean.setReqTypeDefinitionsEA(requestedTypesEA);
286  } // filterTypes()
287 
288  /**
289  * Gets available attributes
290  *
291  * @return Returns available attributes
292  */
293  public ArrayList<String> getAvailableAttributes() {
294  return availableAttributes;
295  }
296 
297  /**
298  * Sets available attributes
299  *
300  * @param availableAttributes Available attributes
301  */
302  public void setAvailableAttributes(ArrayList<String> availableAttributes) {
303  this.availableAttributes = availableAttributes;
304  }
305 
306  /**
307  * Adds available attribute
308  *
309  * @param availableAttribute Available attribute
310  */
311  public void addAvailableAttribute(String availableAttribute) {
312  this.availableAttributes.add(availableAttribute);
313  }
314 
315  /**
316  * Gets type name
317  *
318  * @return Returns type name
319  */
320  public String getTypeName() {
321  return typeName;
322  }
323 
324  /**
325  * Sets type name
326  *
327  * @param typeName Type name
328  */
329  public void setTypeName(String typeName) {
330  this.typeName = typeName;
331  }
332 
333  /**
334  * Gets map with attribute priorities
335  *
336  * @return Returns map with attribute priorities
337  */
338  public HashMap<String, Integer> getPriorities() {
339  return priorities;
340  }
341 
342  /**
343  * Sets map with attribute priorities
344  *
345  * @param priorities Map with attribute priorities
346  */
347  public void setPriorities(HashMap<String, Integer> priorities) {
348  this.priorities = priorities;
349  }
350 
351  /**
352  * Gets priority for given attribute
353  *
354  * @param name Name of attribute
355  * @return Returns priority of attribute with given name
356  */
357  public Integer getPriority(String name) {
358  return priorities.get(name);
359  }
360 
361  /**
362  * Sets priority for given attribute
363  *
364  * @param name Name of attribute
365  * @param priority Priority of attribute
366  */
367  public void setPriority(String name, Integer priority) {
368  priorities.put(name, priority);
369  }
370 
371  /**
372  * Sets entity type description
373  *
374  * @param desc Description of entity type
375  */
376  public void setAltDescDef(String desc){
377  this.altDescDef = desc;
378  }
379 
380  /**
381  * Return entity type description
382  *
383  * @return Returns description of entity type
384  */
385  public String getAltDescDef(){
386  return altDescDef;
387  }
388 
389  /**
390  * Clones object (creates deep copy)
391  *
392  * @return Returns clone (deep copy) of the object
393  */
394  @Override
396  SecApiReqTypeDef result = new SecApiReqTypeDef(this.typeName);
397  if (this.availableAttributes != null) {
398  result.setAvailableAttributes(new ArrayList<String>(this.availableAttributes));
399  }
400  if (this.requestedAttributes != null) {
401  result.setRequestedAttributes(new ArrayList<String>(this.requestedAttributes));
402  }
403  if (this.priorities != null) {
404  result.setPriorities(new HashMap<String, Integer>(this.priorities));
405  }
406  result.setAltDescDef(this.altDescDef);
407 
408  return result;
409  }
410 
411  /**
412  * Gets requested attributes
413  *
414  * @return Returns requested attributes
415  */
416  public ArrayList<String> getRequestedAttributes() {
417  return requestedAttributes;
418  }
419 
420  /**
421  * Sets requested attributes
422  *
423  * @param requestedAttributes Requested attributes
424  */
425  public void setRequestedAttributes(ArrayList<String> requestedAttributes) {
426  this.requestedAttributes = requestedAttributes;
427  }
428 
429  /**
430  * Compares this with other object and returns, whether objects are same type
431  * and have same type name.
432  *
433  * @param obj Object to compare with
434  * @return If object is same type and have same type name, returns true, false otherwise
435  */
436  @Override
437  public boolean equals(Object obj) {
438  if (obj == null) {
439  return false;
440  }
441  if (getClass() != obj.getClass()) {
442  return false;
443  }
444  final SecApiReqTypeDef other = (SecApiReqTypeDef) obj;
445  if ((this.typeName == null) ? (other.typeName != null) : !this.typeName.equals(other.typeName)) {
446  return false;
447  }
448  return true;
449  }
450 
451  @Override
452  public int hashCode() {
453  int hash = 5;
454  hash = 67 * hash + (this.typeName != null ? this.typeName.hashCode() : 0);
455  return hash;
456  }
457 
458 } // class SecApiReqTypeDef()
void setAvailableAttributes(ArrayList< String > availableAttributes)
Singleton for storing global variables.
Definition: AppBean.java:47
void filterAttributes(ArrayList< String > desiredAttributes, ArrayList< String > unnecessaryAttributes)
Class for creating of requested type definition for SEC API.
void setRequestedAttributes(ArrayList< String > requestedAttributes)