Search in sources :

Example 26 with Manufacturer

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);
}
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 27 with Manufacturer

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

Example 28 with Manufacturer

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

Example 29 with Manufacturer

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);
}
Also used : DeviceModel(org.opensmartgridplatform.domain.core.entities.DeviceModel) ArrayList(java.util.ArrayList) Manufacturer(org.opensmartgridplatform.domain.core.entities.Manufacturer) DeviceFirmwareFile(org.opensmartgridplatform.domain.core.entities.DeviceFirmwareFile) FirmwareFile(org.opensmartgridplatform.domain.core.entities.FirmwareFile)

Example 30 with Manufacturer

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);
    }
}
Also used : DeviceModel(org.opensmartgridplatform.domain.core.entities.DeviceModel) Organisation(org.opensmartgridplatform.domain.core.entities.Organisation) Device(org.opensmartgridplatform.domain.core.entities.Device) Manufacturer(org.opensmartgridplatform.domain.core.entities.Manufacturer) 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)

Aggregations

Manufacturer (org.opensmartgridplatform.domain.core.entities.Manufacturer)30 DeviceModel (org.opensmartgridplatform.domain.core.entities.DeviceModel)18 FunctionalException (org.opensmartgridplatform.shared.exceptionhandling.FunctionalException)13 Organisation (org.opensmartgridplatform.domain.core.entities.Organisation)10 Transactional (org.springframework.transaction.annotation.Transactional)10 DeviceFirmwareFile (org.opensmartgridplatform.domain.core.entities.DeviceFirmwareFile)7 FirmwareFile (org.opensmartgridplatform.domain.core.entities.FirmwareFile)6 ExistingEntityException (org.opensmartgridplatform.domain.core.exceptions.ExistingEntityException)6 Device (org.opensmartgridplatform.domain.core.entities.Device)5 Then (io.cucumber.java.en.Then)4 ArrayList (java.util.ArrayList)3 ConstraintViolationException (javax.validation.ConstraintViolationException)3 UnknownEntityException (org.opensmartgridplatform.domain.core.exceptions.UnknownEntityException)3 OsgpException (org.opensmartgridplatform.shared.exceptionhandling.OsgpException)3 PayloadRoot (org.springframework.ws.server.endpoint.annotation.PayloadRoot)3 ResponsePayload (org.springframework.ws.server.endpoint.annotation.ResponsePayload)3 ReadSettingsHelper.getString (org.opensmartgridplatform.cucumber.core.ReadSettingsHelper.getString)2 FirmwareModule (org.opensmartgridplatform.domain.core.entities.FirmwareModule)2 Given (io.cucumber.java.en.Given)1 Date (java.util.Date)1