labelingsystem-server  Version 0.1.0.0
Retcat_Archwort 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
153  {
154  try {
155  String[] id = url.split("/");
156  String newURL = "http://archwort.dainst.org/de/term/xml.php?jsonldTema=" + id[id.length - 1];
157  URL obj = new URL(newURL);
158  HttpURLConnection con = (HttpURLConnection) obj.openConnection();
159  con.setRequestMethod("GET");
160  BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF8"));
161  String inputLine;
162  StringBuilder response = new StringBuilder();
163  while ((inputLine = in.readLine()) != null) {
164  response.append(inputLine);
165  }
166  in.close();
167  // parse json
168  JSONObject jsonOut = new JSONObject();
169  JSONObject jsonObject = (JSONObject) new JSONParser().parse(response.toString());
170  // output
171  JSONObject prefLabel = (JSONObject) jsonObject.get("skos:prefLabel");
172  String lang = (String) prefLabel.get("@language");
173  prefLabel.remove("@language");
174  String label = prefLabel.toJSONString().replace("{\"@value=\":\"", "").replace("\"}", "");
175  jsonOut.put("label", label);
176  jsonOut.put("lang", lang);
177  jsonOut.put("description", "");
178  jsonOut.put("uri", url);
179  // get retcat info
180  String type = "archwort";
181  String quality = "";
182  String group = "";
183  for (RetcatItem item : LocalRetcatItems.getLocalCatalogue()) {
184  if (item.getType().equals(type)) {
185  quality = item.getQuality();
186  group = item.getGroup();
187  }
188  }
189  jsonOut.put("type", type);
190  jsonOut.put("quality", quality);
191  jsonOut.put("group", group);
192  jsonOut.put("scheme", "archwort");
193  // broader and narrower
194  JSONArray broaderTerms = new JSONArray();
195  JSONArray narrowerTerms = new JSONArray();
196  JSONArray broader = (JSONArray) jsonObject.get("skos:broader");
197  JSONArray narrower = (JSONArray) jsonObject.get("skos:narrower");
198  // broader
199  if (broader != null) {
200  for (Object broaderItem : broader) {
201  String tmp = (String) broaderItem;
202  String[] tmpArray = tmp.split("/");
203  String broaderurl = "http://archwort.dainst.org/de/term/xml.php?jsonldTema=" + tmpArray[tmpArray.length - 1];
204  URL obj2 = new URL(broaderurl);
205  HttpURLConnection con2 = (HttpURLConnection) obj2.openConnection();
206  con2.setRequestMethod("GET");
207  BufferedReader in2 = new BufferedReader(new InputStreamReader(con2.getInputStream(), "UTF8"));
208  String inputLine2;
209  StringBuilder response2 = new StringBuilder();
210  while ((inputLine2 = in2.readLine()) != null) {
211  response2.append(inputLine2);
212  }
213  in.close();
214  // parse json
215  JSONObject broaderTmp = new JSONObject();
216  JSONObject jsonObject2 = (JSONObject) new JSONParser().parse(response2.toString());
217  JSONObject prefLabel2 = (JSONObject) jsonObject2.get("skos:prefLabel");
218  prefLabel2.remove("@language");
219  String label2 = prefLabel2.toJSONString().replace("{\"@value=\":\"", "").replace("\"}", "");
220  broaderTmp.put("label", label2);
221  tmp = tmp.replace("http://archwort.dainst.org/thesaurus/de/vocab/?tema=", "http://archwort.dainst.org/de/term/");
222  broaderTmp.put("uri", tmp);
223  broaderTerms.add(broaderTmp);
224  }
225  }
226  // narrower
227  if (narrower != null) {
228  for (Object narrowerItem : narrower) {
229  String tmp = (String) narrowerItem;
230  String[] tmpArray = tmp.split("/");
231  String narrowerurl = "http://archwort.dainst.org/de/term/xml.php?jsonldTema=" + tmpArray[tmpArray.length - 1];
232  URL obj2 = new URL(narrowerurl);
233  HttpURLConnection con2 = (HttpURLConnection) obj2.openConnection();
234  con2.setRequestMethod("GET");
235  BufferedReader in2 = new BufferedReader(new InputStreamReader(con2.getInputStream(), "UTF8"));
236  String inputLine2;
237  StringBuilder response2 = new StringBuilder();
238  while ((inputLine2 = in2.readLine()) != null) {
239  response2.append(inputLine2);
240  }
241  in.close();
242  // parse json
243  JSONObject narrowerTmp = new JSONObject();
244  JSONObject jsonObject2 = (JSONObject) new JSONParser().parse(response2.toString());
245  JSONObject prefLabel2 = (JSONObject) jsonObject2.get("skos:prefLabel");
246  prefLabel2.remove("@language");
247  String label2 = prefLabel2.toJSONString().replace("{\"@value=\":\"", "").replace("\"}", "");
248  narrowerTmp.put("label", label2);
249  tmp = tmp.replace("http://archwort.dainst.org/thesaurus/de/vocab/?tema=", "http://archwort.dainst.org/de/term/");
250  narrowerTmp.put("uri", tmp);
251  narrowerTerms.add(narrowerTmp);
252  }
253  }
254  jsonOut.put("broaderTerms", broaderTerms);
255  jsonOut.put("narrowerTerms", narrowerTerms);
256  if (jsonOut.get("label") != null && !jsonOut.get("label").equals("")) {
257  return jsonOut;
258  } else {
259  throw new RetcatException("no label for this uri available");
260  }
261  } catch (Exception e) {
262  return new JSONObject();
263  }
264  }

