From 5e612ceaf27c06e88615aef327f58a7a7a904bcf Mon Sep 17 00:00:00 2001 From: Arshana Date: Wed, 23 Nov 2022 00:00:52 +0530 Subject: [PATCH 1/2] Add capability to send a notification when device rename --- .../impl/DeviceManagementServiceImpl.java | 33 +++++++++++++++++- .../DeviceManagementProviderService.java | 2 ++ .../DeviceManagementProviderServiceImpl.java | 34 +++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index b8c626cb41..821a47319f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -715,8 +715,35 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { Device persistedDevice = deviceManagementProviderService.getDevice(new DeviceIdentifier (deviceId, deviceType), true); persistedDevice.setName(device.getName()); - boolean response = deviceManagementProviderService.modifyEnrollment(persistedDevice); + System.out.println("This is rename device"); + boolean responseOfmodifyEnrollment = deviceManagementProviderService.modifyEnrollment(persistedDevice); + boolean responseOfDeviceNameChanged = deviceManagementProviderService.SendDeviceNameChangedNotification(persistedDevice); + +// boolean response = responseOfmodifyEnrollment || responseOfDeviceNameChanged; + + +// ProfileOperation operation = new ProfileOperation(); +// operation.setCode("SEND_USERNAME"); +// operation.setType(Operation.Type.PROFILE); +// operation.setPayLoad(device.getName()); +// +// DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); +// deviceIdentifier.setId(persistedDevice.getDeviceIdentifier()); +// deviceIdentifier.setType(persistedDevice.getType()); +// +// List deviceIdentifiers = new ArrayList<>(); +// deviceIdentifiers.add(deviceIdentifier); +// +//// Activity activity; +// Activity activity = deviceManagementProviderService.addOperation(persistedDevice.getType(), operation, deviceIdentifiers); +// ; +// boolean responseOfDeviceNameChanged = activity != null; + + boolean response = responseOfmodifyEnrollment || responseOfDeviceNameChanged; + + return Response.status(Response.Status.CREATED).entity(response).build(); +// return Response.status(Response.Status.CREATED).entity(responseOfmodifyEnrollment).build(); } catch (DeviceManagementException e) { log.error("Error encountered while updating device of type : " + deviceType + " and " + @@ -724,6 +751,10 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { return Response.status(Response.Status.BAD_REQUEST).entity( new ErrorResponse.ErrorResponseBuilder().setMessage("Error while updating " + "device of type " + deviceType + " and ID : " + deviceId).build()).build(); +// } catch (OperationManagementException e) { +// throw new RuntimeException(e); +// } catch (InvalidDeviceException e) { +// throw new RuntimeException(e); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index a84deb8894..cbf57941d2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -1042,4 +1042,6 @@ public interface DeviceManagementProviderService { */ PaginationResult getDevicesDetails(PaginationRequest request, List devicesIds, String groupName) throws DeviceManagementException; + + Boolean SendDeviceNameChangedNotification(Device device) throws DeviceManagementException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 2f0439f8ca..786bbc9b05 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -143,6 +143,7 @@ import org.wso2.carbon.device.mgt.core.internal.PluginInitializationListener; import org.wso2.carbon.device.mgt.core.metadata.mgt.dao.MetadataDAO; import org.wso2.carbon.device.mgt.core.metadata.mgt.dao.MetadataManagementDAOFactory; import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation; +import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import org.wso2.carbon.device.mgt.core.util.HttpReportingUtil; import org.wso2.carbon.email.sender.core.ContentProviderInfo; @@ -4860,4 +4861,37 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv paginationResult.setData(populateAllDeviceInfo(subscribedDeviceDetails)); return paginationResult; } + + @Override + public Boolean SendDeviceNameChangedNotification(Device device) throws DeviceManagementException { + + try { + ProfileOperation operation = new ProfileOperation(); + operation.setCode("SEND_USERNAME"); + operation.setType(Operation.Type.PROFILE); + operation.setPayLoad(device.getName()); + + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(device.getDeviceIdentifier()); + deviceIdentifier.setType(device.getType()); + + List deviceIdentifiers = new ArrayList<>(); + deviceIdentifiers.add(deviceIdentifier); + + Activity activity; + activity = addOperation(device.getType(), operation, deviceIdentifiers); + + return activity != null; + + } catch (OperationManagementException e) { + String msg = "Error occurred while sending operation" ; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (InvalidDeviceException e) { + String msg = "Invalid Device exception occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } + + } } From 777940b896bb2aab3389c5a68a1744a17b2d776a Mon Sep 17 00:00:00 2001 From: Arshana Date: Thu, 24 Nov 2022 13:23:46 +0530 Subject: [PATCH 2/2] Fix SendDeviceNameChangedNotification --- .../impl/DeviceManagementServiceImpl.java | 33 ++----------------- .../device/mgt/core/common/Constants.java | 2 ++ .../DeviceManagementProviderServiceImpl.java | 8 ++--- 3 files changed, 8 insertions(+), 35 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index 821a47319f..464da3a89f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -717,44 +717,17 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { persistedDevice.setName(device.getName()); System.out.println("This is rename device"); boolean responseOfmodifyEnrollment = deviceManagementProviderService.modifyEnrollment(persistedDevice); - boolean responseOfDeviceNameChanged = deviceManagementProviderService.SendDeviceNameChangedNotification(persistedDevice); - -// boolean response = responseOfmodifyEnrollment || responseOfDeviceNameChanged; - - -// ProfileOperation operation = new ProfileOperation(); -// operation.setCode("SEND_USERNAME"); -// operation.setType(Operation.Type.PROFILE); -// operation.setPayLoad(device.getName()); -// -// DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); -// deviceIdentifier.setId(persistedDevice.getDeviceIdentifier()); -// deviceIdentifier.setType(persistedDevice.getType()); -// -// List deviceIdentifiers = new ArrayList<>(); -// deviceIdentifiers.add(deviceIdentifier); -// -//// Activity activity; -// Activity activity = deviceManagementProviderService.addOperation(persistedDevice.getType(), operation, deviceIdentifiers); -// ; -// boolean responseOfDeviceNameChanged = activity != null; - - boolean response = responseOfmodifyEnrollment || responseOfDeviceNameChanged; - + boolean responseOfDeviceNameChanged = deviceManagementProviderService.SendDeviceNameChangedNotification( + persistedDevice); + boolean response = responseOfmodifyEnrollment && responseOfDeviceNameChanged; return Response.status(Response.Status.CREATED).entity(response).build(); -// return Response.status(Response.Status.CREATED).entity(responseOfmodifyEnrollment).build(); - } catch (DeviceManagementException e) { log.error("Error encountered while updating device of type : " + deviceType + " and " + "ID : " + deviceId); return Response.status(Response.Status.BAD_REQUEST).entity( new ErrorResponse.ErrorResponseBuilder().setMessage("Error while updating " + "device of type " + deviceType + " and ID : " + deviceId).build()).build(); -// } catch (OperationManagementException e) { -// throw new RuntimeException(e); -// } catch (InvalidDeviceException e) { -// throw new RuntimeException(e); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/common/Constants.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/common/Constants.java index 0db16ff451..7d7c3ba853 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/common/Constants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/common/Constants.java @@ -24,4 +24,6 @@ public class Constants { public static final String URI_SEPARATOR = "/"; public static final String BASIC_AUTH_HEADER_PREFIX = "Basic "; public static final String BEARER = "Bearer "; + public static final String SEND_USERNAME = "SEND_USERNAME"; + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 786bbc9b05..74700125cd 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -120,6 +120,7 @@ import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.DeviceManagementPluginRepository; import org.wso2.carbon.device.mgt.core.cache.DeviceCacheKey; import org.wso2.carbon.device.mgt.core.cache.impl.DeviceCacheManagerImpl; +import org.wso2.carbon.device.mgt.core.common.Constants; import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig; import org.wso2.carbon.device.mgt.core.dao.ApplicationDAO; @@ -4867,7 +4868,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv try { ProfileOperation operation = new ProfileOperation(); - operation.setCode("SEND_USERNAME"); + operation.setCode(Constants.SEND_USERNAME); operation.setType(Operation.Type.PROFILE); operation.setPayLoad(device.getName()); @@ -4877,14 +4878,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv List deviceIdentifiers = new ArrayList<>(); deviceIdentifiers.add(deviceIdentifier); - Activity activity; activity = addOperation(device.getType(), operation, deviceIdentifiers); return activity != null; - } catch (OperationManagementException e) { - String msg = "Error occurred while sending operation" ; + String msg = "Error occurred while sending operation"; log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (InvalidDeviceException e) { @@ -4892,6 +4891,5 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } - } }