Search in sources :

Example 16 with UnknownEntityException

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);
}
Also used : DeviceModel(org.opensmartgridplatform.domain.core.entities.DeviceModel) Organisation(org.opensmartgridplatform.domain.core.entities.Organisation) UnknownEntityException(org.opensmartgridplatform.domain.core.exceptions.UnknownEntityException) Manufacturer(org.opensmartgridplatform.domain.core.entities.Manufacturer) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException) FirmwareModule(org.opensmartgridplatform.domain.core.entities.FirmwareModule) DeviceFirmwareFile(org.opensmartgridplatform.domain.core.entities.DeviceFirmwareFile) FirmwareFile(org.opensmartgridplatform.domain.core.entities.FirmwareFile) Transactional(org.springframework.transaction.annotation.Transactional)

Example 17 with UnknownEntityException

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);
    }
}
Also used : MessageMetadata(org.opensmartgridplatform.shared.infra.jms.MessageMetadata) RequestMessage(org.opensmartgridplatform.shared.infra.jms.RequestMessage) UnknownEntityException(org.opensmartgridplatform.domain.core.exceptions.UnknownEntityException) EventNotificationDto(org.opensmartgridplatform.dto.valueobjects.EventNotificationDto) List(java.util.List) JMSException(javax.jms.JMSException)

Aggregations

UnknownEntityException (org.opensmartgridplatform.domain.core.exceptions.UnknownEntityException)17 FunctionalException (org.opensmartgridplatform.shared.exceptionhandling.FunctionalException)15 Transactional (org.springframework.transaction.annotation.Transactional)9 Organisation (org.opensmartgridplatform.domain.core.entities.Organisation)8 Device (org.opensmartgridplatform.domain.core.entities.Device)6 DeviceAuthorization (org.opensmartgridplatform.domain.core.entities.DeviceAuthorization)6 NotAuthorizedException (org.opensmartgridplatform.domain.core.exceptions.NotAuthorizedException)4 RequestMessage (org.opensmartgridplatform.shared.infra.jms.RequestMessage)4 PersistenceException (javax.persistence.PersistenceException)3 DeviceFirmwareFile (org.opensmartgridplatform.domain.core.entities.DeviceFirmwareFile)3 DeviceModel (org.opensmartgridplatform.domain.core.entities.DeviceModel)3 FirmwareFile (org.opensmartgridplatform.domain.core.entities.FirmwareFile)3 Manufacturer (org.opensmartgridplatform.domain.core.entities.Manufacturer)3 Ssld (org.opensmartgridplatform.domain.core.entities.Ssld)3 JpaSystemException (org.springframework.orm.jpa.JpaSystemException)3 JMSException (javax.jms.JMSException)2 OptimisticLockException (javax.persistence.OptimisticLockException)2 FirmwareModule (org.opensmartgridplatform.domain.core.entities.FirmwareModule)2 LightMeasurementDevice (org.opensmartgridplatform.domain.core.entities.LightMeasurementDevice)2 RtuDevice (org.opensmartgridplatform.domain.core.entities.RtuDevice)2