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));
}
}
}
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);
}
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);
}
}
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);
}
}
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);
}
}
Aggregations