4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
SECTypesTranslator.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: SECTypesTranslator.java
5  * Description: Translates JSON with types from SEC API to requested type
6  * definitions
7  */
8 
9 package cz.vutbr.fit.knot.annotations.modules.suggestionManager;
10 
12 import java.util.ArrayList;
13 import java.util.Iterator;
14 import java.util.logging.Level;
15 import java.util.logging.Logger;
16 import org.json.simple.JSONArray;
17 import org.json.simple.JSONObject;
18 import org.json.simple.JSONValue;
19 
20 /**
21  * Translates JSON with types from SEC API to requested type definitions
22  *
23  * @brief Translates JSON with types from SEC API to requested type definitions
24  */
25 public class SECTypesTranslator {
26 
27  public static ArrayList<SecApiReqTypeDef> translate(String typesData) {
28  ArrayList<SecApiReqTypeDef> result = new ArrayList<SecApiReqTypeDef>();
29  if (typesData == null || typesData.isEmpty()) {
30  return result;
31  }
32  JSONObject jsonData = (JSONObject)JSONValue.parse(typesData);
33  if (jsonData == null) {
35  String msg = "Unable to parse received JSON string with types and attributes.";
36  Logger.getLogger(SECTypesTranslator.class.getName()).log(Level.SEVERE, msg);
37  }
38  return result;
39  }
40  if (checkException(jsonData)) { // check for exception
41  return result;
42  }
43  try {
44  JSONArray jsonTypes = (JSONArray)jsonData.get("data");
45  if (jsonTypes == null || jsonTypes.isEmpty()) {
47  String msg = "Unable to get types and attributes.";
48  Logger.getLogger(SECTypesTranslator.class.getName()).log(Level.SEVERE, msg);
49  }
50  }
51  int typesLength = jsonTypes.size();
52  for (int i = 0; i < typesLength; i++) {
53  JSONObject typesItem = (JSONObject)jsonTypes.get(i);
54  String name = (String)typesItem.get("type");
55  if (name == null || name.isEmpty()) {
56  continue;
57  }
58 
59  SecApiReqTypeDef satd = new SecApiReqTypeDef(name);
60 
61  JSONArray attributes = (JSONArray)typesItem.get("attributes");
62 
63  int attLength = attributes.size();
64  for (int j = 0; j < attLength; j++) {
65  String attItem = (String) attributes.get(j);
66  if (attItem == null || attItem.isEmpty()) {
67  continue;
68  }
69  satd.addAvailableAttribute(attItem);
70  }
71  if (Constants.ADD_IDENTIFIER && !satd.getAvailableAttributes().contains("identifier")) {
72  // identifier is internal - it is hardcoded hidden feature of SEC API
73  // and must be added manually here
74  satd.addAvailableAttribute("identifier");
75  }
76  if (Constants.ADD_DISAMBIGUATION && !satd.getAvailableAttributes().contains("disambiguation")) {
77  // disambiguation is internal - it is hardcoded hidden feature of SEC API
78  // and must be added manually here
79  satd.addAvailableAttribute("disambiguation");
80  }
81  if (Constants.ADD_CONFIDENCE && !satd.getAvailableAttributes().contains("confidence")) {
82  // confidence is necessary for features which should be available
83  // so if it is missing it must be added manually here
84  satd.addAvailableAttribute("confidence");
85  }
86 
87  JSONObject priorities = (JSONObject)typesItem.get("priorities");
88  if (priorities != null) {
89  Iterator<String> attIt = satd.getAvailableAttributes().iterator();
90  while (attIt.hasNext()) {
91  String att = attIt.next();
92  try {
93  Long priority = (Long) priorities.get(att);
94  if (priority != null) {
95  satd.setPriority(att, priority.intValue());
96  }
97  } catch (Exception e) {
99  String msg = "Unable to load priority of attribute.";
100  Logger.getLogger(SECTypesTranslator.class.getName()).log(Level.SEVERE, msg, e);
101  }
102  }
103  }
104  }
105 
106  result.add(satd);
107  }
108 
109  } catch (Exception e) {
111  String msg = "Unable to parse received JSON string with types and attributes.";
112  Logger.getLogger(SECTypesTranslator.class.getName()).log(Level.SEVERE, msg, e);
113  }
114  return result;
115  }
116 
117  return result;
118  } // translate()
119 
120 
121  /**
122  * Checks if JSON from SEC API contains exception and if yes, logs it
123  *
124  * @param jsonData JSON data from SEC API
125  * @return If exception was presented, returns true, false otherwise
126  */
127  private static boolean checkException(JSONObject jsonData) {
128  String exceptionData = null;
129  try { // try to get exception
130  exceptionData = (String) jsonData.get("exception");
131  } catch (Exception e) { // error never mind here
132  }
133  if (exceptionData != null && !exceptionData.isEmpty()) {
135  String msg = "SEC API returned exception: " + exceptionData;
136  Logger.getLogger(SECTypesTranslator.class.getName()).log(Level.SEVERE, msg);
137  }
138  return true;
139  }
140  return false;
141  }
142 } // class SECTypesTranslator
static ArrayList< SecApiReqTypeDef > translate(String typesData)
Class for creating of requested type definition for SEC API.
Translates JSON with types from SEC API to requested type definitions.