<commit_before>package de.cinovo.cloudconductor.server.handler;

import de.cinovo.cloudconductor.api.enums.ServiceState;
import de.cinovo.cloudconductor.api.model.ServiceStatesChanges;
import de.cinovo.cloudconductor.server.dao.IServiceStateDAO;
import de.cinovo.cloudconductor.server.model.EServiceState;
import de.cinovo.cloudconductor.server.websockets.model.WSChangeEvent;
import de.cinovo.cloudconductor.server.websockets.model.WSChangeEvent.ChangeType;
import de.cinovo.cloudconductor.server.ws.host.HostDetailWSHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Copyright 2018 Cinovo AG<br>
 * <br>
 *
 * @author psigloch
 */
@Service
public class ServiceStateHandler {

	@Autowired
	private IServiceStateDAO serviceStateDAO;
	@Autowired
	private HostDetailWSHandler hostDetailWsHandler;

	public EServiceState handleStartedService(EServiceState serviceState, ServiceStatesChanges stateChanges) {
		switch(serviceState.getState()) {
			case RESTARTING_STARTING:
			case STARTING:
				serviceState.nextState();
				EServiceState save = this.serviceStateDAO.save(serviceState);
				// service is now started, inform user interface via WS
				this.hostDetailWsHandler.broadcastChange(save.getHost().getName(), new WSChangeEvent<>(ChangeType.UPDATED, save.getHost().toApi()));
				return save;
			case STOPPING:
				stateChanges.getToStop().add(serviceState.getService().getInitScript());
				break;
			case RESTARTING_STOPPING:
				serviceState.nextState();
				stateChanges.getToRestart().add(serviceState.getService().getInitScript());
				return this.serviceStateDAO.save(serviceState);
			case STOPPED:
				serviceState.setState(ServiceState.STOPPING);
				stateChanges.getToStop().add(serviceState.getService().getInitScript());
				return this.serviceStateDAO.save(serviceState);
			default:
				break;
		}
		return serviceState;
	}

	public EServiceState handleStopedService(EServiceState serviceState, ServiceStatesChanges stateChanges) {
		switch(serviceState.getState()) {
			case STARTING:
				stateChanges.getToStart().add(serviceState.getService().getInitScript());
				break;
			case STOPPING:
				serviceState.nextState();
				EServiceState save = this.serviceStateDAO.save(serviceState);
				// service is now stopped, inform user interface via WS
				this.hostDetailWsHandler.broadcastChange(save.getHost().getName(), new WSChangeEvent<>(ChangeType.UPDATED, save.getHost().toApi()));
				return save;
			case STARTED:
				stateChanges.getToStart().add(serviceState.getService().getInitScript());
				serviceState.setState(ServiceState.STARTING);
				return this.serviceStateDAO.save(serviceState);
			default:
				break;
		}
		return serviceState;
	}

}<commit_msg>fixes a bug on restarting services not beeing started after stop<commit_after>package de.cinovo.cloudconductor.server.handler;

import de.cinovo.cloudconductor.api.enums.ServiceState;
import de.cinovo.cloudconductor.api.model.ServiceStatesChanges;
import de.cinovo.cloudconductor.server.dao.IServiceStateDAO;
import de.cinovo.cloudconductor.server.model.EServiceState;
import de.cinovo.cloudconductor.server.websockets.model.WSChangeEvent;
import de.cinovo.cloudconductor.server.websockets.model.WSChangeEvent.ChangeType;
import de.cinovo.cloudconductor.server.ws.host.HostDetailWSHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Copyright 2018 Cinovo AG<br>
 * <br>
 *
 * @author psigloch
 */
@Service
public class ServiceStateHandler {

	@Autowired
	private IServiceStateDAO serviceStateDAO;
	@Autowired
	private HostDetailWSHandler hostDetailWsHandler;

	/**
	 * @param serviceState the service state
	 * @param stateChanges the stateChanges
	 * @return the changed serice state
	 */
	public EServiceState handleStartedService(EServiceState serviceState, ServiceStatesChanges stateChanges) {
		switch(serviceState.getState()) {
			case RESTARTING_STARTING:
			case STARTING:
				serviceState.nextState();
				EServiceState save = this.serviceStateDAO.save(serviceState);
				// service is now started, inform user interface via WS
				this.hostDetailWsHandler.broadcastChange(save.getHost().getName(), new WSChangeEvent<>(ChangeType.UPDATED, save.getHost().toApi()));
				return save;
			case STOPPING:
				stateChanges.getToStop().add(serviceState.getService().getInitScript());
				break;
			case RESTARTING_STOPPING:
				serviceState.nextState();
				stateChanges.getToRestart().add(serviceState.getService().getInitScript());
				return this.serviceStateDAO.save(serviceState);
			case STOPPED:
				serviceState.setState(ServiceState.STOPPING);
				stateChanges.getToStop().add(serviceState.getService().getInitScript());
				return this.serviceStateDAO.save(serviceState);
			default:
				break;
		}
		return serviceState;
	}

	/**
	 * @param serviceState the service state
	 * @param stateChanges the stateChanges
	 * @return the changed serice state
	 */
	public EServiceState handleStopedService(EServiceState serviceState, ServiceStatesChanges stateChanges) {
		switch(serviceState.getState()) {
			case STARTING:
				stateChanges.getToStart().add(serviceState.getService().getInitScript());
				break;
			case RESTARTING_STARTING:
				stateChanges.getToStart().add(serviceState.getService().getInitScript());
				break;
			case RESTARTING_STOPPING:
				serviceState.nextState();
				stateChanges.getToStart().add(serviceState.getService().getInitScript());
				return this.serviceStateDAO.save(serviceState);
			case STOPPED:
			case STOPPING:
				serviceState.nextState();
				EServiceState save = this.serviceStateDAO.save(serviceState);
				// service is now stopped, inform user interface via WS
				this.hostDetailWsHandler.broadcastChange(save.getHost().getName(), new WSChangeEvent<>(ChangeType.UPDATED, save.getHost().toApi()));
				return save;
			case STARTED:
				stateChanges.getToStart().add(serviceState.getService().getInitScript());
				serviceState.setState(ServiceState.STARTING);
				return this.serviceStateDAO.save(serviceState);
			default:
				break;
		}
		return serviceState;
	}

}
{'fraction_non_alphanumeric': 0.10569897710667316, 'fraction_numerical': 0.001298912161065108, 'mean_word_length': 5.383419689119171, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '3875719', 'n_tokens_mistral': 2079, 'n_tokens_neox': 1792, 'n_words': 312}