References LocalRetcatItems.getLocalCatalogue().

Referenced by RetcatResource.getInfoArchwort(), and RetcatResource.getQueryResultsARCHWORT().

◆ query()

static Map<String, SuggestionItem> query ( String  searchword) throws IOException, RepositoryException, MalformedQueryException, QueryEvaluationException, SesameSparqlException, ResourceNotAvailableException, ParseException, link.labeling.retcat.exceptions.ResourceNotAvailableException
static
29  {
30  String url = "http://archwort.dainst.org/de/vocab/sparql.php";
31  String sparql = "PREFIX skos: <http://www.w3.org/2004/02/skos/core#> PREFIX dc: <http://purl.org/dc/elements/1.1/> "
32  + "SELECT ?Subject ?prefLabel ?BroaderPreferredTerm ?BroaderPreferred ?NarrowerPreferredTerm ?NarrowerPreferred ?schemeTitle WHERE { "
33  + "?Subject skos:inScheme ?scheme . "
34  + "?Subject skos:prefLabel ?prefLabel . "
35  + "?scheme dc:title ?schemeTitle . "
36  + "OPTIONAL {?Subject skos:broader ?BroaderPreferred . ?BroaderPreferred skos:prefLabel ?BroaderPreferredTerm. } "
37  + "OPTIONAL {?Subject skos:narrower ?NarrowerPreferred . ?NarrowerPreferred skos:prefLabel ?NarrowerPreferredTerm . } "
38  + "FILTER(regex(?prefLabel, '" + searchword + "', 'i')) "
39  + "}";
40  URL obj = new URL(url);
41  HttpURLConnection con = (HttpURLConnection) obj.openConnection();
42  con.setRequestMethod("POST");
43  con.setRequestProperty("Accept", "application/sparql-results+json");
44  String urlParameters = "query=" + sparql;
45  con.setDoOutput(true);
46  DataOutputStream wr = new DataOutputStream(con.getOutputStream());
47  BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(wr, "UTF-8"));
48  writer.write(urlParameters);
49  writer.close();
50  wr.close();
51  BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF8"));
52  String inputLine;
53  StringBuilder response = new StringBuilder();
54  while ((inputLine = in.readLine()) != null) {
55  response.append(inputLine);
56  }
57  in.close();
58  // parse SPARQL results json
59  JSONObject jsonObject = (JSONObject) new JSONParser().parse(response.toString());
60  JSONObject resultsObject = (JSONObject) jsonObject.get("results");
61  JSONArray bindingsArray = (JSONArray) resultsObject.get("bindings");
62  // create unique list of ids
63  HashSet<String> uris = new HashSet<String>();
64  for (Object element : bindingsArray) {
65  JSONObject tmpElement = (JSONObject) element;
66  JSONObject subject = (JSONObject) tmpElement.get("Subject");
67  String subjectValue = (String) subject.get("value");
68  subjectValue = subjectValue.replace("http://archwort.dainst.org/thesaurus/de/vocab/?tema=", "http://archwort.dainst.org/de/term/");
69  uris.add(subjectValue);
70  }
71  // create list of autosuggest objects
72  Map<String, SuggestionItem> autosuggests = new HashMap<String, SuggestionItem>();
73  for (String element : uris) {
74  autosuggests.put(element, new SuggestionItem(element));
75  }
76  int z = 0;
77  // fill objects
78  for (Object element : bindingsArray) {
79  JSONObject tmpElement = (JSONObject) element;
80  // get Subject
81  JSONObject subject = (JSONObject) tmpElement.get("Subject");
82  String subjectValue = (String) subject.get("value");
83  subjectValue = subjectValue.replace("http://archwort.dainst.org/thesaurus/de/vocab/?tema=", "http://archwort.dainst.org/de/term/");
84  // for every subject value get object from list and write values in it
85  SuggestionItem tmpAutosuggest = autosuggests.get(subjectValue);
86  // get Label
87  JSONObject labelObject = (JSONObject) tmpElement.get("prefLabel");
88  String labelValue = (String) labelObject.get("value");
89  String labelLang = (String) labelObject.get("xml:lang");
90  tmpAutosuggest.setLabel(labelValue);
91  tmpAutosuggest.setLanguage(labelLang);
92  // get Scheme
93  JSONObject schemeObject = (JSONObject) tmpElement.get("schemeTitle");
94  String schemeValue = (String) schemeObject.get("value");
95  tmpAutosuggest.setSchemeTitle(schemeValue);
96  // get broader
97  String broaderVL = "";
98  String broaderURI = "";
99  String broaderLang = "";
100  JSONObject broaderObject = (JSONObject) tmpElement.get("BroaderPreferredTerm");
101  if (broaderObject != null) {
102  String broaderValue = (String) broaderObject.get("value");
103  broaderLang = (String) broaderObject.get("xml:lang");
104  broaderVL = broaderValue.replace("<", "").replace(">", "");
105  }
106  JSONObject broaderURIObject = (JSONObject) tmpElement.get("BroaderPreferred");
107  if (broaderURIObject != null) {
108  broaderURI = (String) broaderURIObject.get("value");
109  }
110  if (!broaderURI.equals("")) {
111  HashMap<String, String> hstmpBroader = new HashMap<String, String>();
112  broaderURI = broaderURI.replace("http://archwort.dainst.org/thesaurus/de/vocab/?tema=", "http://archwort.dainst.org/de/term/");
113  hstmpBroader.put(broaderURI, broaderVL);
114  tmpAutosuggest.setBroaderTerm(hstmpBroader);
115  }
116  // get narrower
117  String narrowerVL = "";
118  String narrowerURI = "";
119  String narrowerLang = "";
120  JSONObject narrowerObject = (JSONObject) tmpElement.get("NarrowerPreferredTerm");
121  if (narrowerObject != null) {
122  String narrowerValue = (String) narrowerObject.get("value");
123  narrowerLang = (String) narrowerObject.get("xml:lang");
124  narrowerVL = narrowerValue.replace("<", "").replace(">", "");
125  }
126  JSONObject narrowerURIObject = (JSONObject) tmpElement.get("NarrowerPreferred");
127  if (narrowerURIObject != null) {
128  narrowerURI = (String) narrowerURIObject.get("value");
129  }
130  if (!narrowerURI.equals("")) {
131  HashMap<String, String> hstmpNarrower = new HashMap<String, String>();
132  narrowerURI = narrowerURI.replace("http://archwort.dainst.org/thesaurus/de/vocab/?tema=", "http://archwort.dainst.org/de/term/");
133  hstmpNarrower.put(narrowerURI, narrowerVL);
134  tmpAutosuggest.setNarrowerTerm(hstmpNarrower);
135  }
136  // get retcat info
137  String type = "archwort";
138  String quality = "";
139  String group = "";
140  for (RetcatItem item : LocalRetcatItems.getLocalCatalogue()) {
141  if (item.getType().equals(type)) {
142  quality = item.getQuality();
143  group = item.getGroup();
144  }
145  }
146  tmpAutosuggest.setType(type);
147  tmpAutosuggest.setQuality(quality);
148  tmpAutosuggest.setGroup(group);
149  }
150  return autosuggests;
151  }

References LocalRetcatItems.getLocalCatalogue().

Referenced by RetcatResource.getQueryResultsARCHWORT().

Exception