Skip to content
SearchUCDDialog.java 5.74 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;
import javax.enterprise.context.Dependent;

/**
 *
 * @author Sonia Zorba <zorba at oats.inaf.it>
 */
public class SearchUCDDialog implements Serializable {
    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 ****";

    public static class UCDInfo implements Serializable {

        private static final long serialVersionUID = 585936072742567972L;
        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;
        }

        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 boolean UCDnotFound;
    private String selectedUCD;
    private String suggestedUCD;
    private final List<UCDInfo> suggestedUCDs;

    public SearchUCDDialog() {
        suggestedUCDs = new ArrayList<UCDInfo>();
    }

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

    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(String selectedUCD) {
        this.selectedUCD = selectedUCD;
    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;
            }

        } else {
            throw new Exception("Service error");
        }
    }