From 3766ba0349fd1d46cee21994956d24bd7787cbad Mon Sep 17 00:00:00 2001 From: Harshan Liyanage Date: Wed, 7 Jun 2017 16:29:05 +0530 Subject: [PATCH] Refactored DeviceMgmtProviderService to fetch only the required data which resolves product-iots#991 --- .../provider/fcm/FCMNotificationStrategy.java | 2 +- .../service/api/DeviceManagementService.java | 14 + .../impl/DeviceManagementServiceImpl.java | 36 +- .../impl/PolicyManagementServiceImpl.java | 2 +- .../DeviceManagementAdminServiceImpl.java | 6 +- ...ApplicationManagerProviderServiceImpl.java | 4 +- .../impl/DeviceInformationManagerImpl.java | 14 +- .../DeviceManagementProviderService.java | 316 ++++- .../DeviceManagementProviderServiceImpl.java | 1056 +++++------------ .../GroupManagementProviderServiceImpl.java | 8 +- .../core/task/impl/DeviceTaskManagerImpl.java | 2 +- .../mgt/core/util/DeviceManagerUtil.java | 3 +- .../mgt/core/enforcement/DelegationTask.java | 7 +- .../impl/ComplianceDecisionPointImpl.java | 12 +- .../core/mgt/impl/MonitoringManagerImpl.java | 6 +- .../mgt/core/mgt/impl/PolicyManagerImpl.java | 16 +- .../policy/mgt/core/task/MonitoringTask.java | 6 +- .../policy/mgt/core/MonitoringTestCase.java | 2 +- .../policy/mgt/core/PolicyDAOTestCase.java | 4 +- .../mgt/core/PolicyEvaluationTestCase.java | 2 +- 20 files changed, 680 insertions(+), 838 deletions(-) diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.fcm/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/fcm/FCMNotificationStrategy.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.fcm/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/fcm/FCMNotificationStrategy.java index da488c5435..7085f1244b 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.fcm/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/fcm/FCMNotificationStrategy.java +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.fcm/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/fcm/FCMNotificationStrategy.java @@ -57,7 +57,7 @@ public class FCMNotificationStrategy implements NotificationStrategy { public void execute(NotificationContext ctx) throws PushNotificationExecutionFailedException { try { Device device = - FCMDataHolder.getInstance().getDeviceManagementProviderService().getDevice(ctx.getDeviceId()); + FCMDataHolder.getInstance().getDeviceManagementProviderService().getDeviceWithTypeProperties(ctx.getDeviceId()); this.sendWakeUpCall(ctx.getOperation().getCode(), device); } catch (DeviceManagementException e) { throw new PushNotificationExecutionFailedException("Error occurred while retrieving device information", e); 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 ddbdfcdc08..e337fc88fa 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 @@ -267,6 +267,13 @@ public interface DeviceManagementService { required = false) @HeaderParam("If-Modified-Since") String timestamp, + @ApiParam( + name = "requireDeviceInfo", + value = "Boolean flag indicating whether to include device-info (location, application list etc) \n" + + " to the device object.", + required = false) + @QueryParam("requireDeviceInfo") + boolean requireDeviceInfo, @ApiParam( name = "offset", value = "The starting pagination index for the complete list of qualified items.", @@ -333,6 +340,13 @@ public interface DeviceManagementService { }) @Path("/user-devices") Response getDeviceByUser( + @ApiParam( + name = "requireDeviceInfo", + value = "Boolean flag indicating whether to include device-info (location, application list etc) \n" + + " to the device object.", + required = false) + @QueryParam("requireDeviceInfo") + boolean requireDeviceInfo, @ApiParam( name = "offset", value = "The starting pagination index for the complete list of qualified items.", 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 b201bfd80c..73da222752 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,7 +18,6 @@ */ package org.wso2.carbon.device.mgt.jaxrs.service.impl; -import io.swagger.annotations.ApiParam; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -97,6 +96,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { @QueryParam("groupId") int groupId, @QueryParam("since") String since, @HeaderParam("If-Modified-Since") String ifModifiedSince, + @QueryParam("requireDeviceInfo") boolean requireDeviceInfo, @QueryParam("offset") int offset, @QueryParam("limit") int limit) { try { @@ -180,7 +180,12 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { "string is provided in 'If-Modified-Since' header").build()).build(); } request.setSince(sinceDate); - result = dms.getAllDevices(request); + if (requireDeviceInfo) { + result = dms.getAllDevices(request); + } else { + result = dms.getAllDevices(request, false); + } + if (result == null || result.getData() == null || result.getData().size() <= 0) { return Response.status(Response.Status.NOT_MODIFIED).entity("No device is modified " + "after the timestamp provided in 'If-Modified-Since' header").build(); @@ -196,14 +201,22 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { "string is provided in 'since' filter").build()).build(); } request.setSince(sinceDate); - result = dms.getAllDevices(request); + if (requireDeviceInfo) { + result = dms.getAllDevices(request); + } else { + result = dms.getAllDevices(request, false); + } if (result == null || result.getData() == null || result.getData().size() <= 0) { devices.setList(new ArrayList()); devices.setCount(0); return Response.status(Response.Status.OK).entity(devices).build(); } } else { - result = dms.getAllDevices(request); + if (requireDeviceInfo) { + result = dms.getAllDevices(request); + } else { + result = dms.getAllDevices(request, false); + } int resultCount = result.getRecordsTotal(); if (resultCount == 0) { Response.status(Response.Status.OK).entity(devices).build(); @@ -229,7 +242,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { @GET @Override @Path("/user-devices") - public Response getDeviceByUser(@QueryParam("offset") int offset, + public Response getDeviceByUser(@QueryParam("requireDeviceInfo") boolean requireDeviceInfo, + @QueryParam("offset") int offset, @QueryParam("limit") int limit) { RequestValidationUtil.validatePaginationParameters(offset, limit); @@ -241,7 +255,11 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { request.setOwner(currentUser); try { - result = DeviceMgtAPIUtils.getDeviceManagementService().getDevicesOfUser(request); + if (requireDeviceInfo) { + result = DeviceMgtAPIUtils.getDeviceManagementService().getDevicesOfUser(request); + } else { + result = DeviceMgtAPIUtils.getDeviceManagementService().getDevicesOfUser(request, false); + } devices.setList((List) result.getData()); devices.setCount(result.getRecordsTotal()); return Response.status(Response.Status.OK).entity(devices).build(); @@ -261,7 +279,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { DeviceMgtAPIUtils.getDeviceManagementService(); try { DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, deviceType); - Device persistedDevice = deviceManagementProviderService.getDevice(deviceIdentifier); + Device persistedDevice = deviceManagementProviderService.getDevice(deviceIdentifier, true); if (persistedDevice == null) { return Response.status(Response.Status.NOT_FOUND).build(); } @@ -287,7 +305,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { DeviceManagementProviderService deviceManagementProviderService = DeviceMgtAPIUtils.getDeviceManagementService(); try { Device persistedDevice = deviceManagementProviderService.getDevice(new DeviceIdentifier - (deviceId, deviceType)); + (deviceId, deviceType), true); persistedDevice.setName(device.getName()); boolean response = deviceManagementProviderService.modifyEnrollment(persistedDevice); return Response.status(Response.Status.CREATED).entity(response).build(); @@ -586,7 +604,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { DeviceMgtAPIUtils.getDeviceManagementService(); try { DeviceIdentifier deviceIdentifier = new DeviceIdentifier(id, type); - Device persistedDevice = deviceManagementProviderService.getDevice(deviceIdentifier); + Device persistedDevice = deviceManagementProviderService.getDevice(deviceIdentifier, false); if (persistedDevice == null) { return Response.status(Response.Status.NOT_FOUND).build(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java index f51ebfd928..01dd4880c1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java @@ -128,7 +128,7 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { List deviceIdentifiers = policyWrapper.getDeviceIdentifiers(); if (deviceIdentifiers != null) { for (DeviceIdentifier id : deviceIdentifiers) { - devices.add(DeviceMgtAPIUtils.getDeviceManagementService().getDevice(id)); + devices.add(DeviceMgtAPIUtils.getDeviceManagementService().getDevice(id, false)); } } policy.setDevices(devices); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceManagementAdminServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceManagementAdminServiceImpl.java index cb4b1bb37c..9a968a3ac5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceManagementAdminServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceManagementAdminServiceImpl.java @@ -25,6 +25,7 @@ import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.service.api.admin.DeviceManagementAdminService; @@ -64,8 +65,11 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(DeviceMgtAPIUtils.getTenantId(tenantDomain)); + PaginationRequest request = new PaginationRequest(offset, limit); + request.setDeviceType(type); + request.setDeviceName(name); List devices = DeviceMgtAPIUtils.getDeviceManagementService(). - getDevicesByNameAndType(name, type, offset, limit); + getDevicesByNameAndType(request, false); // setting up paginated result DeviceList deviceList = new DeviceList(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java index 0bc04e6e61..756f09e32a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java @@ -114,7 +114,7 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem for (String user : userNameList) { userName = user; deviceList = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getDevicesOfUser - (user); + (user, false); for (Device device : deviceList) { deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(Integer.toString(device.getId())); @@ -156,7 +156,7 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem for (String role : userRoleList) { userRole = role; deviceList = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider() - .getAllDevicesOfRole(userRole); + .getAllDevicesOfRole(userRole, false); for (Device device : deviceList) { deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(Integer.toString(device.getId())); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index 127216f27b..caf594c113 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -55,7 +55,7 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { public void addDeviceInfo(DeviceIdentifier deviceId, DeviceInfo deviceInfo) throws DeviceDetailsMgtException { try { Device device = DeviceManagementDataHolder.getInstance(). - getDeviceManagementProvider().getDevice(deviceId); + getDeviceManagementProvider().getDevice(deviceId, false); DeviceManagementDAOFactory.beginTransaction(); deviceDAO.updateDevice(device, CarbonContext.getThreadLocalCarbonContext().getTenantId()); @@ -87,7 +87,7 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { Device device; try { device = DeviceManagementDataHolder.getInstance(). - getDeviceManagementProvider().getDevice(deviceId); + getDeviceManagementProvider().getDevice(deviceId, false); if (device == null) { if (log.isDebugEnabled()) { log.debug("No device is found upon the device identifier '" + deviceId.getId() + @@ -123,8 +123,8 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { } try { List deviceIds = new ArrayList<>(); - List devices = DeviceManagementDataHolder.getInstance(). - getDeviceManagementProvider().getAllDevices(); + List devices = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider(). + getAllDevices(false); for (Device device : devices) { if (identifierMap.containsKey(device.getDeviceIdentifier()) && device.getType().equals(identifierMap.get(device.getDeviceIdentifier()).getType())) { @@ -154,7 +154,7 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { try { Device device = DeviceManagementDataHolder.getInstance(). - getDeviceManagementProvider().getDevice(deviceLocation.getDeviceIdentifier()); + getDeviceManagementProvider().getDevice(deviceLocation.getDeviceIdentifier(), false); deviceLocation.setDeviceId(device.getId()); DeviceManagementDAOFactory.beginTransaction(); deviceDAO.updateDevice(device, CarbonContext.getThreadLocalCarbonContext().getTenantId()); @@ -183,7 +183,7 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { public DeviceLocation getDeviceLocation(DeviceIdentifier deviceId) throws DeviceDetailsMgtException { Device device; try { - device = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getDevice(deviceId); + device = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getDevice(deviceId, false); if (device == null) { if (log.isDebugEnabled()) { log.debug("No device is found upon the device identifier '" + deviceId.getId() + @@ -212,7 +212,7 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { try { List devices = DeviceManagementDataHolder.getInstance(). - getDeviceManagementProvider().getAllDevices(deviceIdentifiers.get(0).getType()); + getDeviceManagementProvider().getAllDevices(deviceIdentifiers.get(0).getType(), false); List deviceLocations = new ArrayList<>(); DeviceManagementDAOFactory.openConnection(); for (Device device : devices) { 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 9117468541..d1d3f5f03d 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 @@ -54,6 +54,18 @@ public interface DeviceManagementProviderService { */ List getAllDevices(String deviceType) throws DeviceManagementException; + /** + * Method to retrieve all the devices of a given device type. + * + * @param deviceType Device-type of the required devices + * @param requireDeviceInfo - A boolean indicating whether the device-info (location, app-info etc) is also required + * along with the device data. + * @return List of devices of given device-type. + * @throws DeviceManagementException If some unusual behaviour is observed while fetching the + * devices. + */ + List getAllDevices(String deviceType, boolean requireDeviceInfo) throws DeviceManagementException; + /** * Method to retrieve all the devices registered in the system. * @@ -63,6 +75,38 @@ public interface DeviceManagementProviderService { */ List getAllDevices() throws DeviceManagementException; + /** + * Method to retrieve all the devices registered in the system. + * + * @param requireDeviceInfo - A boolean indicating whether the device-info (location, app-info etc) is also required + * along with the device data. + * @return List of registered devices. + * @throws DeviceManagementException If some unusual behaviour is observed while fetching the + * devices. + */ + List getAllDevices(boolean requireDeviceInfo) throws DeviceManagementException; + + /** + * Method to retrieve all the devices registered in the system. + * + * @param since - Date value where the resource was last modified + * @return List of registered devices. + * @throws DeviceManagementException If some unusual behaviour is observed while fetching the + * devices. + */ + List getDevices(Date since) throws DeviceManagementException; + + /** + * Method to retrieve all the devices registered in the system. + * + * @param requireDeviceInfo - A boolean indicating whether the device-info (location, app-info etc) is also required + * along with the device data. + * @return List of registered devices. + * @throws DeviceManagementException If some unusual behaviour is observed while fetching the + * devices. + */ + List getDevices(Date since, boolean requireDeviceInfo) throws DeviceManagementException; + /** * Method to retrieve all the devices with pagination support. * @@ -73,6 +117,18 @@ public interface DeviceManagementProviderService { */ PaginationResult getDevicesByType(PaginationRequest request) throws DeviceManagementException; + /** + * Method to retrieve all the devices with pagination support. + * + * @param request PaginationRequest object holding the data for pagination + * @param requireDeviceInfo - A boolean indicating whether the device-info (location, app-info etc) is also required + * along with the device data. + * @return PaginationResult - Result including the required parameters necessary to do pagination. + * @throws DeviceManagementException If some unusual behaviour is observed while fetching the + * devices. + */ + PaginationResult getDevicesByType(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException; + /** * Method to retrieve all the devices with pagination support. * @@ -83,21 +139,91 @@ public interface DeviceManagementProviderService { */ PaginationResult getAllDevices(PaginationRequest request) throws DeviceManagementException; - void sendEnrolmentInvitation(String templateName, EmailMetaInfo metaInfo) throws DeviceManagementException; + /** + * Method to retrieve all the devices with pagination support. + * + * @param request PaginationRequest object holding the data for pagination + * @param requireDeviceInfo - A boolean indicating whether the device-info (location, app-info etc) is also required + * along with the device data. + * @return PaginationResult - Result including the required parameters necessary to do pagination. + * @throws DeviceManagementException If some unusual behaviour is observed while fetching the + * devices. + */ + PaginationResult getAllDevices(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException; - void sendRegistrationEmail(EmailMetaInfo metaInfo) throws DeviceManagementException; + /** + * Returns the device of specified id. + * + * @param deviceId device Id + * @return Device returns null when device is not available. + * @throws DeviceManagementException + */ + Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException; - FeatureManager getFeatureManager(String deviceType) throws DeviceManagementException; + /** + * Returns the device of specified id. + * + * @param deviceId device Id + * @return Device returns null when device is not available. + * @throws DeviceManagementException + */ + Device getDeviceWithTypeProperties(DeviceIdentifier deviceId) throws DeviceManagementException; /** - * Proxy method to get the tenant configuration of a given platform. + * Returns the device of specified id. * - * @param deviceType Device platform - * @return Tenant configuration settings of the particular tenant and platform. - * @throws DeviceManagementException If some unusual behaviour is observed while fetching the - * configuration. + * @param deviceId device Id + * @param requireDeviceInfo - A boolean indicating whether the device-info (location, app-info etc) is also required + * along with the device data. + * @return Device returns null when device is not available. + * @throws DeviceManagementException */ - PlatformConfiguration getConfiguration(String deviceType) throws DeviceManagementException; + Device getDevice(DeviceIdentifier deviceId, boolean requireDeviceInfo) throws DeviceManagementException; + + /** + * Returns the device of specified id. + * + * @param deviceId device Id + * @param since - Date value where the resource was last modified + * @return Device returns null when device is not available. + * @throws DeviceManagementException + */ + Device getDevice(DeviceIdentifier deviceId, Date since) throws DeviceManagementException; + + /** + * Returns the device of specified id. + * + * @param deviceId device Id + * @param since - Date value where the resource was last modified + * @param requireDeviceInfo - A boolean indicating whether the device-info (location, app-info etc) is also required + * along with the device data. + * @return Device returns null when device is not available. + * @throws DeviceManagementException + */ + Device getDevice(DeviceIdentifier deviceId, Date since, boolean requireDeviceInfo) throws DeviceManagementException; + + /** + * Returns the device of specified id with the given status. + * + * @param deviceId device Id + * @param status - Status of the device + * + * @return Device returns null when device is not available. + * @throws DeviceManagementException + */ + Device getDevice(DeviceIdentifier deviceId, EnrolmentInfo.Status status) throws DeviceManagementException; + + /** + * Returns the device of specified id with the given status. + * + * @param deviceId device Id + * @param status - Status of the device + * @param requireDeviceInfo - A boolean indicating whether the device-info (location, app-info etc) is also required + * along with the device data. + * @return Device returns null when device is not available. + * @throws DeviceManagementException + */ + Device getDevice(DeviceIdentifier deviceId, EnrolmentInfo.Status status, boolean requireDeviceInfo) throws DeviceManagementException; /** * Method to get the list of devices owned by an user with paging information. @@ -109,6 +235,18 @@ public interface DeviceManagementProviderService { */ PaginationResult getDevicesOfUser(PaginationRequest request) throws DeviceManagementException; + /** + * Method to get the list of devices owned by an user with paging information. + * + * @param request PaginationRequest object holding the data for pagination + * @param requireDeviceInfo - A boolean indicating whether the device-info (location, app-info etc) is also required + * along with the device data. + * @return List of devices owned by a particular user along with the required parameters necessary to do pagination. + * @throws DeviceManagementException If some unusual behaviour is observed while fetching the + * device list + */ + PaginationResult getDevicesOfUser(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException; + /** * Method to get the list of devices filtered by the ownership with paging information. * @@ -119,6 +257,18 @@ public interface DeviceManagementProviderService { */ PaginationResult getDevicesByOwnership(PaginationRequest request) throws DeviceManagementException; + /** + * Method to get the list of devices filtered by the ownership with paging information. + * + * @param request PaginationRequest object holding the data for pagination + * @param requireDeviceInfo - A boolean indicating whether the device-info (location, app-info etc) is also required + * along with the device data. + * @return List of devices owned by a particular user along with the required parameters necessary to do pagination. + * @throws DeviceManagementException If some unusual behaviour is observed while fetching the + * device list + */ + PaginationResult getDevicesByOwnership(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException; + /** * Method to get the list of devices owned by an user. * @@ -129,16 +279,42 @@ public interface DeviceManagementProviderService { */ List getDevicesOfUser(String userName) throws DeviceManagementException; + /** + * Method to get the list of devices owned by an user. + * + * @param userName Username of the user + * @param requireDeviceInfo - A boolean indicating whether the device-info (location, app-info etc) is also required + * along with the device data. + * @return List of devices owned by a particular user + * @throws DeviceManagementException If some unusual behaviour is observed while fetching the + * device list + */ + List getDevicesOfUser(String userName, boolean requireDeviceInfo) throws DeviceManagementException; + /** * This method returns the list of device owned by a user of given device type. * * @param userName user name. * @param deviceType device type name - * @return - * @throws DeviceManagementException + * @return List of device owned by the given user and type. + * @throws DeviceManagementException If some unusual behaviour is observed while fetching the + * device list */ List getDevicesOfUser(String userName, String deviceType) throws DeviceManagementException; + /** + * This method returns the list of device owned by a user of given device type. + * + * @param userName user name. + * @param deviceType device type name + * @param requireDeviceInfo - A boolean indicating whether the device-info (location, app-info etc) is also required + * along with the device data. + * @return List of device owned by the given user and type. + * @throws DeviceManagementException If some unusual behaviour is observed while fetching the + * device list + */ + List getDevicesOfUser(String userName, String deviceType, boolean requireDeviceInfo) throws DeviceManagementException; + /** * Method to get the list of devices owned by users of a particular user-role. * @@ -150,32 +326,50 @@ public interface DeviceManagementProviderService { List getAllDevicesOfRole(String roleName) throws DeviceManagementException; /** - * Method to get the device count of user. + * Method to get the list of devices owned by users of a particular user-role. * - * @return device count - * @throws DeviceManagementException If some unusual behaviour is observed while counting - * the devices + * @param roleName Role name of the users + * @param requireDeviceInfo - A boolean indicating whether the device-info (location, app-info etc) is also required + * along with the device data. + * @return List of devices owned by users of a particular role + * @throws DeviceManagementException If some unusual behaviour is observed while fetching the + * device list */ - int getDeviceCount(String username) throws DeviceManagementException; + List getAllDevicesOfRole(String roleName, boolean requireDeviceInfo) throws DeviceManagementException; /** - * Method to get the count of all types of devices. + * This method is used to retrieve list of devices based on the device status with paging information. * - * @return device count - * @throws DeviceManagementException If some unusual behaviour is observed while counting - * the devices + * @param request PaginationRequest object holding the data for pagination and filter info + * @return List of devices in given status along with the required parameters necessary to do pagination. + * @throws DeviceManagementException If some unusual behaviour is observed while fetching the + * device list */ - int getDeviceCount() throws DeviceManagementException; + PaginationResult getDevicesByStatus(PaginationRequest request) throws DeviceManagementException; + + /** + * This method is used to retrieve list of devices based on the device status with paging information. + * + * @param request PaginationRequest object holding the data for pagination and filter info + * @param requireDeviceInfo - A boolean indicating whether the device-info (location, app-info etc) is also required + * along with the device data. + * @return List of devices in given status along with the required parameters necessary to do pagination. + * @throws DeviceManagementException If some unusual behaviour is observed while fetching the + * device list + */ + PaginationResult getDevicesByStatus(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException; /** * Method to get the list of devices that matches with the given device name. * - * @param deviceName name of the device + * @param request PaginationRequest object holding the data for pagination and filter info + * @param requireDeviceInfo - A boolean indicating whether the device-info (location, app-info etc) is also required + * along with the device data. * @return List of devices that matches with the given device name. * @throws DeviceManagementException If some unusual behaviour is observed while fetching the * device list */ - List getDevicesByNameAndType(String deviceName, String type, int offset, int limit) throws DeviceManagementException; + List getDevicesByNameAndType(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException; /** * This method is used to retrieve list of devices that matches with the given device name with paging information. @@ -187,7 +381,17 @@ public interface DeviceManagementProviderService { */ PaginationResult getDevicesByName(PaginationRequest request) throws DeviceManagementException; - void updateDeviceEnrolmentInfo(Device device, EnrolmentInfo.Status active) throws DeviceManagementException; + /** + * This method is used to retrieve list of devices that matches with the given device name with paging information. + * + * @param request PaginationRequest object holding the data for pagination + * @param requireDeviceInfo - A boolean indicating whether the device-info (location, app-info etc) is also required + * along with the device data. + * @return List of devices in given status along with the required parameters necessary to do pagination. + * @throws DeviceManagementException If some unusual behaviour is observed while fetching the + * device list + */ + PaginationResult getDevicesByName(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException; /** * This method is used to retrieve list of devices based on the device status. @@ -199,14 +403,53 @@ public interface DeviceManagementProviderService { List getDevicesByStatus(EnrolmentInfo.Status status) throws DeviceManagementException; /** - * This method is used to retrieve list of devices based on the device status with paging information. + * This method is used to retrieve list of devices based on the device status. * - * @param request PaginationRequest object holding the data for pagination - * @return List of devices in given status along with the required parameters necessary to do pagination. + * @param status Device status + * @param requireDeviceInfo - A boolean indicating whether the device-info (location, app-info etc) is also required + * along with the device data. + * @return List of devices + * @throws DeviceManagementException + */ + List getDevicesByStatus(EnrolmentInfo.Status status, boolean requireDeviceInfo) throws DeviceManagementException; + + /** + * Method to get the device count of user. + * + * @return device count + * @throws DeviceManagementException If some unusual behaviour is observed while counting + * the devices + */ + int getDeviceCount(String username) throws DeviceManagementException; + + /** + * Method to get the count of all types of devices. + * + * @return device count + * @throws DeviceManagementException If some unusual behaviour is observed while counting + * the devices + */ + int getDeviceCount() throws DeviceManagementException; + + HashMap getTenantedDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementException; + + void sendEnrolmentInvitation(String templateName, EmailMetaInfo metaInfo) throws DeviceManagementException; + + void sendRegistrationEmail(EmailMetaInfo metaInfo) throws DeviceManagementException; + + FeatureManager getFeatureManager(String deviceType) throws DeviceManagementException; + + /** + * Proxy method to get the tenant configuration of a given platform. + * + * @param deviceType Device platform + * @return Tenant configuration settings of the particular tenant and platform. * @throws DeviceManagementException If some unusual behaviour is observed while fetching the - * device list + * configuration. */ - PaginationResult getDevicesByStatus(PaginationRequest request) throws DeviceManagementException; + PlatformConfiguration getConfiguration(String deviceType) throws DeviceManagementException; + + void updateDeviceEnrolmentInfo(Device device, EnrolmentInfo.Status active) throws DeviceManagementException; /** * This method is used to check whether the device is enrolled with the give user. @@ -247,21 +490,6 @@ public interface DeviceManagementProviderService { boolean setActive(DeviceIdentifier deviceId, boolean status) throws DeviceManagementException; - /** - * Returns the device of specified id. - * - * @param deviceId device Id - * @return Device returns null when device is not avaialble. - * @throws DeviceManagementException - */ - Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException; - - Device getDevice(DeviceIdentifier deviceId, Date since) throws DeviceManagementException; - - HashMap getTenantedDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementException; - - Device getDevice(DeviceIdentifier deviceId, EnrolmentInfo.Status status) throws DeviceManagementException; - List getAvailableDeviceTypes() throws DeviceManagementException; boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, 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 120d430b43..12b873a228 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 @@ -176,7 +176,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } int tenantId = this.getTenantId(); - Device existingDevice = this.getDevice(deviceIdentifier); + Device existingDevice = this.getDevice(deviceIdentifier, false); if (existingDevice != null) { EnrolmentInfo existingEnrolmentInfo = existingDevice.getEnrolmentInfo(); @@ -414,9 +414,45 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return deviceManager.setActive(deviceId, status); } + @Override + public List getAllDevices(String deviceType) throws DeviceManagementException { + return this.getAllDevices(deviceType, true); + } + + @Override + public List getAllDevices(String deviceType, boolean requireDeviceInfo) throws DeviceManagementException { + List allDevices; + try { + DeviceManagementDAOFactory.openConnection(); + allDevices = deviceDAO.getDevices(deviceType, this.getTenantId()); + if (allDevices == null) { + if (log.isDebugEnabled()) { + log.debug("No device is found upon the type '" + deviceType + "'"); + } + return null; + } + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementException("Error occurred while retrieving all devices of type '" + + deviceType + "' that are being managed within the scope of current tenant", e); + } catch (SQLException e) { + throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + + if (requireDeviceInfo) { + return this.getAllDeviceInfo(allDevices); + } + return allDevices; + } + @Override public List getAllDevices() throws DeviceManagementException { - List devices = new ArrayList<>(); + return this.getAllDevices(true); + } + + @Override + public List getAllDevices(boolean requireDeviceInfo) throws DeviceManagementException { List allDevices; try { DeviceManagementDAOFactory.openConnection(); @@ -430,60 +466,19 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.closeConnection(); } - for (Device device : allDevices) { - DeviceInfo info = null; - try { - DeviceManagementDAOFactory.openConnection(); - info = deviceInfoDAO.getDeviceInformation(device.getId()); - DeviceLocation location = deviceInfoDAO.getDeviceLocation(device.getId()); - if (info != null) { - info.setLocation(location); - } - } catch (DeviceDetailsMgtDAOException e) { - log.error("Error occurred while retrieving advance info of '" + device.getType() + - "' that carries the id '" + device.getDeviceIdentifier() + "'", e); - } catch (SQLException e) { - log.error("Error occurred while opening a connection to the data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - device.setDeviceInfo(info); - - try { - DeviceManagementDAOFactory.openConnection(); - List applications = applicationDAO.getInstalledApplications(device.getId()); - 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); - } catch (SQLException e) { - log.error("Error occurred while opening a connection to the data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - - DeviceManager deviceManager = this.getDeviceManager(device.getType()); - if (deviceManager == null) { - if (log.isDebugEnabled()) { - log.debug("Device Manager associated with the device type '" + device.getType() + "' is null. " + - "Therefore, not attempting method 'isEnrolled'"); - } - devices.add(device); - continue; - } - Device dmsDevice = - deviceManager.getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); - if (dmsDevice != null) { - device.setFeatures(dmsDevice.getFeatures()); - device.setProperties(dmsDevice.getProperties()); - } - devices.add(device); + if (requireDeviceInfo) { + return this.getAllDeviceInfo(allDevices); } - return devices; + return allDevices; } + @Override public List getDevices(Date since) throws DeviceManagementException { - List devices = new ArrayList<>(); + return this.getDevices(since, true); + } + + @Override + public List getDevices(Date since, boolean requireDeviceInfo) throws DeviceManagementException { List allDevices; try { DeviceManagementDAOFactory.openConnection(); @@ -497,62 +492,20 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.closeConnection(); } - for (Device device : allDevices) { - DeviceInfo info = null; - try { - DeviceManagementDAOFactory.openConnection(); - info = deviceInfoDAO.getDeviceInformation(device.getId()); - DeviceLocation location = deviceInfoDAO.getDeviceLocation(device.getId()); - if (info != null) { - info.setLocation(location); - } - } catch (DeviceDetailsMgtDAOException e) { - log.error("Error occurred while retrieving advance info of '" + device.getType() + - "' that carries the id '" + device.getDeviceIdentifier() + "'"); - } catch (SQLException e) { - log.error("Error occurred while opening a connection to the data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - device.setDeviceInfo(info); - - try { - DeviceManagementDAOFactory.openConnection(); - List applications = applicationDAO.getInstalledApplications(device.getId()); - 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); - } catch (SQLException e) { - log.error("Error occurred while opening a connection to the data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - - DeviceManager deviceManager = this.getDeviceManager(device.getType()); - if (deviceManager == null) { - if (log.isDebugEnabled()) { - log.debug("Device Manager associated with the device type '" + device.getType() + "' is null. " + - "Therefore, not attempting method 'isEnrolled'"); - } - devices.add(device); - continue; - } - Device dmsDevice = - deviceManager.getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); - if (dmsDevice != null) { - device.setFeatures(dmsDevice.getFeatures()); - device.setProperties(dmsDevice.getProperties()); - } - devices.add(device); + if (requireDeviceInfo) { + return this.getAllDeviceInfo(allDevices); } - return devices; + return allDevices; } @Override public PaginationResult getDevicesByType(PaginationRequest request) throws DeviceManagementException { + return this.getDevicesByType(request, true); + } + + @Override + public PaginationResult getDevicesByType(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException { PaginationResult paginationResult = new PaginationResult(); - List devices = new ArrayList<>(); List allDevices = new ArrayList<>(); int count = 0; int tenantId = this.getTenantId(); @@ -570,43 +523,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } finally { DeviceManagementDAOFactory.closeConnection(); } - for (Device device : allDevices) { - DeviceInfo info = null; - try { - DeviceManagementDAOFactory.openConnection(); - info = deviceInfoDAO.getDeviceInformation(device.getId()); - DeviceLocation location = deviceInfoDAO.getDeviceLocation(device.getId()); - if (info != null) { - info.setLocation(location); - } - device.setDeviceInfo(info); - } catch (DeviceDetailsMgtDAOException e) { - log.error("Error occurred while retrieving advance info of '" + device.getType() + - "' that carries the id '" + device.getDeviceIdentifier() + "'"); - } catch (SQLException e) { - log.error("Error occurred while opening a connection to the data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - device.setDeviceInfo(info); - DeviceManager deviceManager = this.getDeviceManager(device.getType()); - if (deviceManager == null) { - if (log.isDebugEnabled()) { - log.debug("Device Manager associated with the device type '" + device.getType() + "' is null. " + - "Therefore, not attempting method 'isEnrolled'"); - } - devices.add(device); - continue; - } - Device dmsDevice = - deviceManager.getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); - if (dmsDevice != null) { - device.setFeatures(dmsDevice.getFeatures()); - device.setProperties(dmsDevice.getProperties()); - } - devices.add(device); + + if (requireDeviceInfo) { + paginationResult.setData(this.getAllDeviceInfo(allDevices)); + } else { + paginationResult.setData(allDevices); } - paginationResult.setData(devices); + paginationResult.setRecordsFiltered(count); paginationResult.setRecordsTotal(count); return paginationResult; @@ -614,168 +537,75 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public PaginationResult getAllDevices(PaginationRequest request) throws DeviceManagementException { + return this.getAllDevices(request, true); + } + + @Override + public PaginationResult getAllDevices(PaginationRequest request, boolean requireDeviceInfo) 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); - } catch (SQLException e) { - throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); - } 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 { - DeviceManagementDAOFactory.openConnection(); - info = deviceInfoDAO.getDeviceInformation(device.getId()); - DeviceLocation location = deviceInfoDAO.getDeviceLocation(device.getId()); - if (info != null) { - info.setLocation(location); + devicesForRoles = this.getAllDevicesOfRole(request.getOwnerRole(), false); + if (devicesForRoles != null) { + count = devicesForRoles.size(); + if (requireDeviceInfo) { + paginationResult.setData(getAllDeviceInfo(devicesForRoles)); } - device.setDeviceInfo(info); - } catch (DeviceDetailsMgtDAOException e) { - log.error("Error occurred while retrieving advance info of '" + device.getType() + - "' that carries the id '" + device.getDeviceIdentifier() + "'"); - } catch (SQLException e) { - log.error("Error occurred while opening a connection to the data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); } - device.setDeviceInfo(info); - + } else { try { DeviceManagementDAOFactory.openConnection(); - List applications = applicationDAO.getInstalledApplications(device.getId()); - device.setApplications(applications); + allDevices = deviceDAO.getDevices(request, tenantId); + count = deviceDAO.getDeviceCount(request, tenantId); } catch (DeviceManagementDAOException e) { - log.error("Error occurred while retrieving the application list of '" + device.getType() + "', " + - "which carries the id '" + device.getId() + "'", e); + throw new DeviceManagementException("Error occurred while retrieving device list pertaining to " + + "the current tenant", e); } catch (SQLException e) { - log.error("Error occurred while opening a connection to the data source", e); + throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); } finally { DeviceManagementDAOFactory.closeConnection(); } - - DeviceManager deviceManager = this.getDeviceManager(device.getType()); - if (deviceManager == null) { - if (log.isDebugEnabled()) { - log.debug("Device Manager associated with the device type '" + device.getType() + "' is null. " + - "Therefore, not attempting method 'isEnrolled'"); - } - devices.add(device); - continue; - } - Device dmsDevice = - deviceManager.getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); - if (dmsDevice != null) { - device.setFeatures(dmsDevice.getFeatures()); - device.setProperties(dmsDevice.getProperties()); + if (requireDeviceInfo) { + paginationResult.setData(getAllDeviceInfo(allDevices)); + } else { + paginationResult.setData(allDevices); } - devices.add(device); } - return devices; + paginationResult.setRecordsFiltered(count); + paginationResult.setRecordsTotal(count); + return paginationResult; } @Override - public List getAllDevices(String deviceType) throws DeviceManagementException { - List devices = new ArrayList<>(); - List allDevices; + public Device getDevice(DeviceIdentifier deviceId, boolean requireDeviceInfo) throws DeviceManagementException { + Device device; try { DeviceManagementDAOFactory.openConnection(); - allDevices = deviceDAO.getDevices(deviceType, this.getTenantId()); - if (allDevices == null) { + device = deviceDAO.getDevice(deviceId, this.getTenantId()); + if (device == null) { + String msg = "No device is found upon the type '" + deviceId.getType() + "' and id '" + + deviceId.getId() + "'"; if (log.isDebugEnabled()) { - log.debug("No device is found upon the type '" + deviceType + "'"); + log.debug(msg); } return null; } } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while retrieving all devices of type '" + - deviceType + "' that are being managed within the scope of current tenant", e); + throw new DeviceManagementException("Error occurred while obtaining the device for id " + + "'" + deviceId.getId() + "'", e); } catch (SQLException e) { throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); } finally { DeviceManagementDAOFactory.closeConnection(); } - - for (Device device : allDevices) { - DeviceInfo info = null; - try { - DeviceManagementDAOFactory.openConnection(); - info = deviceInfoDAO.getDeviceInformation(device.getId()); - DeviceLocation location = deviceInfoDAO.getDeviceLocation(device.getId()); - if (info != null) { - info.setLocation(location); - } - } catch (DeviceDetailsMgtDAOException e) { - log.error("Error occurred while retrieving advance info of '" + device.getType() + - "' that carries the id '" + device.getDeviceIdentifier() + "'"); - } catch (SQLException e) { - log.error("Error occurred while opening a connection to the data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - device.setDeviceInfo(info); - - try { - DeviceManagementDAOFactory.openConnection(); - List applications = applicationDAO.getInstalledApplications(device.getId()); - 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); - } catch (SQLException e) { - log.error("Error occurred while opening a connection to the data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - - DeviceManager deviceManager = this.getDeviceManager(deviceType); - if (deviceManager == null) { - if (log.isDebugEnabled()) { - log.debug("Device Manager associated with the device type '" + deviceType + "' is null. " + - "Therefore, not attempting method 'isEnrolled'"); - } - devices.add(device); - continue; - } - Device dmsDevice = - deviceManager.getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); - if (dmsDevice != null) { - device.setFeatures(dmsDevice.getFeatures()); - device.setProperties(dmsDevice.getProperties()); - } - devices.add(device); + if (requireDeviceInfo) { + device = this.getAllDeviceInfo(device); } - return devices; + return device; } @Override @@ -855,57 +685,55 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { + return this.getDevice(deviceId, true); + } + + @Override + public Device getDeviceWithTypeProperties(DeviceIdentifier deviceId) throws DeviceManagementException { Device device; try { DeviceManagementDAOFactory.openConnection(); device = deviceDAO.getDevice(deviceId, this.getTenantId()); if (device == null) { - String msg = "No device is found upon the type '" + deviceId.getType() + "' and id '" + - deviceId.getId() + "'"; if (log.isDebugEnabled()) { - log.debug(msg); + log.debug("No device is found upon the type '" + deviceId.getType() + "' and id '" + + deviceId.getId() + "'"); } return null; } - DeviceInfo info = deviceInfoDAO.getDeviceInformation(device.getId()); - DeviceLocation location = deviceInfoDAO.getDeviceLocation(device.getId()); - if (info != null) { - info.setLocation(location); - } - device.setDeviceInfo(info); - - List applications = applicationDAO.getInstalledApplications(device.getId()); - device.setApplications(applications); } catch (DeviceManagementDAOException e) { throw new DeviceManagementException("Error occurred while obtaining the device for id " + "'" + deviceId.getId() + "'", e); } catch (SQLException e) { throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); - } catch (DeviceDetailsMgtDAOException e) { - throw new DeviceManagementException("Error occurred while fetching advanced device information", e); } finally { DeviceManagementDAOFactory.closeConnection(); } - // The changes made here to prevent unit tests getting failed. They failed because when running the unit - // tests there is no osgi services. So getDeviceManager() returns a null. - DeviceManager deviceManager = this.getDeviceManager(deviceId.getType()); + + DeviceManager deviceManager = this.getDeviceManager(device.getType()); if (deviceManager == null) { if (log.isDebugEnabled()) { - log.debug("Device Manager associated with the device type '" + deviceId.getType() + "' is null. " + - "Therefore, not attempting method 'getDevice'"); + log.debug("Device Manager associated with the device type '" + device.getType() + "' is null. " + + "Therefore, not attempting method 'isEnrolled'"); } return device; } - Device pluginSpecificInfo = deviceManager.getDevice(deviceId); - if (pluginSpecificInfo != null) { - device.setFeatures(pluginSpecificInfo.getFeatures()); - device.setProperties(pluginSpecificInfo.getProperties()); + Device dmsDevice = + deviceManager.getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); + if (dmsDevice != null) { + device.setFeatures(dmsDevice.getFeatures()); + device.setProperties(dmsDevice.getProperties()); } return device; } @Override public Device getDevice(DeviceIdentifier deviceId, Date since) throws DeviceManagementException { + return this.getDevice(deviceId, since, true); + } + + @Override + public Device getDevice(DeviceIdentifier deviceId, Date since, boolean requireDeviceInfo) throws DeviceManagementException { Device device; try { DeviceManagementDAOFactory.openConnection(); @@ -917,43 +745,28 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } return null; } - DeviceLocation location = deviceInfoDAO.getDeviceLocation(device.getId()); - if (device.getDeviceInfo() != null) { - device.getDeviceInfo().setLocation(location); - } - - List applications = applicationDAO.getInstalledApplications(device.getId()); - device.setApplications(applications); } catch (DeviceManagementDAOException e) { throw new DeviceManagementException("Error occurred while obtaining the device for id " + "'" + deviceId.getId() + "'", e); } catch (SQLException e) { throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); - } catch (DeviceDetailsMgtDAOException e) { - throw new DeviceManagementException("Error occurred while fetching advanced device information", e); } finally { DeviceManagementDAOFactory.closeConnection(); } - // The changes made here to prevent unit tests getting failed. They failed because when running the unit - // tests there is no osgi services. So getDeviceManager() returns a null. - DeviceManager deviceManager = this.getDeviceManager(deviceId.getType()); - if (deviceManager == null) { - if (log.isDebugEnabled()) { - log.debug("Device Manager associated with the device type '" + deviceId.getType() + "' is null. " + - "Therefore, not attempting method 'getDevice'"); - } - return device; - } - Device pluginSpecificInfo = deviceManager.getDevice(deviceId); - if (pluginSpecificInfo != null) { - device.setFeatures(pluginSpecificInfo.getFeatures()); - device.setProperties(pluginSpecificInfo.getProperties()); + if (requireDeviceInfo) { + device = this.getAllDeviceInfo(device); } return device; } @Override public Device getDevice(DeviceIdentifier deviceId, EnrolmentInfo.Status status) throws DeviceManagementException { + return this.getDevice(deviceId, status, true); + } + + @Override + public Device getDevice(DeviceIdentifier deviceId, EnrolmentInfo.Status status, boolean requireDeviceInfo) + throws DeviceManagementException { Device device; try { DeviceManagementDAOFactory.openConnection(); @@ -965,41 +778,18 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } return null; } - DeviceInfo info = deviceInfoDAO.getDeviceInformation(device.getId()); - DeviceLocation location = deviceInfoDAO.getDeviceLocation(device.getId()); - if (info != null) { - info.setLocation(location); - } - device.setDeviceInfo(info); - - List applications = applicationDAO.getInstalledApplications(device.getId()); - device.setApplications(applications); } catch (DeviceManagementDAOException e) { throw new DeviceManagementException("Error occurred while obtaining the device for id " + "'" + deviceId.getId() + "'", e); } catch (SQLException e) { throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); - } catch (DeviceDetailsMgtDAOException e) { - throw new DeviceManagementException("Error occurred while obtaining information of the device with id " + - "'" + deviceId.getId() + "'", e); } finally { DeviceManagementDAOFactory.closeConnection(); } - // The changes made here to prevent unit tests getting failed. They failed because when running the unit - // tests there is no osgi services. So getDeviceManager() returns a null. - DeviceManager deviceManager = this.getDeviceManager(deviceId.getType()); - if (deviceManager == null) { - if (log.isDebugEnabled()) { - log.debug("Device Manager associated with the device type '" + deviceId.getType() + "' is null. " + - "Therefore, not attempting method 'getDevice'"); - } - return device; - } - Device pluginSpecificInfo = deviceManager.getDevice(deviceId); - if (pluginSpecificInfo != null) { - device.setFeatures(pluginSpecificInfo.getFeatures()); - device.setProperties(pluginSpecificInfo.getProperties()); + if (requireDeviceInfo) { + device = this.getAllDeviceInfo(device); } + return device; } @@ -1288,7 +1078,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getDevicesOfUser(String username) throws DeviceManagementException { - List devices = new ArrayList<>(); + return this.getDevicesOfUser(username, true); + } + + @Override + public List getDevicesOfUser(String username, boolean requireDeviceInfo) throws DeviceManagementException { List userDevices; try { DeviceManagementDAOFactory.openConnection(); @@ -1302,61 +1096,20 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.closeConnection(); } - for (Device device : userDevices) { - DeviceInfo info = null; - try { - DeviceManagementDAOFactory.openConnection(); - info = deviceInfoDAO.getDeviceInformation(device.getId()); - DeviceLocation location = deviceInfoDAO.getDeviceLocation(device.getId()); - if (info != null) { - info.setLocation(location); - } - } catch (DeviceDetailsMgtDAOException e) { - log.error("Error occurred while retrieving advance info of '" + device.getType() + - "' that carries the id '" + device.getDeviceIdentifier() + "'"); - } catch (SQLException e) { - log.error("Error occurred while opening a connection to the data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - device.setDeviceInfo(info); - - try { - DeviceManagementDAOFactory.openConnection(); - List applications = applicationDAO.getInstalledApplications(device.getId()); - 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); - } catch (SQLException e) { - log.error("Error occurred while opening a connection to the data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - - DeviceManager deviceManager = this.getDeviceManager(device.getType()); - if (deviceManager == null) { - if (log.isDebugEnabled()) { - log.debug("Device Manager associated with the device type '" + device.getType() + "' is null. " + - "Therefore, not attempting method 'isEnrolled'"); - } - devices.add(device); - continue; - } - Device dmsDevice = - deviceManager.getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); - if (dmsDevice != null) { - device.setFeatures(dmsDevice.getFeatures()); - device.setProperties(dmsDevice.getProperties()); - } - devices.add(device); + if (requireDeviceInfo) { + return this.getAllDeviceInfo(userDevices); } - return devices; + return userDevices; } @Override public List getDevicesOfUser(String username, String deviceType) throws DeviceManagementException { - List devices = new ArrayList<>(); + return this.getDevicesOfUser(username, deviceType, true); + } + + @Override + public List getDevicesOfUser(String username, String deviceType, boolean requireDeviceInfo) throws + DeviceManagementException { List userDevices; try { DeviceManagementDAOFactory.openConnection(); @@ -1370,66 +1123,24 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.closeConnection(); } - for (Device device : userDevices) { - DeviceInfo info = null; - try { - DeviceManagementDAOFactory.openConnection(); - info = deviceInfoDAO.getDeviceInformation(device.getId()); - DeviceLocation location = deviceInfoDAO.getDeviceLocation(device.getId()); - if (info != null) { - info.setLocation(location); - } - } catch (DeviceDetailsMgtDAOException e) { - log.error("Error occurred while retrieving advance info of '" + device.getType() + - "' that carries the id '" + device.getDeviceIdentifier() + "'"); - } catch (SQLException e) { - log.error("Error occurred while opening a connection to the data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - device.setDeviceInfo(info); - - try { - DeviceManagementDAOFactory.openConnection(); - List applications = applicationDAO.getInstalledApplications(device.getId()); - 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); - } catch (SQLException e) { - log.error("Error occurred while opening a connection to the data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - - DeviceManager deviceManager = this.getDeviceManager(device.getType()); - if (deviceManager == null) { - if (log.isDebugEnabled()) { - log.debug("Device Manager associated with the device type '" + device.getType() + "' is null. " + - "Therefore, not attempting method 'isEnrolled'"); - } - devices.add(device); - continue; - } - Device dmsDevice = - deviceManager.getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); - if (dmsDevice != null) { - device.setFeatures(dmsDevice.getFeatures()); - device.setProperties(dmsDevice.getProperties()); - } - devices.add(device); + if (requireDeviceInfo) { + return this.getAllDeviceInfo(userDevices); } - return devices; + return userDevices; } @Override - public PaginationResult getDevicesOfUser(PaginationRequest request) + public PaginationResult getDevicesOfUser(PaginationRequest request) throws DeviceManagementException { + return this.getDevicesOfUser(request, true); + } + + @Override + public PaginationResult getDevicesOfUser(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException { PaginationResult result = new PaginationResult(); int deviceCount = 0; int tenantId = this.getTenantId(); String username = request.getOwner(); - List devices = new ArrayList<>(); List userDevices = new ArrayList<>(); request = DeviceManagerUtil.validateDeviceListPageSize(request); try { @@ -1445,56 +1156,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.closeConnection(); } - for (Device device : userDevices) { - DeviceInfo info = null; - try { - DeviceManagementDAOFactory.openConnection(); - info = deviceInfoDAO.getDeviceInformation(device.getId()); - DeviceLocation location = deviceInfoDAO.getDeviceLocation(device.getId()); - if (info != null) { - info.setLocation(location); - } - } catch (DeviceDetailsMgtDAOException e) { - log.error("Error occurred while retrieving advance info of '" + device.getType() + - "' that carries the id '" + device.getDeviceIdentifier() + "'"); - } catch (SQLException e) { - log.error("Error occurred while opening a connection to the data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - device.setDeviceInfo(info); - - try { - DeviceManagementDAOFactory.openConnection(); - List applications = applicationDAO.getInstalledApplications(device.getId()); - 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); - } catch (SQLException e) { - log.error("Error occurred while opening a connection to the data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - - DeviceManager deviceManager = this.getDeviceManager(device.getType()); - if (deviceManager == null) { - if (log.isDebugEnabled()) { - log.debug("Device Manager associated with the device type '" + device.getType() + "' is null. " + - "Therefore, not attempting method 'isEnrolled'"); - } - devices.add(device); - continue; - } - Device dmsDevice = - deviceManager.getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); - if (dmsDevice != null) { - device.setFeatures(dmsDevice.getFeatures()); - device.setProperties(dmsDevice.getProperties()); - } - devices.add(device); + if (requireDeviceInfo) { + result.setData(this.getAllDeviceInfo(userDevices)); + } else { + result.setData(userDevices); } - result.setData(devices); + result.setRecordsTotal(deviceCount); result.setRecordsFiltered(deviceCount); return result; @@ -1503,8 +1170,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public PaginationResult getDevicesByOwnership(PaginationRequest request) throws DeviceManagementException { + return this.getDevicesByOwnership(request, true); + } + + @Override + public PaginationResult getDevicesByOwnership(PaginationRequest request, boolean requireDeviceInfo) + throws DeviceManagementException { PaginationResult result = new PaginationResult(); - List devices = new ArrayList<>(); List allDevices; int deviceCount = 0; int tenantId = this.getTenantId(); @@ -1522,47 +1194,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } finally { DeviceManagementDAOFactory.closeConnection(); } - for (Device device : allDevices) { - DeviceInfo info = null; - try { - DeviceManagementDAOFactory.openConnection(); - info = deviceInfoDAO.getDeviceInformation(device.getId()); - DeviceLocation location = deviceInfoDAO.getDeviceLocation(device.getId()); - if (info != null) { - info.setLocation(location); - } - } catch (DeviceDetailsMgtDAOException e) { - log.error("Error occurred while retrieving advance info of '" + device.getType() + - "' that carries the id '" + device.getDeviceIdentifier() + "'"); - } catch (SQLException e) { - log.error("Error occurred while opening a connection to the data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - device.setDeviceInfo(info); - - try { - DeviceManagementDAOFactory.openConnection(); - List applications = applicationDAO.getInstalledApplications(device.getId()); - 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); - } catch (SQLException e) { - log.error("Error occurred while opening a connection to the data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - - Device dmsDevice = this.getDeviceManager(device.getType()). - getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); - if (dmsDevice != null) { - device.setFeatures(dmsDevice.getFeatures()); - device.setProperties(dmsDevice.getProperties()); - } - devices.add(device); + if (requireDeviceInfo) { + result.setData(this.getAllDeviceInfo(allDevices)); + } else { + result.setData(allDevices); } - result.setData(devices); + result.setRecordsTotal(deviceCount); result.setRecordsFiltered(deviceCount); return result; @@ -1570,6 +1207,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getAllDevicesOfRole(String role) throws DeviceManagementException { + return this.getAllDevicesOfRole(role, true); + } + + //TODO FIX THIS + @Override + public List getAllDevicesOfRole(String role, boolean requireDeviceInfo) throws DeviceManagementException { List devices = new ArrayList<>(); String[] users; int tenantId = this.getTenantId(); @@ -1592,45 +1235,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } finally { DeviceManagementDAOFactory.closeConnection(); } - for (Device device : userDevices) { - DeviceInfo info = null; - try { - DeviceManagementDAOFactory.openConnection(); - info = deviceInfoDAO.getDeviceInformation(device.getId()); - DeviceLocation location = deviceInfoDAO.getDeviceLocation(device.getId()); - if (info != null) { - info.setLocation(location); - } - } catch (DeviceDetailsMgtDAOException e) { - log.error("Error occurred while retrieving advance info of '" + device.getType() + - "' that carries the id '" + device.getDeviceIdentifier() + "'"); - } catch (SQLException e) { - log.error("Error occurred while opening a connection to the data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - device.setDeviceInfo(info); - - try { - DeviceManagementDAOFactory.openConnection(); - List applications = applicationDAO.getInstalledApplications(device.getId()); - 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); - } catch (SQLException e) { - log.error("Error occurred while opening a connection to the data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - - Device dmsDevice = this.getDeviceManager(device.getType()). - getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); - if (dmsDevice != null) { - device.setFeatures(dmsDevice.getFeatures()); - device.setProperties(dmsDevice.getProperties()); - } - devices.add(device); + if (requireDeviceInfo) { + this.getAllDeviceInfo(userDevices); } } return devices; @@ -1666,70 +1272,40 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public List getDevicesByNameAndType(String deviceName, String type, int offset, int limit) throws DeviceManagementException { + public List getDevicesByNameAndType(PaginationRequest request, boolean requireDeviceInfo) + throws DeviceManagementException { List devices = new ArrayList<>(); List allDevices; - limit = DeviceManagerUtil.validateDeviceListPageSize(limit); + int limit = DeviceManagerUtil.validateDeviceListPageSize(request.getRowCount()); try { DeviceManagementDAOFactory.openConnection(); - allDevices = deviceDAO.getDevicesByNameAndType(deviceName, type, this.getTenantId(), offset, limit); + allDevices = deviceDAO.getDevicesByNameAndType(request.getDeviceName(), request.getDeviceType(), + this.getTenantId(), request.getStartIndex(), limit); } catch (DeviceManagementDAOException e) { throw new DeviceManagementException("Error occurred while fetching the list of devices that matches to '" - + deviceName + "'", e); + + request.getDeviceName() + "'", e); } catch (SQLException e) { throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); } finally { DeviceManagementDAOFactory.closeConnection(); } - for (Device device : allDevices) { - DeviceInfo info = null; - try { - DeviceManagementDAOFactory.openConnection(); - info = deviceInfoDAO.getDeviceInformation(device.getId()); - DeviceLocation location = deviceInfoDAO.getDeviceLocation(device.getId()); - if (info != null) { - info.setLocation(location); - } - } catch (DeviceDetailsMgtDAOException e) { - log.error("Error occurred while retrieving advance info of '" + device.getType() + - "' that carries the id '" + device.getDeviceIdentifier() + "'"); - } catch (SQLException e) { - log.error("Error occurred while opening a connection to the data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - device.setDeviceInfo(info); - try { - DeviceManagementDAOFactory.openConnection(); - List applications = applicationDAO.getInstalledApplications(device.getId()); - 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); - } catch (SQLException e) { - log.error("Error occurred while opening a connection to the data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - - Device dmsDevice = this.getDeviceManager(device.getType()). - getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); - if (dmsDevice != null) { - device.setFeatures(dmsDevice.getFeatures()); - device.setProperties(dmsDevice.getProperties()); - } - devices.add(device); + if (requireDeviceInfo) { + return this.getAllDeviceInfo(allDevices); } - return devices; + return allDevices; } @Override - public PaginationResult getDevicesByName(PaginationRequest request) - throws DeviceManagementException { + public PaginationResult getDevicesByName(PaginationRequest request) throws DeviceManagementException { + return this.getDevicesByName(request, true); + } + + @Override + public PaginationResult getDevicesByName(PaginationRequest request, boolean requireDeviceInfo) throws + DeviceManagementException { PaginationResult result = new PaginationResult(); int tenantId = this.getTenantId(); - List devices = new ArrayList<>(); List allDevices = new ArrayList<>(); String deviceName = request.getDeviceName(); request = DeviceManagerUtil.validateDeviceListPageSize(request); @@ -1747,47 +1323,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } finally { DeviceManagementDAOFactory.closeConnection(); } - for (Device device : allDevices) { - DeviceInfo info = null; - try { - DeviceManagementDAOFactory.openConnection(); - info = deviceInfoDAO.getDeviceInformation(device.getId()); - DeviceLocation location = deviceInfoDAO.getDeviceLocation(device.getId()); - if (info != null) { - info.setLocation(location); - } - } catch (DeviceDetailsMgtDAOException e) { - log.error("Error occurred while retrieving advance info of '" + device.getType() + - "' that carries the id '" + device.getDeviceIdentifier() + "'"); - } catch (SQLException e) { - log.error("Error occurred while opening a connection to the data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - device.setDeviceInfo(info); - - try { - DeviceManagementDAOFactory.openConnection(); - List applications = applicationDAO.getInstalledApplications(device.getId()); - 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); - } catch (SQLException e) { - log.error("Error occurred while opening a connection to the data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - - Device dmsDevice = this.getDeviceManager(device.getType()). - getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); - if (dmsDevice != null) { - device.setFeatures(dmsDevice.getFeatures()); - device.setProperties(dmsDevice.getProperties()); - } - devices.add(device); + if (requireDeviceInfo) { + result.setData(this.getAllDeviceInfo(allDevices)); + } else { + result.setData(allDevices); } - result.setData(devices); return result; } @@ -1830,8 +1370,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } } + @Override public List getDevicesByStatus(EnrolmentInfo.Status status) throws DeviceManagementException { - List devices = new ArrayList<>(); + return this.getDevicesByStatus(status, true); + } + + @Override + public List getDevicesByStatus(EnrolmentInfo.Status status, boolean requireDeviceInfo) throws + DeviceManagementException { List allDevices; try { DeviceManagementDAOFactory.openConnection(); @@ -1844,54 +1390,21 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } finally { DeviceManagementDAOFactory.closeConnection(); } - for (Device device : allDevices) { - DeviceInfo info = null; - try { - DeviceManagementDAOFactory.openConnection(); - info = deviceInfoDAO.getDeviceInformation(device.getId()); - DeviceLocation location = deviceInfoDAO.getDeviceLocation(device.getId()); - if (info != null) { - info.setLocation(location); - } - } catch (DeviceDetailsMgtDAOException e) { - log.error("Error occurred while retrieving advance info of '" + device.getType() + - "' that carries the id '" + device.getDeviceIdentifier() + "'"); - } catch (SQLException e) { - log.error("Error occurred while opening a connection to the data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - device.setDeviceInfo(info); - - try { - DeviceManagementDAOFactory.openConnection(); - List applications = applicationDAO.getInstalledApplications(device.getId()); - 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); - } catch (SQLException e) { - log.error("Error occurred while opening a connection to the data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - - Device dmsDevice = this.getDeviceManager(device.getType()). - getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); - if (dmsDevice != null) { - device.setFeatures(dmsDevice.getFeatures()); - device.setProperties(dmsDevice.getProperties()); - } - devices.add(device); + if (requireDeviceInfo) { + return this.getAllDeviceInfo(allDevices); } - return devices; + return allDevices; } @Override - public PaginationResult getDevicesByStatus(PaginationRequest request) + public PaginationResult getDevicesByStatus(PaginationRequest request) throws DeviceManagementException { + return this.getDevicesByStatus(request, true); + } + + @Override + public PaginationResult getDevicesByStatus(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException { PaginationResult result = new PaginationResult(); - List devices = new ArrayList<>(); List allDevices = new ArrayList<>(); int tenantId = this.getTenantId(); String status = request.getStatus(); @@ -1910,47 +1423,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } finally { DeviceManagementDAOFactory.closeConnection(); } - for (Device device : allDevices) { - DeviceInfo info = null; - try { - DeviceManagementDAOFactory.openConnection(); - info = deviceInfoDAO.getDeviceInformation(device.getId()); - DeviceLocation location = deviceInfoDAO.getDeviceLocation(device.getId()); - if (info != null) { - info.setLocation(location); - } - } catch (DeviceDetailsMgtDAOException e) { - log.error("Error occurred while retrieving advance info of '" + device.getType() + - "' that carries the id '" + device.getDeviceIdentifier() + "'"); - } catch (SQLException e) { - log.error("Error occurred while opening a connection to the data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - device.setDeviceInfo(info); - - try { - DeviceManagementDAOFactory.openConnection(); - List applications = applicationDAO.getInstalledApplications(device.getId()); - 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); - } catch (SQLException e) { - log.error("Error occurred while opening a connection to the data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - - Device dmsDevice = this.getDeviceManager(device.getType()). - getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); - if (dmsDevice != null) { - device.setFeatures(dmsDevice.getFeatures()); - device.setProperties(dmsDevice.getProperties()); - } - devices.add(device); + if (requireDeviceInfo) { + result.setData(this.getAllDeviceInfo(allDevices)); + } else { + result.setData(allDevices); } - result.setData(devices); return result; } @@ -1996,7 +1473,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv public boolean changeDeviceStatus(DeviceIdentifier deviceIdentifier, EnrolmentInfo.Status newStatus) throws DeviceManagementException { boolean isDeviceUpdated = false; - Device device = getDevice(deviceIdentifier); + Device device = getDevice(deviceIdentifier, false); int deviceId = device.getId(); EnrolmentInfo enrolmentInfo = device.getEnrolmentInfo(); enrolmentInfo.setStatus(newStatus); @@ -2148,4 +1625,105 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return defaultGroup; } } + + /** + * Returns all the device-info including location of the given device. + */ + private DeviceInfo getDeviceInfo(Device device) { + DeviceInfo info = null; + try { + DeviceManagementDAOFactory.openConnection(); + info = deviceInfoDAO.getDeviceInformation(device.getId()); + DeviceLocation location = deviceInfoDAO.getDeviceLocation(device.getId()); + if (info != null) { + info.setLocation(location); + } + } catch (DeviceDetailsMgtDAOException e) { + log.error("Error occurred while retrieving advance info of '" + device.getType() + + "' that carries the id '" + device.getDeviceIdentifier() + "'"); + } catch (SQLException e) { + log.error("Error occurred while opening a connection to the data source", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + return info; + } + + /** + * Returns all the installed apps of the given device. + */ + private List getInstalledApplications(Device device) { + List applications = new ArrayList<>(); + try { + DeviceManagementDAOFactory.openConnection(); + applications = applicationDAO.getInstalledApplications(device.getId()); + 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); + } catch (SQLException e) { + log.error("Error occurred while opening a connection to the data source", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + return applications; + } + + /** + * Returns all the available information (device-info, location, applications and plugin-db data) + * of the given device list. + */ + private List getAllDeviceInfo(List allDevices) + throws DeviceManagementException { + List devices = new ArrayList<>(); + if (allDevices != null) { + for (Device device : allDevices) { + device.setDeviceInfo(this.getDeviceInfo(device)); + device.setApplications(this.getInstalledApplications(device)); + + DeviceManager deviceManager = this.getDeviceManager(device.getType()); + if (deviceManager == null) { + if (log.isDebugEnabled()) { + log.debug("Device Manager associated with the device type '" + device.getType() + "' is null. " + + "Therefore, not attempting method 'isEnrolled'"); + } + devices.add(device); + continue; + } + Device dmsDevice = + deviceManager.getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); + if (dmsDevice != null) { + device.setFeatures(dmsDevice.getFeatures()); + device.setProperties(dmsDevice.getProperties()); + } + devices.add(device); + } + } + return devices; + } + + /** + * Returns all the available information (device-info, location, applications and plugin-db data) + * of a given device. + */ + private Device getAllDeviceInfo(Device device) throws DeviceManagementException { + device.setDeviceInfo(this.getDeviceInfo(device)); + device.setApplications(this.getInstalledApplications(device)); + + DeviceManager deviceManager = this.getDeviceManager(device.getType()); + if (deviceManager == null) { + if (log.isDebugEnabled()) { + log.debug("Device Manager associated with the device type '" + device.getType() + "' is null. " + + "Therefore, not attempting method 'isEnrolled'"); + } + return device; + } + Device dmsDevice = + deviceManager.getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); + if (dmsDevice != null) { + device.setFeatures(dmsDevice.getFeatures()); + device.setProperties(dmsDevice.getProperties()); + } + return device; + } } \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java index 44a5995b55..57e232dbb7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java @@ -472,7 +472,8 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); GroupManagementDAOFactory.beginTransaction(); for (DeviceIdentifier deviceIdentifier : deviceIdentifiers) { - device = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getDevice(deviceIdentifier); + device = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider(). + getDevice(deviceIdentifier, false); if (device == null) { throw new DeviceNotFoundException("Device not found for id '" + deviceIdentifier.getId() + "'"); } @@ -504,7 +505,8 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); GroupManagementDAOFactory.beginTransaction(); for (DeviceIdentifier deviceIdentifier : deviceIdentifiers) { - device = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getDevice(deviceIdentifier); + device = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider(). + getDevice(deviceIdentifier, false); if (device == null) { throw new DeviceNotFoundException("Device not found for id '" + deviceIdentifier.getId() + "'"); } @@ -553,7 +555,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid public List getGroups(DeviceIdentifier deviceIdentifier) throws GroupManagementException { DeviceManagementProviderService managementProviderService = new DeviceManagementProviderServiceImpl(); try { - Device device = managementProviderService.getDevice(deviceIdentifier); + Device device = managementProviderService.getDevice(deviceIdentifier, false); GroupManagementDAOFactory.openConnection(); return groupDAO.getGroups(device.getId(), PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerImpl.java index 16fff9ce13..83850b3182 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerImpl.java @@ -90,7 +90,7 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager { List operations; operations = this.getValidOperationNames(); //list operations for each device type - devices = deviceManagementProviderService.getAllDevices(deviceType);//list devices for each type + devices = deviceManagementProviderService.getAllDevices(deviceType, false);//list devices for each type if (!devices.isEmpty()) { for (String str : operations) { CommandOperation operation = new CommandOperation(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java index bad9d7d628..1c224ebd80 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java @@ -431,7 +431,8 @@ public final class DeviceManagerUtil { } public static boolean isValidDeviceIdentifier(DeviceIdentifier deviceIdentifier) throws DeviceManagementException { - Device device = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getDevice(deviceIdentifier); + Device device = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getDevice(deviceIdentifier, + false); if (device == null || device.getDeviceIdentifier() == null || device.getDeviceIdentifier().isEmpty() || device.getEnrolmentInfo() == null) { return false; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/DelegationTask.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/DelegationTask.java index adccce9723..bae2b48623 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/DelegationTask.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/DelegationTask.java @@ -22,7 +22,6 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.ntask.core.Task; import org.wso2.carbon.policy.mgt.common.PolicyManagementException; @@ -64,15 +63,15 @@ public class DelegationTask implements Task { log.debug("Number of device types which policies are changed .......... : " + deviceTypes.size()); } if (!deviceTypes.isEmpty()) { - DeviceManagementProviderService service = PolicyManagementDataHolder.getInstance() - .getDeviceManagementService(); + DeviceManagementProviderService service = PolicyManagementDataHolder.getInstance(). + getDeviceManagementService(); List devices; List toBeNotified; for (String deviceType : deviceTypes) { try { devices = new ArrayList<>(); toBeNotified = new ArrayList<>(); - devices.addAll(service.getAllDevices(deviceType)); + devices.addAll(service.getAllDevices(deviceType, false)); //HashMap deviceIdPolicy = policyManager.getAppliedPolicyIdsDeviceIds(); for (Device device : devices) { // if (deviceIdPolicy.containsKey(device.getId())) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/ComplianceDecisionPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/ComplianceDecisionPointImpl.java index a9add947bf..0f4ca3ad60 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/ComplianceDecisionPointImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/ComplianceDecisionPointImpl.java @@ -54,7 +54,7 @@ public class ComplianceDecisionPointImpl implements ComplianceDecisionPoint { try { DeviceManagementProviderService service = this.getDeviceManagementProviderService(); for (DeviceIdentifier deviceIdentifier : deviceIdentifiers) { - Device device = service.getDevice(deviceIdentifier); + Device device = service.getDevice(deviceIdentifier, false); service.setStatus(deviceIdentifier, device.getEnrolmentInfo().getOwner(), EnrolmentInfo.Status.UNREACHABLE); } @@ -71,7 +71,7 @@ public class ComplianceDecisionPointImpl implements ComplianceDecisionPoint { try { DeviceManagementProviderService service = this.getDeviceManagementProviderService(); for (DeviceIdentifier deviceIdentifier : deviceIdentifiers) { - Device device = service.getDevice(deviceIdentifier); + Device device = service.getDevice(deviceIdentifier, false); service.setStatus(deviceIdentifier, device.getEnrolmentInfo().getOwner(), EnrolmentInfo.Status.INACTIVE); } @@ -106,7 +106,7 @@ public class ComplianceDecisionPointImpl implements ComplianceDecisionPoint { try { DeviceManagementProviderService service = this.getDeviceManagementProviderService(); - Device device = service.getDevice(deviceIdentifier); + Device device = service.getDevice(deviceIdentifier, false); service.setStatus(deviceIdentifier, device.getEnrolmentInfo().getOwner(), EnrolmentInfo.Status.ACTIVE); @@ -213,7 +213,7 @@ public class ComplianceDecisionPointImpl implements ComplianceDecisionPoint { try { DeviceManagementProviderService service = this.getDeviceManagementProviderService(); - Device device = service.getDevice(deviceIdentifier); + Device device = service.getDevice(deviceIdentifier, false); service.setStatus(deviceIdentifier, device.getEnrolmentInfo().getOwner(), EnrolmentInfo.Status.ACTIVE); @@ -231,7 +231,7 @@ public class ComplianceDecisionPointImpl implements ComplianceDecisionPoint { try { DeviceManagementProviderService service = this.getDeviceManagementProviderService(); - Device device = service.getDevice(deviceIdentifier); + Device device = service.getDevice(deviceIdentifier, false); service.setStatus(deviceIdentifier, device.getEnrolmentInfo().getOwner(), EnrolmentInfo.Status.INACTIVE); @@ -248,7 +248,7 @@ public class ComplianceDecisionPointImpl implements ComplianceDecisionPoint { try { DeviceManagementProviderService service = this.getDeviceManagementProviderService(); - Device device = service.getDevice(deviceIdentifier); + Device device = service.getDevice(deviceIdentifier, false); service.setStatus(deviceIdentifier, device.getEnrolmentInfo().getOwner(), EnrolmentInfo.Status.ACTIVE); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java index 9fade918e1..4dd4d3054d 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java @@ -79,7 +79,7 @@ public class MonitoringManagerImpl implements MonitoringManager { DeviceManagementProviderService service = PolicyManagementDataHolder.getInstance().getDeviceManagementService(); PolicyManager manager = PolicyManagementDataHolder.getInstance().getPolicyManager(); - Device device = service.getDevice(deviceIdentifier); + Device device = service.getDevice(deviceIdentifier, false); Policy policy = manager.getAppliedPolicyToDevice(deviceIdentifier); if (policy != null) { PolicyMonitoringManager monitoringService = PolicyManagementDataHolder.getInstance(). @@ -177,7 +177,7 @@ public class MonitoringManagerImpl implements MonitoringManager { try { DeviceManagementProviderService service = PolicyManagementDataHolder.getInstance().getDeviceManagementService(); - Device device = service.getDevice(deviceIdentifier); + Device device = service.getDevice(deviceIdentifier, false); PolicyManagementDAOFactory.openConnection(); NonComplianceData complianceData = monitoringDAO.getCompliance(device.getId(), device.getEnrolmentInfo() .getId()); @@ -207,7 +207,7 @@ public class MonitoringManagerImpl implements MonitoringManager { PolicyManagementDAOFactory.openConnection(); DeviceManagementProviderService service = PolicyManagementDataHolder.getInstance().getDeviceManagementService(); - Device device = service.getDevice(deviceIdentifier); + Device device = service.getDevice(deviceIdentifier, false); complianceData = monitoringDAO.getCompliance(device.getId(), device.getEnrolmentInfo().getId()); List complianceFeatures = monitoringDAO.getNoneComplianceFeatures(complianceData.getId()); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java index b8c0efd98f..5307c5f29f 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java @@ -392,7 +392,7 @@ public class PolicyManagerImpl implements PolicyManager { DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); for (DeviceIdentifier deviceIdentifier : deviceIdentifierList) { try { - Device device = service.getDevice(deviceIdentifier); + Device device = service.getDevice(deviceIdentifier, false); deviceList.add(device); } catch (DeviceManagementException e) { throw new PolicyManagementException("Error occurred while retrieving device information", e); @@ -641,7 +641,7 @@ public class PolicyManagerImpl implements PolicyManager { try { DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); - Device device = service.getDevice(deviceIdentifier); + Device device = service.getDevice(deviceIdentifier, false); PolicyManagementDAOFactory.openConnection(); policyIdList = policyDAO.getPolicyIdsOfDevice(device); @@ -807,7 +807,7 @@ public class PolicyManagerImpl implements PolicyManager { int deviceId = -1; try { DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); - Device device = service.getDevice(deviceIdentifier); + Device device = service.getDevice(deviceIdentifier, false); deviceId = device.getId(); PolicyManagementDAOFactory.beginTransaction(); @@ -879,7 +879,7 @@ public class PolicyManagerImpl implements PolicyManager { int deviceId = -1; try { DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); - Device device = service.getDevice(deviceIdentifier); + Device device = service.getDevice(deviceIdentifier, false); deviceId = device.getId(); PolicyManagementDAOFactory.beginTransaction(); @@ -909,7 +909,7 @@ public class PolicyManagerImpl implements PolicyManager { int deviceId = -1; try { DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); - Device device = service.getDevice(deviceIdentifier); + Device device = service.getDevice(deviceIdentifier, false); deviceId = device.getId(); PolicyManagementDAOFactory.beginTransaction(); @@ -937,7 +937,7 @@ public class PolicyManagerImpl implements PolicyManager { boolean exist; try { DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); - Device device = service.getDevice(deviceIdentifier); + Device device = service.getDevice(deviceIdentifier, false); PolicyManagementDAOFactory.openConnection(); exist = policyDAO.checkPolicyAvailable(device.getId(), device.getEnrolmentInfo().getId()); } catch (PolicyManagerDAOException e) { @@ -958,7 +958,7 @@ public class PolicyManagerImpl implements PolicyManager { public boolean setPolicyApplied(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { try { DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); - Device device = service.getDevice(deviceIdentifier); + Device device = service.getDevice(deviceIdentifier, false); PolicyManagementDAOFactory.openConnection(); policyDAO.setPolicyApplied(device.getId(), device.getEnrolmentInfo().getId()); @@ -996,7 +996,7 @@ public class PolicyManagerImpl implements PolicyManager { DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); Device device; try { - device = service.getDevice(deviceId); + device = service.getDevice(deviceId, false); if (device == null) { if (log.isDebugEnabled()) { log.debug("No device is found upon the device identifier '" + deviceId.getId() + diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java index eb28b9e7d8..a70fb4db75 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java @@ -24,11 +24,9 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager; -import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; -import org.wso2.carbon.device.mgt.core.config.policy.PolicyConfiguration; +import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.PolicyComplianceException; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.ntask.core.Task; -import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.PolicyComplianceException; import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import org.wso2.carbon.policy.mgt.core.mgt.MonitoringManager; @@ -83,7 +81,7 @@ public class MonitoringTask implements Task { PolicyMonitoringManager monitoringService = PolicyManagementDataHolder.getInstance().getDeviceManagementService() .getPolicyMonitoringManager(deviceType); - List devices = deviceManagementProviderService.getAllDevices(deviceType); + List devices = deviceManagementProviderService.getAllDevices(deviceType, false); if (monitoringService != null && !devices.isEmpty()) { List notifiableDevices = new ArrayList<>(); if (log.isDebugEnabled()) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/MonitoringTestCase.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/MonitoringTestCase.java index 889a29415a..a2d8d4b725 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/MonitoringTestCase.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/MonitoringTestCase.java @@ -140,7 +140,7 @@ public class MonitoringTestCase extends BasePolicyManagementDAOTest { MonitoringManager monitoringManager = new MonitoringManagerImpl(); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); - List devices = service.getAllDevices(ANDROID); + List devices = service.getAllDevices(ANDROID, false); // monitoringManager.addMonitoringOperation(devices); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java index 04d4cfdf43..d056f8a1ff 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java @@ -118,7 +118,7 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { if (log.isDebugEnabled()) { log.debug("Printing device taken by calling the service layer with device type."); } - List devices3 = service.getAllDevices("android"); + List devices3 = service.getAllDevices("android", false); log.debug("Device list size ...! " + devices3.size()); for (Device device : devices3) { @@ -437,7 +437,7 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { PolicyManagerService policyManagerService = new PolicyManagerServiceImpl(); List policies = policyManagerService.getPolicies("android"); - List devices = service.getAllDevices("android"); + List devices = service.getAllDevices("android", false); for (Policy policy : policies) { log.debug("Policy Name : " + policy.getPolicyName()); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyEvaluationTestCase.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyEvaluationTestCase.java index e60bb6deaf..03eb400364 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyEvaluationTestCase.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyEvaluationTestCase.java @@ -95,7 +95,7 @@ public class PolicyEvaluationTestCase extends BasePolicyManagementDAOTest { log.debug("Getting effective policy for device started .........."); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); - List devices = service.getAllDevices(ANDROID); + List devices = service.getAllDevices(ANDROID, false); PolicyEvaluationPoint evaluationPoint = PolicyManagementDataHolder.getInstance().getPolicyEvaluationPoint();