Search in sources :

Example 1 with NotValidUpdateException

use of com.adja.evchargerappserver.api.NotValidUpdateException in project iet-hf-2022-k-k-k-k-k-k by BME-MIT-IET.

the class AbstractService method post.

public ENTITY post(ENTITY entity) throws NotValidUpdateException {
    if (this.validateEntity(entity)) {
        entity.setId(null);
        Long id = this.repository.save(entity).getId();
        return this.getById(id);
    } else
        throw new NotValidUpdateException("");
}
Also used : NotValidUpdateException(com.adja.evchargerappserver.api.NotValidUpdateException)

Example 2 with NotValidUpdateException

use of com.adja.evchargerappserver.api.NotValidUpdateException in project iet-hf-2022-k-k-k-k-k-k by BME-MIT-IET.

the class ChargerService method carAttemptsCharging.

@Transactional(isolation = Isolation.REPEATABLE_READ, rollbackFor = NotValidUpdateException.class)
public boolean carAttemptsCharging(Long chargerId, Long carId) throws NotValidUpdateException {
    Optional<Charger> chargerById = this.repository.findById(chargerId);
    if (chargerById.isEmpty()) {
        throw new NotValidUpdateException("");
    } else {
        Charger charger = chargerById.get();
        if (charger.getCurrentlyChargingCar() != null) {
            return false;
        } else {
            Optional<ElectricCar> carOptional = this.electricCarRepository.findById(carId);
            if (carOptional.isEmpty())
                throw new NotValidUpdateException("");
            ElectricCar car = carOptional.get();
            if (!car.getCarType().getCompatibleChargerTypes().contains(charger.getChargerType())) {
                return false;
            }
            car.setCharger(charger);
            this.electricCarRepository.save(car);
            charger.setCurrentlyChargingCar(car);
            this.repository.save(charger);
            return true;
        }
    }
}
Also used : ElectricCar(com.adja.evchargerappserver.api.electriccar.ElectricCar) NotValidUpdateException(com.adja.evchargerappserver.api.NotValidUpdateException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with NotValidUpdateException

use of com.adja.evchargerappserver.api.NotValidUpdateException in project iet-hf-2022-k-k-k-k-k-k by BME-MIT-IET.

the class ElectricCarService method endCharging.

@Transactional(isolation = Isolation.REPEATABLE_READ, rollbackFor = NotValidUpdateException.class)
public boolean endCharging(Long carId) throws NotValidUpdateException {
    Optional<ElectricCar> carById = this.repository.findById(carId);
    if (carById.isEmpty()) {
        throw new NotValidUpdateException("");
    }
    ElectricCar car = carById.get();
    if (car.getCharger() != null) {
        Charger charger = this.chargerService.getById(car.getCharger().getId());
        charger.setCurrentlyChargingCar(null);
        car.setCharger(null);
        this.chargerService.put(charger.getId(), charger);
        this.put(carId, car);
        mockElectricCarHandler.endCharging(car);
        this.notificationService.chargingEnded(charger.getId());
    }
    return true;
}
Also used : NotValidUpdateException(com.adja.evchargerappserver.api.NotValidUpdateException) Charger(com.adja.evchargerappserver.api.charger.Charger) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with NotValidUpdateException

use of com.adja.evchargerappserver.api.NotValidUpdateException in project iet-hf-2022-k-k-k-k-k-k by BME-MIT-IET.

the class PersonService method setCarToPerson.

@Transactional(isolation = Isolation.REPEATABLE_READ, rollbackFor = NotValidUpdateException.class)
public void setCarToPerson(Long id, Long carId) throws NotValidUpdateException {
    try {
        Person person = this.getById(id);
        ElectricCar car = this.electricCarService.getById(carId);
        if (car.getDriver() != null)
            throw new NotValidUpdateException("");
        if (person.getCar() != null) {
            person.getCar().setDriver(null);
        }
        car.setDriver(person);
        person.setCar(car);
        this.put(id, person);
    } catch (EntityNotFoundException e) {
        throw new NotValidUpdateException("");
    }
}
Also used : ElectricCar(com.adja.evchargerappserver.api.electriccar.ElectricCar) NotValidUpdateException(com.adja.evchargerappserver.api.NotValidUpdateException) EntityNotFoundException(javax.persistence.EntityNotFoundException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

NotValidUpdateException (com.adja.evchargerappserver.api.NotValidUpdateException)4 Transactional (org.springframework.transaction.annotation.Transactional)3 ElectricCar (com.adja.evchargerappserver.api.electriccar.ElectricCar)2 Charger (com.adja.evchargerappserver.api.charger.Charger)1 EntityNotFoundException (javax.persistence.EntityNotFoundException)1