From 374d50988172157dc71d73e0bc20cb631688fe8e Mon Sep 17 00:00:00 2001 From: dilanua Date: Tue, 31 May 2016 18:34:00 +0530 Subject: [PATCH] Updating DeviceManagementAdminServiceImpl --- .../DeviceManagementAdminServiceImpl.java | 4 +- .../mgt/jaxrs/util/DeviceMgtAPIUtils.java | 18 +++++++-- .../carbon/device/mgt/core/dao/DeviceDAO.java | 2 +- .../core/dao/impl/AbstractDeviceDAOImpl.java | 38 +++++++++++++++---- .../DeviceManagementProviderService.java | 2 +- .../DeviceManagementProviderServiceImpl.java | 32 +++++++++------- 6 files changed, 69 insertions(+), 27 deletions(-) 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 7c6d779591..54140ccada 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 @@ -59,8 +59,10 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe } PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(DeviceMgtAPIUtils.getTenantId(tenantDomain)); - List devices = DeviceMgtAPIUtils.getDeviceManagementService().getDevicesByName(name); + List devices = DeviceMgtAPIUtils.getDeviceManagementService(). + getDevicesByNameAndType(name, type, offset, limit); if (devices == null) { return Response.status(Response.Status.NOT_FOUND).entity("No device, which carries the name '" + name + "', is currently enrolled in the system").build(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java index dfb8bc19df..ccafdd0e90 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java @@ -20,14 +20,13 @@ package org.wso2.carbon.device.mgt.jaxrs.util; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.certificate.mgt.core.service.CertificateManagementService; import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.analytics.dashboard.GadgetDataService; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfigurationManagementService; -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry; import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagementService; import org.wso2.carbon.device.mgt.core.app.mgt.ApplicationManagementProviderService; import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManager; @@ -237,4 +236,17 @@ public class DeviceMgtAPIUtils { return gadgetDataService; } + public static int getTenantId(String tenantDomain) throws DeviceManagementException { + RealmService realmService = + (RealmService) PrivilegedCarbonContext.getThreadLocalCarbonContext().getOSGiService(RealmService.class, null); + if (realmService == null) { + throw new IllegalStateException(""); + } + try { + return realmService.getTenantManager().getTenantId(tenantDomain); + } catch (UserStoreException e) { + throw new DeviceManagementException(""); + } + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index 462f403693..a65f40db65 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -259,7 +259,7 @@ public interface DeviceDAO { * @return returns list of devices. * @throws DeviceManagementDAOException */ - List getDevicesByName(String deviceName, int tenantId) throws DeviceManagementDAOException; + List getDevicesByNameAndType(String deviceName, String type, int tenantId, int offset, int limit) throws DeviceManagementDAOException; /** * This method is used to retrieve devices of a given device name as a paginated result. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index 868d070d24..0bc4d83f64 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -628,7 +628,18 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { * @throws DeviceManagementDAOException */ @Override - public List getDevicesByName(String deviceName, int tenantId) throws DeviceManagementDAOException { + public List getDevicesByNameAndType(String deviceName, String type, int tenantId, int offset, int limit) + throws DeviceManagementDAOException { + + String filteringString = ""; + if (deviceName != null && !deviceName.isEmpty()) { + filteringString = filteringString + " AND d.NAME LIKE ?"; + } + + if (type != null && !type.isEmpty()) { + filteringString = filteringString + " AND t.NAME = ?"; + } + Connection conn; PreparedStatement stmt = null; List devices = new ArrayList<>(); @@ -638,13 +649,26 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { String sql = "SELECT d1.ID AS DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, (SELECT d.ID, d.NAME, " + - "d.DESCRIPTION, t.NAME AS DEVICE_TYPE, d.DEVICE_IDENTIFICATION FROM DM_DEVICE d, " + - "DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.NAME LIKE ? AND d.TENANT_ID = ?) d1 " + - "WHERE DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?"; + "d.DESCRIPTION, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " + + "DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?" + filteringString + + ") d1 WHERE d1.ID = e.DEVICE_ID LIMIT ?, ?"; + stmt = conn.prepareStatement(sql); - stmt.setString(1, deviceName + "%"); - stmt.setInt(2, tenantId); - stmt.setInt(3, tenantId); + stmt.setInt(1, tenantId); + + int i = 1; + + if (deviceName != null && !deviceName.isEmpty()) { + stmt.setString(++i, deviceName + "%"); + } + + if (type != null && !type.isEmpty()) { + stmt.setString(++i, type); + } + + stmt.setInt(++i, offset); + stmt.setInt(++i, limit); + rs = stmt.executeQuery(); while (rs.next()) { 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 b07e1ebf51..8396e694ff 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 @@ -139,7 +139,7 @@ public interface DeviceManagementProviderService { * @throws DeviceManagementException If some unusual behaviour is observed while fetching the * device list */ - List getDevicesByName(String deviceName) throws DeviceManagementException; + List getDevicesByNameAndType(String deviceName, String type, int offset, int limit) throws DeviceManagementException; /** * This method is used to retrieve list of devices that matches with the given device name with paging information. 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 5f3cad82f2..75c3cf9971 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 @@ -31,11 +31,7 @@ import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.core.DeviceManagementPluginRepository; -import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; -import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; -import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; -import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO; -import org.wso2.carbon.device.mgt.core.dao.EnrollmentDAO; +import org.wso2.carbon.device.mgt.core.dao.*; import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent; @@ -50,13 +46,7 @@ import org.wso2.carbon.email.sender.core.TypedValue; import org.wso2.carbon.user.api.UserStoreException; import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; public class DeviceManagementProviderServiceImpl implements DeviceManagementProviderService, PluginInitializationListener { @@ -1062,12 +1052,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public List getDevicesByName(String deviceName) throws DeviceManagementException { + public List getDevicesByNameAndType(String deviceName, String type, int offset, int limit) throws DeviceManagementException { List devices = new ArrayList<>(); List allDevices; try { DeviceManagementDAOFactory.openConnection(); - allDevices = deviceDAO.getDevicesByName(deviceName, this.getTenantId()); + allDevices = deviceDAO.getDevicesByNameAndType(deviceName, type, this.getTenantId(), offset, limit); } catch (DeviceManagementDAOException e) { throw new DeviceManagementException("Error occurred while fetching the list of devices that matches to '" + deviceName + "'", e); @@ -1240,7 +1230,21 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return CarbonContext.getThreadLocalCarbonContext().getTenantId(); } +// private int getTenantId(String tenantDomain) throws DeviceManagementException { +// RealmService realmService = +// (RealmService) PrivilegedCarbonContext.getThreadLocalCarbonContext().getOSGiService(RealmService.class, null); +// if (realmService == null) { +// throw new IllegalStateException(""); +// } +// try { +// return realmService.getTenantManager().getTenantId(tenantDomain); +// } catch (UserStoreException e) { +// throw new DeviceManagementException(""); +// } +// } + private DeviceManager getDeviceManager(String deviceType) { + DeviceManagementService deviceManagementService = pluginRepository.getDeviceManagementService(deviceType, this.getTenantId()); if (deviceManagementService == null) {