Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package it.inaf.oats.ia2.tap.tsm.rest;
import it.inaf.oats.ia2.tap.tsm.model.TapSchema;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Sonia Zorba <zorba at oats.inaf.it>
*/
public class SearchUCDDialog {
static final transient String UCD_REST_SERVICE = "http://localhost:8080/UCDRest/";
static final transient String UCD_NOT_FOUND = "**** Could not find UCD ****";
class UCDInfo {
String score;
String flag;
String word;
UCDInfo(String score, String flag, String word) {
this.score = score;
this.flag = flag;
this.word = word;
}
}
private boolean UCDnotFound;
private String selectedUCD;
private String suggestedUCD;
private final List<UCDInfo> suggestedUCDs;
public SearchUCDDialog() {
suggestedUCDs = new ArrayList<UCDInfo>();
}
public void setDefault() {
UCDnotFound = false;
selectedUCD = null;
suggestedUCD = null;
suggestedUCDs.clear();
}
public void search(String description) throws Exception {
String searchText = URLEncoder.encode(description, "UTF-8").replace("+", "%20");
suggestedUCDs.clear();
// Search assign value
URL urlAssign = new URL(UCD_REST_SERVICE + "assign/" + searchText);
HttpURLConnection connectionAssign = (HttpURLConnection) urlAssign.openConnection();
int responseCodeAssign = connectionAssign.getResponseCode();
if (responseCodeAssign == 200) {
BufferedReader br;
String line, response = "";
br = new BufferedReader(new InputStreamReader(connectionAssign.getInputStream()));
while ((line = br.readLine()) != null) {
response += line;
}
if (response.equals(UCD_NOT_FOUND)) {
UCDnotFound = true;
} else {
selectedUCD = response;
// Search suggested values
URL urlSuggest = new URL(UCD_REST_SERVICE + "suggest/" + searchText);
HttpURLConnection connectionSuggest = (HttpURLConnection) urlSuggest.openConnection();
int responseCodeSuggest = connectionSuggest.getResponseCode();
if (responseCodeSuggest == 200) {
br = new BufferedReader(new InputStreamReader(connectionSuggest.getInputStream()));
while ((line = br.readLine()) != null) {
String[] split1 = line.split("\\|");
if (split1.length == 2) {
String score = split1[0].trim();
String[] split2 = split1[1].trim().split(" ");
if (split2.length == 2) {
String flag = split2[0];
String word = split2[1];
suggestedUCDs.add(new UCDInfo(score, flag, word));
}
}
}
} else if (responseCodeSuggest == 204) {
UCDnotFound = true;
} else {
throw new Exception("Service error");
}
}
} else if (responseCodeAssign == 204) {
UCDnotFound = true;
} else {
throw new Exception("Service error");
}
}
}