Search in sources :

Example 1 with ExistingEntityException

use of org.opensmartgridplatform.domain.core.exceptions.ExistingEntityException in project open-smart-grid-platform by OSGP.

the class DeviceManagementService method addOrganisation.

public void addOrganisation(@Identification final String organisationIdentification, @Valid @NotNull final Organisation newOrganisation) throws FunctionalException {
    LOGGER.debug("addOrganisation called with organisation {} and new organisation {}", organisationIdentification, newOrganisation.getOrganisationIdentification());
    final Organisation organisation = this.findOrganisation(organisationIdentification);
    this.isAllowed(organisation, PlatformFunction.CREATE_ORGANISATION);
    try {
        // Save the organisation.
        this.organisationRepository.save(newOrganisation);
    } catch (final JpaSystemException ex) {
        if (ex.getCause() instanceof PersistenceException) {
            LOGGER.error("Add organisation failure JpaSystemException", ex);
            throw new FunctionalException(FunctionalExceptionType.EXISTING_ORGANISATION, ComponentType.WS_ADMIN, new ExistingEntityException(Organisation.class, newOrganisation.getOrganisationIdentification(), ex));
        }
    }
}
Also used : JpaSystemException(org.springframework.orm.jpa.JpaSystemException) Organisation(org.opensmartgridplatform.domain.core.entities.Organisation) PersistenceException(javax.persistence.PersistenceException) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException) ExistingEntityException(org.opensmartgridplatform.domain.core.exceptions.ExistingEntityException)

Example 2 with ExistingEntityException

use of org.opensmartgridplatform.domain.core.exceptions.ExistingEntityException in project open-smart-grid-platform by OSGP.

the class FirmwareManagementService method removeFirmware.

/**
 * Removes a {@link FirmwareFile} from the platform. Throws exception if {@link FirmwareFile}
 * doesn't exist
 */
@Transactional(value = "writableTransactionManager")
public void removeFirmware(@Identification final String organisationIdentification, @Valid final int firmwareIdentification) throws OsgpException {
    final Organisation organisation = this.domainHelperService.findOrganisation(organisationIdentification);
    this.domainHelperService.isAllowed(organisation, PlatformFunction.REMOVE_FIRMWARE);
    final FirmwareFile removedFirmwareFile = this.firmwareFileRepository.findById((long) firmwareIdentification).orElseThrow(supplyFirmwareFileNotFoundException(firmwareIdentification));
    final List<DeviceFirmwareFile> deviceFirmwares = this.deviceFirmwareFileRepository.findByFirmwareFile(removedFirmwareFile);
    if (!deviceFirmwares.isEmpty()) {
        LOGGER.info("FirmwareFile is linked to device.");
        throw new FunctionalException(FunctionalExceptionType.EXISTING_FIRMWARE_DEVICEFIRMWARE, ComponentType.WS_CORE, new ExistingEntityException(DeviceFirmwareFile.class, deviceFirmwares.get(0).getFirmwareFile().getDescription()));
    }
    /*
     * A firmware file has been changed to be related to (possibly) multiple
     * device models to be usable across different value streams for all
     * kinds of devices.
     *
     * If this code gets used in a scenario where multiple device models are
     * actually related to the firmware file it may need to be updated to
     * deal with this.
     */
    final Set<DeviceModel> deviceModels = removedFirmwareFile.getDeviceModels();
    if (deviceModels.size() != 1) {
        LOGGER.warn("Remove Firmware assumes a single DeviceModel, FirmwareFile (id={}) has {}: {}", removedFirmwareFile.getId(), deviceModels.size(), deviceModels);
    }
    final DeviceModel deviceModel = deviceModels.iterator().next();
    // Only remove the file if no other firmware is using it.
    if (deviceModel.isFileStorage() && this.firmwareFileRepository.findByDeviceModelAndFilename(deviceModel, removedFirmwareFile.getFilename()).size() == 1) {
        this.removePhysicalFirmwareFile(this.createFirmwarePath(deviceModel, removedFirmwareFile.getFilename()));
    }
    this.firmwareFileRepository.delete(removedFirmwareFile);
}
Also used : DeviceModel(org.opensmartgridplatform.domain.core.entities.DeviceModel) DeviceFirmwareFile(org.opensmartgridplatform.domain.core.entities.DeviceFirmwareFile) Organisation(org.opensmartgridplatform.domain.core.entities.Organisation) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException) ExistingEntityException(org.opensmartgridplatform.domain.core.exceptions.ExistingEntityException) DeviceFirmwareFile(org.opensmartgridplatform.domain.core.entities.DeviceFirmwareFile) FirmwareFile(org.opensmartgridplatform.domain.core.entities.FirmwareFile) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with ExistingEntityException

use of org.opensmartgridplatform.domain.core.exceptions.ExistingEntityException in project open-smart-grid-platform by OSGP.

the class FirmwareManagementService method changeManufacturer.

/**
 * Updates a Manufacturer to the platform. Throws exception if {@link Manufacturer} doesn't exist.
 */
