From 9a68aa92bc11a4e7f11a42922c249dd83935604b Mon Sep 17 00:00:00 2001 From: harshanl Date: Wed, 14 Oct 2015 22:33:36 +0530 Subject: [PATCH 1/8] Removed the use of web-app context in permission.xml of webapps --- .../permission/PermissionConfiguration.java | 10 ++++++++++ .../WebAppDeploymentLifecycleListener.java | 18 ++++++++++++++---- .../mgt/PermissionManagerServiceImpl.java | 9 --------- .../core/permission/mgt/PermissionUtils.java | 7 +++++++ ...DynamicClientWebAppRegistrationManager.java | 3 +-- .../PermissionBasedScopeValidator.java | 3 ++- .../authenticator/OAuthAuthenticator.java | 4 ++-- 7 files changed, 36 insertions(+), 18 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/permission/PermissionConfiguration.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/permission/PermissionConfiguration.java index 482f80b6f7..f974ea5f20 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/permission/PermissionConfiguration.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/permission/PermissionConfiguration.java @@ -31,6 +31,16 @@ import java.util.List; public class PermissionConfiguration { private List permissions; + private String apiVersion; + + public String getApiVersion() { + return apiVersion; + } + + @XmlElement (name = "APIVersion", required = true) + public void setApiVersion(String apiVersion) { + this.apiVersion = apiVersion; + } public List getPermissions() { return permissions; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/permission/lifecycle/WebAppDeploymentLifecycleListener.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/permission/lifecycle/WebAppDeploymentLifecycleListener.java index 557ce64859..ae7b9f709e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/permission/lifecycle/WebAppDeploymentLifecycleListener.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/permission/lifecycle/WebAppDeploymentLifecycleListener.java @@ -24,9 +24,11 @@ import org.apache.catalina.LifecycleListener; import org.apache.catalina.core.StandardContext; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.permission.mgt.Permission; import org.wso2.carbon.device.mgt.common.permission.mgt.PermissionManagementException; import org.wso2.carbon.device.mgt.core.config.permission.PermissionConfiguration; import org.wso2.carbon.device.mgt.core.permission.mgt.PermissionManagerServiceImpl; +import org.wso2.carbon.device.mgt.core.permission.mgt.PermissionUtils; import javax.servlet.ServletContext; import javax.xml.bind.JAXBContext; @@ -34,6 +36,7 @@ import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import java.io.File; import java.io.InputStream; +import java.util.List; /** * This listener class will initiate the permission addition of permissions defined in @@ -50,6 +53,7 @@ public class WebAppDeploymentLifecycleListener implements LifecycleListener { if (Lifecycle.AFTER_START_EVENT.equals(lifecycleEvent.getType())) { StandardContext context = (StandardContext) lifecycleEvent.getLifecycle(); ServletContext servletContext = context.getServletContext(); + String contextPath = servletContext.getContextPath(); try { InputStream permissionStream = servletContext.getResourceAsStream(PERMISSION_CONFIG_PATH); if (permissionStream != null) { @@ -58,10 +62,16 @@ public class WebAppDeploymentLifecycleListener implements LifecycleListener { Unmarshaller unmarshaller = cdmContext.createUnmarshaller(); PermissionConfiguration permissionConfiguration = (PermissionConfiguration) unmarshaller.unmarshal(permissionStream); - if (permissionConfiguration != null && - permissionConfiguration.getPermissions() != null) { - PermissionManagerServiceImpl.getInstance().addPermissions( - permissionConfiguration.getPermissions()); + List permissions = permissionConfiguration.getPermissions(); + String apiVersion = permissionConfiguration.getApiVersion(); + if (permissionConfiguration != null && permissions != null) { + for (Permission permission : permissions) { + // update the permission path to absolute permission path + permission.setPath(PermissionUtils.getAbsolutePermissionPath(permission.getPath())); + permission.setUrl(PermissionUtils.getAbsoluteContextPathOfAPI(contextPath, apiVersion, + permission.getUrl())); + PermissionManagerServiceImpl.getInstance().addPermission(permission); + } } } } catch (JAXBException e) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceImpl.java index bee2ce0646..190b90dbc7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionManagerServiceImpl.java @@ -51,17 +51,8 @@ public class PermissionManagerServiceImpl implements PermissionManagerService { return registryBasedPermissionManager; } - public boolean addPermissions(List permissions) throws PermissionManagementException { - for (Permission permission : permissions) { - this.addPermission(permission); - } - return true; - } - @Override public boolean addPermission(Permission permission) throws PermissionManagementException { - // update the permission path to absolute permission path - permission.setPath(PermissionUtils.getAbsolutePermissionPath(permission.getPath())); // adding a permission to the tree permissionTree.addPermission(permission); return PermissionUtils.putPermission(permission); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionUtils.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionUtils.java index 3ed44c63b1..d70247f683 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionUtils.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/permission/mgt/PermissionUtils.java @@ -59,6 +59,13 @@ public class PermissionUtils { return PermissionUtils.ADMIN_PERMISSION_REGISTRY_PATH + permissionPath; } + public static String getAbsoluteContextPathOfAPI(String contextPath, String version, String url) { + if((version != null) && !version.isEmpty()) { + return contextPath + "/" + version + url; + } + return contextPath + url; + } + public static Permission getPermission(String path) throws PermissionManagementException { try { Resource resource = PermissionUtils.getGovernanceRegistry().get(path); diff --git a/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/DynamicClientWebAppRegistrationManager.java b/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/DynamicClientWebAppRegistrationManager.java index d7456f4f8e..fbb6023251 100644 --- a/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/DynamicClientWebAppRegistrationManager.java +++ b/components/identity-extensions/dynamic-client-registration/org.wso2.carbon.dynamic.client.web.app.registration/src/main/java/org/wso2/carbon/dynamic/client/web/app/registration/DynamicClientWebAppRegistrationManager.java @@ -120,10 +120,9 @@ public class DynamicClientWebAppRegistrationManager { String requiredDynamicClientRegistration, webAppName; ServletContext servletContext; RegistrationProfile registrationProfile; - OAuthAppDetails oAuthAppDetails = null; + OAuthAppDetails oAuthAppDetails; DynamicClientWebAppRegistrationManager dynamicClientWebAppRegistrationManager = DynamicClientWebAppRegistrationManager.getInstance(); - //todo move enumeration to while loop Enumeration enumeration = new IteratorEnumeration(DynamicClientWebAppRegistrationManager. webAppContexts.keySet().iterator()); if (log.isDebugEnabled()) { diff --git a/components/identity-extensions/org.wso2.carbon.device.mgt.oauth.extensions/src/main/java/org/wso2/carbon/device/mgt/oauth/extensions/validators/PermissionBasedScopeValidator.java b/components/identity-extensions/org.wso2.carbon.device.mgt.oauth.extensions/src/main/java/org/wso2/carbon/device/mgt/oauth/extensions/validators/PermissionBasedScopeValidator.java index ba56143668..78eed7d06a 100644 --- a/components/identity-extensions/org.wso2.carbon.device.mgt.oauth.extensions/src/main/java/org/wso2/carbon/device/mgt/oauth/extensions/validators/PermissionBasedScopeValidator.java +++ b/components/identity-extensions/org.wso2.carbon.device.mgt.oauth.extensions/src/main/java/org/wso2/carbon/device/mgt/oauth/extensions/validators/PermissionBasedScopeValidator.java @@ -51,6 +51,7 @@ public class PermissionBasedScopeValidator extends OAuth2ScopeValidator { public static final String WRITE = "write"; public static final String DELETE = "delete"; public static final String ACTION = "action"; + public static final String UI_EXECUTE = "ui.execute"; } private static final Log log = LogFactory.getLog(PermissionBasedScopeValidator.class); @@ -77,7 +78,7 @@ public class PermissionBasedScopeValidator extends OAuth2ScopeValidator { if (userRealm != null && userRealm.getAuthorizationManager() != null) { status = userRealm.getAuthorizationManager() .isUserAuthorized(username, permission.getPath(), - PermissionMethod.READ); + PermissionMethod.UI_EXECUTE); } } } catch (PermissionManagementException e) { diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/OAuthAuthenticator.java b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/OAuthAuthenticator.java index 241e7de9b4..fd63a5efd1 100644 --- a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/OAuthAuthenticator.java +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/OAuthAuthenticator.java @@ -81,8 +81,8 @@ public class OAuthAuthenticator implements WebappAuthenticator { authenticationInfo.setStatus(Status.CONTINUE); } String apiVersion = tokenizer.nextToken(); - String authLevel = authenticator.getResourceAuthenticationScheme(context, apiVersion, requestUri, requestMethod); - //String authLevel = "any"; + //String authLevel = authenticator.getResourceAuthenticationScheme(context, apiVersion, requestUri, requestMethod); + String authLevel = "any"; try { if (Constants.NO_MATCHING_AUTH_SCHEME.equals(authLevel)) { AuthenticationFrameworkUtil.handleNoMatchAuthScheme(request, response, requestMethod, apiVersion, From 6c196d5317775290c5b72a2eeeafaddde9de9150 Mon Sep 17 00:00:00 2001 From: harshanl Date: Fri, 16 Oct 2015 23:40:13 +0530 Subject: [PATCH 2/8] Added pagination support --- .../wso2/carbon/device/mgt/common/Device.java | 2 - .../mgt/common/DeviceManagementConstants.java | 12 ++ .../device/mgt/common/PaginationResult.java | 67 ++++++++++ .../carbon/device/mgt/core/dao/DeviceDAO.java | 36 ++++- .../core/dao/DeviceManagementDAOFactory.java | 29 +++- ...AOImpl.java => AbstractDeviceDAOImpl.java} | 69 +++++----- .../core/dao/impl/GenericDeviceDAOImpl.java | 126 ++++++++++++++++++ .../core/dao/impl/OracleDeviceDAOImpl.java | 122 +++++++++++++++++ .../dao/impl/PostgreSQLDeviceDAOImpl.java | 122 +++++++++++++++++ .../core/dao/impl/SQLServerDeviceDAOImpl.java | 122 +++++++++++++++++ .../dao/util/DeviceManagementDAOUtil.java | 24 ++++ .../internal/SCEPManagerServiceComponent.java | 17 +++ .../DeviceManagementProviderService.java | 4 + .../DeviceManagementProviderServiceImpl.java | 78 +++++++++++ 14 files changed, 793 insertions(+), 37 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/PaginationResult.java rename components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/{DeviceDAOImpl.java => AbstractDeviceDAOImpl.java} (93%) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GenericDeviceDAOImpl.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/OracleDeviceDAOImpl.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/PostgreSQLDeviceDAOImpl.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/SQLServerDeviceDAOImpl.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/Device.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/Device.java index 07842fbc6b..be6f7bf951 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/Device.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/Device.java @@ -17,8 +17,6 @@ */ package org.wso2.carbon.device.mgt.common; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; import java.io.Serializable; import java.util.List; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementConstants.java index 69f95923e8..3b9035ec13 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementConstants.java @@ -78,4 +78,16 @@ public final class DeviceManagementConstants { } public static final String NOTIFICATION_CONFIG_FILE = "notification-messages.xml"; } + + public static final class DataBaseTypes { + private DataBaseTypes() { + throw new AssertionError(); + } + public static final String DB_TYPE_MYSQL = "MySQL"; + public static final String DB_TYPE_ORACLE = "Oracle"; + public static final String DB_TYPE_MSSQL = "MSSQL"; + public static final String DB_TYPE_DB2 = "DB2"; + public static final String DB_TYPE_H2 = "H2"; + public static final String DB_TYPE_POSTGRESQL = "PostgreSQL"; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/PaginationResult.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/PaginationResult.java new file mode 100644 index 0000000000..0125b0fd90 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/PaginationResult.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * you may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.device.mgt.common; + +import java.io.Serializable; +import java.util.List; + +/** + * This class holds necessary data to represent a paginated result. + */ +public class PaginationResult implements Serializable { + + private static final long serialVersionUID = 1998101711L; + private int recordsTotal; + private int recordsFiltered; + private int draw; + private List data; + + public int getRecordsTotal() { + return recordsTotal; + } + + public int getRecordsFiltered() { + return recordsFiltered; + } + + public void setRecordsFiltered(int recordsFiltered) { + this.recordsFiltered = recordsFiltered; + } + + public void setRecordsTotal(int recordsTotal) { + this.recordsTotal = recordsTotal; + + } + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + public int getDraw() { + return draw; + } + + public void setDraw(int draw) { + this.draw = draw; + } +} 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 12c8014d7b..6b593e85c6 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 @@ -22,6 +22,7 @@ import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.EnrolmentInfo.Status; +import org.wso2.carbon.device.mgt.common.PaginationResult; import java.util.HashMap; import java.util.List; @@ -31,6 +32,16 @@ import java.util.List; */ public interface DeviceDAO { + /** + * This method is used to add a device. + * + * @param type device type. + * @param tenantId tenant id. + * @return returns the device count of given type. + * @throws DeviceManagementDAOException + */ + int getDeviceCount(String type, int tenantId) throws DeviceManagementDAOException; + /** * This method is used to add a device. * @@ -100,10 +111,33 @@ public interface DeviceDAO { */ List getDevices(int tenantId) throws DeviceManagementDAOException; + /** + * This method is used to retrieve the devices of a given tenant as a paginated result. + * + * @param index start index of result set. + * @param limit number of records to be returned. + * @param tenantId tenant id. + * @return returns a PaginationResult including the requested data. + * @throws DeviceManagementDAOException + */ + PaginationResult getDevices(int index, int limit, int tenantId) throws DeviceManagementDAOException; + + /** + * This method is used to retrieve the devices of a given tenant and type as a paginated result. + * + * @param type device type. + * @param index start index of result set. + * @param limit number of records to be returned. + * @param tenantId tenant id. + * @return returns a PaginationResult including the requested data. + * @throws DeviceManagementDAOException + */ + PaginationResult getDevices(String type, int index, int limit, int tenantId) throws DeviceManagementDAOException; + /** * This method is used to retrieve all the devices of a given tenant and device type. * - * @param type device type. + * @param type device type. * @param tenantId tenant id. * @return returns list of devices. * @throws DeviceManagementDAOException diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java index 670935f3bb..173688058c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java @@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.core.dao; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.IllegalTransactionStateException; import org.wso2.carbon.device.mgt.common.TransactionManagementException; import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig; @@ -82,11 +83,27 @@ import java.util.List; public class DeviceManagementDAOFactory { private static DataSource dataSource; + private static String databaseEngine; private static final Log log = LogFactory.getLog(DeviceManagementDAOFactory.class); private static ThreadLocal currentConnection = new ThreadLocal(); + public static DeviceDAO getDeviceDAO() { - return new DeviceDAOImpl(); + if(databaseEngine != null) { + switch (databaseEngine) { + case DeviceManagementConstants.DataBaseTypes.DB_TYPE_ORACLE: + return new OracleDeviceDAOImpl(); + case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MSSQL: + return new SQLServerDeviceDAOImpl(); + case DeviceManagementConstants.DataBaseTypes.DB_TYPE_POSTGRESQL: + case DeviceManagementConstants.DataBaseTypes.DB_TYPE_H2: + case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MYSQL: + default: + return new GenericDeviceDAOImpl(); + } + } else { + return new GenericDeviceDAOImpl(); + } } public static DeviceTypeDAO getDeviceTypeDAO() { @@ -107,10 +124,20 @@ public class DeviceManagementDAOFactory { public static void init(DataSourceConfig config) { dataSource = resolveDataSource(config); + try { + databaseEngine = dataSource.getConnection().getMetaData().getDatabaseProductName(); + } catch (SQLException e) { + log.error("Error occurred while retrieving config.datasource connection", e); + } } public static void init(DataSource dtSource) { dataSource = dtSource; + try { + databaseEngine = dataSource.getConnection().getMetaData().getDatabaseProductName(); + } catch (SQLException e) { + log.error("Error occurred while retrieving config.datasource connection", e); + } } public static void beginTransaction() throws TransactionManagementException { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java similarity index 93% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java rename to components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index e38a3e6df6..a2e4f4d12c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -34,7 +34,7 @@ import java.util.Iterator; import java.util.HashMap; import java.util.List; -public class DeviceDAOImpl implements DeviceDAO { +public abstract class AbstractDeviceDAOImpl implements DeviceDAO { @Override public int addDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException { @@ -122,7 +122,7 @@ public class DeviceDAOImpl implements DeviceDAO { stmt.setInt(4, tenantId); rs = stmt.executeQuery(); if (rs.next()) { - device = this.loadDevice(rs); + device = DeviceManagementDAOUtil.loadDevice(rs); } } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while listing devices for type " + @@ -152,7 +152,7 @@ public class DeviceDAOImpl implements DeviceDAO { stmt.setString(2, deviceIdentifier.getId()); rs = stmt.executeQuery(); if (rs.next()) { - device = this.loadDevice(rs); + device = DeviceManagementDAOUtil.loadDevice(rs); deviceHashMap.put(rs.getInt("TENANT_ID"), device); } } catch (SQLException e) { @@ -184,7 +184,7 @@ public class DeviceDAOImpl implements DeviceDAO { stmt.setInt(3, tenantId); rs = stmt.executeQuery(); if (rs.next()) { - device = this.loadDevice(rs); + device = DeviceManagementDAOUtil.loadDevice(rs); } } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while retrieving device for id " + @@ -215,7 +215,7 @@ public class DeviceDAOImpl implements DeviceDAO { rs = stmt.executeQuery(); devices = new ArrayList<>(); while (rs.next()) { - Device device = this.loadDevice(rs); + Device device = DeviceManagementDAOUtil.loadDevice(rs); devices.add(device); } } catch (SQLException e) { @@ -248,7 +248,7 @@ public class DeviceDAOImpl implements DeviceDAO { rs = stmt.executeQuery(); devices = new ArrayList<>(); while (rs.next()) { - Device device = this.loadDevice(rs); + Device device = DeviceManagementDAOUtil.loadDevice(rs); devices.add(device); } } catch (SQLException e) { @@ -279,7 +279,7 @@ public class DeviceDAOImpl implements DeviceDAO { ResultSet rs = stmt.executeQuery(); while (rs.next()) { - Device device = this.loadDevice(rs); + Device device = DeviceManagementDAOUtil.loadDevice(rs); devices.add(device); } } catch (SQLException e) { @@ -324,6 +324,31 @@ public class DeviceDAOImpl implements DeviceDAO { return deviceCount; } + @Override + public int getDeviceCount(String type, int tenantId) throws DeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + int deviceCount = 0; + try { + conn = this.getConnection(); + String sql = "SELECT COUNT(d.ID) AS DEVICE_COUNT FROM DM_DEVICE d, (SELECT t.ID AS TYPE_ID FROM DM_DEVICE_TYPE t " + + "WHERE t.NAME = ?) d1 WHERE TYPE_ID = d.DEVICE_TYPE_ID AND d.TENANT_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, type); + stmt.setInt(2, tenantId); + rs = stmt.executeQuery(); + if (rs.next()) { + deviceCount = rs.getInt("DEVICE_COUNT"); + } + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while getting the device count", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + return deviceCount; + } + /** * Get the list of devices that matches with the given device name. * @@ -352,7 +377,7 @@ public class DeviceDAOImpl implements DeviceDAO { ResultSet rs = stmt.executeQuery(); while (rs.next()) { - Device device = this.loadDevice(rs); + Device device = DeviceManagementDAOUtil.loadDevice(rs); devices.add(device); } } catch (SQLException e) { @@ -475,7 +500,7 @@ public class DeviceDAOImpl implements DeviceDAO { stmt.setInt(5, tenantId); rs = stmt.executeQuery(); if (rs.next()) { - enrolmentInfo = this.loadEnrolment(rs); + enrolmentInfo = DeviceManagementDAOUtil.loadEnrolment(rs); } return enrolmentInfo; } catch (SQLException e) { @@ -551,7 +576,7 @@ public class DeviceDAOImpl implements DeviceDAO { stmt.setInt(index, tenantId); rs = stmt.executeQuery(); if (rs.next()) { - enrolments.add(this.loadEnrolment(rs)); + enrolments.add(DeviceManagementDAOUtil.loadEnrolment(rs)); } return enrolments; } catch (SQLException e) { @@ -562,28 +587,6 @@ public class DeviceDAOImpl implements DeviceDAO { } } - private Device loadDevice(ResultSet rs) throws SQLException { - Device device = new Device(); - device.setId(rs.getInt("DEVICE_ID")); - device.setName(rs.getString("DEVICE_NAME")); - device.setDescription(rs.getString("DESCRIPTION")); - device.setType(rs.getString("DEVICE_TYPE")); - device.setDeviceIdentifier(rs.getString("DEVICE_IDENTIFICATION")); - device.setEnrolmentInfo(this.loadEnrolment(rs)); - return device; - } - - private EnrolmentInfo loadEnrolment(ResultSet rs) throws SQLException { - EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); - enrolmentInfo.setId(rs.getInt("ENROLMENT_ID")); - enrolmentInfo.setOwner(rs.getString("OWNER")); - enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.valueOf(rs.getString("OWNERSHIP"))); - enrolmentInfo.setDateOfEnrolment(rs.getTimestamp("DATE_OF_ENROLMENT").getTime()); - enrolmentInfo.setDateOfLastUpdate(rs.getTimestamp("DATE_OF_LAST_UPDATE").getTime()); - enrolmentInfo.setStatus(EnrolmentInfo.Status.valueOf(rs.getString("STATUS"))); - return enrolmentInfo; - } - public List getDevicesByStatus(EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException { Connection conn; @@ -604,7 +607,7 @@ public class DeviceDAOImpl implements DeviceDAO { ResultSet rs = stmt.executeQuery(); while (rs.next()) { - Device device = this.loadDevice(rs); + Device device = DeviceManagementDAOUtil.loadDevice(rs); devices.add(device); } } catch (SQLException e) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GenericDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GenericDeviceDAOImpl.java new file mode 100644 index 0000000000..efd0d39da5 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GenericDeviceDAOImpl.java @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * you may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.device.mgt.core.dao.impl; + +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.EnrolmentInfo; +import org.wso2.carbon.device.mgt.common.PaginationResult; +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.util.DeviceManagementDAOUtil; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +/** + * This class holds the generic implementation of DeviceDAO which can be used to support ANSI db syntax. + */ +public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { + + @Override + public PaginationResult getDevices(int index, int limit, int tenantId) + throws DeviceManagementDAOException { + PaginationResult result = new PaginationResult(); + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + List devices = null; + try { + conn = this.getConnection(); + String sql = "SELECT d1.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 AS DEVICE_ID, " + + "d.DESCRIPTION, d.NAME, 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 = ?) d1 " + + "WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ?,?"; + // String sql = "SELECT * FROM DM_DEVICE WHERE TENANT_ID = ? LIMIT ?,?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, tenantId); + stmt.setInt(2, tenantId); + stmt.setInt(3, index); + stmt.setInt(4, limit); + rs = stmt.executeQuery(); + devices = new ArrayList<>(); + while (rs.next()) { + Device device = DeviceManagementDAOUtil.loadDevice(rs); + devices.add(device); + } + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving information of all " + + "registered devices", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + int count = this.getDeviceCount(tenantId); + result.setData(devices); + result.setRecordsFiltered(count); + result.setRecordsTotal(count); + return result; + } + + @Override + public PaginationResult getDevices(String type, int index, int limit, int tenantId) + throws DeviceManagementDAOException { + PaginationResult result = new PaginationResult(); + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + List devices = null; + try { + conn = this.getConnection(); + 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.DESCRIPTION, " + + "d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " + + "DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " + + "AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ?,?"; + //String sql = "SELECT * FROM DM_DEVICE d, (SELECT t.ID AS TYPE_ID FROM DM_DEVICE_TYPE t WHERE t.NAME = ?)" + + // " d1 WHERE TYPE_ID = d.DEVICE_TYPE_ID AND d.TENANT_ID = ? LIMIT ?,?"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, type); + stmt.setInt(2, tenantId); + stmt.setInt(3, tenantId); + stmt.setInt(4, index); + stmt.setInt(5, limit); + rs = stmt.executeQuery(); + devices = new ArrayList<>(); + while (rs.next()) { + Device device = DeviceManagementDAOUtil.loadDevice(rs); + devices.add(device); + } + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while listing devices for type '" + type + "'", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + int count = this.getDeviceCount(type, tenantId); + result.setData(devices); + result.setRecordsFiltered(count); + result.setRecordsTotal(count); + return result; + } + + private Connection getConnection() throws SQLException { + return DeviceManagementDAOFactory.getConnection(); + } +} \ 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/dao/impl/OracleDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/OracleDeviceDAOImpl.java new file mode 100644 index 0000000000..2524945f59 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/OracleDeviceDAOImpl.java @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * you may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.device.mgt.core.dao.impl; + +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.PaginationResult; +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.util.DeviceManagementDAOUtil; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +/** + * This class holds the generic implementation of DeviceDAO which can be used to support ANSI db syntax. + */ +public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl { + + @Override + public PaginationResult getDevices(int index, int limit, int tenantId) + throws DeviceManagementDAOException { + PaginationResult result = new PaginationResult(); + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + List devices = null; + try { + conn = this.getConnection(); + String sql = "SELECT d1.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 AS DEVICE_ID, " + + "d.DESCRIPTION, d.NAME, 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 = ?) d1 " + + "WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ?,? ROW_NUMBER <= ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, tenantId); + stmt.setInt(2, tenantId); + stmt.setInt(3, index); + stmt.setInt(4, limit); + rs = stmt.executeQuery(); + devices = new ArrayList<>(); + while (rs.next()) { + Device device = DeviceManagementDAOUtil.loadDevice(rs); + devices.add(device); + } + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving information of all " + + "registered devices", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + int count = this.getDeviceCount(tenantId); + result.setData(devices); + result.setRecordsFiltered(count); + result.setRecordsTotal(count); + return result; + } + + @Override + public PaginationResult getDevices(String type, int index, int limit, int tenantId) + throws DeviceManagementDAOException { + PaginationResult result = new PaginationResult(); + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + List devices = null; + try { + conn = this.getConnection(); + 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.DESCRIPTION, " + + "d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " + + "DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " + + "AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ?,?"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, type); + stmt.setInt(2, tenantId); + stmt.setInt(3, tenantId); + stmt.setInt(4, index); + stmt.setInt(5, limit); + rs = stmt.executeQuery(); + devices = new ArrayList<>(); + while (rs.next()) { + Device device = DeviceManagementDAOUtil.loadDevice(rs); + devices.add(device); + } + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while listing devices for type '" + type + "'", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + int count = this.getDeviceCount(type, tenantId); + result.setData(devices); + result.setRecordsFiltered(count); + result.setRecordsTotal(count); + return result; + } + + private Connection getConnection() throws SQLException { + return DeviceManagementDAOFactory.getConnection(); + } +} \ 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/dao/impl/PostgreSQLDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/PostgreSQLDeviceDAOImpl.java new file mode 100644 index 0000000000..5a65c807c2 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/PostgreSQLDeviceDAOImpl.java @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * you may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.device.mgt.core.dao.impl; + +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.PaginationResult; +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.util.DeviceManagementDAOUtil; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +/** + * This class holds the generic implementation of DeviceDAO which can be used to support ANSI db syntax. + */ +public class PostgreSQLDeviceDAOImpl extends AbstractDeviceDAOImpl { + + @Override + public PaginationResult getDevices(int index, int limit, int tenantId) + throws DeviceManagementDAOException { + PaginationResult result = new PaginationResult(); + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + List devices = null; + try { + conn = this.getConnection(); + String sql = "SELECT d1.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 AS DEVICE_ID, " + + "d.DESCRIPTION, d.NAME, 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 = ?) d1 " + + "WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? OFFSET ? LIMIT ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, tenantId); + stmt.setInt(2, tenantId); + stmt.setInt(3, index); + stmt.setInt(4, limit); + rs = stmt.executeQuery(); + devices = new ArrayList<>(); + while (rs.next()) { + Device device = DeviceManagementDAOUtil.loadDevice(rs); + devices.add(device); + } + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving information of all " + + "registered devices", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + int count = this.getDeviceCount(tenantId); + result.setData(devices); + result.setRecordsFiltered(count); + result.setRecordsTotal(count); + return result; + } + + @Override + public PaginationResult getDevices(String type, int index, int limit, int tenantId) + throws DeviceManagementDAOException { + PaginationResult result = new PaginationResult(); + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + List devices = null; + try { + conn = this.getConnection(); + 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.DESCRIPTION, " + + "d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " + + "DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " + + "AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? OFFSET ? LIMIT ?"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, type); + stmt.setInt(2, tenantId); + stmt.setInt(3, tenantId); + stmt.setInt(4, index); + stmt.setInt(5, limit); + rs = stmt.executeQuery(); + devices = new ArrayList<>(); + while (rs.next()) { + Device device = DeviceManagementDAOUtil.loadDevice(rs); + devices.add(device); + } + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while listing devices for type '" + type + "'", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + int count = this.getDeviceCount(type, tenantId); + result.setData(devices); + result.setRecordsFiltered(count); + result.setRecordsTotal(count); + return result; + } + + private Connection getConnection() throws SQLException { + return DeviceManagementDAOFactory.getConnection(); + } +} \ 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/dao/impl/SQLServerDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/SQLServerDeviceDAOImpl.java new file mode 100644 index 0000000000..224c7ee280 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/SQLServerDeviceDAOImpl.java @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * you may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.device.mgt.core.dao.impl; + +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.PaginationResult; +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.util.DeviceManagementDAOUtil; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +/** + * This class holds the generic implementation of DeviceDAO which can be used to support ANSI db syntax. + */ +public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl { + + @Override + public PaginationResult getDevices(int index, int limit, int tenantId) + throws DeviceManagementDAOException { + PaginationResult result = new PaginationResult(); + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + List devices = null; + try { + conn = this.getConnection(); + String sql = "SELECT d1.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 AS DEVICE_ID, " + + "d.DESCRIPTION, d.NAME, 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 = ?) d1 " + + "WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, tenantId); + stmt.setInt(2, tenantId); + stmt.setInt(3, index); + stmt.setInt(4, limit); + rs = stmt.executeQuery(); + devices = new ArrayList<>(); + while (rs.next()) { + Device device = DeviceManagementDAOUtil.loadDevice(rs); + devices.add(device); + } + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving information of all " + + "registered devices", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + int count = this.getDeviceCount(tenantId); + result.setData(devices); + result.setRecordsFiltered(count); + result.setRecordsTotal(count); + return result; + } + + @Override + public PaginationResult getDevices(String type, int index, int limit, int tenantId) + throws DeviceManagementDAOException { + PaginationResult result = new PaginationResult(); + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + List devices = null; + try { + conn = this.getConnection(); + 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.DESCRIPTION, " + + "d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " + + "DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " + + "AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, type); + stmt.setInt(2, tenantId); + stmt.setInt(3, tenantId); + stmt.setInt(4, index); + stmt.setInt(5, limit); + rs = stmt.executeQuery(); + devices = new ArrayList<>(); + while (rs.next()) { + Device device = DeviceManagementDAOUtil.loadDevice(rs); + devices.add(device); + } + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while listing devices for type '" + type + "'", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + int count = this.getDeviceCount(type, tenantId); + result.setData(devices); + result.setRecordsFiltered(count); + result.setRecordsTotal(count); + return result; + } + + private Connection getConnection() throws SQLException { + return DeviceManagementDAOFactory.getConnection(); + } +} \ 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/dao/util/DeviceManagementDAOUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java index 225e27d83a..6e640802eb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java @@ -20,6 +20,8 @@ package org.wso2.carbon.device.mgt.core.dao.util; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.CarbonContext; +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.user.api.UserStoreException; @@ -120,4 +122,26 @@ public final class DeviceManagementDAOUtil { } } + public static Device loadDevice(ResultSet rs) throws SQLException { + Device device = new Device(); + device.setId(rs.getInt("DEVICE_ID")); + device.setName(rs.getString("DEVICE_NAME")); + device.setDescription(rs.getString("DESCRIPTION")); + device.setType(rs.getString("DEVICE_TYPE")); + device.setDeviceIdentifier(rs.getString("DEVICE_IDENTIFICATION")); + device.setEnrolmentInfo(loadEnrolment(rs)); + return device; + } + + public static EnrolmentInfo loadEnrolment(ResultSet rs) throws SQLException { + EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); + enrolmentInfo.setId(rs.getInt("ENROLMENT_ID")); + enrolmentInfo.setOwner(rs.getString("OWNER")); + enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.valueOf(rs.getString("OWNERSHIP"))); + enrolmentInfo.setDateOfEnrolment(rs.getTimestamp("DATE_OF_ENROLMENT").getTime()); + enrolmentInfo.setDateOfLastUpdate(rs.getTimestamp("DATE_OF_LAST_UPDATE").getTime()); + enrolmentInfo.setStatus(EnrolmentInfo.Status.valueOf(rs.getString("STATUS"))); + return enrolmentInfo; + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/SCEPManagerServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/SCEPManagerServiceComponent.java index 9c3b20a7a8..f59c04586a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/SCEPManagerServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/SCEPManagerServiceComponent.java @@ -4,11 +4,18 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.osgi.framework.BundleContext; import org.osgi.service.component.ComponentContext; +import org.wso2.carbon.device.mgt.core.app.mgt.ApplicationManagementProviderService; import org.wso2.carbon.device.mgt.core.scep.SCEPManager; import org.wso2.carbon.device.mgt.core.scep.SCEPManagerImpl; /** * @scr.component name="org.wso2.carbon.device.mgt.core.scep" immediate="true" + * @scr.reference name="app.mgt.service" + * interface="org.wso2.carbon.device.mgt.core.app.mgt.ApplicationManagementProviderService" + * cardinality="1..1" + * policy="dynamic" + * bind="setApplicationManagementProviderService" + * unbind="unsetApplicationManagementProviderService" */ public class SCEPManagerServiceComponent { @@ -40,4 +47,14 @@ public class SCEPManagerServiceComponent { } } + protected void unsetApplicationManagementProviderService(ApplicationManagementProviderService + applicationManagementProviderService) { + //do nothing + } + + protected void setApplicationManagementProviderService(ApplicationManagementProviderService + applicationManagementProviderService) { + //do nothing + } + } 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 bd0223e962..aead0bda52 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 @@ -36,6 +36,10 @@ public interface DeviceManagementProviderService extends OperationManager { List getAllDevices() throws DeviceManagementException; + PaginationResult getAllDevices(String deviceType, int index, int limit) throws DeviceManagementException; + + PaginationResult getAllDevices(int index, int limit) throws DeviceManagementException; + void sendEnrolmentInvitation(EmailMessageProperties config) throws DeviceManagementException; void sendRegistrationEmail(EmailMessageProperties config) 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 494b03da1d..f28c7f8bcf 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 @@ -348,6 +348,84 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return devices; } + @Override + public PaginationResult getAllDevices(String deviceType, int index, int limit) throws DeviceManagementException { + PaginationResult paginationResult; + List devices = new ArrayList<>(); + List allDevices; + try { + DeviceManagementDAOFactory.openConnection(); + paginationResult = deviceDAO.getDevices(deviceType, index, limit, this.getTenantId()); + } 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(); + } + allDevices = (List) paginationResult.getData(); + for (Device device : allDevices) { + 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); + } + paginationResult.setData(devices); + return paginationResult; + } + + @Override + public PaginationResult getAllDevices(int index, int limit) throws DeviceManagementException { + PaginationResult paginationResult; + List devices = new ArrayList<>(); + List allDevices; + try { + DeviceManagementDAOFactory.openConnection(); + paginationResult = deviceDAO.getDevices(index, limit, this.getTenantId()); + } 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(); + } + allDevices = (List) paginationResult.getData(); + for (Device device : allDevices) { + 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); + } + paginationResult.setData(devices); + return paginationResult; + } + @Override public List getAllDevices(String deviceType) throws DeviceManagementException { List devices = new ArrayList<>(); From 00f60c2addd4f93a475623ba0cb8cfe46a3a8640 Mon Sep 17 00:00:00 2001 From: harshanl Date: Sat, 17 Oct 2015 17:11:26 +0530 Subject: [PATCH 3/8] Refactored pom file --- pom.xml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 12b522c5bb..8a207c97a7 100644 --- a/pom.xml +++ b/pom.xml @@ -404,12 +404,12 @@ org.eclipse.osgi org.eclipse.osgi.services - 3.3.100.v20120522-1822 + ${eclipse.equinox.services.version} org.osgi.ut org.eclipse.osgi - 3.3.100.v20120522-1822 + ${eclipse.equinox.services.version} @@ -1437,6 +1437,7 @@ 3.6.100.v20120522-1841 + 3.3.100.v20120522-1822 3.8.1.v20120830-144521 1.2.140.wso2v3 From 81b6770c089b4bb16580d447d74b6a4f028940bc Mon Sep 17 00:00:00 2001 From: harshanl Date: Sun, 18 Oct 2015 01:44:28 +0530 Subject: [PATCH 4/8] Fixed oauth issue with tenants --- .../WebAppDeploymentLifecycleListener.java | 4 +- .../core/dao/impl/GenericDeviceDAOImpl.java | 30 ++++++------ .../pom.xml | 24 ++++++++-- .../mgt/oauth/extensions/OAuthExtUtils.java | 48 +++++++++++++++++++ .../PermissionBasedScopeValidator.java | 11 +++-- 5 files changed, 92 insertions(+), 25 deletions(-) create mode 100644 components/identity-extensions/org.wso2.carbon.device.mgt.oauth.extensions/src/main/java/org/wso2/carbon/device/mgt/oauth/extensions/OAuthExtUtils.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/permission/lifecycle/WebAppDeploymentLifecycleListener.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/permission/lifecycle/WebAppDeploymentLifecycleListener.java index ae7b9f709e..1d004f7e09 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/permission/lifecycle/WebAppDeploymentLifecycleListener.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/permission/lifecycle/WebAppDeploymentLifecycleListener.java @@ -53,7 +53,7 @@ public class WebAppDeploymentLifecycleListener implements LifecycleListener { if (Lifecycle.AFTER_START_EVENT.equals(lifecycleEvent.getType())) { StandardContext context = (StandardContext) lifecycleEvent.getLifecycle(); ServletContext servletContext = context.getServletContext(); - String contextPath = servletContext.getContextPath(); + String contextPath = context.getServletContext().getContextPath(); try { InputStream permissionStream = servletContext.getResourceAsStream(PERMISSION_CONFIG_PATH); if (permissionStream != null) { @@ -77,7 +77,7 @@ public class WebAppDeploymentLifecycleListener implements LifecycleListener { } catch (JAXBException e) { log.error( "Exception occurred while parsing the permission configuration of webapp : " - + servletContext.getContextPath(), e); + + context.getServletContext().getContextPath(), e); } catch (PermissionManagementException e) { log.error("Exception occurred while adding the permissions from webapp : " + servletContext.getContextPath(), e); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GenericDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GenericDeviceDAOImpl.java index efd0d39da5..c824a5bf23 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GenericDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GenericDeviceDAOImpl.java @@ -47,13 +47,13 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { List devices = null; try { conn = this.getConnection(); - String sql = "SELECT d1.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 AS DEVICE_ID, " + - "d.DESCRIPTION, d.NAME, 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 = ?) d1 " + - "WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ?,?"; - // String sql = "SELECT * FROM DM_DEVICE WHERE TENANT_ID = ? LIMIT ?,?"; +// String sql = "SELECT d1.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 AS DEVICE_ID, " + +// "d.DESCRIPTION, d.NAME, 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 = ?) d1 " + +// "WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ?,?"; + String sql = "SELECT * FROM DM_DEVICE WHERE TENANT_ID = ? LIMIT ?,?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); stmt.setInt(2, tenantId); @@ -88,14 +88,14 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { List devices = null; try { conn = this.getConnection(); - 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.DESCRIPTION, " + - "d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " + - "DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " + - "AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ?,?"; - //String sql = "SELECT * FROM DM_DEVICE d, (SELECT t.ID AS TYPE_ID FROM DM_DEVICE_TYPE t WHERE t.NAME = ?)" + - // " d1 WHERE TYPE_ID = d.DEVICE_TYPE_ID AND d.TENANT_ID = ? LIMIT ?,?"; +// 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.DESCRIPTION, " + +// "d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " + +// "DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " + +// "AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ?,?"; + String sql = "SELECT * FROM DM_DEVICE d, (SELECT t.ID AS TYPE_ID FROM DM_DEVICE_TYPE t WHERE t.NAME = ?)" + + " d1 WHERE TYPE_ID = d.DEVICE_TYPE_ID AND d.TENANT_ID = ? LIMIT ?,?"; stmt = conn.prepareStatement(sql); stmt.setString(1, type); stmt.setInt(2, tenantId); diff --git a/components/identity-extensions/org.wso2.carbon.device.mgt.oauth.extensions/pom.xml b/components/identity-extensions/org.wso2.carbon.device.mgt.oauth.extensions/pom.xml index e3547c731d..275f92c61a 100644 --- a/components/identity-extensions/org.wso2.carbon.device.mgt.oauth.extensions/pom.xml +++ b/components/identity-extensions/org.wso2.carbon.device.mgt.oauth.extensions/pom.xml @@ -17,7 +17,8 @@ ~ under the License. --> - + org.wso2.carbon.devicemgt @@ -72,10 +73,25 @@ org.wso2.carbon.device.mgt.oauth.extensions.internal !org.wso2.carbon.device.mgt.oauth.extensions.internal, - org.wso2.carbon.device.mgt.oauth.extensions.handlers.*, - org.wso2.carbon.device.mgt.oauth.extensions.validators.* + org.wso2.carbon.device.mgt.oauth.extensions.* - * + + javax.security.auth.*, + org.apache.commons.logging, + org.osgi.service.component, + org.wso2.carbon.device.mgt.common.permission.mgt, + org.wso2.carbon.device.mgt.oauth.extensions.*, + org.wso2.carbon.device.mgt.* + org.wso2.carbon.identity.application.common.model, + org.wso2.carbon.identity.oauth.callback, + org.wso2.carbon.identity.oauth2, + org.wso2.carbon.identity.oauth2.model, + org.wso2.carbon.identity.oauth2.validators, + org.wso2.carbon.user.api, + org.wso2.carbon.user.core.service, + org.wso2.carbon.identity.application.common.model, + org.wso2.carbon.user.core.tenant + diff --git a/components/identity-extensions/org.wso2.carbon.device.mgt.oauth.extensions/src/main/java/org/wso2/carbon/device/mgt/oauth/extensions/OAuthExtUtils.java b/components/identity-extensions/org.wso2.carbon.device.mgt.oauth.extensions/src/main/java/org/wso2/carbon/device/mgt/oauth/extensions/OAuthExtUtils.java new file mode 100644 index 0000000000..aa5a73b140 --- /dev/null +++ b/components/identity-extensions/org.wso2.carbon.device.mgt.oauth.extensions/src/main/java/org/wso2/carbon/device/mgt/oauth/extensions/OAuthExtUtils.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * you may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.device.mgt.oauth.extensions; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.oauth.extensions.internal.OAuthExtensionsDataHolder; +import org.wso2.carbon.user.api.TenantManager; +import org.wso2.carbon.user.api.UserStoreException; + +/** + * This class holds util methods used by OAuth extension bundle. + */ +public class OAuthExtUtils { + + private static final Log log = LogFactory.getLog(OAuthExtUtils.class); + + public static int getTenantId(String tenantDomain) { + int tenantId = 0; + if (tenantDomain != null) { + try { + TenantManager tenantManager = OAuthExtensionsDataHolder.getInstance().getRealmService().getTenantManager(); + tenantId = tenantManager.getTenantId(tenantDomain); + } catch (UserStoreException e) { + String errorMsg = "Error when getting the tenant id from the tenant domain : " + + tenantDomain; + log.error(errorMsg, e); + } + } + return tenantId; + } +} diff --git a/components/identity-extensions/org.wso2.carbon.device.mgt.oauth.extensions/src/main/java/org/wso2/carbon/device/mgt/oauth/extensions/validators/PermissionBasedScopeValidator.java b/components/identity-extensions/org.wso2.carbon.device.mgt.oauth.extensions/src/main/java/org/wso2/carbon/device/mgt/oauth/extensions/validators/PermissionBasedScopeValidator.java index 78eed7d06a..67b944dc16 100644 --- a/components/identity-extensions/org.wso2.carbon.device.mgt.oauth.extensions/src/main/java/org/wso2/carbon/device/mgt/oauth/extensions/validators/PermissionBasedScopeValidator.java +++ b/components/identity-extensions/org.wso2.carbon.device.mgt.oauth.extensions/src/main/java/org/wso2/carbon/device/mgt/oauth/extensions/validators/PermissionBasedScopeValidator.java @@ -20,11 +20,12 @@ package org.wso2.carbon.device.mgt.oauth.extensions.validators; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.device.mgt.common.permission.mgt.Permission; import org.wso2.carbon.device.mgt.common.permission.mgt.PermissionManagementException; import org.wso2.carbon.device.mgt.common.permission.mgt.PermissionManagerService; +import org.wso2.carbon.device.mgt.oauth.extensions.OAuthExtUtils; import org.wso2.carbon.device.mgt.oauth.extensions.internal.OAuthExtensionsDataHolder; +import org.wso2.carbon.identity.application.common.model.User; import org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception; import org.wso2.carbon.identity.oauth2.model.AccessTokenDO; import org.wso2.carbon.identity.oauth2.validators.OAuth2ScopeValidator; @@ -72,9 +73,11 @@ public class PermissionBasedScopeValidator extends OAuth2ScopeValidator { getPermissionManagerService(); try { Permission permission = permissionManagerService.getPermission(properties); - if ((permission != null) && (accessTokenDO.getAuthzUser() != null)) { - String username = accessTokenDO.getAuthzUser().getUserName(); - UserRealm userRealm = CarbonContext.getThreadLocalCarbonContext().getUserRealm(); + User authzUser = accessTokenDO.getAuthzUser(); + if ((permission != null) && (authzUser != null)) { + String username = authzUser.getUserName(); + int tenantId = OAuthExtUtils.getTenantId(authzUser.getTenantDomain()); + UserRealm userRealm = OAuthExtensionsDataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId); if (userRealm != null && userRealm.getAuthorizationManager() != null) { status = userRealm.getAuthorizationManager() .isUserAuthorized(username, permission.getPath(), From 25d7459974ae96ab90909333525bf71b31ae241d Mon Sep 17 00:00:00 2001 From: harshanl Date: Sun, 18 Oct 2015 20:50:39 +0530 Subject: [PATCH 5/8] Pagination changes --- .../core/dao/impl/GenericDeviceDAOImpl.java | 28 +++++++++--------- .../dao/util/DeviceManagementDAOUtil.java | 29 ++++++++++++------- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GenericDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GenericDeviceDAOImpl.java index c824a5bf23..ca9c053c63 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GenericDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GenericDeviceDAOImpl.java @@ -47,13 +47,13 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { List devices = null; try { conn = this.getConnection(); -// String sql = "SELECT d1.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 AS DEVICE_ID, " + -// "d.DESCRIPTION, d.NAME, 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 = ?) d1 " + -// "WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ?,?"; - String sql = "SELECT * FROM DM_DEVICE WHERE TENANT_ID = ? LIMIT ?,?"; + String sql = "SELECT d1.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 AS DEVICE_ID, " + + "d.DESCRIPTION, d.NAME, 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 = ?) d1 " + + "WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ?,?"; +// String sql = "SELECT * FROM DM_DEVICE WHERE TENANT_ID = ? LIMIT ?,?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); stmt.setInt(2, tenantId); @@ -88,13 +88,13 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { List devices = null; try { conn = this.getConnection(); -// 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.DESCRIPTION, " + -// "d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " + -// "DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " + -// "AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ?,?"; - String sql = "SELECT * FROM DM_DEVICE d, (SELECT t.ID AS TYPE_ID FROM DM_DEVICE_TYPE t WHERE t.NAME = ?)" + + 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.DESCRIPTION, " + + "d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " + + "DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " + + "AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ?,?"; +// String sql = "SELECT * FROM DM_DEVICE d, (SELECT t.ID AS TYPE_ID FROM DM_DEVICE_TYPE t WHERE t.NAME = ?)" + " d1 WHERE TYPE_ID = d.DEVICE_TYPE_ID AND d.TENANT_ID = ? LIMIT ?,?"; stmt = conn.prepareStatement(sql); stmt.setString(1, type); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java index 6e640802eb..201265f4d5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java @@ -121,17 +121,16 @@ public final class DeviceManagementDAOUtil { throw new RuntimeException("Error in looking up data source: " + e.getMessage(), e); } } - +/* public static Device loadDevice(ResultSet rs) throws SQLException { - Device device = new Device(); - device.setId(rs.getInt("DEVICE_ID")); - device.setName(rs.getString("DEVICE_NAME")); - device.setDescription(rs.getString("DESCRIPTION")); - device.setType(rs.getString("DEVICE_TYPE")); - device.setDeviceIdentifier(rs.getString("DEVICE_IDENTIFICATION")); - device.setEnrolmentInfo(loadEnrolment(rs)); - return device; - } + Device device = new Device(); + device.setId(rs.getInt("ID")); + device.setName(rs.getString("NAME")); + device.setDescription(rs.getString("DESCRIPTION")); + device.setType(rs.getString("DEVICE_TYPE_ID")); + device.setDeviceIdentifier(rs.getString("DEVICE_IDENTIFICATION")); + return device; + }*/ public static EnrolmentInfo loadEnrolment(ResultSet rs) throws SQLException { EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); @@ -144,4 +143,14 @@ public final class DeviceManagementDAOUtil { return enrolmentInfo; } + public static Device loadDevice(ResultSet rs) throws SQLException { + Device device = new Device(); + device.setId(rs.getInt("DEVICE_ID")); + device.setName(rs.getString("DEVICE_NAME")); + device.setDescription(rs.getString("DESCRIPTION")); + device.setType(rs.getString("DEVICE_TYPE")); + device.setDeviceIdentifier(rs.getString("DEVICE_IDENTIFICATION")); + device.setEnrolmentInfo(loadEnrolment(rs)); + return device; + } } From 89e930d530d275a5772cac4f05d14ae4d265c10a Mon Sep 17 00:00:00 2001 From: harshanl Date: Sun, 18 Oct 2015 21:56:13 +0530 Subject: [PATCH 6/8] Fixed isssues in pagination --- .../carbon/device/mgt/core/dao/impl/GenericDeviceDAOImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GenericDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GenericDeviceDAOImpl.java index ca9c053c63..089d4e09b5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GenericDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GenericDeviceDAOImpl.java @@ -95,7 +95,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { "DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " + "AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ?,?"; // String sql = "SELECT * FROM DM_DEVICE d, (SELECT t.ID AS TYPE_ID FROM DM_DEVICE_TYPE t WHERE t.NAME = ?)" + - " d1 WHERE TYPE_ID = d.DEVICE_TYPE_ID AND d.TENANT_ID = ? LIMIT ?,?"; +// " d1 WHERE TYPE_ID = d.DEVICE_TYPE_ID AND d.TENANT_ID = ? LIMIT ?,?"; stmt = conn.prepareStatement(sql); stmt.setString(1, type); stmt.setInt(2, tenantId); From c46c4f1be9b6f58bc8a56d1e8d1a7844a8a7b026 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Mon, 19 Oct 2015 20:34:02 +0530 Subject: [PATCH 7/8] Fixing the policy update issues --- .../mgt/core/mgt/impl/PolicyManagerImpl.java | 72 ++++++++++++++++--- .../policy/mgt/core/PolicyDAOTestCase.java | 5 ++ 2 files changed, 67 insertions(+), 10 deletions(-) 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 263817802b..e563d8c631 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 @@ -106,7 +106,7 @@ public class PolicyManagerImpl implements PolicyManager { policyDAO.addPolicyCriteriaProperties(policy.getPolicyCriterias()); } - if(policy.isActive()){ + if (policy.isActive()) { policyDAO.activatePolicy(policy.getId()); } PolicyManagementDAOFactory.commitTransaction(); @@ -135,18 +135,52 @@ public class PolicyManagerImpl implements PolicyManager { try { // Previous policy needs to be obtained before begining the transaction - Policy previousPolicy = getPolicy(policy.getId()); + Policy previousPolicy = this.getPolicy(policy.getId()); PolicyManagementDAOFactory.beginTransaction(); // This will keep track of the policies updated. policyDAO.recordUpdatedPolicy(policy); + + List existingFeaturesList = new ArrayList<>(); + List newFeaturesList = new ArrayList<>(); + List temp = new ArrayList<>(); + + List updatedFeatureList = policy.getProfile().getProfileFeaturesList(); + + List existingProfileFeaturesList = previousPolicy.getProfile().getProfileFeaturesList(); + + // Checks for the existing features + for (ProfileFeature feature : updatedFeatureList) { + for (ProfileFeature fe : existingProfileFeaturesList) { + if (feature.getFeatureCode().equalsIgnoreCase(fe.getFeatureCode())) { + existingFeaturesList.add(feature); + temp.add(feature.getFeatureCode()); + } + } + } + + // Checks for the new features + for (ProfileFeature feature : updatedFeatureList) { + if (!temp.contains(feature.getFeatureCode())) { + newFeaturesList.add(feature); + } + } + + int profileId = previousPolicy.getProfile().getProfileId(); + policy.getProfile().setProfileId(profileId); + policy.setProfileId(profileId); + Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); + policy.getProfile().setUpdatedDate(currentTimestamp); + policyDAO.updatePolicy(policy); profileDAO.updateProfile(policy.getProfile()); - featureDAO.updateProfileFeatures(policy.getProfile().getProfileFeaturesList(), policy.getProfile() - .getProfileId()); - policyDAO.deleteAllPolicyRelatedConfigs(policy.getId()); + featureDAO.updateProfileFeatures(existingFeaturesList, profileId); + if (!newFeaturesList.isEmpty()) { + featureDAO.addProfileFeatures(newFeaturesList, profileId); + } + policyDAO.deleteAllPolicyRelatedConfigs(policy.getId()); if (policy.getUsers() != null) { @@ -478,20 +512,24 @@ public class PolicyManagerImpl implements PolicyManager { roleNames = policyDAO.getPolicyAppliedRoles(policyId); userNames = policyDAO.getPolicyAppliedUsers(policyId); - Profile profile = profileDAO.getProfile(policy.getProfileId()); - policy.setProfile(profile); + //Profile profile = profileDAO.getProfile(policy.getProfileId()); + + policy.setRoles(roleNames); policy.setUsers(userNames); } catch (PolicyManagerDAOException e) { throw new PolicyManagementException("Error occurred while getting the policy related to policy ID (" + policyId + ")", e); - } catch (ProfileManagerDAOException e) { - throw new PolicyManagementException("Error occurred while getting the profile related to policy ID (" + - policyId + ")", e); +// } catch (ProfileManagerDAOException e) { +// throw new PolicyManagementException("Error occurred while getting the profile related to policy ID (" + +// policyId + ")", e); } catch (SQLException e) { throw new PolicyManagementException("Error occurred while opening a connection to the data source", e); +// } catch (ProfileManagementException e) { +// throw new PolicyManagementException("Error occurred while getting the profile related to policy ID (" + +// policyId + ")", e); } finally { PolicyManagementDAOFactory.closeConnection(); } @@ -499,6 +537,20 @@ public class PolicyManagerImpl implements PolicyManager { // This is done because connection close in below method too. deviceList = this.getPolicyAppliedDevicesIds(policyId); policy.setDevices(deviceList); + + try { + // PolicyManagementDAOFactory.openConnection(); + Profile profile = profileManager.getProfile(policy.getProfileId()); + policy.setProfile(profile); + } catch (ProfileManagementException e) { + throw new PolicyManagementException("Error occurred while getting the profile related to policy ID (" + + policyId + ")", e); +// } catch (SQLException e) { +// throw new PolicyManagementException("Error occurred while opening a connection to the data source", e); +// } finally { +// PolicyManagementDAOFactory.closeConnection(); + } + return policy; } 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 b17fda051c..931d0b4253 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 @@ -314,6 +314,11 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { users.add("Udara"); users.add("Dileesha"); policy.setUsers(users); + + Profile profile2 = ProfileCreator.getProfile3(FeatureCreator.getFeatureList4()); + + Profile pf = new Profile(); + pap.updatePolicy(policy); pap.activatePolicy(policy.getId()); } From 09fbf9e8a1633dd04316d72570874ab27eff05fe Mon Sep 17 00:00:00 2001 From: harshanl Date: Mon, 19 Oct 2015 22:45:39 +0530 Subject: [PATCH 8/8] Fixed DAO issues --- .../carbon/device/mgt/core/dao/impl/GenericDeviceDAOImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GenericDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GenericDeviceDAOImpl.java index 089d4e09b5..f24b3c761e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GenericDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GenericDeviceDAOImpl.java @@ -94,7 +94,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { "d.NAME, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM DM_DEVICE d, " + "DM_DEVICE_TYPE t WHERE DEVICE_TYPE_ID = t.ID AND t.NAME = ? " + "AND d.TENANT_ID = ?) d1 WHERE d1.ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ?,?"; -// String sql = "SELECT * FROM DM_DEVICE d, (SELECT t.ID AS TYPE_ID FROM DM_DEVICE_TYPE t WHERE t.NAME = ?)" + +// String sql = "SELECT * FROM DM_DEVICE d, (SELECT t.ID AS TYPE_ID FROM DM_DEVICE_TYPE t WHERE t.NAME = ?)" + // " d1 WHERE TYPE_ID = d.DEVICE_TYPE_ID AND d.TENANT_ID = ? LIMIT ?,?"; stmt = conn.prepareStatement(sql); stmt.setString(1, type);