Newer
Older
package it.inaf.oats.ia2.tapschemamanager.webapp;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.HttpURLConnection;
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 implements Serializable {
static final transient String UCD_REST_SERVICE = "http://localhost:8080/ucd/";
static final transient String UCD_NOT_FOUND = "**** Could not find UCD ****";
class UCDInfo implements Serializable {
String score;
String flag;
String word;
UCDInfo(String score, String flag, String word) {
this.score = score;
this.flag = flag;
this.word = word;
}
}
private String description;
private boolean UCDnotFound;
private String selectedUCD;
private String suggestedUCD;
private final List<UCDInfo> suggestedUCDs;
public SearchUCDDialog() {
suggestedUCDs = new ArrayList<UCDInfo>();
}
public void setDefault() {
description = null;
UCDnotFound = false;
selectedUCD = null;
suggestedUCD = null;
suggestedUCDs.clear();
}
public void search(String description) throws Exception {
setDefault();
this.description = description;
String searchText = URLEncoder.encode(this.description, "UTF-8").replace("+", "%20");
URL urlAssign = new URL(UCD_REST_SERVICE + "assign?value=" + 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;
suggestedUCD = response;
// Search suggested values
URL urlSuggest = new URL(UCD_REST_SERVICE + "suggest?value=" + searchText);
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
109
110
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");
}
}
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
public void selectUCD() {
selectedUCD = suggestedUCD;
}
public void selectUCD(int index) {
selectedUCD = suggestedUCDs.get(index).word;
}
private String explain(String ucd) throws Exception {
String searchText = URLEncoder.encode(ucd, "UTF-8").replace("+", "%20");
URL url = new URL(UCD_REST_SERVICE + "explain?value=" + searchText);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line, response = "";
while ((line = br.readLine()) != null) {
response += line;
}
return response;
} else {
throw new Exception("Service error");
}
}
public String explain(int index) throws Exception {
return explain(suggestedUCDs.get(index).word);
}
public String explain() throws Exception {
return explain(suggestedUCD);
}
public String getSelectedUCD() {
return selectedUCD;
}