<commit_before>package org.safehaus.subutai.core.command.ui.old;


import java.util.Set;
import java.util.concurrent.ExecutorService;

import org.safehaus.subutai.common.command.AgentResult;
import org.safehaus.subutai.common.command.Command;
import org.safehaus.subutai.common.command.CommandCallback;
import org.safehaus.subutai.common.command.CommandException;
import org.safehaus.subutai.common.command.RequestBuilder;
import org.safehaus.subutai.common.enums.RequestType;
import org.safehaus.subutai.common.enums.ResponseType;
import org.safehaus.subutai.common.protocol.Agent;
import org.safehaus.subutai.common.protocol.Response;
import org.safehaus.subutai.common.settings.Common;
import org.safehaus.subutai.common.util.NumUtil;
import org.safehaus.subutai.common.util.StringUtil;
import org.safehaus.subutai.core.agent.api.AgentManager;
import org.safehaus.subutai.core.dispatcher.api.CommandDispatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Strings;
import com.vaadin.ui.Button;


/**
 * Handles send button click, executes commands and displays responses
 */
public class SendButtonListener implements Button.ClickListener
{
    private static final Logger LOG = LoggerFactory.getLogger( SendButtonListener.class.getName() );

    private final TerminalForm form;
    private final AgentManager agentManager;
    private final CommandDispatcher commandDispatcher;
    private final ExecutorService executor;


    public SendButtonListener( final TerminalForm form, final AgentManager agentManager,
                               final CommandDispatcher commandDispatcher, final ExecutorService executor )
    {
        this.form = form;
        this.agentManager = agentManager;
        this.commandDispatcher = commandDispatcher;
        this.executor = executor;
    }


    @Override
    public void buttonClick( Button.ClickEvent event )
    {
        Set<Agent> agents = form.agentTree.getSelectedAgents();
        if ( agents.isEmpty() )
        {
            form.show( "Please, select nodes" );
        }
        else if ( form.programTxtFld.getValue() == null || Strings.isNullOrEmpty( form.programTxtFld.getValue() ) )
        {
            form.show( "Please, enter command" );
        }
        else
        {
            executeCommand( agents );
        }
    }


    private void executeCommand( Set<Agent> agents )
    {

        RequestBuilder requestBuilder = new RequestBuilder( form.programTxtFld.getValue() );

        if ( checkRequest( requestBuilder ) )
        {

            if ( form.requestTypeCombo.getValue() == RequestType.TERMINATE_REQUEST )
            {
                requestBuilder.withPid( Integer.valueOf( form.programTxtFld.getValue() ) );
                requestBuilder.withType( RequestType.TERMINATE_REQUEST );
            }
            else if ( form.requestTypeCombo.getValue() == RequestType.PS_REQUEST )
            {
                requestBuilder.withType( RequestType.PS_REQUEST );
            }

            if ( form.workDirTxtFld.getValue() != null && !Strings.isNullOrEmpty( form.workDirTxtFld.getValue() ) )
            {
                requestBuilder.withCwd( form.workDirTxtFld.getValue() );
            }

            int timeout = Integer.valueOf( form.timeoutTxtFld.getValue() );
            requestBuilder.withTimeout( timeout );

            form.indicator.setVisible( true );

            executor.execute(
                    new ExecuteCommandTask( commandDispatcher.createCommand( requestBuilder, agents ), agentManager,
                            form ) );
        }
    }


    private boolean checkRequest( RequestBuilder requestBuilder )
    {
        if ( form.requestTypeCombo.getValue() == RequestType.TERMINATE_REQUEST )
        {
            if ( !( StringUtil.isNumeric( form.programTxtFld.getValue() )
                    && Integer.valueOf( form.programTxtFld.getValue() ) > 0 ) )
            {
                form.show( "Please, enter numeric PID greater than 0 to kill" );
                return false;
            }
        }

        if ( !( StringUtil.isNumeric( form.timeoutTxtFld.getValue() ) && NumUtil
                .isNumBetween( Integer.valueOf( form.timeoutTxtFld.getValue() ), Common.MIN_COMMAND_TIMEOUT_SEC,
                        Common.MAX_COMMAND_TIMEOUT_SEC ) ) )
        {

            form.show( String.format( "Timeout must be between %d and %d", Common.MIN_COMMAND_TIMEOUT_SEC,
                    Common.MAX_COMMAND_TIMEOUT_SEC ) );
            return false;
        }


        return true;
    }


