labelingsystem-server  Version 0.1.0.0
Retcat_Finto Class Reference

Static Public Member Functions

static Map< String, SuggestionItem > query (String searchword) throws IOException, RepositoryException, MalformedQueryException, QueryEvaluationException, SesameSparqlException, ResourceNotAvailableException, ParseException, link.labeling.retcat.exceptions.ResourceNotAvailableException
 
static JSONObject info (String url) throws IOException, RepositoryException, MalformedQueryException, QueryEvaluationException, SesameSparqlException, ResourceNotAvailableException, ParseException, RetcatException
 

Member Function Documentation

◆ info()

static JSONObject info ( String  url) throws IOException, RepositoryException, MalformedQueryException, QueryEvaluationException, SesameSparqlException, ResourceNotAvailableException, ParseException, RetcatException
static
101  {
102  try {
103  String outputUrl = url;
104  String vocab = url.split("/")[4];
105  url = GeneralFunctions.encodeURIComponent(url);
106  url = "http://api.finto.fi/rest/v1/" + vocab + "/label?lang=en&uri=" + url;
107  URL obj = new URL(url);
108  HttpURLConnection con = (HttpURLConnection) obj.openConnection();
109  con.setRequestMethod("GET");
110  BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF8"));
111  String inputLine;
112  StringBuilder response = new StringBuilder();
113  while ((inputLine = in.readLine()) != null) {
114  response.append(inputLine);
115  }
116  in.close();
117  // parse json
118  JSONObject jsonObject = (JSONObject) new JSONParser().parse(response.toString());
119  String labelValue = (String) jsonObject.get("prefLabel");
120  // output
121  JSONObject jsonOut = new JSONObject();
122  jsonOut.put("label", labelValue);
123  jsonOut.put("lang", "");
124  // get retcat info
125  String type = "finto";
126  String quality = "";
127  String group = "en";
128  for (RetcatItem item : LocalRetcatItems.getLocalCatalogue()) {
129  if (item.getType().equals(type)) {
130  quality = item.getQuality();
131  group = item.getGroup();
132  }
133  }
134  jsonOut.put("type", type);
135  jsonOut.put("quality", quality);
136  jsonOut.put("group", group);
137  jsonOut.put("description", "");
138  jsonOut.put("uri", outputUrl);
139  jsonOut.put("scheme", vocab);
140  // broader and narrower
141  JSONArray broaderTerms = new JSONArray();
142  JSONArray narrowerTerms = new JSONArray();
143  // broader
144  String urlB = "http://api.finto.fi/rest/v1/" + vocab + "/broader?lang=en&uri=" + url;
145  URL objB = new URL(urlB);
146  HttpURLConnection conB = (HttpURLConnection) objB.openConnection();
147  conB.setRequestMethod("GET");
148  BufferedReader inB = new BufferedReader(new InputStreamReader(conB.getInputStream(), "UTF8"));
149  String inputLineB;
150  StringBuilder responseB = new StringBuilder();
151  while ((inputLineB = inB.readLine()) != null) {
152  responseB.append(inputLineB);
153  }
154  inB.close();
155  // parse json
156  JSONObject jsonObjectB = (JSONObject) new JSONParser().parse(responseB.toString());
157  JSONArray broaderArray = (JSONArray) jsonObjectB.get("broader");
158  for (Object bo : broaderArray) {
159  JSONObject tmp = (JSONObject) bo;
160  JSONObject tmpObject = new JSONObject();
161  String broaderURI = (String) tmp.get("uri");
162  String broaderLabel = (String) tmp.get("prefLabel");
163  tmpObject.put("label", broaderLabel);
164  tmpObject.put("uri", broaderURI);
165  broaderTerms.add(tmpObject);
166  }
167  // narrower
168  String urlN = "http://api.finto.fi/rest/v1/" + vocab + "/narrower?lang=en&uri=" + url;
169  URL objN = new URL(urlN);
170  HttpURLConnection conN = (HttpURLConnection) objN.openConnection();
171  conN.setRequestMethod("GET");
172  BufferedReader inN = new BufferedReader(new InputStreamReader(conN.getInputStream(), "UTF8"));
173  String inputLineN;
174  StringBuilder responseN = new StringBuilder();
175  while ((inputLineN = inN.readLine()) != null) {
176  responseN.append(inputLineN);
177  }
178  inN.close();
179  // parse json
180  JSONObject jsonObjectN = (JSONObject) new JSONParser().parse(responseN.toString());
181  JSONArray narrowerArray = (JSONArray) jsonObjectN.get("narrower");
182  for (Object no : narrowerArray) {
183  JSONObject tmp = (JSONObject) no;
184  JSONObject tmpObject = new JSONObject();
185  String narrowerURI = (String) tmp.get("uri");
186  String narrowerLabel = (String) tmp.get("prefLabel");
187  tmpObject.put("label", narrowerLabel);
188  tmpObject.put("uri", narrowerURI);
189  narrowerTerms.add(tmpObject);
190  }
191  jsonOut.put("broaderTerms", broaderTerms);
192  jsonOut.put("narrowerTerms", narrowerTerms);
193  if (jsonOut.get("label") != null && !jsonOut.get("label").equals("")) {
194  return jsonOut;
195  } else {
196  throw new RetcatException("no label for this uri available");
197  }
198  } catch (Exception e) {
199  return new JSONObject();
200  }
201  }

