package com.sap.cloud.lm.sl.cf.process.steps;

import org.cloudfoundry.client.lib.CloudControllerClient;
import org.cloudfoundry.client.lib.CloudControllerException;
import org.cloudfoundry.client.lib.CloudOperationException;
import org.cloudfoundry.client.lib.domain.CloudApplication;
import org.cloudfoundry.client.lib.domain.CloudApplication.AppState;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.sap.cloud.lm.sl.cf.core.model.HookPhase;
import com.sap.cloud.lm.sl.cf.process.message.Messages;
import com.sap.cloud.lm.sl.common.SLException;

@Component("stopAppStep")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class StopAppStep extends SyncFlowableStepWithHooks {

    private static final String STOP_APP_ON_COMPLETE_HOOK_MESSAGE = "stopAppHookMessage";

    @Override
    protected StepPhase executeStepInternal(ExecutionWrapper execution) {
        // Get the next cloud application from the context
        CloudApplication app = StepsUtil.getApp(execution.getContext());

        // Get the existing application from the context
        CloudApplication existingApp = StepsUtil.getExistingApp(execution.getContext());

        try {
            if (existingApp != null && !existingApp.getState()
                .equals(AppState.STOPPED)) {
                getStepLogger().info(Messages.STOPPING_APP, app.getName());

                // Get a cloud foundry client
                CloudControllerClient client = execution.getControllerClient();

                // Stop the application
                client.stopApplication(app.getName());

                getStepLogger().debug(Messages.APP_STOPPED, app.getName());
            } else {
                getStepLogger().debug("Application \"{0}\" already stopped", app.getName());
            }

            return StepPhase.DONE;
        } catch (CloudOperationException coe) {
            CloudControllerException e = new CloudControllerException(coe);
            getStepLogger().error(e, Messages.ERROR_STOPPING_APP, app.getName());
            throw e;
        } catch (SLException e) {
            getStepLogger().error(e, Messages.ERROR_STOPPING_APP, app.getName());
            throw e;
        }
    }

    @Override
    protected HookPhase getHookPhaseBeforeStep() {
        return HookPhase.APPLICATION_BEFORE_STOP;
    }

    @Override
    protected String getOnCompleteHookMessageName() {
        return STOP_APP_ON_COMPLETE_HOOK_MESSAGE;
    }

}

@@ -2475,32 +2475,157 @@
 STOP;%0D%0A    %7D%0D%0A%0D%0A
+    @Override%0D%0A    protected HookPhase getHookPhaseAfterStep() %7B%0D%0A        return HookPhase.APPLICATION_AFTER_STOP;%0D%0A    %7D%0D%0A%0D%0A
     @Override%0D%0A 

Introduce application.after-stop hook phase