<commit_before>package org.safehaus.subutai.core.environment.ui.manage;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.safehaus.subutai.core.environment.api.helper.EnvironmentBuildProcess;
import org.safehaus.subutai.core.environment.api.helper.ProcessStatusEnum;
import org.safehaus.subutai.core.environment.ui.EnvironmentManagerPortalModule;
import org.safehaus.subutai.core.environment.ui.executor.BuildCommandFactory;
import org.safehaus.subutai.core.environment.ui.executor.BuildProcessExecutionEvent;
import org.safehaus.subutai.core.environment.ui.executor.BuildProcessExecutionEventType;
import org.safehaus.subutai.core.environment.ui.executor.BuildProcessExecutionListener;
import org.safehaus.subutai.core.environment.ui.executor.BuildProcessExecutor;
import org.safehaus.subutai.core.environment.ui.executor.BuildProcessExecutorImpl;
import org.safehaus.subutai.core.environment.ui.text.EnvAnswer;
import org.safehaus.subutai.core.environment.ui.window.EnvironmentBuildProcessDetails;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.server.ThemeResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;


@SuppressWarnings("serial")
public class EnvironmentsBuildProcessForm implements BuildProcessExecutionListener
{

    private static final String OK_ICON_SOURCE = "img/ok.png";
    private static final String ERROR_ICON_SOURCE = "img/cancel.png";
    private static final String LOAD_ICON_SOURCE = "img/spinner.gif";
    private static final String STATUS = "Status";
    private static final String ACTION = "Action";
    private Map<UUID, ExecutorService> executorServiceMap = new HashMap<>();
    private VerticalLayout contentRoot;
    private Table environmentsTable;
    private EnvironmentManagerPortalModule managerUI;


    public EnvironmentsBuildProcessForm( final EnvironmentManagerPortalModule managerUI )
    {
        this.managerUI = managerUI;

        contentRoot = new VerticalLayout();
        contentRoot.setSpacing( true );
        contentRoot.setMargin( true );

        environmentsTable = createTable( "Environments Build Process", 300 );

        Button getEnvironmentsButton = new Button( "View" );
        getEnvironmentsButton.addClickListener( new Button.ClickListener()
        {
            @Override
            public void buttonClick( final Button.ClickEvent clickEvent )
            {
                updateTableData();
            }
        } );
        contentRoot.addComponent( getEnvironmentsButton );
        contentRoot.addComponent( environmentsTable );
    }


    private Table createTable( String caption, int size )
    {
        Table table = new Table( caption );
        table.addContainerProperty( "Name", String.class, null );
        table.addContainerProperty( STATUS, Embedded.class, null );
        table.addContainerProperty( "Info", Button.class, null );
        table.addContainerProperty( ACTION, Button.class, null );
        table.addContainerProperty( "Destroy", Button.class, null );
        table.setPageLength( 10 );
        table.setSelectable( false );
        table.setEnabled( true );
        table.setImmediate( true );
        table.setSizeFull();
        return table;
    }


