From 26f2e2d2ae3277ba6bbc86767071c7a715a47064 Mon Sep 17 00:00:00 2001 From: lakshani Date: Mon, 30 Jan 2017 07:50:10 +0530 Subject: [PATCH 1/2] added role query param to getdevices restapis --- .../service/api/DeviceManagementService.java | 7 ++++ .../impl/DeviceManagementServiceImpl.java | 11 ++++++ ...ApplicationManagementAdminServiceImpl.java | 10 +++--- .../device/mgt/common/PaginationRequest.java | 8 +++++ .../DeviceManagementProviderServiceImpl.java | 34 ++++++++++++++----- 5 files changed, 58 insertions(+), 12 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java index 2938b7bb96..05a22be16d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java @@ -205,6 +205,13 @@ public interface DeviceManagementService { required = false) @QueryParam("user") String user, + @ApiParam( + name = "role", + value = "A role of device owners. Ex : store-admin", + required = false) + @QueryParam("role") + @Size(max = 45) + String role, @ApiParam( name = "ownership", allowableValues = "BYOD, COPE", 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 9a8f3b80f3..bab0d1fd39 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 @@ -18,6 +18,7 @@ */ package org.wso2.carbon.device.mgt.jaxrs.service.impl; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.CarbonContext; @@ -84,6 +85,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { @QueryParam("name") String name, @QueryParam("type") String type, @QueryParam("user") String user, + @QueryParam("role") String role, @QueryParam("ownership") String ownership, @QueryParam("status") String status, @QueryParam("groupId") int groupId, @@ -92,6 +94,12 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { @QueryParam("offset") int offset, @QueryParam("limit") int limit) { try { + if (!StringUtils.isEmpty(name) && !StringUtils.isEmpty(role)) { + return Response.status(Response.Status.BAD_REQUEST).entity( + new ErrorResponse.ErrorResponseBuilder().setMessage("Request contains both name and role " + + "parameters. Only one is allowed " + + "at once.").build()).build(); + } // RequestValidationUtil.validateSelectionCriteria(type, user, roleName, ownership, status); RequestValidationUtil.validatePaginationParameters(offset, limit); DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService(); @@ -126,6 +134,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { if (groupId != 0 ) { request.setGroupId(groupId); } + if (role != null && !role.isEmpty()) { + request.setOwnerRole(role); + } // this is the user who initiates the request String authorizedUser = MultitenantUtils.getTenantAwareUsername(CarbonContext.getThreadLocalCarbonContext().getUsername()); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/ApplicationManagementAdminServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/ApplicationManagementAdminServiceImpl.java index 59f32534c9..5141399fa1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/ApplicationManagementAdminServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/ApplicationManagementAdminServiceImpl.java @@ -65,9 +65,10 @@ public class ApplicationManagementAdminServiceImpl implements ApplicationManagem if (applicationWrapper.getDeviceIdentifiers() != null) { for (DeviceIdentifier deviceIdentifier : applicationWrapper.getDeviceIdentifiers()) { - if (Platform.ANDROID.toString().equals(deviceIdentifier.getType())) { + String deviceType = deviceIdentifier.getType().toUpperCase(); + if (Platform.ANDROID.toString().equals(deviceType)) { operation = MDMAndroidOperationUtil.createInstallAppOperation(mobileApp); - } else if (Platform.IOS.toString().equals(deviceIdentifier.getType())) { + } else if (Platform.IOS.toString().equals(deviceType)) { operation = MDMIOSOperationUtil.createInstallAppOperation(mobileApp); } } @@ -114,9 +115,10 @@ public class ApplicationManagementAdminServiceImpl implements ApplicationManagem if (applicationWrapper.getDeviceIdentifiers() != null) { for (DeviceIdentifier deviceIdentifier : applicationWrapper.getDeviceIdentifiers()) { - if (Platform.ANDROID.toString().equals(deviceIdentifier.getType())) { + String deviceType = deviceIdentifier.getType().toUpperCase(); + if (Platform.ANDROID.toString().equals(deviceType)) { operation = MDMAndroidOperationUtil.createAppUninstallOperation(mobileApp); - } else if (deviceIdentifier.getType().equals(Platform.IOS.toString())) { + } else if (deviceType.equals(Platform.IOS.toString())) { operation = MDMIOSOperationUtil.createAppUninstallOperation(mobileApp); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/PaginationRequest.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/PaginationRequest.java index 6c3a6c9f60..151e86b6ad 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/PaginationRequest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/PaginationRequest.java @@ -33,6 +33,7 @@ public class PaginationRequest { private String deviceType; private String deviceName; private String ownership; + private String ownerRole; private Date since; public PaginationRequest(int start, int rowCount) { @@ -112,4 +113,11 @@ public class PaginationRequest { this.since = since; } + public String getOwnerRole() { + return ownerRole; + } + + public void setOwnerRole(String ownerRole) { + this.ownerRole = ownerRole; + } } 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 39389ee879..1dee64926e 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 @@ -17,6 +17,7 @@ */ package org.wso2.carbon.device.mgt.core.service; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.CarbonConstants; @@ -607,16 +608,22 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public PaginationResult getAllDevices(PaginationRequest request) throws DeviceManagementException { + List devicesForRoles = null; PaginationResult paginationResult = new PaginationResult(); List devices = new ArrayList<>(); List allDevices = new ArrayList<>(); int count = 0; int tenantId = this.getTenantId(); request = DeviceManagerUtil.validateDeviceListPageSize(request); + if (!StringUtils.isEmpty(request.getOwnerRole())) { + devicesForRoles = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider() + .getAllDevicesOfRole(request.getOwnerRole()); + } try { DeviceManagementDAOFactory.openConnection(); allDevices = deviceDAO.getDevices(request, tenantId); count = deviceDAO.getDeviceCount(request, tenantId); + } catch (DeviceManagementDAOException e) { throw new DeviceManagementException("Error occurred while retrieving device list pertaining to " + "the current tenant", e); @@ -625,6 +632,21 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } finally { DeviceManagementDAOFactory.closeConnection(); } + + devices = processDevices(devices, allDevices); + + if (devicesForRoles != null) { + count += devicesForRoles.size(); + devices = processDevices(devices, devicesForRoles); + } + + paginationResult.setData(devices); + paginationResult.setRecordsFiltered(count); + paginationResult.setRecordsTotal(count); + return paginationResult; + } + + private List processDevices(List devices, List allDevices) throws DeviceManagementException { for (Device device : allDevices) { DeviceInfo info = null; try { @@ -637,7 +659,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv device.setDeviceInfo(info); } catch (DeviceDetailsMgtDAOException e) { log.error("Error occurred while retrieving advance info of '" + device.getType() + - "' that carries the id '" + device.getDeviceIdentifier() + "'"); + "' that carries the id '" + device.getDeviceIdentifier() + "'"); } catch (SQLException e) { log.error("Error occurred while opening a connection to the data source", e); } finally { @@ -651,7 +673,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv device.setApplications(applications); } catch (DeviceManagementDAOException e) { log.error("Error occurred while retrieving the application list of '" + device.getType() + "', " + - "which carries the id '" + device.getId() + "'", e); + "which carries the id '" + device.getId() + "'", e); } catch (SQLException e) { log.error("Error occurred while opening a connection to the data source", e); } finally { @@ -662,7 +684,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv if (deviceManager == null) { if (log.isDebugEnabled()) { log.debug("Device Manager associated with the device type '" + device.getType() + "' is null. " + - "Therefore, not attempting method 'isEnrolled'"); + "Therefore, not attempting method 'isEnrolled'"); } devices.add(device); continue; @@ -675,12 +697,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } devices.add(device); } - paginationResult.setData(devices); - paginationResult.setRecordsFiltered(count); - paginationResult.setRecordsTotal(count); - return paginationResult; + return devices; } - @Override public List getAllDevices(String deviceType) throws DeviceManagementException { List devices = new ArrayList<>(); From bbcdf7aeda72885c09a0502f0db8df3a0aeac923 Mon Sep 17 00:00:00 2001 From: lakshani Date: Mon, 30 Jan 2017 07:54:43 +0530 Subject: [PATCH 2/2] removed unwanted new line --- .../mgt/core/service/DeviceManagementProviderServiceImpl.java | 1 - 1 file changed, 1 deletion(-) 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 1dee64926e..ed1036909f 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 @@ -623,7 +623,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.openConnection(); allDevices = deviceDAO.getDevices(request, tenantId); count = deviceDAO.getDeviceCount(request, tenantId); - } catch (DeviceManagementDAOException e) { throw new DeviceManagementException("Error occurred while retrieving device list pertaining to " + "the current tenant", e);