Skip to content
SearchUCDDialog.java 4.89 KiB
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;
        String definition;

        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 {
        setDefault();
        this.description = description;
        String searchText = URLEncoder.encode(this.description, "UTF-8").replace("+", "%20");

        // Search assign value
        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;

                // Search suggested values
                URL urlSuggest = new URL(UCD_REST_SERVICE + "suggest?value=" + 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");
        }
    }

    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;
    }