    private void updateTableData()
    {
        environmentsTable.removeAllItems();
        List<EnvironmentBuildProcess> environmentBuildProcessList =
                managerUI.getEnvironmentManager().getBuildProcesses();
        if ( !environmentBuildProcessList.isEmpty() )
        {
            for ( final EnvironmentBuildProcess environmentBuildProcess : environmentBuildProcessList )
            {
                Button viewEnvironmentInfoButton = new Button( "Info" );
                viewEnvironmentInfoButton.addClickListener( new Button.ClickListener()
                {
                    @Override
                    public void buttonClick( final Button.ClickEvent clickEvent )
                    {
                        EnvironmentBuildProcessDetails detailsWindow =
                                new EnvironmentBuildProcessDetails( "Environment details" );
                        Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
                        String json = gson.toJson( environmentBuildProcess, EnvironmentBuildProcess.class );
                        detailsWindow.setContent( json );
                        contentRoot.getUI().addWindow( detailsWindow );
                        detailsWindow.setVisible( true );
                    }
                } );

                Button processButton = null;
                Button destroyButton = null;
                Embedded icon = null;

                switch ( environmentBuildProcess.getProcessStatusEnum() )
                {
                    case NEW_PROCESS:
                    {
                        processButton = new Button( "Build" );
                        icon = new Embedded( "", new ThemeResource( OK_ICON_SOURCE ) );
                        processButton.addClickListener( new Button.ClickListener()
                        {
                            @Override
                            public void buttonClick( final Button.ClickEvent clickEvent )
                            {
                                startBuildProcess( environmentBuildProcess );
                            }
                        } );

                        destroyButton = new Button( "Destroy" );
                        destroyButton.addClickListener( new Button.ClickListener()
                        {
                            @Override
                            public void buttonClick( final Button.ClickEvent clickEvent )
                            {
                                destroyBuildProcess( environmentBuildProcess );
                            }
                        } );


                        break;
                    }
                    case IN_PROGRESS:
                    {
                        processButton = new Button( "Terminate" );
                        icon = new Embedded( "", new ThemeResource( LOAD_ICON_SOURCE ) );
                        processButton.addClickListener( new Button.ClickListener()
                        {
                            @Override
                            public void buttonClick( final Button.ClickEvent clickEvent )
                            {
                                terminateBuildProcess( environmentBuildProcess );
                            }
                        } );
                        break;
                    }
                    case FAILED:
                    case TERMINATED:
                    {
                        processButton = new Button( "Destroy" );
                        icon = new Embedded( "", new ThemeResource( ERROR_ICON_SOURCE ) );
                        processButton.addClickListener( new Button.ClickListener()
                        {
                            @Override
                            public void buttonClick( final Button.ClickEvent clickEvent )
                            {
                                destroyBuildProcess( environmentBuildProcess );
                            }
                        } );
                        break;
                    }
                    case SUCCESSFUL:
                    {
                        processButton = new Button( "Configure" );
                        icon = new Embedded( "", new ThemeResource( OK_ICON_SOURCE ) );
                        processButton.addClickListener( new Button.ClickListener()
                        {
                            @Override
                            public void buttonClick( final Button.ClickEvent clickEvent )
                            {
                                configureEnvironment( environmentBuildProcess );
                            }
                        } );
                        break;
                    }

                    default:
                    {
                        break;
                    }
                }
                environmentsTable.addItem( new Object[] {
                        environmentBuildProcess.getEnvironmentName(), icon, viewEnvironmentInfoButton, processButton,
                        destroyButton
                }, environmentBuildProcess.getUuid() );
            }
        }
        else
        {
            Notification.show( EnvAnswer.NO_BUILD_PROCESS.getAnswer() );
        }
        environmentsTable.refreshRowCache();
    }<commit_msg>Environment manager code refactor.<commit_after>package org.safehaus.subutai.core.environment.ui.manage;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.safehaus.subutai.core.environment.api.helper.EnvironmentBuildProcess;
import org.safehaus.subutai.core.environment.api.helper.ProcessStatusEnum;
import org.safehaus.subutai.core.environment.ui.EnvironmentManagerPortalModule;
import org.safehaus.subutai.core.environment.ui.executor.BuildCommandFactory;
import org.safehaus.subutai.core.environment.ui.executor.BuildProcessExecutionEvent;
import org.safehaus.subutai.core.environment.ui.executor.BuildProcessExecutionEventType;
import org.safehaus.subutai.core.environment.ui.executor.BuildProcessExecutionListener;
import org.safehaus.subutai.core.environment.ui.executor.BuildProcessExecutor;
import org.safehaus.subutai.core.environment.ui.executor.BuildProcessExecutorImpl;
import org.safehaus.subutai.core.environment.ui.text.EnvAnswer;
import org.safehaus.subutai.core.environment.ui.window.EnvironmentBuildProcessDetails;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.server.ThemeResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;


@SuppressWarnings( "serial" )
public class EnvironmentsBuildProcessForm implements BuildProcessExecutionListener
{

    private static final String OK_ICON_SOURCE = "img/ok.png";
    private static final String ERROR_ICON_SOURCE = "img/cancel.png";
    private static final String LOAD_ICON_SOURCE = "img/spinner.gif";
    private static final String STATUS = "Status";
    private static final String ACTION = "Action";
    private Map<UUID, ExecutorService> executorServiceMap = new HashMap<>();
    private VerticalLayout contentRoot;
    private Table environmentsTable;
    private EnvironmentManagerPortalModule managerUI;


    public EnvironmentsBuildProcessForm( final EnvironmentManagerPortalModule managerUI )
    {
        this.managerUI = managerUI;

        contentRoot = new VerticalLayout();
        contentRoot.setSpacing( true );
        contentRoot.setMargin( true );

        environmentsTable = createTable( "Environments Build Process", 300 );

        Button getEnvironmentsButton = new Button( "View" );
        getEnvironmentsButton.addClickListener( new Button.ClickListener()
        {
            @Override
            public void buttonClick( final Button.ClickEvent clickEvent )
            {
                updateTableData();
            }
        } );
        contentRoot.addComponent( getEnvironmentsButton );
        contentRoot.addComponent( environmentsTable );
    }


