use of org.opensmartgridplatform.domain.core.exceptions.NotAuthorizedException in project open-smart-grid-platform by OSGP.
the class DeviceManagementService method setMaintenanceStatus.
@Transactional(value = "writableTransactionManager")
public void setMaintenanceStatus(@Identification final String organisationIdentification, final String deviceIdentification, final boolean status) throws FunctionalException {
final Device existingDevice = this.writableDeviceRepository.findByDeviceIdentification(deviceIdentification);
if (existingDevice == null) {
// device does not exist
LOGGER.info("Device does not exist, cannot set maintenance status.");
throw new FunctionalException(FunctionalExceptionType.UNKNOWN_DEVICE, ComponentType.WS_CORE, new UnknownEntityException(Device.class, deviceIdentification));
} else {
// Check to see if the organisation is CONFIGURATION or OWNER
// authorized
boolean isAuthorized = false;
for (final DeviceAuthorization authorizations : existingDevice.getAuthorizations()) {
if (organisationIdentification.equals(authorizations.getOrganisation().getOrganisationIdentification()) && (DeviceFunctionGroup.OWNER.equals(authorizations.getFunctionGroup()) || DeviceFunctionGroup.CONFIGURATION.equals(authorizations.getFunctionGroup()))) {
isAuthorized = true;
existingDevice.updateInMaintenance(status);
this.writableDeviceRepository.save(existingDevice);
break;
}
}
if (!isAuthorized) {
// unauthorized, throwing exception.
throw new FunctionalException(FunctionalExceptionType.UNAUTHORIZED, ComponentType.WS_CORE, new NotAuthorizedException(organisationIdentification));
}
}
}
use of org.opensmartgridplatform.domain.core.exceptions.NotAuthorizedException in project open-smart-grid-platform by OSGP.
the class DeviceManagementService method updateDevice.
@Transactional(value = "writableTransactionManager")
public void updateDevice(@Identification final String organisationIdentification, final String deviceToUpdateIdentification, @Valid final Ssld updateDevice) throws FunctionalException {
final Device existingDevice = this.writableDeviceRepository.findByDeviceIdentification(deviceToUpdateIdentification);
if (existingDevice == 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, deviceToUpdateIdentification));
}
final List<DeviceAuthorization> owners = this.writableAuthorizationRepository.findByDeviceAndFunctionGroup(existingDevice, 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 the device
existingDevice.updateMetaData(updateDevice.getAlias(), updateDevice.getContainerAddress(), updateDevice.getGpsCoordinates());
existingDevice.setActivated(updateDevice.isActivated());
if (updateDevice.getDeviceLifecycleStatus() != null) {
existingDevice.setDeviceLifecycleStatus(updateDevice.getDeviceLifecycleStatus());
}
if (updateDevice.getTechnicalInstallationDate() != null) {
existingDevice.setTechnicalInstallationDate(updateDevice.getTechnicalInstallationDate());
}
final Ssld ssld = this.writableSsldRepository.findById(existingDevice.getId()).orElseThrow(() -> new FunctionalException(FunctionalExceptionType.UNKNOWN_DEVICE, ComponentType.WS_CORE));
ssld.updateOutputSettings(updateDevice.receiveOutputSettings());
ssld.setEans(updateDevice.getEans());
for (final Ean ean : updateDevice.getEans()) {
ean.setDevice(ssld);
}
this.writableSsldRepository.save(ssld);
}
use of org.opensmartgridplatform.domain.core.exceptions.NotAuthorizedException 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.exceptions.NotAuthorizedException in project open-smart-grid-platform by OSGP.
the class DeviceInstallationService method updateDevice.
@Transactional(value = "writableTransactionManager")
public void updateDevice(@Identification final String organisationIdentification, @Valid final Ssld updateDevice) throws FunctionalException {
final Ssld existingDevice = this.writableSsldRepository.findByDeviceIdentification(updateDevice.getDeviceIdentification());
if (existingDevice == 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, updateDevice.getDeviceIdentification()));
}
final List<DeviceAuthorization> owners = this.writableAuthorizationRepository.findByDeviceAndFunctionGroup(existingDevice, 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 the device
existingDevice.updateMetaData(updateDevice.getAlias(), updateDevice.getContainerAddress(), updateDevice.getGpsCoordinates());
existingDevice.setPublicKeyPresent(updateDevice.isPublicKeyPresent());
this.writableSsldRepository.save(existingDevice);
}
Aggregations