<commit_before>package org.safehaus.kiskis.mgmt.server.ui.modules.hive.action.chain;

import org.safehaus.kiskis.mgmt.server.ui.modules.hive.action.BasicListener;
import org.safehaus.kiskis.mgmt.server.ui.modules.hive.common.chain.Chain;
import org.safehaus.kiskis.mgmt.server.ui.modules.hive.common.chain.Context;
import org.safehaus.kiskis.mgmt.server.ui.modules.hive.common.command.ActionListener;
import org.safehaus.kiskis.mgmt.server.ui.modules.hive.common.command.CommandAction;
import org.safehaus.kiskis.mgmt.server.ui.modules.hive.view.UILogger;
import org.safehaus.kiskis.mgmt.shared.protocol.Response;

public class RemoveChainBuilder extends AbstractChainBuilder {

    private static final String HIVE_REMOVE_COMMAND = "apt-get --force-yes --assume-yes --purge remove ksks-hive";
    private static final String DERBY_REMOVE_COMMAND = "apt-get --force-yes --assume-yes --purge remove ksks-derby";

    private static final String HIVE_NOT_INSTALLED = "hiveNotInstalled";

    public RemoveChainBuilder(UILogger logger) {
        super(logger);
    }

    public Chain getChain() {
        return new Chain(agentInitAction,
                new CommandAction(STATUS_COMMAND, getStatusListener()),
                new CommandAction(HIVE_REMOVE_COMMAND, getHiveRemoveListener()),
                new CommandAction(DERBY_REMOVE_COMMAND, getDerbyRemoveListener())
        );
    }

    public ActionListener getStatusListener() {
        return new BasicListener(logger, "Checking status before removing, please wait...") {
            @Override
            protected boolean onComplete(Context context, String stdOut, String stdErr, Response response) {
                context.put(HIVE_NOT_INSTALLED, !stdOut.contains("ksks-hive"));
                return true;
            }
        };
    }

    public ActionListener getHiveRemoveListener() {
        return new BasicListener(logger, "Removing Hive, please wait...") {

            @Override
            protected Result onStart(Context context, String programLine) {

                String msg = executeMessage;<commit_msg>Fixed SUBUTAI-507 - Hive: remove doesn't work if its services are running has been successfully created<commit_after>package org.safehaus.kiskis.mgmt.server.ui.modules.hive.action.chain;

import org.safehaus.kiskis.mgmt.server.ui.modules.hive.action.BasicListener;
import org.safehaus.kiskis.mgmt.server.ui.modules.hive.common.chain.Chain;
import org.safehaus.kiskis.mgmt.server.ui.modules.hive.common.chain.Context;
import org.safehaus.kiskis.mgmt.server.ui.modules.hive.common.command.ActionListener;
import org.safehaus.kiskis.mgmt.server.ui.modules.hive.common.command.CommandAction;
import org.safehaus.kiskis.mgmt.server.ui.modules.hive.view.UILogger;
import org.safehaus.kiskis.mgmt.shared.protocol.Response;

public class RemoveChainBuilder extends AbstractChainBuilder {

    private static final String STOP_SERVICES = "service derby stop; . /etc/profile && service hive-thrift stop";

    private static final String HIVE_REMOVE_COMMAND = "apt-get --force-yes --assume-yes --purge remove ksks-hive";
    private static final String DERBY_REMOVE_COMMAND = "apt-get --force-yes --assume-yes --purge remove ksks-derby";

    private static final String HIVE_NOT_INSTALLED = "hiveNotInstalled";

    public RemoveChainBuilder(UILogger logger) {
        super(logger);
    }

    public Chain getChain() {
        return new Chain(agentInitAction,
                new CommandAction(STATUS_COMMAND, getStatusListener() ),
                new CommandAction(STOP_SERVICES, getServicesStopListener() ),
                new CommandAction(HIVE_REMOVE_COMMAND, getHiveRemoveListener() ),
                new CommandAction(DERBY_REMOVE_COMMAND, getDerbyRemoveListener() )
        );
    }

    public ActionListener getStatusListener() {
        return new BasicListener(logger, "Checking status before removing, please wait...") {
            @Override
            protected boolean onComplete(Context context, String stdOut, String stdErr, Response response) {
                context.put(HIVE_NOT_INSTALLED, !stdOut.contains("ksks-hive"));
                return true;
            }
        };
    }

    public ActionListener getServicesStopListener() {
        return new BasicListener(logger, "Stopping the services: derby, hive-thrift...") {

            @Override
            protected boolean onComplete(Context context, String stdOut, String stdErr, Response response) {

                if (response.getExitCode() != null && response.getExitCode() != 0) {
                    logger.complete("Error occurred while stopping services. Please see the server logs for details.");
                    return false;
                }

                logger.info("Services stopped successfully");

                return true;
            }
        };
    }

    public ActionListener getHiveRemoveListener() {
        return new BasicListener(logger, "Removing Hive, please wait...") {

            @Override
            protected Result onStart(Context context, String programLine) {

                String msg = executeMessage;
{'fraction_non_alphanumeric': 0.0958609918000781, 'fraction_numerical': 0.0007809449433814916, 'mean_word_length': 3.0054730258014075, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6352363', 'n_tokens_mistral': 1460, 'n_tokens_neox': 1417, 'n_words': 304}