    private Table createTable( String caption, int size )
    {
        Table table = new Table( caption );
        table.addContainerProperty( "Name", String.class, null );
        table.addContainerProperty( STATUS, Embedded.class, null );
        table.addContainerProperty( "Info", Button.class, null );
        table.addContainerProperty( ACTION, Button.class, null );
        table.addContainerProperty( "Destroy", Button.class, null );
        table.setPageLength( 10 );
        table.setSelectable( false );
        table.setEnabled( true );
        table.setImmediate( true );
        table.setSizeFull();
        return table;
    }


    private void updateTableData()
    {
        environmentsTable.removeAllItems();
        List<EnvironmentBuildProcess> environmentBuildProcessList =
                managerUI.getEnvironmentManager().getBuildProcesses();
        if ( !environmentBuildProcessList.isEmpty() )
        {
            for ( final EnvironmentBuildProcess environmentBuildProcess : environmentBuildProcessList )
            {
                Button viewEnvironmentInfoButton = new Button( "Info" );
                viewEnvironmentInfoButton.addClickListener( new Button.ClickListener()
                {
                    @Override
                    public void buttonClick( final Button.ClickEvent clickEvent )
                    {
                        EnvironmentBuildProcessDetails detailsWindow =
                                new EnvironmentBuildProcessDetails( "Environment details" );
                        Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
                        String json = gson.toJson( environmentBuildProcess, EnvironmentBuildProcess.class );
                        detailsWindow.setContent( json );
                        contentRoot.getUI().addWindow( detailsWindow );
                        detailsWindow.setVisible( true );
                    }
                } );

                Button processButton = null;
                Button destroyButton = null;
                Embedded icon = null;

                switch ( environmentBuildProcess.getProcessStatusEnum() )
                {
                    case NEW_PROCESS:
                    {
                        processButton = new Button( "Build" );
                        icon = new Embedded( "", new ThemeResource( OK_ICON_SOURCE ) );
                        processButton.addClickListener( new Button.ClickListener()
                        {
                            @Override
                            public void buttonClick( final Button.ClickEvent clickEvent )
                            {
                                startBuildProcess( environmentBuildProcess );
                            }
                        } );

                        break;
                    }
                    case IN_PROGRESS:
                    {
                        processButton = new Button( "Terminate" );
                        icon = new Embedded( "", new ThemeResource( LOAD_ICON_SOURCE ) );
                        processButton.addClickListener( new Button.ClickListener()
                        {
                            @Override
                            public void buttonClick( final Button.ClickEvent clickEvent )
                            {
                                terminateBuildProcess( environmentBuildProcess );
                            }
                        } );
                        break;
                    }
                    case FAILED:
                        icon = new Embedded( "", new ThemeResource( ERROR_ICON_SOURCE ) );
                        break;
                    case TERMINATED:
                    {
                        icon = new Embedded( "", new ThemeResource( ERROR_ICON_SOURCE ) );
                        break;
                    }
                    case SUCCESSFUL:
                    {
                        icon = new Embedded( "", new ThemeResource( OK_ICON_SOURCE ) );
                        break;
                    }

                    default:
                    {
                        break;
                    }
                }

                destroyButton = new Button( "Destroy" );
                destroyButton.addClickListener( new Button.ClickListener()
                {
                    @Override
                    public void buttonClick( final Button.ClickEvent clickEvent )
                    {
                        destroyBuildProcess( environmentBuildProcess );
                    }
                } );
                environmentsTable.addItem( new Object[] {
                        environmentBuildProcess.getEnvironmentName(), icon, viewEnvironmentInfoButton, processButton,
                        destroyButton
                }, environmentBuildProcess.getUuid() );
            }
        }
        else
        {
            Notification.show( EnvAnswer.NO_BUILD_PROCESS.getAnswer() );
        }
        environmentsTable.refreshRowCache();
    }
{'fraction_non_alphanumeric': 0.07461538461538461, 'fraction_numerical': 0.0007100591715976331, 'mean_word_length': 1.664512060539177, 'pattern_counts': {'":': 0, '<': 9, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3530062', 'n_tokens_mistral': 3926, 'n_tokens_neox': 3693, 'n_words': 751}