From db2065435b855df93e2358f5089ffeac4e80640e Mon Sep 17 00:00:00 2001 From: harshanl Date: Sun, 17 May 2015 17:56:44 +0530 Subject: [PATCH] Added getAllDevicesOfUser and getAllDevicesOfRole methods with user feature --- .../org.wso2.carbon.device.mgt.core/pom.xml | 12 ++- .../mgt/core/DeviceManagementRepository.java | 5 + .../DeviceManagementServiceProviderImpl.java | 102 +++++++++++++++++- .../internal/DeviceManagementDataHolder.java | 11 ++ .../DeviceManagementServiceComponent.java | 32 +++++- .../core/service/DeviceManagementService.java | 20 ++++ .../service/DeviceManagementServiceImpl.java | 14 +++ .../DeviceMgtUserServiceComponent.java | 2 +- .../pom.xml | 4 +- .../src/main/resources/build.properties | 0 .../src/main/resources/p2.inf | 0 features/user-mgt/pom.xml | 2 +- 12 files changed, 197 insertions(+), 7 deletions(-) rename features/user-mgt/{org.wso2.carbon.devicemgt.user.server.feature => org.wso2.carbon.device.mgt.user.server.feature}/pom.xml (96%) rename features/user-mgt/{org.wso2.carbon.devicemgt.user.server.feature => org.wso2.carbon.device.mgt.user.server.feature}/src/main/resources/build.properties (100%) rename features/user-mgt/{org.wso2.carbon.devicemgt.user.server.feature => org.wso2.carbon.device.mgt.user.server.feature}/src/main/resources/p2.inf (100%) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml index b69b06d957b..80f79adcc44 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml @@ -82,7 +82,9 @@ org.wso2.carbon.apimgt.impl, org.wso2.carbon.identity.oauth.stub, org.wso2.carbon.identity.oauth.stub.dto, - org.wso2.carbon.ndatasource.core + org.wso2.carbon.ndatasource.core, + org.wso2.carbon.device.mgt.user.core, + org.wso2.carbon.device.mgt.user.common, !org.wso2.carbon.device.mgt.core.internal, @@ -122,6 +124,14 @@ org.wso2.carbon.devicemgt org.wso2.carbon.device.mgt.common + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.user.core + + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.user.common + org.wso2.carbon org.wso2.carbon.logging diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepository.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepository.java index 596d1202a04..7633849ebde 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepository.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepository.java @@ -21,6 +21,7 @@ import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.spi.DeviceMgtService; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; +import java.util.Collection; import java.util.HashMap; import java.util.Map; @@ -52,4 +53,8 @@ public class DeviceManagementRepository { return providers.get(type); } + public Collection getDeviceManagementProviders(){ + return providers.values(); + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java index de39d9fbf35..db05ae66b3c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java @@ -471,4 +471,104 @@ public class DeviceManagementServiceProviderImpl implements DeviceManagementServ throws OperationManagementException { return DeviceManagementDataHolder.getInstance().getOperationManager().getOperationsForStatus(status); } -} + + @Override + public List getAllDevicesOfUser(String userName) + throws DeviceManagementException { + List devicesOfUser = new ArrayList(); + List devicesList; + Device convertedDevice; + DeviceIdentifier deviceIdentifier; + DeviceManager dms; + Device dmsDevice; + org.wso2.carbon.device.mgt.core.dto.Device device; + int tenantId = DeviceManagerUtil.getTenantId(); + //Fetch the DeviceList from Core + try { + devicesList = this.getDeviceDAO().getDeviceListOfUser(userName, tenantId); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementException("Error occurred while obtaining the devices of user '" + + userName + "'", e); + } + + //Fetch the DeviceList from device plugin dbs & append the properties + for (int x = 0; x < devicesList.size(); x++) { + device = devicesList.get(x); + try { + //TODO : Possible improvement if DeviceTypes have been cached + device.setDeviceType(deviceTypeDAO.getDeviceType(device.getDeviceTypeId())); + dms = this.getPluginRepository().getDeviceManagementProvider(device.getDeviceType().getName()); + convertedDevice = DeviceManagementDAOUtil.convertDevice(device, device.getDeviceType()); + deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(device.getDeviceIdentificationId()); + deviceIdentifier.setType(device.getDeviceType().getName()); + dmsDevice = dms.getDevice(deviceIdentifier); + if (dmsDevice != null) { + convertedDevice.setProperties(dmsDevice.getProperties()); + convertedDevice.setFeatures(dmsDevice.getFeatures()); + } + devicesOfUser.add(convertedDevice); + } catch (DeviceManagementDAOException e) { + log.error("Error occurred while obtaining the device type of DeviceTypeId '"+ + device.getDeviceTypeId() + "'",e); + } + } + return devicesOfUser; + } + + @Override + public List getAllDevicesOfRole(String roleName) + throws DeviceManagementException { + List devicesOfRole = new ArrayList(); + List devicesList; + List users; + Device convertedDevice; + DeviceIdentifier deviceIdentifier; + DeviceManager dms; + Device dmsDevice; + org.wso2.carbon.device.mgt.core.dto.Device device; + String userName = ""; + int tenantId = DeviceManagerUtil.getTenantId(); + //Obtaining the list of users of role + try { + users = DeviceManagementDataHolder.getInstance().getUserManager().getUsersForTenantAndRole( + tenantId, roleName); + } catch (org.wso2.carbon.device.mgt.user.common.UserManagementException e) { + throw new DeviceManagementException("Error occurred while obtaining the users of role '" + + roleName + "'", e); + } + + //Obtaining the devices per user + for(org.wso2.carbon.device.mgt.user.common.User user:users){ + try { + userName = user.getUserName(); + devicesList = this.getDeviceDAO().getDeviceListOfUser(userName, tenantId); + for (int x = 0; x < devicesList.size(); x++) { + device = devicesList.get(x); + try { + //TODO : Possible improvement if DeviceTypes have been cached + device.setDeviceType(deviceTypeDAO.getDeviceType(device.getDeviceTypeId())); + dms = this.getPluginRepository().getDeviceManagementProvider(device.getDeviceType().getName()); + convertedDevice = DeviceManagementDAOUtil.convertDevice(device, device.getDeviceType()); + deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(device.getDeviceIdentificationId()); + deviceIdentifier.setType(device.getDeviceType().getName()); + dmsDevice = dms.getDevice(deviceIdentifier); + if (dmsDevice != null) { + convertedDevice.setProperties(dmsDevice.getProperties()); + convertedDevice.setFeatures(dmsDevice.getFeatures()); + } + devicesOfRole.add(convertedDevice); + } catch (DeviceManagementDAOException e) { + log.error("Error occurred while obtaining the device type of DeviceTypeId '"+ + device.getDeviceTypeId() + "'",e); + } + } + } catch (DeviceManagementDAOException e) { + log.error("Error occurred while obtaining the devices of user '" + + userName + "'", e); + } + } + return devicesOfRole; + } +} \ 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/internal/DeviceManagementDataHolder.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java index 30a619ea1e6..3651a3f4d0a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java @@ -29,6 +29,7 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementService; import org.wso2.carbon.registry.core.service.RegistryService; import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.user.core.tenant.TenantManager; +import org.wso2.carbon.device.mgt.user.core.UserManager; public class DeviceManagementDataHolder { @@ -42,6 +43,7 @@ public class DeviceManagementDataHolder { private AppManagerConnector appManager; private AppManagementConfig appManagerConfig; private OperationManager operationManager; + private UserManager userManager; private static DeviceManagementDataHolder thisInstance = new DeviceManagementDataHolder(); @@ -135,4 +137,13 @@ public class DeviceManagementDataHolder { public void setOperationManager(OperationManager operationManager) { this.operationManager = operationManager; } + + + public UserManager getUserManager() { + return userManager; + } + + public void setUserManager(UserManager userManager) { + this.userManager = userManager; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java index 54b279bc45e..c253594710a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java @@ -29,7 +29,6 @@ import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManager; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; -import org.wso2.carbon.device.mgt.common.spi.DeviceManager; import org.wso2.carbon.device.mgt.common.spi.DeviceMgtService; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.DeviceManagementRepository; @@ -55,6 +54,7 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOF import org.wso2.carbon.device.mgt.core.service.DeviceManagementService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementServiceImpl; import org.wso2.carbon.device.mgt.core.util.DeviceManagementSchemaInitializer; +import org.wso2.carbon.device.mgt.user.core.UserManager; import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.ndatasource.core.DataSourceService; import org.wso2.carbon.registry.core.service.RegistryService; @@ -93,6 +93,12 @@ import java.util.List; * policy="dynamic" * bind="setDataSourceService" * unbind="unsetDataSourceService" + * @scr.reference name="org.wso2.carbon.device.mgt.usermanager.service" + * interface="org.wso2.carbon.device.mgt.user.core.UserManager" + * cardinality="1..1" + * policy="dynamic" + * bind="setUserManager" + * unbind="unsetUserManager" */ public class DeviceManagementServiceComponent { @@ -324,6 +330,30 @@ public class DeviceManagementServiceComponent { DeviceManagementDataHolder.getInstance().setRegistryService(null); } + /** + * Sets UserManager Service. + * + * @param userMgtService An instance of UserManager + */ + protected void setUserManager(UserManager userMgtService) { + if (log.isDebugEnabled()) { + log.debug("Setting UserManager Service"); + } + DeviceManagementDataHolder.getInstance().setUserManager(userMgtService); + } + + /** + * Unsets UserManager Service. + * + * @param userMgtService An instance of UserManager + */ + protected void unsetUserManager(UserManager userMgtService) { + if (log.isDebugEnabled()) { + log.debug("Unsetting UserManager Service"); + } + DeviceManagementDataHolder.getInstance().setUserManager(null); + } + private DeviceManagementRepository getPluginRepository() { return pluginRepository; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java index 7a3af5b836b..9a8ad08398c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java @@ -50,4 +50,24 @@ public interface DeviceManagementService extends DeviceManager, LicenseManager, */ Device getCoreDevice(DeviceIdentifier deviceId) throws DeviceManagementException; + /** + * Method to get the list of devices owned by an user. + * + * @param userName Username of the user + * @return List of devices owned by a particular user + * @throws DeviceManagementException If some unusual behaviour is observed while fetching the + * device list + */ + List getAllDevicesOfUser(String userName) throws DeviceManagementException; + + /** + * Method to get the list of devices owned by users of a particular user-role. + * + * @param roleName Role name of the users + * @return List of devices owned by users of a particular role + * @throws DeviceManagementException If some unusual behaviour is observed while fetching the + * device list + */ + List getAllDevicesOfRole(String roleName) throws DeviceManagementException; + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java index 582fbe72145..a0c363854df 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java @@ -189,4 +189,18 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { DeviceManagementDataHolder.getInstance().getDeviceManagementProvider() .sendRegistrationEmail(emailMessageProperties); } + + @Override + public List getAllDevicesOfUser(String userName) + throws DeviceManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider() + .getAllDevicesOfUser(userName); + } + + @Override + public List getAllDevicesOfRole(String roleName) + throws DeviceManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider() + .getAllDevicesOfRole(roleName); + } } \ No newline at end of file diff --git a/components/user-mgt/org.wso2.carbon.device.mgt.user.core/src/main/java/org/wso2/carbon/device/mgt/user/core/internal/DeviceMgtUserServiceComponent.java b/components/user-mgt/org.wso2.carbon.device.mgt.user.core/src/main/java/org/wso2/carbon/device/mgt/user/core/internal/DeviceMgtUserServiceComponent.java index cca59fea573..4e0902269b0 100644 --- a/components/user-mgt/org.wso2.carbon.device.mgt.user.core/src/main/java/org/wso2/carbon/device/mgt/user/core/internal/DeviceMgtUserServiceComponent.java +++ b/components/user-mgt/org.wso2.carbon.device.mgt.user.core/src/main/java/org/wso2/carbon/device/mgt/user/core/internal/DeviceMgtUserServiceComponent.java @@ -55,7 +55,7 @@ public class DeviceMgtUserServiceComponent { } /* Registering User Management service */ BundleContext bundleContext = componentContext.getBundleContext(); - bundleContext.registerService(UserManagementService.class.getName(), + bundleContext.registerService(UserManager.class, new UserManagementService(), null); if (log.isDebugEnabled()) { log.debug("User management core bundle has been successfully initialized"); diff --git a/features/user-mgt/org.wso2.carbon.devicemgt.user.server.feature/pom.xml b/features/user-mgt/org.wso2.carbon.device.mgt.user.server.feature/pom.xml similarity index 96% rename from features/user-mgt/org.wso2.carbon.devicemgt.user.server.feature/pom.xml rename to features/user-mgt/org.wso2.carbon.device.mgt.user.server.feature/pom.xml index ace0db9693a..889c4667c55 100644 --- a/features/user-mgt/org.wso2.carbon.devicemgt.user.server.feature/pom.xml +++ b/features/user-mgt/org.wso2.carbon.device.mgt.user.server.feature/pom.xml @@ -32,7 +32,7 @@ 4.0.0 org.wso2.carbon.devicemgt - org.wso2.carbon.devicemgt.user.server.feature + org.wso2.carbon.device.mgt.user.server.feature pom 0.9.2-SNAPSHOT WSO2 Carbon - User Management Server Feature @@ -91,7 +91,7 @@ p2-feature-gen - org.wso2.carbon.devicemgt.user.server + org.wso2.carbon.device.mgt.user.server ../../../features/etc/feature.properties diff --git a/features/user-mgt/org.wso2.carbon.devicemgt.user.server.feature/src/main/resources/build.properties b/features/user-mgt/org.wso2.carbon.device.mgt.user.server.feature/src/main/resources/build.properties similarity index 100% rename from features/user-mgt/org.wso2.carbon.devicemgt.user.server.feature/src/main/resources/build.properties rename to features/user-mgt/org.wso2.carbon.device.mgt.user.server.feature/src/main/resources/build.properties diff --git a/features/user-mgt/org.wso2.carbon.devicemgt.user.server.feature/src/main/resources/p2.inf b/features/user-mgt/org.wso2.carbon.device.mgt.user.server.feature/src/main/resources/p2.inf similarity index 100% rename from features/user-mgt/org.wso2.carbon.devicemgt.user.server.feature/src/main/resources/p2.inf rename to features/user-mgt/org.wso2.carbon.device.mgt.user.server.feature/src/main/resources/p2.inf diff --git a/features/user-mgt/pom.xml b/features/user-mgt/pom.xml index 1f916bd68aa..2dd255a300b 100644 --- a/features/user-mgt/pom.xml +++ b/features/user-mgt/pom.xml @@ -41,7 +41,7 @@ - org.wso2.carbon.devicemgt.user.server.feature + org.wso2.carbon.device.mgt.user.server.feature \ No newline at end of file