use of org.opensmartgridplatform.domain.core.entities.Manufacturer in project open-smart-grid-platform by OSGP.
the class FirmwareManagementService method addFirmware.
/**
* Adds new {@link FirmwareFile} to the platform. Throws exception if {@link FirmwareFile} already
* exists
*/
@Transactional(value = "writableTransactionManager")
public void addFirmware(@Identification final String organisationIdentification, final FirmwareFileRequest firmwareFileRequest, final byte[] file, final String manufacturer, final String modelCode, final FirmwareModuleData firmwareModuleData) throws OsgpException {
final Organisation organisation = this.domainHelperService.findOrganisation(organisationIdentification);
this.domainHelperService.isAllowed(organisation, PlatformFunction.CREATE_FIRMWARE);
final Manufacturer databaseManufacturer = this.findManufacturerByCode(manufacturer);
final DeviceModel databaseDeviceModel = this.deviceModelRepository.findByManufacturerAndModelCode(databaseManufacturer, modelCode);
if (databaseDeviceModel == null) {
LOGGER.info("DeviceModel doesn't exist.");
throw new FunctionalException(FunctionalExceptionType.UNKNOWN_DEVICEMODEL, ComponentType.WS_CORE, new UnknownEntityException(DeviceModel.class, modelCode));
}
final Map<FirmwareModule, String> firmwareVersionsByModule = firmwareModuleData.getVersionsByModule(this.firmwareModuleRepository, false);
FirmwareFile savedFirmwareFile;
// file == null, user selected an existing firmware file
if (file == null) {
final List<FirmwareFile> databaseFirmwareFiles = this.firmwareFileRepository.findByDeviceModelAndFilename(databaseDeviceModel, firmwareFileRequest.getFileName());
if (databaseFirmwareFiles.isEmpty()) {
LOGGER.error("Firmware file doesn't exist.");
throw new FunctionalException(FunctionalExceptionType.UNKNOWN_FIRMWARE, ComponentType.WS_CORE, new UnknownEntityException(DeviceModel.class, firmwareFileRequest.getFileName()));
}
if (databaseDeviceModel.isFileStorage()) {
// The file is already in the directory, so nothing else has to
// happen
savedFirmwareFile = this.firmwareFileFrom(firmwareFileRequest);
} else {
// Storing the file in the database
savedFirmwareFile = this.createNewFirmwareFile(firmwareFileRequest, databaseFirmwareFiles.get(0).getFile());
}
} else if (databaseDeviceModel.isFileStorage()) {
// Saving the file to the file system
this.writeToFilesystem(file, firmwareFileRequest.getFileName(), databaseDeviceModel);
savedFirmwareFile = this.firmwareFileFrom(firmwareFileRequest);
} else {
// Storing the file in the database
savedFirmwareFile = this.createNewFirmwareFile(firmwareFileRequest, file);
}
if (firmwareFileRequest.isPushToNewDevices()) {
final List<FirmwareFile> firmwareFiles = this.firmwareFileRepository.findByDeviceModel(databaseDeviceModel);
this.setPushToNewDevicesToFalse(firmwareFiles);
}
/*
* Save the firmware file before adding the device model and updating
* the firmware module data. Trying to save a new firmware file with the
* related entities that were persisted earlier causes Hibernate
* exceptions referring to persistent entities in detached state.
*/
savedFirmwareFile = this.firmwareFileRepository.save(savedFirmwareFile);
savedFirmwareFile.addDeviceModel(databaseDeviceModel);
savedFirmwareFile.updateFirmwareModuleData(firmwareVersionsByModule);
this.firmwareFileRepository.save(savedFirmwareFile);
}
use of org.opensmartgridplatform.domain.core.entities.Manufacturer in project open-smart-grid-platform by OSGP.
the class FirmwareManagementService method removeManufacturer.
/**
* Removes a Manufacturer from the platform. Throws exception if {@link Manufacturer} doesn't
* exist
*/
@Transactional(value = "writableTransactionManager")
public void removeManufacturer(@Identification final String organisationIdentification, @Valid final String manufacturerCode) throws FunctionalException {
final Organisation organisation = this.domainHelperService.findOrganisation(organisationIdentification);
this.domainHelperService.isAllowed(organisation, PlatformFunction.REMOVE_MANUFACTURER);
final Manufacturer databaseManufacturer = this.manufacturerRepository.findByCode(manufacturerCode);
final List<DeviceModel> deviceModels = this.deviceModelRepository.findByManufacturer(databaseManufacturer);
if (!deviceModels.isEmpty()) {
LOGGER.info("Manufacturer is linked to a Model.");
throw new FunctionalException(FunctionalExceptionType.EXISTING_DEVICEMODEL_MANUFACTURER, ComponentType.WS_CORE, new ExistingEntityException(DeviceModel.class, deviceModels.get(0).getModelCode()));
}
if (databaseManufacturer == null) {
LOGGER.info("Manufacturer not found.");
throw new FunctionalException(FunctionalExceptionType.UNKNOWN_MANUFACTURER, ComponentType.WS_CORE, new ExistingEntityException(Manufacturer.class, manufacturerCode));
} else {
this.manufacturerRepository.delete(databaseManufacturer);
}
}
use of org.opensmartgridplatform.domain.core.entities.Manufacturer in project open-smart-grid-platform by OSGP.
the class FirmwareManagementService method addDeviceModel.
/**
* Adds new deviceModel to the platform. Throws exception if {@link DeviceModel} already exists
*/
@Transactional(value = "writableTransactionManager")
public void addDeviceModel(@Identification final String organisationIdentification, final String manufacturerCode, final String modelCode, final String description) throws FunctionalException {
final Organisation organisation = this.domainHelperService.findOrganisation(organisationIdentification);
this.domainHelperService.isAllowed(organisation, PlatformFunction.CREATE_DEVICE_MODEL);
final Manufacturer manufacturer = this.findManufacturerByCode(manufacturerCode);
final DeviceModel savedDeviceModel = this.deviceModelRepository.findByManufacturerAndModelCode(manufacturer, modelCode);
if (savedDeviceModel != null) {
LOGGER.info("DeviceModel already exists.");
throw new FunctionalException(FunctionalExceptionType.EXISTING_DEVICEMODEL, ComponentType.WS_CORE, new ExistingEntityException(DeviceModel.class, manufacturerCode));
} else {
final DeviceModel deviceModel = new DeviceModel(manufacturer, modelCode, description, this.firmwareFileStorage);
this.deviceModelRepository.save(deviceModel);
}
}
use of org.opensmartgridplatform.domain.core.entities.Manufacturer in project open-smart-grid-platform by OSGP.
the class FirmwareManagementService method getFirmwareFiles.
private List<FirmwareFile> getFirmwareFiles(final String manufacturerCode, final String modelCode, final Boolean active) {
if (StringUtils.isBlank(manufacturerCode) || StringUtils.isBlank(modelCode)) {
return new ArrayList<>();
}
final Manufacturer manufacturer = this.manufacturerRepository.findByCode(manufacturerCode);
final DeviceModel deviceModel = this.deviceModelRepository.findByManufacturerAndModelCode(manufacturer, modelCode);
if (deviceModel == null) {
return new ArrayList<>();
}
Specification<FirmwareFile> specification = where(FirmwareFileSpecifications.forDeviceModel(deviceModel));
if (specification == null) {
return new ArrayList<>();
}
specification = specification.and(FirmwareFileSpecifications.forActiveFirmwareFilesOnly(active));
if (specification == null) {
return new ArrayList<>();
}
return this.firmwareFileRepository.findAll(specification);
}
use of org.opensmartgridplatform.domain.core.entities.Manufacturer in project open-smart-grid-platform by OSGP.
the class FirmwareManagementService method removeDeviceModel.
/**
* Removes a DeviceModel from the platform. Throws exception if {@link DeviceModel} doesn't exist
*/
@Transactional(value = "writableTransactionManager")
public void removeDeviceModel(@Identification final String organisationIdentification, @Valid final String manufacturer, final String modelCode) throws FunctionalException {
final Organisation organisation = this.domainHelperService.findOrganisation(organisationIdentification);
this.domainHelperService.isAllowed(organisation, PlatformFunction.REMOVE_DEVICE_MODEL);
final Manufacturer databaseManufacturer = this.manufacturerRepository.findByCode(manufacturer);
final DeviceModel removedDeviceModel = this.deviceModelRepository.findByManufacturerAndModelCode(databaseManufacturer, modelCode);
if (removedDeviceModel == null) {
LOGGER.info("DeviceModel not found.");
throw new FunctionalException(FunctionalExceptionType.UNKNOWN_DEVICEMODEL, ComponentType.WS_CORE, new ExistingEntityException(Manufacturer.class, modelCode));
} else {
final List<Device> devices = this.deviceRepository.findByDeviceModel(removedDeviceModel);
if (!devices.isEmpty()) {
LOGGER.info("DeviceModel is linked to a device.");
throw new FunctionalException(FunctionalExceptionType.EXISTING_DEVICE_DEVICEMODEL, ComponentType.WS_CORE, new ExistingEntityException(Device.class, devices.get(0).getDeviceIdentification()));
}
final List<FirmwareFile> firmwareFiles = this.firmwareFileRepository.findByDeviceModel(removedDeviceModel);
if (!firmwareFiles.isEmpty()) {
LOGGER.info("DeviceModel is linked to a firmware file.");
throw new FunctionalException(FunctionalExceptionType.EXISTING_DEVICEMODEL_FIRMWARE, ComponentType.WS_CORE, new ExistingEntityException(FirmwareFile.class, firmwareFiles.get(0).getFilename()));
}
this.deviceModelRepository.delete(removedDeviceModel);
}
}
Aggregations