/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.lagunex.nlp;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import org.springframework.web.client.RestTemplate;

/**
 *
 * @author carloshq
 */
public class SentimentAnalysis {
    private static SentimentAnalysis instance;
    private static final String URL = "https://api.idolondemand.com/1/api/sync/analyzesentiment/v1";
    private final String API_KEY;
    
    private final List<Locale> LANGS = Arrays.asList(new Locale[]{
        Locale.ENGLISH, new Locale("es"), Locale.FRENCH, Locale.GERMAN,
        Locale.ITALIAN, Locale.CHINESE, new Locale("por"), new Locale("dut"),
        new Locale("rus"), new Locale("cze"), new Locale("tur")
    });

    public static SentimentAnalysis getInstance() {
        if (instance == null) {
            instance = new SentimentAnalysis();
        }
        return instance;
    }
    
    private SentimentAnalysis(){
        API_KEY = getApiKey();
        if (API_KEY == null) {
            throw new RuntimeException("No API key found"); 
        }
    }

    private String getApiKey() {
        String apiKey = System.getProperty("idolOnDemand.apiKey");
        try {
            InputStream is = SentimentAnalysis.class.getResourceAsStream("/idol.properties");
            Properties p = new Properties();
            if (is != null) p.load(is);
            if (p.containsKey("idolOnDemand.apiKey")) {
                apiKey = p.getProperty("idolOnDemand.apiKey");
            }
        } catch (IOException e) {
        }
        return apiKey;
    }

    public SentimentResult analyse(String opinion) {
        return analyse(opinion, Locale.ENGLISH);
    }

    public SentimentResult analyse(String opinion, Locale locale) {
        if (LANGS.contains(locale)) {
            return analyse(opinion, locale.getISO3Language());
        } else {
            return null;
        }
    }

    private SentimentResult analyse(String opinion, String lang) {
        RestTemplate rest = new RestTemplate();
        return rest.getForObject(
                URL+"?apikey="+this.API_KEY+"&text="+opinion+"&language="+lang,
                SentimentResult.class 
        );
    }
}

@@ -260,16 +260,89 @@
 Stream;%0A
+import java.io.UnsupportedEncodingException;%0Aimport java.net.URLEncoder;%0A
 import j
@@ -435,16 +435,140 @@
 erties;%0A
+import java.util.logging.Level;%0Aimport java.util.logging.Logger;%0Aimport org.springframework.web.client.RestClientException;%0A
 import o
@@ -680,55 +680,8 @@
 s %7B%0A
-    private static SentimentAnalysis instance;%0A
     
@@ -777,16 +777,162 @@
 nt/v1%22;%0A
+    private static final Logger LOGGER = Logger.getLogger(SentimentAnalysis.class.getName());%0A    %0A    private static SentimentAnalysis instance;%0A
     priv
@@ -1498,110 +1498,8 @@
 ();%0A
-        if (API_KEY == null) %7B%0A            throw new RuntimeException(%22No API key found%22); %0A        %7D%0A
     
@@ -1963,32 +1963,156 @@
  e) %7B%0A        %7D%0A
+        if (apiKey == null) %7B%0A            throw new RuntimeException(%22property idolOnDemand.apiKey not defined%22);%0A        %7D%0A
         return a
@@ -2310,20 +2310,74 @@
         
-if (
+opinion = encode(opinion);%0A        if (opinion != null && 
 LANGS.co
@@ -2405,39 +2405,43 @@
          return 
-analyse
+callRestApi
 (opinion, locale
@@ -2533,38 +2533,329 @@
 rivate S
-entimentResult analyse
+tring encode(String opinion) %7B%0A        String encoded = null;%0A        try %7B%0A            encoded = URLEncoder.encode(opinion, %22UTF-8%22);%0A        %7D catch (UnsupportedEncodingException ex) %7B%0A            LOGGER.warning(ex.getMessage());%0A        %7D %0A        return encoded;%0A    %7D%0A%0A    private SentimentResult callRestApi
 (String 
@@ -2874,24 +2874,63 @@
 ing lang) %7B%0A
+        SentimentResult result = null;%0A
         Rest
@@ -2973,22 +2973,42 @@
         
-return
+try %7B%0A            result =
  rest.ge
@@ -3136,16 +3136,18 @@
 lt.class
+);
  %0A      
@@ -3148,17 +3148,127 @@
         
-)
+%7D catch (RestClientException ex) %7B%0A            LOGGER.warning(ex.getMessage());%0A        %7D%0A        return result
 ;%0A    %7D%0A

refactor and return null on errors