@Transactional(value = "writableTransactionManager")
public void changeManufacturer(@Identification final String organisationIdentification, @Valid final Manufacturer manufacturer) throws FunctionalException {
    final Organisation organisation = this.domainHelperService.findOrganisation(organisationIdentification);
    this.domainHelperService.isAllowed(organisation, PlatformFunction.CHANGE_MANUFACTURER);
    final Manufacturer databaseManufacturer = this.manufacturerRepository.findByCode(manufacturer.getCode());
    if (databaseManufacturer == null) {
        LOGGER.info("Manufacturer not found.");
        throw new FunctionalException(FunctionalExceptionType.UNKNOWN_MANUFACTURER, ComponentType.WS_CORE, new ExistingEntityException(Manufacturer.class, manufacturer.getCode()));
    } else {
        databaseManufacturer.setCode(manufacturer.getCode());
        databaseManufacturer.setName(manufacturer.getName());
        databaseManufacturer.setUsePrefix(manufacturer.isUsePrefix());
        this.manufacturerRepository.save(databaseManufacturer);
    }
}
Also used : Organisation(org.opensmartgridplatform.domain.core.entities.Organisation) Manufacturer(org.opensmartgridplatform.domain.core.entities.Manufacturer) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException) ExistingEntityException(org.opensmartgridplatform.domain.core.exceptions.ExistingEntityException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with ExistingEntityException

use of org.opensmartgridplatform.domain.core.exceptions.ExistingEntityException in project open-smart-grid-platform by OSGP.

the class FirmwareManagementService method addManufacturer.

/**
 * Adds new Manufacturer to the platform. Throws exception if {@link Manufacturer} already exists
 */
@Transactional(value = "writableTransactionManager")
public void addManufacturer(@Identification final String organisationIdentification, @Valid final Manufacturer manufacturer) throws FunctionalException {
    final Organisation organisation = this.domainHelperService.findOrganisation(organisationIdentification);
    this.domainHelperService.isAllowed(organisation, PlatformFunction.CREATE_MANUFACTURER);
    final Manufacturer databaseManufacturer = this.manufacturerRepository.findByCode(manufacturer.getCode());
    if (databaseManufacturer != null) {
        LOGGER.info("Manufacturer already exists.");
        throw new FunctionalException(FunctionalExceptionType.EXISTING_MANUFACTURER, ComponentType.WS_CORE, new ExistingEntityException(Manufacturer.class, manufacturer.getCode()));
    } else {
        this.manufacturerRepository.save(manufacturer);
    }
}
Also used : Organisation(org.opensmartgridplatform.domain.core.entities.Organisation) Manufacturer(org.opensmartgridplatform.domain.core.entities.Manufacturer) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException) ExistingEntityException(org.opensmartgridplatform.domain.core.exceptions.ExistingEntityException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with ExistingEntityException

use of org.opensmartgridplatform.domain.core.exceptions.ExistingEntityException in project open-smart-grid-platform by OSGP.

the class FirmwareManagementService method changeDeviceModel.

/**
 * Updates a DeviceModel to the platform. Throws exception if {@link DeviceModel} doesn't exist.
 */
@Transactional(value = "writableTransactionManager")
public void changeDeviceModel(@Identification final String organisationIdentification, final String manufacturer, final String modelCode, final String description) throws FunctionalException {
    final Organisation organisation = this.domainHelperService.findOrganisation(organisationIdentification);
    this.domainHelperService.isAllowed(organisation, PlatformFunction.CHANGE_DEVICE_MODEL);
    final Manufacturer databaseManufacturer = this.manufacturerRepository.findByCode(manufacturer);
    final DeviceModel changedDeviceModel = this.deviceModelRepository.findByManufacturerAndModelCode(databaseManufacturer, modelCode);
    if (changedDeviceModel == null) {
        LOGGER.info("DeviceModel not found.");
        throw new FunctionalException(FunctionalExceptionType.UNKNOWN_DEVICEMODEL, ComponentType.WS_CORE, new ExistingEntityException(Manufacturer.class, modelCode));
    } else {
        changedDeviceModel.setDescription(description);
        this.deviceModelRepository.save(changedDeviceModel);
    }
}
Also used : DeviceModel(org.opensmartgridplatform.domain.core.entities.DeviceModel) Organisation(org.opensmartgridplatform.domain.core.entities.Organisation) Manufacturer(org.opensmartgridplatform.domain.core.entities.Manufacturer) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException) ExistingEntityException(org.opensmartgridplatform.domain.core.exceptions.ExistingEntityException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Organisation (org.opensmartgridplatform.domain.core.entities.Organisation)10 ExistingEntityException (org.opensmartgridplatform.domain.core.exceptions.ExistingEntityException)10 FunctionalException (org.opensmartgridplatform.shared.exceptionhandling.FunctionalException)10 Transactional (org.springframework.transaction.annotation.Transactional)9 Manufacturer (org.opensmartgridplatform.domain.core.entities.Manufacturer)6 DeviceModel (org.opensmartgridplatform.domain.core.entities.DeviceModel)5 Device (org.opensmartgridplatform.domain.core.entities.Device)3 DeviceAuthorization (org.opensmartgridplatform.domain.core.entities.DeviceAuthorization)2 DeviceFirmwareFile (org.opensmartgridplatform.domain.core.entities.DeviceFirmwareFile)2 FirmwareFile (org.opensmartgridplatform.domain.core.entities.FirmwareFile)2 LightMeasurementDevice (org.opensmartgridplatform.domain.core.entities.LightMeasurementDevice)2 PersistenceException (javax.persistence.PersistenceException)1 Ssld (org.opensmartgridplatform.domain.core.entities.Ssld)1 JpaSystemException (org.springframework.orm.jpa.JpaSystemException)1