References GeneralFunctions.encodeURIComponent(), and LocalRetcatItems.getLocalCatalogue().

Referenced by RetcatResource.getInfoSkosmosFinto(), and RetcatResource.getQueryResultsSkosmosFINTO().

◆ query()

static Map<String, SuggestionItem> query ( String  searchword) throws IOException, RepositoryException, MalformedQueryException, QueryEvaluationException, SesameSparqlException, ResourceNotAvailableException, ParseException, link.labeling.retcat.exceptions.ResourceNotAvailableException
static
27  {
28  searchword = GeneralFunctions.encodeURIComponent(searchword);
29  String url_string = "http://finto.fi/rest/v1/search?query=*" + searchword + "*&lang=en&type=skos:Concept&fields=narrower%20broader%20scopeNote&vocab=allars%20koko%20ponduskategorier%20ysa%20yso%20juho%20jupo%20keko%20okm-tieteenala%20liito%20mero%20puho%20tsr%20afo%20kassu%20mesh%20tero%20maotao%20musa%20muso%20valo%20kauno%20kito%20kto&limit=" + RetcatResource.getLimit();
30  URL url = new URL(url_string);
31  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
32  BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
33  String inputLine;
34  StringBuilder response = new StringBuilder();
35  while ((inputLine = br.readLine()) != null) {
36  response.append(inputLine);
37  }
38  br.close();
39  // fill objects
40  JSONObject jsonObject = (JSONObject) new JSONParser().parse(response.toString());
41  JSONArray resultsArray = (JSONArray) jsonObject.get("results");
42  Map<String, SuggestionItem> autosuggests = new HashMap<String, SuggestionItem>();
43  for (Object element : resultsArray) {
44  JSONObject tmpElement = (JSONObject) element;
45  String uriValue = (String) tmpElement.get("uri");
46  autosuggests.put(uriValue, new SuggestionItem(uriValue));
47  SuggestionItem tmpAutosuggest = autosuggests.get(uriValue);
48  String labelValue = (String) tmpElement.get("prefLabel");
49  tmpAutosuggest.setLabel(labelValue);
50  JSONArray desArray = (JSONArray) tmpElement.get("skos:scopeNote");
51  if (desArray != null) {
52  for (Object e : desArray) {
53  String descStr = e.toString();
54  tmpAutosuggest.setDescription(descStr);
55  }
56  }
57  String vocabValue = (String) tmpElement.get("vocab");
58  tmpAutosuggest.setSchemeTitle(vocabValue);
59  JSONArray boraderArray = (JSONArray) tmpElement.get("broader");
60  JSONArray narrowerArray = (JSONArray) tmpElement.get("narrower");
61  tmpAutosuggest.setLanguage("en");
62  // query for broader
63  if (boraderArray != null) {
64  for (Object item : boraderArray) {
65  JSONObject tmpObject = (JSONObject) item;
66  HashMap<String, String> hstmp = new HashMap();
67  String uriValueTmp = (String) tmpObject.get("uri");
68  String labelValueTmp = (String) tmpObject.get("prefLabel");
69  hstmp.put(uriValueTmp, labelValueTmp);
70  tmpAutosuggest.setBroaderTerm(hstmp);
71  }
72  }
73  // query for narrower
74  if (narrowerArray != null) {
75  for (Object item : boraderArray) {
76  JSONObject tmpObject = (JSONObject) item;
77  HashMap<String, String> hstmp = new HashMap();
78  String uriValueTmp = (String) tmpObject.get("uri");
79  String labelValueTmp = (String) tmpObject.get("prefLabel");
80  hstmp.put(uriValueTmp, labelValueTmp);
81  tmpAutosuggest.setNarrowerTerm(hstmp);
82  }
83  }
84  // get retcat info
85  String type = "finto";
86  String quality = "";
87  String group = "";
88  for (RetcatItem item : LocalRetcatItems.getLocalCatalogue()) {
89  if (item.getType().equals(type)) {
90  quality = item.getQuality();
91  group = item.getGroup();
92  }
93  }
94  tmpAutosuggest.setType(type);
95  tmpAutosuggest.setQuality(quality);
96  tmpAutosuggest.setGroup(group);
97  }
98  return autosuggests;
99  }

References GeneralFunctions.encodeURIComponent(), RetcatResource.getLimit(), and LocalRetcatItems.getLocalCatalogue().

Referenced by RetcatResource.getQueryResultsSkosmosFINTO().

Exception