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;
Sonia Zorba
committed
import javax.enterprise.context.Dependent;
/**
*
* @author Sonia Zorba <zorba at oats.inaf.it>
*/
Sonia Zorba
committed
@Dependent
public class SearchUCDDialog implements Serializable {
Sonia Zorba
committed
private static final long serialVersionUID = -3503024742241865133L;
static final transient String UCD_REST_SERVICE = "http://localhost:8080/ucd/";
static final transient String UCD_NOT_FOUND = "**** Could not find UCD ****";
Sonia Zorba
committed
public static class UCDInfo implements Serializable {
private static final long serialVersionUID = 585936072742567972L;
Sonia Zorba
committed
private final String score;
private final String flag;
private final String word;
private String definition;
UCDInfo(String score, String flag, String word) {
this.score = score;
this.flag = flag;
this.word = word;
}
Sonia Zorba
committed
public String getScore() {
return score;
}
public String getFlag() {
return flag;
}
public String getWord() {
return word;
}
public String getDefinition() {
return definition;
}
public void setDefinition(String definition) {
this.definition = definition;
}
private String description;
private boolean UCDnotFound;
private String selectedUCD;
private String suggestedUCD;
private final List<UCDInfo> suggestedUCDs;
public SearchUCDDialog() {
suggestedUCDs = new ArrayList<UCDInfo>();
}
Sonia Zorba
committed
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public boolean isUCDnotFound() {
return UCDnotFound;
}
public String getSelectedUCD() {
return selectedUCD;
}
public String getSuggestedUCD() {
return suggestedUCD;
}
public List<UCDInfo> getSuggestedUCDs() {
return suggestedUCDs;
}
description = null;
UCDnotFound = false;
selectedUCD = null;
suggestedUCD = null;
suggestedUCDs.clear();
}
public void search(String description) throws Exception {
setDefault();
this.description = description;
Sonia Zorba
committed
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);
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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");
}
}
Sonia Zorba
committed
public void selectUCD(String selectedUCD) {
this.selectedUCD = selectedUCD;
}
Sonia Zorba
committed
public void explain(UCDInfo ucdInfo) throws Exception {
System.out.println("explain: " + ucdInfo.getWord());
String searchText = URLEncoder.encode(ucdInfo.getWord(), "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;
}
Sonia Zorba
committed
ucdInfo.setDefinition(response);
} else {
throw new Exception("Service error");
}
}