@Override
@Transactional
public void persistActions(Request request) {
RequestEntity requestEntity = request.constructNewPersistenceEntity();
Long clusterId = Long.valueOf(-1L);
ClusterEntity clusterEntity = clusterDAO.findById(request.getClusterId());
if (clusterEntity != null) {
clusterId = clusterEntity.getClusterId();
}
requestEntity.setClusterId(clusterId);
requestDAO.create(requestEntity);
//TODO wire request to cluster
List<StageEntity> stageEntities = new ArrayList<StageEntity>(request.getStages().size());
for (Stage stage : request.getStages()) {
StageEntity stageEntity = stage.constructNewPersistenceEntity();
stageEntities.add(stageEntity);
stageEntity.setClusterId(clusterId);
//TODO refactor to reduce merges
stageEntity.setRequest(requestEntity);
stageDAO.create(stageEntity);
List<HostRoleCommand> orderedHostRoleCommands = stage.getOrderedHostRoleCommands();
List<HostRoleCommandEntity> hostRoleCommandEntities = new ArrayList<HostRoleCommandEntity>();
for (HostRoleCommand hostRoleCommand : orderedHostRoleCommands) {
HostRoleCommandEntity hostRoleCommandEntity = hostRoleCommand.constructNewPersistenceEntity();
hostRoleCommandEntities.add(hostRoleCommandEntity);
hostRoleCommandEntity.setStage(stageEntity);
HostEntity hostEntity = hostDAO.findByName(hostRoleCommandEntity.getHostName());
if (hostEntity == null) {
LOG.error("Host {} doesn't exists in database" + hostRoleCommandEntity.getHostName());
throw new RuntimeException("Host '" + hostRoleCommandEntity.getHostName() + "' doesn't exists in database");
}
hostRoleCommandEntity.setHost(hostEntity);
hostRoleCommandDAO.create(hostRoleCommandEntity);
assert hostRoleCommandEntity.getTaskId() != null;
hostRoleCommand.setTaskId(hostRoleCommandEntity.getTaskId());
ExecutionCommandEntity executionCommandEntity = hostRoleCommand.constructExecutionCommandEntity();
executionCommandEntity.setHostRoleCommand(hostRoleCommandEntity);
executionCommandEntity.setTaskId(hostRoleCommandEntity.getTaskId());
hostRoleCommandEntity.setExecutionCommand(executionCommandEntity);
executionCommandDAO.create(hostRoleCommandEntity.getExecutionCommand());
hostRoleCommandDAO.merge(hostRoleCommandEntity);
hostDAO.merge(hostEntity);
}
for (RoleSuccessCriteriaEntity roleSuccessCriteriaEntity : stageEntity.getRoleSuccessCriterias()) {
roleSuccessCriteriaDAO.create(roleSuccessCriteriaEntity);
}
stageDAO.create(stageEntity);
}
requestEntity.setStages(stageEntities);
requestDAO.merge(requestEntity);
}