From a14980df1c0318fcb85165723f944565b1e1d86c Mon Sep 17 00:00:00 2001 From: Geeth Munasinghe Date: Tue, 9 Dec 2014 14:57:06 +0530 Subject: [PATCH 1/2] Changing the policy interface --- .../policy/mgt/common/spi/PolicyManagerService.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/spi/PolicyManagerService.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/spi/PolicyManagerService.java index 941647a09..063ac757c 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/spi/PolicyManagerService.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/spi/PolicyManagerService.java @@ -31,7 +31,7 @@ public interface PolicyManagerService { /** * This method adds a policy to the platform * @param policy - * @return + * @return primary key (generated key) */ int addPolicy(Policy policy); @@ -41,7 +41,7 @@ public interface PolicyManagerService { * @param deviceId * @param deviceType * @param policy - * @return + * @return primary key (generated key) */ int addPolicyToDevice(String deviceId, String deviceType, Policy policy) throws FeatureManagementException, PolicyManagementException; @@ -50,7 +50,7 @@ public interface PolicyManagerService { * This method adds a policy to device type by the related device type plugins. * @param deviceType * @param policy - * @return + * @return primary key (generated key) */ int addPolicyToDeviceType(String deviceType,Policy policy) throws FeatureManagementException, PolicyManagementException; @@ -59,7 +59,7 @@ public interface PolicyManagerService { * This method adds the policy to specific role. * @param roleName * @param policy - * @return + * @return primary key (generated key) */ int addPolicyToRole(String roleName, Policy policy) throws FeatureManagementException, PolicyManagementException; From 81dd70bb6cf5e9970e3b253b2882b4a0a01bf1f1 Mon Sep 17 00:00:00 2001 From: prabathabey Date: Tue, 9 Dec 2014 16:56:32 +0530 Subject: [PATCH 2/2] cleaning up DAO layer --- .../mgt/common/DeviceManagementConstants.java | 6 --- .../carbon/device/mgt/core/DeviceManager.java | 8 ++-- .../carbon/device/mgt/core/dao/DeviceDAO.java | 2 +- .../device/mgt/core/dao/DeviceTypeDAO.java | 3 ++ .../mgt/core/dao/impl/DeviceDAOImpl.java | 7 ++-- .../mgt/core/dao/impl/DeviceTypeDAOImpl.java | 6 +++ .../dao/util/DeviceManagementDAOUtil.java | 18 +++++++++ .../carbon/device/mgt/core/dto/Device.java | 37 ++++++++++--------- 8 files changed, 56 insertions(+), 31 deletions(-) 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 ee5024a94..9220c079a 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 @@ -17,12 +17,6 @@ package org.wso2.carbon.device.mgt.common; public final class DeviceManagementConstants { - /*DEVICE Manager operations*/ - public static final String ADD_DEVICE_ENTRY = "Add device entry"; - public static final String STANDARD_TRANSACTION_MANAGER_JNDI_NAME = "java:comp/TransactionManager"; - public static final String STANDARD_USER_TRANSACTION_JNDI_NAME = "java:comp/UserTransaction"; - /*Data Source Configurations */ - public static final class DataSourceProperties { private DataSourceProperties() { throw new AssertionError(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManager.java index b196bf802..95d2ef67e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManager.java @@ -1,5 +1,5 @@ /** - * Copyright (c) 2012, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,6 +24,7 @@ import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO; +import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import java.util.List; @@ -52,7 +53,8 @@ public class DeviceManager implements DeviceManagerService { this.getPluginRepository().getDeviceManagementProvider(device.getType()); dms.enrollDevice(device); try { - this.getDeviceDAO().addDevice(device); + this.getDeviceTypeDAO().getDeviceType(); + this.getDeviceDAO().addDevice(DeviceManagementDAOUtil.convertDevice(device)); } catch (DeviceManagementDAOException e) { throw new DeviceManagementException("Error occurred while enrolling the device '" + device.getId() + "'", e); @@ -65,7 +67,7 @@ public class DeviceManager implements DeviceManagerService { this.getPluginRepository().getDeviceManagementProvider(device.getType()); dms.modifyEnrollment(device); try { - this.getDeviceDAO().updateDevice(device); + this.getDeviceDAO().updateDevice(DeviceManagementDAOUtil.convertDevice(device)); } catch (DeviceManagementDAOException e) { throw new DeviceManagementException("Error occurred while modifying the device '" + device.getId() + "'", e); 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 6c17a7d7c..5bf4335db 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 @@ -15,7 +15,7 @@ package org.wso2.carbon.device.mgt.core.dao; -import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.core.dto.Device; import org.wso2.carbon.device.mgt.core.dto.Status; import java.util.List; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java index 668f0db11..1947922f5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java @@ -18,6 +18,7 @@ package org.wso2.carbon.device.mgt.core.dao; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.core.dto.DeviceType; import java.util.List; @@ -33,4 +34,6 @@ public interface DeviceTypeDAO { List getDeviceTypes() throws DeviceManagementDAOException; + DeviceIdentifier getDeviceType() throws DeviceManagementDAOException; + } 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/DeviceDAOImpl.java index ece6a3ea9..94bdc0604 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/DeviceDAOImpl.java @@ -38,6 +38,7 @@ public class DeviceDAOImpl implements DeviceDAO { private DataSource dataSource; private static final Log log = LogFactory.getLog(DeviceDAOImpl.class); + public DeviceDAOImpl(DataSource dataSource) { this.dataSource = dataSource; } @@ -60,13 +61,13 @@ public class DeviceDAOImpl implements DeviceDAO { stmt.setLong(4, new Date().getTime()); stmt.setString(5, device.getOwnerShip()); stmt.setString(6, device.getStatus().toString()); - stmt.setLong(7, device.getDeviceType().getId()); - stmt.setLong(8, device.getDeviceIdentificationId()); + stmt.setString(7, device.getDeviceType()); + stmt.setString(8, device.getDeviceIdentificationId()); stmt.setString(9, device.getOwnerId()); stmt.setInt(10, DeviceManagementDAOUtil.getTenantId()); stmt.executeUpdate(); } catch (SQLException e) { - String msg = "Failed to enroll device '" + device.getName() + "'"; + String msg = "Error occurred while enrolling device '" + device.getName() + "'"; log.error(msg, e); throw new DeviceManagementDAOException(msg, e); } finally { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java index 6a0ee2368..aa1386c86 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java @@ -15,6 +15,7 @@ */ package org.wso2.carbon.device.mgt.core.dao.impl; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; @@ -60,6 +61,11 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO { return null; } + @Override + public DeviceIdentifier getDeviceType() throws DeviceManagementDAOException { + return new DeviceIdentifier(); + } + private Connection getConnection() throws DeviceManagementDAOException { try { return dataSource.getConnection(); 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 354487812..e9fdd98fb 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 @@ -19,6 +19,9 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; +import org.wso2.carbon.device.mgt.core.dto.Device; +import org.wso2.carbon.device.mgt.core.dto.DeviceType; +import org.wso2.carbon.device.mgt.core.dto.Status; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.core.tenant.TenantManager; @@ -99,4 +102,19 @@ public final class DeviceManagementDAOUtil { } } + public static Device convertDevice(org.wso2.carbon.device.mgt.common.Device + device) throws DeviceManagementDAOException { + Device deviceBO = new Device(); + deviceBO.setDescription(device.getDescription()); + deviceBO.setName(device.getName()); + deviceBO.setDateOfEnrolment(device.getDateOfEnrolment()); + deviceBO.setDateOfLastUpdate(device.getDateOfLastUpdate()); + deviceBO.setStatus(Status.valueOf(String.valueOf(device.isStatus()))); + deviceBO.setOwnerId(device.getOwner()); + deviceBO.setOwnerShip(device.getOwnership()); + deviceBO.setTenantId(DeviceManagementDAOUtil.getTenantId()); + deviceBO.setDeviceType(device.getType()); + return deviceBO; + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/Device.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/Device.java index c988968ce..325a4322a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/Device.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/Device.java @@ -19,35 +19,36 @@ package org.wso2.carbon.device.mgt.core.dto; import java.io.Serializable; +import java.util.Date; public class Device implements Serializable { private static final long serialVersionUID = -8101106997837486245L; - private Long id; + private int id; private String description; private String name; - private Long dateOfEnrolment; - private Long dateOfLastUpdate; - private Long deviceIdentificationId; + private Date dateOfEnrolment; + private Date dateOfLastUpdate; + private String deviceIdentificationId; private Status status; private String ownerId; private String ownerShip; - private Long tenantId; - private DeviceType deviceType; + private int tenantId; + private String deviceType; - public DeviceType getDeviceType() { + public String getDeviceType() { return deviceType; } - public void setDeviceType(DeviceType deviceType) { + public void setDeviceType(String deviceType) { this.deviceType = deviceType; } - public Long getId() { + public int getId() { return id; } - public void setId(Long id) { + public void setId(int id) { this.id = id; } @@ -67,27 +68,27 @@ public class Device implements Serializable { this.name = name; } - public Long getDateOfEnrolment() { + public Date getDateOfEnrolment() { return dateOfEnrolment; } - public void setDateOfEnrolment(Long dateOfEnrolment) { + public void setDateOfEnrolment(Date dateOfEnrolment) { this.dateOfEnrolment = dateOfEnrolment; } - public Long getDateOfLastUpdate() { + public Date getDateOfLastUpdate() { return dateOfLastUpdate; } - public void setDateOfLastUpdate(Long dateOfLastUpdate) { + public void setDateOfLastUpdate(Date dateOfLastUpdate) { this.dateOfLastUpdate = dateOfLastUpdate; } - public Long getDeviceIdentificationId() { + public String getDeviceIdentificationId() { return deviceIdentificationId; } - public void setDeviceIdentificationId(Long deviceIdentificationId) { + public void setDeviceIdentificationId(String deviceIdentificationId) { this.deviceIdentificationId = deviceIdentificationId; } @@ -115,11 +116,11 @@ public class Device implements Serializable { this.ownerId = ownerId; } - public Long getTenantId() { + public int getTenantId() { return tenantId; } - public void setTenantId(Long tenantId) { + public void setTenantId(int tenantId) { this.tenantId = tenantId; } }