Java tutorial
/******************************************************************************* * (c) Copyright 2014 Hewlett-Packard Development Company, L.P. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Apache License v2.0 which accompany this distribution. * * The Apache License is available at * http://www.apache.org/licenses/LICENSE-2.0 * *******************************************************************************/ package io.cloudslang.orchestrator.services; import io.cloudslang.score.facade.execution.ExecutionActionException; import io.cloudslang.score.facade.execution.ExecutionActionResult; import io.cloudslang.score.facade.execution.ExecutionStatus; import io.cloudslang.score.facade.entities.Execution; import io.cloudslang.orchestrator.entities.ExecutionState; import io.cloudslang.orchestrator.repositories.ExecutionStateRepository; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.Validate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; import java.util.Arrays; import java.util.List; /** * User: * Date: 12/05/2014 */ public class ExecutionStateServiceImpl implements ExecutionStateService { @Autowired private ExecutionStateRepository executionStateRepository; @Autowired private ExecutionSerializationUtil executionSerializationUtil; @Override @Transactional(readOnly = true) public ExecutionState readByExecutionIdAndBranchId(Long executionId, String branchId) { validateExecutionId(executionId); validateBranchId(branchId); return executionStateRepository.findByExecutionIdAndBranchId(executionId, branchId); } @Override @Transactional(readOnly = true) public List<ExecutionState> readByExecutionId(Long executionId) { validateExecutionId(executionId); return executionStateRepository.findByExecutionId(executionId); } @Override @Transactional(readOnly = true) public List<Long> readExecutionIdByStatuses(List<ExecutionStatus> statuses) { return executionStateRepository.findExecutionIdByStatuses(statuses); } @Override @Transactional(readOnly = true) public ExecutionState readCancelledExecution(Long executionId) { validateExecutionId(executionId); return executionStateRepository.findByExecutionIdAndBranchIdAndStatusIn(executionId, ExecutionState.EMPTY_BRANCH, getCancelStatuses()); } @Override @Transactional public ExecutionState createParentExecution(Long executionId) { validateExecutionId(executionId); ExecutionState executionState = new ExecutionState(); executionState.setExecutionId(executionId); executionState.setBranchId(ExecutionState.EMPTY_BRANCH); executionState.setStatus(ExecutionStatus.RUNNING); return executionStateRepository.save(executionState); } @Override @Transactional public ExecutionState createExecutionState(Long executionId, String branchId) { validateExecutionId(executionId); validateBranchId(branchId); ExecutionState executionState = new ExecutionState(); executionState.setExecutionId(executionId); executionState.setBranchId(branchId); executionState.setStatus(ExecutionStatus.PENDING_PAUSE); return executionStateRepository.save(executionState); } @Override @Transactional(readOnly = true) public Execution readExecutionObject(Long executionId, String branchId) { validateExecutionId(executionId); validateBranchId(branchId); ExecutionState executionState = findByExecutionIdAndBranchId(executionId, branchId); if (executionState.getExecutionObject() != null) { return executionSerializationUtil.objFromBytes(executionState.getExecutionObject()); } else { return null; } } @Override @Transactional public void updateExecutionObject(Long executionId, String branchId, Execution execution) { validateExecutionId(executionId); validateBranchId(branchId); ExecutionState executionState = findByExecutionIdAndBranchId(executionId, branchId); executionState.setExecutionObject(executionSerializationUtil.objToBytes(execution)); } @Override @Transactional public void updateExecutionStateStatus(Long executionId, String branchId, ExecutionStatus status) { validateExecutionId(executionId); validateBranchId(branchId); Validate.notNull(status, "status cannot be null"); ExecutionState executionState = findByExecutionIdAndBranchId(executionId, branchId); executionState.setStatus(status); } private ExecutionState findByExecutionIdAndBranchId(Long executionId, String branchId) { ExecutionState executionState = executionStateRepository.findByExecutionIdAndBranchId(executionId, branchId); if (executionState == null) { throw new ExecutionActionException( "Could not find execution state. executionId: " + executionId + ", branchId: " + branchId, ExecutionActionResult.FAILED_NOT_FOUND); } return executionState; } private List<ExecutionStatus> getCancelStatuses() { return Arrays.asList(ExecutionStatus.CANCELED, ExecutionStatus.PENDING_CANCEL); } @Override public void deleteExecutionState(Long executionId, String branchId) { validateExecutionId(executionId); validateBranchId(branchId); ExecutionState executionState = executionStateRepository.findByExecutionIdAndBranchId(executionId, branchId); if (executionState != null) { executionStateRepository.delete(executionState); } } private void validateBranchId(String branchId) { Validate.notEmpty(StringUtils.trim(branchId), "branchId cannot be null or empty"); } private void validateExecutionId(Long executionId) { Validate.notNull(executionId, "executionId cannot be null or empty"); } }