Search in sources :

Example 11 with DeviceAuthorization

use of org.opensmartgridplatform.domain.core.entities.DeviceAuthorization in project open-smart-grid-platform by OSGP.

the class DeviceInstallationService method addLightMeasurementDevice.

@Transactional(value = "writableTransactionManager")
public void addLightMeasurementDevice(@Identification final String organisationIdentification, @Valid final LightMeasurementDevice newLightMeasurementDevice, @Identification final String ownerOrganisationIdentification) throws FunctionalException {
    final Organisation organisation = this.domainHelperService.findOrganisation(organisationIdentification);
    this.domainHelperService.isAllowed(organisation, PlatformFunction.GET_ORGANISATIONS);
    this.domainHelperService.isOrganisationEnabled(organisation);
    // If the device already exists, throw an exception.
    final Device existingDevice = this.writableDeviceRepository.findByDeviceIdentification(newLightMeasurementDevice.getDeviceIdentification());
    // field
    if (existingDevice != null && StringUtils.isNotBlank(existingDevice.getDeviceType())) {
        throw new FunctionalException(FunctionalExceptionType.EXISTING_DEVICE, ComponentType.WS_CORE, new ExistingEntityException(Device.class, newLightMeasurementDevice.getDeviceIdentification()));
    }
    LightMeasurementDevice lmd;
    if (existingDevice != null) {
        // Update existing light measurement device
        lmd = this.writableLightMeasurementDeviceRepository.findByDeviceIdentification(newLightMeasurementDevice.getDeviceIdentification());
        lmd.updateMetaData(newLightMeasurementDevice.getAlias(), newLightMeasurementDevice.getContainerAddress(), newLightMeasurementDevice.getGpsCoordinates());
        lmd.getAuthorizations().clear();
    } else {
        // Create a new LMD instance.
        lmd = new LightMeasurementDevice(newLightMeasurementDevice.getDeviceIdentification(), newLightMeasurementDevice.getAlias(), newLightMeasurementDevice.getContainerAddress(), newLightMeasurementDevice.getGpsCoordinates(), null);
    }
    lmd.setDeviceModel(newLightMeasurementDevice.getDeviceModel());
    lmd.setDescription(newLightMeasurementDevice.getDescription());
    lmd.setCode(newLightMeasurementDevice.getCode());
    lmd.setColor(newLightMeasurementDevice.getColor());
    lmd.setDigitalInput(newLightMeasurementDevice.getDigitalInput());
    final Organisation ownerOrganisation = this.domainHelperService.findOrganisation(ownerOrganisationIdentification);
    lmd = this.writableLightMeasurementDeviceRepository.save(lmd);
    final DeviceAuthorization authorization = lmd.addAuthorization(ownerOrganisation, DeviceFunctionGroup.OWNER);
    this.writableAuthorizationRepository.save(authorization);
    LOGGER.info("Created new light measurement device {} with owner {}", newLightMeasurementDevice.getDeviceIdentification(), ownerOrganisationIdentification);
}
Also used : Organisation(org.opensmartgridplatform.domain.core.entities.Organisation) LightMeasurementDevice(org.opensmartgridplatform.domain.core.entities.LightMeasurementDevice) LightMeasurementDevice(org.opensmartgridplatform.domain.core.entities.LightMeasurementDevice) Device(org.opensmartgridplatform.domain.core.entities.Device) DeviceAuthorization(org.opensmartgridplatform.domain.core.entities.DeviceAuthorization) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException) ExistingEntityException(org.opensmartgridplatform.domain.core.exceptions.ExistingEntityException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 12 with DeviceAuthorization

use of org.opensmartgridplatform.domain.core.entities.DeviceAuthorization in project open-smart-grid-platform by OSGP.

the class DeviceInstallationService method updateLightMeasurementDevice.

@Transactional(value = "writableTransactionManager")
public void updateLightMeasurementDevice(@Identification final String organisationIdentification, @Valid final LightMeasurementDevice updateLightMeasurementDevice) throws FunctionalException {
    final LightMeasurementDevice existingLmd = this.writableLightMeasurementDeviceRepository.findByDeviceIdentification(updateLightMeasurementDevice.getDeviceIdentification());
    if (existingLmd == null) {
        // device does not exist
        LOGGER.info("Device does not exist, nothing to update.");
        throw new FunctionalException(FunctionalExceptionType.UNKNOWN_DEVICE, ComponentType.WS_CORE, new UnknownEntityException(Device.class, updateLightMeasurementDevice.getDeviceIdentification()));
    }
    final List<DeviceAuthorization> owners = this.writableAuthorizationRepository.findByDeviceAndFunctionGroup(existingLmd, DeviceFunctionGroup.OWNER);
    // Check organisation against owner of device
    boolean isOwner = false;
    for (final DeviceAuthorization owner : owners) {
        if (owner.getOrganisation().getOrganisationIdentification().equalsIgnoreCase(organisationIdentification)) {
            isOwner = true;
        }
    }
    if (!isOwner) {
        LOGGER.info("Device has no owner yet, or organisation is not the owner.");
        throw new FunctionalException(FunctionalExceptionType.UNAUTHORIZED, ComponentType.WS_CORE, new NotAuthorizedException(organisationIdentification));
    }
    // Update LMD
    existingLmd.updateMetaData(updateLightMeasurementDevice.getAlias(), updateLightMeasurementDevice.getContainerAddress(), updateLightMeasurementDevice.getGpsCoordinates());
    existingLmd.setDeviceModel(updateLightMeasurementDevice.getDeviceModel());
    existingLmd.setDescription(updateLightMeasurementDevice.getDescription());
    existingLmd.setCode(updateLightMeasurementDevice.getCode());
    existingLmd.setColor(updateLightMeasurementDevice.getColor());
    existingLmd.setDigitalInput(updateLightMeasurementDevice.getDigitalInput());
    this.writableLightMeasurementDeviceRepository.save(existingLmd);
}
Also used : LightMeasurementDevice(org.opensmartgridplatform.domain.core.entities.LightMeasurementDevice) LightMeasurementDevice(org.opensmartgridplatform.domain.core.entities.LightMeasurementDevice) Device(org.opensmartgridplatform.domain.core.entities.Device) UnknownEntityException(org.opensmartgridplatform.domain.core.exceptions.UnknownEntityException) DeviceAuthorization(org.opensmartgridplatform.domain.core.entities.DeviceAuthorization) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException) NotAuthorizedException(org.opensmartgridplatform.domain.core.exceptions.NotAuthorizedException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 13 with DeviceAuthorization

use of org.opensmartgridplatform.domain.core.entities.DeviceAuthorization in project open-smart-grid-platform by OSGP.

the class RtuDeviceService method storeAuthorization.

private void storeAuthorization(final String organisationIdentification, final org.opensmartgridplatform.domain.core.entities.RtuDevice rtuDevice) {
    final Organisation organisation = this.organisationRepository.findByOrganisationIdentification(organisationIdentification);
    final DeviceAuthorization authorization = rtuDevice.addAuthorization(organisation, DeviceFunctionGroup.OWNER);
    this.deviceAuthorizationRepository.save(authorization);
}
Also used : Organisation(org.opensmartgridplatform.domain.core.entities.Organisation) DeviceAuthorization(org.opensmartgridplatform.domain.core.entities.DeviceAuthorization)

Example 14 with DeviceAuthorization

use of org.opensmartgridplatform.domain.core.entities.DeviceAuthorization in project open-smart-grid-platform by OSGP.

the class AbstractPlatformDeviceCreator method addDeviceAuthorization.

private DeviceAuthorization addDeviceAuthorization(final Device device, final Organisation organization) {
    DeviceAuthorization deviceAuthorization = device.addAuthorization(organization, DeviceFunctionGroup.OWNER);
    deviceAuthorization = this.deviceAuthorizationRepository.save(deviceAuthorization);
    return deviceAuthorization;
}
Also used : DeviceAuthorization(org.opensmartgridplatform.domain.core.entities.DeviceAuthorization)

Example 15 with DeviceAuthorization

use of org.opensmartgridplatform.domain.core.entities.DeviceAuthorization in project open-smart-grid-platform by OSGP.

the class SmartMeterService method storeAuthorization.

private void storeAuthorization(final String organisationIdentification, final SmartMeter smartMeter) {
    final Organisation organisation = this.organisationRepository.findByOrganisationIdentification(organisationIdentification);
    final DeviceAuthorization authorization = smartMeter.addAuthorization(organisation, DeviceFunctionGroup.OWNER);
    this.deviceAuthorizationRepository.save(authorization);
}
Also used : Organisation(org.opensmartgridplatform.domain.core.entities.Organisation) DeviceAuthorization(org.opensmartgridplatform.domain.core.entities.DeviceAuthorization)

Aggregations

DeviceAuthorization (org.opensmartgridplatform.domain.core.entities.DeviceAuthorization)25 Device (org.opensmartgridplatform.domain.core.entities.Device)17 Organisation (org.opensmartgridplatform.domain.core.entities.Organisation)15 Transactional (org.springframework.transaction.annotation.Transactional)9 FunctionalException (org.opensmartgridplatform.shared.exceptionhandling.FunctionalException)7 LightMeasurementDevice (org.opensmartgridplatform.domain.core.entities.LightMeasurementDevice)5 UnknownEntityException (org.opensmartgridplatform.domain.core.exceptions.UnknownEntityException)5 Then (io.cucumber.java.en.Then)4 Ssld (org.opensmartgridplatform.domain.core.entities.Ssld)4 NotAuthorizedException (org.opensmartgridplatform.domain.core.exceptions.NotAuthorizedException)4 DeviceFunctionGroup (org.opensmartgridplatform.domain.core.valueobjects.DeviceFunctionGroup)4 ReadSettingsHelper.getString (org.opensmartgridplatform.cucumber.core.ReadSettingsHelper.getString)3 Given (io.cucumber.java.en.Given)2 ExistingEntityException (org.opensmartgridplatform.domain.core.exceptions.ExistingEntityException)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 EntityNotFoundException (javax.persistence.EntityNotFoundException)1 PersistenceException (javax.persistence.PersistenceException)1