    private static class ExecuteCommandTask implements Runnable
    {
<commit_msg>sonar bugfix<commit_after>package org.safehaus.subutai.core.command.ui.old;


import java.util.Set;
import java.util.concurrent.ExecutorService;

import org.safehaus.subutai.common.command.AgentResult;
import org.safehaus.subutai.common.command.Command;
import org.safehaus.subutai.common.command.CommandCallback;
import org.safehaus.subutai.common.command.CommandException;
import org.safehaus.subutai.common.command.RequestBuilder;
import org.safehaus.subutai.common.enums.RequestType;
import org.safehaus.subutai.common.enums.ResponseType;
import org.safehaus.subutai.common.protocol.Agent;
import org.safehaus.subutai.common.protocol.Response;
import org.safehaus.subutai.common.settings.Common;
import org.safehaus.subutai.common.util.NumUtil;
import org.safehaus.subutai.common.util.StringUtil;
import org.safehaus.subutai.core.agent.api.AgentManager;
import org.safehaus.subutai.core.dispatcher.api.CommandDispatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Strings;
import com.vaadin.ui.Button;


/**
 * Handles send button click, executes commands and displays responses
 */
public class SendButtonListener implements Button.ClickListener
{
    private static final Logger LOG = LoggerFactory.getLogger( SendButtonListener.class.getName() );

    private final TerminalForm form;
    private final AgentManager agentManager;
    private final CommandDispatcher commandDispatcher;
    private final ExecutorService executor;


    public SendButtonListener( final TerminalForm form, final AgentManager agentManager,
                               final CommandDispatcher commandDispatcher, final ExecutorService executor )
    {
        this.form = form;
        this.agentManager = agentManager;
        this.commandDispatcher = commandDispatcher;
        this.executor = executor;
    }


    @Override
    public void buttonClick( Button.ClickEvent event )
    {
        Set<Agent> agents = form.agentTree.getSelectedAgents();
        if ( agents.isEmpty() )
        {
            form.show( "Please, select nodes" );
        }
        else if ( form.programTxtFld.getValue() == null || Strings.isNullOrEmpty( form.programTxtFld.getValue() ) )
        {
            form.show( "Please, enter command" );
        }
        else
        {
            executeCommand( agents );
        }
    }


    private void executeCommand( Set<Agent> agents )
    {

        RequestBuilder requestBuilder = new RequestBuilder( form.programTxtFld.getValue() );

        if ( checkRequest( requestBuilder ) )
        {

            if ( form.requestTypeCombo.getValue() == RequestType.TERMINATE_REQUEST )
            {
                requestBuilder.withPid( Integer.valueOf( form.programTxtFld.getValue() ) );
                requestBuilder.withType( RequestType.TERMINATE_REQUEST );
            }
            else if ( form.requestTypeCombo.getValue() == RequestType.PS_REQUEST )
            {
                requestBuilder.withType( RequestType.PS_REQUEST );
            }

            if ( form.workDirTxtFld.getValue() != null && !Strings.isNullOrEmpty( form.workDirTxtFld.getValue() ) )
            {
                requestBuilder.withCwd( form.workDirTxtFld.getValue() );
            }

            int timeout = Integer.valueOf( form.timeoutTxtFld.getValue() );
            requestBuilder.withTimeout( timeout );

            form.indicator.setVisible( true );

            executor.execute(
                    new ExecuteCommandTask( commandDispatcher.createCommand( requestBuilder, agents ), agentManager,
                            form ) );
        }
    }


    private boolean checkRequest( RequestBuilder requestBuilder )
    {
        if ( form.requestTypeCombo.getValue() == RequestType.TERMINATE_REQUEST && !(
                StringUtil.isNumeric( form.programTxtFld.getValue() )
                        && Integer.valueOf( form.programTxtFld.getValue() ) > 0 ) )
        {

            form.show( "Please, enter numeric PID greater than 0 to kill" );
            return false;
        }

        if ( !( StringUtil.isNumeric( form.timeoutTxtFld.getValue() ) && NumUtil
                .isNumBetween( Integer.valueOf( form.timeoutTxtFld.getValue() ), Common.MIN_COMMAND_TIMEOUT_SEC,
                        Common.MAX_COMMAND_TIMEOUT_SEC ) ) )
        {

            form.show( String.format( "Timeout must be between %d and %d", Common.MIN_COMMAND_TIMEOUT_SEC,
                    Common.MAX_COMMAND_TIMEOUT_SEC ) );
            return false;
        }


        return true;
    }


    private static class ExecuteCommandTask implements Runnable
    {

{'fraction_non_alphanumeric': 0.09339948231233822, 'fraction_numerical': 0.0008628127696289905, 'mean_word_length': 2.529881994670727, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7157011', 'n_tokens_mistral': 2582, 'n_tokens_neox': 2503, 'n_words': 470}