All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.teradata.benchto.service.EnvironmentService Maven / Gradle / Ivy

There is a newer version: 0.4
Show newest version
/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.teradata.benchto.service;

import com.teradata.benchto.service.model.Environment;
import com.teradata.benchto.service.repo.EnvironmentRepo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.TransientDataAccessException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.teradata.benchto.service.utils.TimeUtils.currentDateTime;
import static java.util.Optional.ofNullable;

@Service
public class EnvironmentService
{
    private static final Logger LOG = LoggerFactory.getLogger(EnvironmentService.class);

    @Autowired
    private EnvironmentRepo environmentRepo;

    @Retryable(value = {TransientDataAccessException.class, DataIntegrityViolationException.class})
    @Transactional
    public void storeEnvironment(String name, Map attributes)
    {
        Optional environmentOptional = tryFindEnvironment(name);
        if (!environmentOptional.isPresent()) {
            Environment environment = new Environment();
            environment.setName(name);
            environment.setAttributes(attributes);
            environment.setStarted(currentDateTime());
            environmentOptional = Optional.of(environmentRepo.save(environment));
        } else {
            environmentOptional.get().setAttributes(attributes);
        }
        LOG.debug("Starting environment - {}", environmentOptional.get());
    }

    @Transactional(readOnly = true)
    public Environment findEnvironment(String name)
    {
        Optional environment = tryFindEnvironment(name);
        if (!environment.isPresent()) {
            throw new IllegalArgumentException("Could not find environment " + name);
        }
        return environment.get();
    }

    @Transactional(readOnly = true)
    public Optional tryFindEnvironment(String name)
    {
        return ofNullable(environmentRepo.findByName(name));
    }

    public List findEnvironments()
    {
        return environmentRepo.findAll();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy