use of org.opensmartgridplatform.domain.core.exceptions.UnknownEntityException 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.exceptions.UnknownEntityException in project open-smart-grid-platform by OSGP.
the class EventNotificationMessageProcessor method processMessage.
@Override
public void processMessage(final ObjectMessage message) throws JMSException {
final MessageMetadata metadata = MessageMetadata.fromMessage(message);
LOGGER.info("Received message of messageType: {} organisationIdentification: {} deviceIdentification: {}", this.messageType, metadata.getOrganisationIdentification(), metadata.getDeviceIdentification());
final RequestMessage requestMessage = (RequestMessage) message.getObject();
final Object dataObject = requestMessage.getRequest();
try {
if (dataObject instanceof EventNotificationDto) {
final EventNotificationDto eventNotification = (EventNotificationDto) dataObject;
this.eventNotificationMessageService.handleEvent(metadata.getDeviceIdentification(), eventNotification);
} else if (dataObject instanceof List) {
final List<EventNotificationDto> eventNotificationDtoList = (List<EventNotificationDto>) dataObject;
this.eventNotificationMessageService.handleEvents(metadata.getDeviceIdentification(), eventNotificationDtoList);
}
} catch (final UnknownEntityException e) {
final String errorMessage = String.format("%s occurred, reason: %s", e.getClass().getName(), e.getMessage());
LOGGER.error(errorMessage, e);
throw new JMSException(errorMessage);
}
}
Aggregations