package org.xbib.elasticsearch.action.langdetect;

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.action.support.TransportAction;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.threadpool.ThreadPool;
import org.xbib.elasticsearch.common.langdetect.Language;
import org.xbib.elasticsearch.common.langdetect.LanguageDetectionException;
import org.xbib.elasticsearch.common.langdetect.LangdetectService;

import java.util.List;

public class TransportLangdetectAction extends TransportAction<LangdetectRequest, LangdetectResponse> {

    private final LangdetectService service;

    private final LangdetectService shortService;

    @Inject
    public TransportLangdetectAction(Settings settings, ThreadPool threadPool,
                                     ActionFilters actionFilters,  IndexNameExpressionResolver indexNameExpressionResolver) {
        super(settings, LangdetectAction.NAME, threadPool, actionFilters, indexNameExpressionResolver);
        this.service = new LangdetectService(settings);
        this.shortService = new LangdetectService(settings, "short-text");
    }

    @Override
    protected void doExecute(LangdetectRequest request, ActionListener<LangdetectResponse> listener) {
        try {
            List<Language> langs;
            if ("short-text".equals(request.getProfile())) {
                langs = shortService.detectAll(request.getText());
            } else {
                langs = service.detectAll(request.getText());
            }
            listener.onResponse(new LangdetectResponse().setLanguages(langs).setProfile(request.getProfile()));
        } catch (LanguageDetectionException e) {
            listener.onFailure(e);
        }
    }
}

@@ -640,20 +640,68 @@
 va.util.
-List
+HashMap;%0Aimport java.util.List;%0Aimport java.util.Map
 ;%0A%0Apubli
@@ -822,54 +822,26 @@
 nal 
-LangdetectService service;%0A%0A    private final 
+static Map%3CString,
 Lang
@@ -853,29 +853,44 @@
 tService
+%3E
  s
-hortS
 ervice
+s = new HashMap%3C%3E()
 ;%0A%0A    @
@@ -1217,83 +1217,24 @@
     
-this.
 service
- = new LangdetectService(settings);%0A        this.shortService =
+s.put(%22%22,
  new
@@ -1264,22 +1264,9 @@
 ings
-, %22short-text%22
+)
 );%0A 
@@ -1418,88 +1418,173 @@
     
-List%3CLanguage%3E langs;%0A            if (%22short-text%22.equals(request.getP
+String profile = request.getProfile();%0A            if (profile == null) %7B%0A                profile = %22%22;%0A            %7D%0A            if (!services.containsKey(p
 rofile
-(
 ))
-)
  %7B%0A 
@@ -1602,55 +1602,69 @@
     
-langs = shortService.detectAll(request.getText(
+services.put(profile, new LangdetectService(settings, profile
 ));%0A
@@ -1680,15 +1680,8 @@
    %7D
- else %7B
 %0A   
@@ -1689,19 +1689,30 @@
         
-   
+List%3CLanguage%3E
  langs =
@@ -1719,16 +1719,30 @@
  service
+s.get(profile)
 .detectA
@@ -1760,38 +1760,24 @@
 getText());%0A
-            %7D%0A
             

replace hardcoded langdetect services with a map