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