From b8152d8abe30328482cab2a2817fd38e79e4bb91 Mon Sep 17 00:00:00 2001 From: inosh-perera Date: Mon, 22 Dec 2014 12:56:36 +0530 Subject: [PATCH 1/4] chnaged the version of wso2 carbon dependance --- components/device-mgt/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/device-mgt/pom.xml b/components/device-mgt/pom.xml index 7b248638d7..3efbad04e3 100644 --- a/components/device-mgt/pom.xml +++ b/components/device-mgt/pom.xml @@ -92,7 +92,7 @@ org.wso2.carbon org.wso2.carbon.tomcat.ext - 4.3.0-SNAPSHOT + 4.3.0 From f103b73367c13ccc8df45207f4fae83ea1b10fda Mon Sep 17 00:00:00 2001 From: inosh-perera Date: Tue, 6 Jan 2015 18:58:03 +0530 Subject: [PATCH 2/4] adding featureDAO classes and unit tests --- .../pom.xml | 10 + .../device/mgt/mobile/dao/FeatureDAO.java | 51 +++++ .../mgt/mobile/dao/MobileDeviceDAO.java | 3 + .../mgt/mobile/dao/impl/FeatureDAOImpl.java | 187 ++++++++++++++++++ .../mobile/dao/impl/MobileDeviceDAOImpl.java | 35 ++++ .../carbon/device/mgt/mobile/dto/Feature.java | 46 +++++ .../android/AndroidDeviceManagerService.java | 59 ++++-- .../mgt/mobile/impl/common/DBTypes.java | 29 +++ .../impl/common/TestDBConfiguration.java | 90 +++++++++ .../impl/common/TestDBConfigurations.java | 39 ++++ .../mobile/impl/dao/FeatureDAOTestSuite.java | 151 ++++++++++++++ .../src/test/resources/sql/CreateH2TestDB.sql | 24 +++ .../src/test/resources/testdbconfig.xml | 24 +++ .../src/test/resources/testng.xml | 28 +++ 14 files changed, 757 insertions(+), 19 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/FeatureDAO.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/impl/FeatureDAOImpl.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dto/Feature.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/org/wso2/carbon/device/mgt/mobile/impl/common/DBTypes.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/org/wso2/carbon/device/mgt/mobile/impl/common/TestDBConfiguration.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/org/wso2/carbon/device/mgt/mobile/impl/common/TestDBConfigurations.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/org/wso2/carbon/device/mgt/mobile/impl/dao/FeatureDAOTestSuite.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/resources/sql/CreateH2TestDB.sql create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/resources/testdbconfig.xml create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/resources/testng.xml diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/pom.xml index aabc554c47..128317376c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/pom.xml @@ -107,6 +107,16 @@ org.wso2.carbon org.wso2.carbon.apimgt.core + + org.testng + testng + + + commons-dbcp + commons-dbcp + 1.2.2 + test + \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/FeatureDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/FeatureDAO.java new file mode 100644 index 0000000000..489e22f238 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/FeatureDAO.java @@ -0,0 +1,51 @@ +package org.wso2.carbon.device.mgt.mobile.dao; + +import org.wso2.carbon.device.mgt.mobile.dto.Feature; + +import java.util.List; + +/** + * This class represents the key operations associated with persisting feature related + * information. + */ +public interface FeatureDAO { + + /** + * Add a new feature to plugin feature table. + * @param feature Feature object that holds data related to the feature to be inserted. + * @return The status of the operation. If the insert was successful or not. + * @throws MobileDeviceManagementDAOException + */ + boolean addFeature(Feature feature) throws MobileDeviceManagementDAOException; + + /** + * Update a feature in the feature table. + * @param feature Feature object that holds data has to be updated. + * @return The status of the operation. If the update was successful or not. + * @throws MobileDeviceManagementDAOException + */ + boolean updateFeature(Feature feature) throws MobileDeviceManagementDAOException; + + /** + * Delete a given feature from plugin database. + * @param featureCode Feature code of the feature to be deleted. + * @return The status of the operation. If the delete was successful or not. + * @throws MobileDeviceManagementDAOException + */ + boolean deleteDevice(String featureCode) throws MobileDeviceManagementDAOException; + + /** + * Retrieve a given feature from plugin database. + * @param featureCode Feature code of the feature to be retrieved. + * @return Feature object that holds data of the feature represented by featureCode. + * @throws MobileDeviceManagementDAOException + */ + Feature getFeature(String featureCode) throws MobileDeviceManagementDAOException; + + /** + * Retrieve all the features from plugin database. + * @return Feature object list. + * @throws MobileDeviceManagementDAOException + */ + List getAllFeatures() throws MobileDeviceManagementDAOException; +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/MobileDeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/MobileDeviceDAO.java index ecb72d88d5..c1eb2c3339 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/MobileDeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/MobileDeviceDAO.java @@ -17,6 +17,7 @@ package org.wso2.carbon.device.mgt.mobile.dao; import org.wso2.carbon.device.mgt.mobile.dto.MobileDevice; +import java.util.List; /** * This class represents the key operations associated with persisting mobile-device related @@ -32,4 +33,6 @@ public interface MobileDeviceDAO { boolean deleteDevice(String deviceId) throws MobileDeviceManagementDAOException; + List getAllDevices() throws MobileDeviceManagementDAOException; + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/impl/FeatureDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/impl/FeatureDAOImpl.java new file mode 100644 index 0000000000..60bf32fdc0 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/impl/FeatureDAOImpl.java @@ -0,0 +1,187 @@ +package org.wso2.carbon.device.mgt.mobile.dao.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.mobile.dao.FeatureDAO; +import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOException; +import org.wso2.carbon.device.mgt.mobile.dao.util.MobileDeviceManagementDAOUtil; +import org.wso2.carbon.device.mgt.mobile.dto.Feature; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +/** + * Implementation of FeatureDAO + */ +public class FeatureDAOImpl implements FeatureDAO { + + private DataSource dataSource; + private static final Log log = LogFactory.getLog(FeatureDAOImpl.class); + + public FeatureDAOImpl(DataSource dataSource) { + this.dataSource = dataSource; + } + + @Override + public boolean addFeature(Feature feature) throws MobileDeviceManagementDAOException { + boolean status = false; + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String createDBQuery = + "INSERT INTO MBL_FEATURES(CODE, NAME, DESCRIPTION) VALUES (?, ?, ?)"; + + stmt = conn.prepareStatement(createDBQuery); + stmt.setString(1, feature.getCode()); + stmt.setString(2, feature.getName()); + stmt.setString(3, feature.getDescription()); + int rows = stmt.executeUpdate(); + if (rows > 0) { + status = true; + } + } catch (SQLException e) { + String msg = "Error occurred while adding feature code - '" + + feature.getCode() + "'"; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + return status; + } + + @Override + public boolean updateFeature(Feature feature) + throws MobileDeviceManagementDAOException { + boolean status = false; + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String updateDBQuery = + "UPDATE MBL_FEATURES SET CODE = ?, NAME = ?, DESCRIPTION = ? WHERE FEATURE_ID = ?"; + stmt = conn.prepareStatement(updateDBQuery); + stmt.setString(1, feature.getCode()); + stmt.setString(2, feature.getName()); + stmt.setString(3, feature.getDescription()); + stmt.setInt(4, feature.getId()); + int rows = stmt.executeUpdate(); + if (rows > 0) { + status = true; + } + } catch (SQLException e) { + String msg = "Error occurred while updating the feature with feature code - '" + + feature.getId() + "'"; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + return status; + } + + @Override + public boolean deleteDevice(String featureCode) + throws MobileDeviceManagementDAOException { + boolean status = false; + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String deleteDBQuery = + "DELETE FROM MBL_FEATURES WHERE FEATURE_ID = ?"; + stmt = conn.prepareStatement(deleteDBQuery); + stmt.setString(1, featureCode); + int rows = stmt.executeUpdate(); + if(rows>0){ + status = true; + } + } catch (SQLException e) { + String msg = "Error occurred while deleting feature with code - " + featureCode; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + return status; + } + + @Override + public Feature getFeature(String featureCode) + throws MobileDeviceManagementDAOException { + Connection conn = null; + PreparedStatement stmt = null; + Feature feature = null; + try { + conn = this.getConnection(); + String selectDBQuery = + "SELECT FEATURE_ID, CODE, NAME, DESCRIPTION FROM MBL_FEATURE WHERE FEATURE_ID = ?"; + stmt = conn.prepareStatement(selectDBQuery); + stmt.setString(1, featureCode); + ResultSet resultSet = stmt.executeQuery(); + while (resultSet.next()) { + feature = new Feature(); + feature.setId(resultSet.getInt(1)); + feature.setCode(resultSet.getString(2)); + feature.setName(resultSet.getString(3)); + feature.setDescription(resultSet.getString(4)); + break; + } + } catch (SQLException e) { + String msg = "Error occurred while fetching feature code - '" + + featureCode + "'"; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + return feature; + } + + @Override + public List getAllFeatures() throws MobileDeviceManagementDAOException { + Connection conn = null; + PreparedStatement stmt = null; + Feature feature; + List features=new ArrayList(); + try { + conn = this.getConnection(); + String selectDBQuery = + "SELECT FEATURE_ID, CODE, NAME, DESCRIPTION FROM MBL_FEATURE"; + stmt = conn.prepareStatement(selectDBQuery); + ResultSet resultSet = stmt.executeQuery(); + while (resultSet.next()) { + feature = new Feature(); + feature.setId(resultSet.getInt(1)); + feature.setCode(resultSet.getString(2)); + feature.setName(resultSet.getString(3)); + feature.setDescription(resultSet.getString(4)); + features.add(feature); + } + return features; + } catch (SQLException e) { + String msg = "Error occurred while fetching all features.'"; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + } + + private Connection getConnection() throws MobileDeviceManagementDAOException { + try { + return dataSource.getConnection(); + } catch (SQLException e) { + String msg = "Error occurred while obtaining a connection from the mobile device " + + "management metadata repository datasource."; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/impl/MobileDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/impl/MobileDeviceDAOImpl.java index f479d3b128..66c547bdc5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/impl/MobileDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/impl/MobileDeviceDAOImpl.java @@ -28,6 +28,8 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; /** * Implementation of MobileDeviceDAO. @@ -169,6 +171,39 @@ public class MobileDeviceDAOImpl implements MobileDeviceDAO { return status; } + @Override + public List getAllDevices() throws MobileDeviceManagementDAOException { + Connection conn = null; + PreparedStatement stmt = null; + MobileDevice mobileDevice; + List mobileDevices=new ArrayList(); + try { + conn = this.getConnection(); + String selectDBQuery = + "SELECT * FROM MBL_DEVICE"; + stmt = conn.prepareStatement(selectDBQuery); + ResultSet resultSet = stmt.executeQuery(); + while (resultSet.next()) { + mobileDevice = new MobileDevice(); + mobileDevice.setMobileDeviceId(resultSet.getString(1)); + mobileDevice.setRegId(resultSet.getString(2)); + mobileDevice.setImei(resultSet.getString(3)); + mobileDevice.setImsi(resultSet.getString(4)); + mobileDevice.setOsVersion(resultSet.getString(5)); + mobileDevice.setModel(resultSet.getString(6)); + mobileDevice.setVendor(resultSet.getString(7)); + mobileDevices.add(mobileDevice); + } + return mobileDevices; + } catch (SQLException e) { + String msg = "Error occurred while fetching all mobile device data'"; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + } + private Connection getConnection() throws MobileDeviceManagementDAOException { try { return dataSource.getConnection(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dto/Feature.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dto/Feature.java new file mode 100644 index 0000000000..0c10e8eab9 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dto/Feature.java @@ -0,0 +1,46 @@ +package org.wso2.carbon.device.mgt.mobile.dto; + +import java.io.Serializable; + +/** + * DTO of features. + */ +public class Feature implements Serializable { + int id; + String code; + String name; + String description; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidDeviceManagerService.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidDeviceManagerService.java index bf29c70cae..b0feb67222 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidDeviceManagerService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidDeviceManagerService.java @@ -25,6 +25,7 @@ import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.mobile.dto.MobileDevice; import org.wso2.carbon.device.mgt.mobile.util.MobileDeviceManagementUtil; +import java.util.ArrayList; import java.util.List; /** @@ -33,11 +34,11 @@ import java.util.List; public class AndroidDeviceManagerService implements DeviceManagerService { private static final Log log = LogFactory.getLog(AndroidDeviceManagerService.class); - private OperationManager operationManager; + private OperationManager operationManager; - public AndroidDeviceManagerService() { - this.operationManager = new AndroidMobileOperationManager(); - } + public AndroidDeviceManagerService() { + this.operationManager = new AndroidMobileOperationManager(); + } @Override public String getProviderType() { @@ -64,7 +65,8 @@ public class AndroidDeviceManagerService implements DeviceManagerService { boolean status = false; MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device); try { - status = MobileDeviceManagementDAOFactory.getMobileDeviceDAO().updateDevice(mobileDevice); + status = MobileDeviceManagementDAOFactory.getMobileDeviceDAO() + .updateDevice(mobileDevice); } catch (MobileDeviceManagementDAOException e) { String msg = "Error while updating the enrollment of the Android device : " + device.getDeviceIdentifier(); @@ -78,7 +80,8 @@ public class AndroidDeviceManagerService implements DeviceManagerService { public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { boolean status = false; try { - status = MobileDeviceManagementDAOFactory.getMobileDeviceDAO().deleteDevice(deviceId.getId()); + status = MobileDeviceManagementDAOFactory.getMobileDeviceDAO() + .deleteDevice(deviceId.getId()); } catch (MobileDeviceManagementDAOException e) { String msg = "Error while removing the Android device : " + deviceId.getId(); log.error(msg, e); @@ -94,7 +97,7 @@ public class AndroidDeviceManagerService implements DeviceManagerService { MobileDevice mobileDevice = MobileDeviceManagementDAOFactory.getMobileDeviceDAO().getDevice( deviceId.getId()); - if(mobileDevice!=null){ + if (mobileDevice != null) { isEnrolled = true; } } catch (MobileDeviceManagementDAOException e) { @@ -117,11 +120,6 @@ public class AndroidDeviceManagerService implements DeviceManagerService { return true; } - @Override - public List getAllDevices() throws DeviceManagementException { - return null; - } - @Override public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { Device device = null; @@ -143,23 +141,46 @@ public class AndroidDeviceManagerService implements DeviceManagerService { return true; } - @Override + @Override public boolean updateDeviceInfo(Device device) throws DeviceManagementException { boolean status = false; MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device); try { - status = MobileDeviceManagementDAOFactory.getMobileDeviceDAO().updateDevice(mobileDevice); + status = MobileDeviceManagementDAOFactory.getMobileDeviceDAO() + .updateDevice(mobileDevice); } catch (MobileDeviceManagementDAOException e) { - String msg = "Error while updating the Android device : " + device.getDeviceIdentifier(); + String msg = + "Error while updating the Android device : " + device.getDeviceIdentifier(); log.error(msg, e); throw new DeviceManagementException(msg, e); } return status; } - @Override - public OperationManager getOperationManager() throws DeviceManagementException { - return operationManager; - } + @Override + public List getAllDevices() throws DeviceManagementException { + List devices = null; + try { + List mobileDevices = + MobileDeviceManagementDAOFactory.getMobileDeviceDAO(). + getAllDevices(); + if (mobileDevices != null) { + devices = new ArrayList(); + for (int x = 0; x < mobileDevices.size(); x++) { + devices.add(MobileDeviceManagementUtil.convertToDevice(mobileDevices.get(x))); + } + } + } catch (MobileDeviceManagementDAOException e) { + String msg = "Error while fetching all Android devices."; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } + return devices; + } + + @Override + public OperationManager getOperationManager() throws DeviceManagementException { + return operationManager; + } } \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/org/wso2/carbon/device/mgt/mobile/impl/common/DBTypes.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/org/wso2/carbon/device/mgt/mobile/impl/common/DBTypes.java new file mode 100644 index 0000000000..02258e96e7 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/org/wso2/carbon/device/mgt/mobile/impl/common/DBTypes.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2014, 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.mobile.impl.common; + + +public enum DBTypes { + Oracle("Oracle"),H2("H2"),MySql("MySql"); + + String dbName ; + DBTypes(String dbStrName) { + dbName = dbStrName; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/org/wso2/carbon/device/mgt/mobile/impl/common/TestDBConfiguration.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/org/wso2/carbon/device/mgt/mobile/impl/common/TestDBConfiguration.java new file mode 100644 index 0000000000..3b40007e3e --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/org/wso2/carbon/device/mgt/mobile/impl/common/TestDBConfiguration.java @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2014, 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.mobile.impl.common; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "DBType") +public class TestDBConfiguration { + + private String connectionUrl; + private String driverClass; + private String userName; + private String pwd; + + @Override public String toString() { + return "TestDBConfiguration{" + + "connectionUrl='" + connectionUrl + '\'' + + ", driverClass='" + driverClass + '\'' + + ", userName='" + userName + '\'' + + ", pwd='" + pwd + '\'' + + ", dbType='" + dbType + '\'' + + '}'; + } + + private String dbType; + + @XmlElement(name = "connectionurl", nillable = false) + public String getConnectionUrl() { + return connectionUrl; + } + + public void setConnectionUrl(String connectionUrl) { + this.connectionUrl = connectionUrl; + } + + @XmlElement(name = "driverclass", nillable = false) + public String getDriverClass() { + return driverClass; + } + + public void setDriverClass(String driverClass) { + this.driverClass = driverClass; + } + + @XmlElement(name = "userName", nillable = false) + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + @XmlElement(name = "pwd", nillable = false) + public String getPwd() { + return pwd; + } + + public void setPwd(String pwd) { + this.pwd = pwd; + } + + @XmlAttribute(name = "typeName") + public String getDbType() { + return dbType; + } + + public void setDbType(String dbType) { + this.dbType = dbType; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/org/wso2/carbon/device/mgt/mobile/impl/common/TestDBConfigurations.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/org/wso2/carbon/device/mgt/mobile/impl/common/TestDBConfigurations.java new file mode 100644 index 0000000000..42a6fcf4d5 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/org/wso2/carbon/device/mgt/mobile/impl/common/TestDBConfigurations.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2014, 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.mobile.impl.common; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import java.util.List; + +@XmlRootElement(name = "DeviceMgtTestDBConfigurations") +public class TestDBConfigurations { + + private List dbTypesList; + + @XmlElement(name = "DBType") + public List getDbTypesList() { + return dbTypesList; + } + + public void setDbTypesList(List dbTypesList) { + this.dbTypesList = dbTypesList; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/org/wso2/carbon/device/mgt/mobile/impl/dao/FeatureDAOTestSuite.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/org/wso2/carbon/device/mgt/mobile/impl/dao/FeatureDAOTestSuite.java new file mode 100644 index 0000000000..6411406b2c --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/org/wso2/carbon/device/mgt/mobile/impl/dao/FeatureDAOTestSuite.java @@ -0,0 +1,151 @@ +/* + * Copyright (c) 2014, 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.mobile.impl.dao; + + +import org.apache.commons.dbcp.BasicDataSource; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Parameters; +import org.testng.annotations.Test; +import org.w3c.dom.Document; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.core.common.DBTypes; +import org.wso2.carbon.device.mgt.core.common.TestDBConfiguration; +import org.wso2.carbon.device.mgt.core.common.TestDBConfigurations; +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.OwnerShip; +import org.wso2.carbon.device.mgt.core.dto.Status; +import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; +import org.wso2.carbon.device.mgt.mobile.dao.FeatureDAO; +import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOException; +import org.wso2.carbon.device.mgt.mobile.dao.impl.FeatureDAOImpl; +import org.wso2.carbon.device.mgt.mobile.impl.common.DBTypes; +import org.wso2.carbon.device.mgt.mobile.impl.common.TestDBConfiguration; +import org.wso2.carbon.device.mgt.mobile.impl.common.TestDBConfigurations; +import org.wso2.carbon.device.mgt.mobile.dto.*; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; +import java.io.File; +import java.sql.*; +import java.util.Date; +import java.util.Iterator; + +public class FeatureDAOTestSuite { + + private TestDBConfiguration testDBConfiguration; + private Connection conn = null; + private Statement stmt = null; + private FeatureDAOImpl featureDAO; + + @BeforeClass + @Parameters("dbType") + public void setUpDB(String dbTypeStr) throws Exception { + + DBTypes dbType = DBTypes.valueOf(dbTypeStr); + testDBConfiguration = getTestDBConfiguration(dbType); + + switch (dbType) { + case H2: + createH2DB(testDBConfiguration); + BasicDataSource testDataSource = new BasicDataSource(); + testDataSource.setDriverClassName(testDBConfiguration.getDriverClass()); + testDataSource.setUrl(testDBConfiguration.getConnectionUrl()); + testDataSource.setUsername(testDBConfiguration.getUserName()); + testDataSource.setPassword(testDBConfiguration.getPwd()); + featureDAO = new FeatureDAOImpl(testDataSource); + default: + } + } + + private TestDBConfiguration getTestDBConfiguration(DBTypes dbType) throws + MobileDeviceManagementDAOException, + DeviceManagementException { + + File deviceMgtConfig = new File("src/test/resources/testdbconfig.xml"); + Document doc = null; + testDBConfiguration = null; + TestDBConfigurations testDBConfigurations = null; + + doc = DeviceManagerUtil.convertToDocument(deviceMgtConfig); + JAXBContext testDBContext = null; + + try { + testDBContext = JAXBContext.newInstance(TestDBConfigurations.class); + Unmarshaller unmarshaller = testDBContext.createUnmarshaller(); + testDBConfigurations = (TestDBConfigurations) unmarshaller.unmarshal(doc); + } catch (JAXBException e) { + throw new MobileDeviceManagementDAOException("Error parsing test db configurations", e); + } + + Iterator itrDBConfigs = testDBConfigurations.getDbTypesList().iterator(); + while (itrDBConfigs.hasNext()) { + testDBConfiguration = itrDBConfigs.next(); + if (testDBConfiguration.getDbType().equals(dbType.toString())) { + break; + } + } + + return testDBConfiguration; + } + + private void createH2DB(TestDBConfiguration testDBConf) throws Exception { + + Class.forName(testDBConf.getDriverClass()); + conn = DriverManager.getConnection(testDBConf.getConnectionUrl()); + stmt = conn.createStatement(); + stmt.executeUpdate("RUNSCRIPT FROM './src/test/resources/sql/CreateH2TestDB.sql'"); + stmt.close(); + conn.close(); + + } + + + @Test + public void addFeature() throws MobileDeviceManagementDAOException, DeviceManagementException { + + + + Feature feature = new Feature(); + feature.setCode("Camera"); + feature.setDescription("Camera enable or disable"); + feature.setName("Camera"); + boolean added = featureDAO.addFeature(feature); +// Long deviceId = null; +// try { +// conn = DeviceManagementDAOFactory.getDataSource().getConnection(); +// stmt = conn.createStatement(); +// ResultSet resultSet = stmt +// .executeQuery("SELECT ID from DM_DEVICE DEVICE where DEVICE.DEVICE_IDENTIFICATION='111'"); +// +// while (resultSet.next()) { +// deviceId = resultSet.getLong(1); +// } +// conn.close(); +// } catch (SQLException sqlEx) { +// throw new DeviceManagementDAOException("error in fetch device by device identification id", sqlEx); +// } + + Assert.assertTrue(added, "Device Id is null"); + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/resources/sql/CreateH2TestDB.sql b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/resources/sql/CreateH2TestDB.sql new file mode 100644 index 0000000000..afcd19ca06 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/resources/sql/CreateH2TestDB.sql @@ -0,0 +1,24 @@ +CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE +( + ID INT auto_increment NOT NULL, + NAME VARCHAR(300) NULL DEFAULT NULL, + PRIMARY KEY (ID) +); + +CREATE TABLE IF NOT EXISTS DM_DEVICE +( + ID INT auto_increment NOT NULL, + DESCRIPTION TEXT NULL DEFAULT NULL, + NAME VARCHAR(100) NULL DEFAULT NULL, + DATE_OF_ENROLLMENT BIGINT NULL DEFAULT NULL, + DATE_OF_LAST_UPDATE BIGINT NULL DEFAULT NULL, + OWNERSHIP VARCHAR(45) NULL DEFAULT NULL, + STATUS VARCHAR(15) NULL DEFAULT NULL, + DEVICE_TYPE_ID INT(11) NULL DEFAULT NULL, + DEVICE_IDENTIFICATION VARCHAR(300) NULL DEFAULT NULL, + OWNER VARCHAR(45) NULL DEFAULT NULL, + TENANT_ID INTEGER DEFAULT 0, + PRIMARY KEY (ID), + CONSTRAINT fk_DM_DEVICE_DM_DEVICE_TYPE2 FOREIGN KEY (DEVICE_TYPE_ID ) + REFERENCES DM_DEVICE_TYPE (ID ) ON DELETE NO ACTION ON UPDATE NO ACTION +); \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/resources/testdbconfig.xml b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/resources/testdbconfig.xml new file mode 100644 index 0000000000..34e89a1392 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/resources/testdbconfig.xml @@ -0,0 +1,24 @@ + + + + + + jdbc:h2:mem:cdm-test-db;DB_CLOSE_DELAY=-1 + org.h2.Driver + + + + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/resources/testng.xml b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/resources/testng.xml new file mode 100644 index 0000000000..1bf0f08f81 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/resources/testng.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + \ No newline at end of file From b02d6ecea0f4d9efc6e44faa72f5324e2cab857a Mon Sep 17 00:00:00 2001 From: inosh-perera Date: Tue, 6 Jan 2015 18:59:10 +0530 Subject: [PATCH 3/4] changes in local notification --- .../cdm/agent/services/LocalNotification.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 product/modules/agents/android/client/src/org/wso2/cdm/agent/services/LocalNotification.java diff --git a/product/modules/agents/android/client/src/org/wso2/cdm/agent/services/LocalNotification.java b/product/modules/agents/android/client/src/org/wso2/cdm/agent/services/LocalNotification.java new file mode 100644 index 0000000000..177cde2253 --- /dev/null +++ b/product/modules/agents/android/client/src/org/wso2/cdm/agent/services/LocalNotification.java @@ -0,0 +1,45 @@ +/** + * 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. + * 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.cdm.agent.services; + +import android.app.AlarmManager; +import android.app.PendingIntent; +import android.content.Context; +import android.content.Intent; +import android.os.SystemClock; + +/** + * Local notification is a communication mechanism that essentially, + * polls to server based on a predefined to retrieve pending data. + */ +public class LocalNotification { + public static void startPolling(Context context) { + int interval=10000; +// int interval=Preference.getInt(context, context.getResources().getString(R.string.shared_pref_interval)); + //TODO:remove hard coded value + + long firstTime = SystemClock.elapsedRealtime(); + firstTime += 1000; + + Intent downloader = new Intent(context, AlarmReceiver.class); + PendingIntent recurringDownload = PendingIntent.getBroadcast(context, + 0, downloader, PendingIntent.FLAG_CANCEL_CURRENT); + AlarmManager alarms = (AlarmManager) context + .getSystemService(Context.ALARM_SERVICE); + alarms.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, + interval, recurringDownload); + } +} From f2c6eab6a32ad424b539dc4ba77106981b2f6918 Mon Sep 17 00:00:00 2001 From: inosh-perera Date: Fri, 9 Jan 2015 10:41:06 +0530 Subject: [PATCH 4/4] OperationDAO, FeaturePropertyDAO, OperationProperty and related implementaions --- .../pom1.xml~ | 0 .../mgt/mobile/dao/DeviceOperationDAO.java | 63 ++++++ .../device/mgt/mobile/dao/FeatureDAO.java | 37 +++- .../mgt/mobile/dao/FeaturePropertyDAO.java | 32 ++- .../device/mgt/mobile/dao/OperationDAO.java | 45 ++++ .../mgt/mobile/dao/OperationPropertyDAO.java | 61 ++++++ .../dao/impl/DeviceOperationDAOImpl.java | 196 ++++++++++++++++++ .../mgt/mobile/dao/impl/FeatureDAOImpl.java | 80 ++++++- .../dao/impl/FeaturePropertyDAOImpl.java | 186 +++++++++++++++++ .../mgt/mobile/dao/impl/OperationDAOImpl.java | 151 ++++++++++++++ .../dao/impl/OperationPropertyDAOImpl.java | 87 ++++++++ .../mgt/mobile/dto/DeviceOperation.java | 44 ++++ .../mgt/mobile/dto/FeatureProperty.java | 11 +- .../device/mgt/mobile/dto/Operation.java | 36 ++++ .../mgt/mobile/dto/OperationProperty.java | 44 ++++ .../mobile/impl/dao/FeatureDAOTestSuite.java | 14 +- .../src/test/resources/testng.xml | 2 +- 17 files changed, 1042 insertions(+), 47 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/pom1.xml~ create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/DeviceOperationDAO.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/OperationDAO.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/OperationPropertyDAO.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/impl/DeviceOperationDAOImpl.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/impl/FeaturePropertyDAOImpl.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/impl/OperationDAOImpl.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/impl/OperationPropertyDAOImpl.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dto/DeviceOperation.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dto/Operation.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dto/OperationProperty.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/pom1.xml~ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/pom1.xml~ new file mode 100644 index 0000000000..e69de29bb2 diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/DeviceOperationDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/DeviceOperationDAO.java new file mode 100644 index 0000000000..4273e13b5a --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/DeviceOperationDAO.java @@ -0,0 +1,63 @@ +package org.wso2.carbon.device.mgt.mobile.dao; + +import org.wso2.carbon.device.mgt.mobile.dto.DeviceOperation; + +import java.util.List; + +/** + * This class represents the mapping between device and operations. + */ +public interface DeviceOperationDAO { + /** + * Add a new mapping to plugin device_operation table. + * + * @param deviceOperation DeviceOperation object that holds data related to the DeviceOperation + * to be inserted. + * @return The status of the operation. If the insert was successful or not. + * @throws MobileDeviceManagementDAOException + */ + boolean addDeviceOperation(DeviceOperation deviceOperation) + throws MobileDeviceManagementDAOException; + + /** + * Update a feature in the feature table. + * + * @param deviceOperation DeviceOperation object that holds data has to be updated. + * @return The status of the operation. If the update was successful or not. + * @throws MobileDeviceManagementDAOException + */ + boolean updateDeviceOperation(DeviceOperation deviceOperation) + throws MobileDeviceManagementDAOException; + + /** + * Delete a given device operation from device operation table. + * + * @param deviceId Device id of the mapping to be deleted. + * @param operationId Operation id of the mapping to be deleted. + * @return The status of the operation. If the deletion was successful or not. + * @throws MobileDeviceManagementDAOException + */ + boolean deleteDeviceOperation(String deviceId, int operationId) + throws MobileDeviceManagementDAOException; + + /** + * Retrieve a given device operation from plugin database. + * + * @param deviceId Device id of the mapping to be retrieved. + * @param operationId Operation id of the mapping to be retrieved. + * @return DeviceOperation object that holds data of the device operation mapping represented by + * deviceId and operationId. + * @throws MobileDeviceManagementDAOException + */ + DeviceOperation getDeviceOperation(String deviceId, int operationId) + throws MobileDeviceManagementDAOException; + + /** + * Retrieve all the device operation mapping from plugin database. + * + * @return Device operation mapping object list. + * @throws MobileDeviceManagementDAOException + */ + List getAllDeviceOperationOfDevice(String deviceId) + throws MobileDeviceManagementDAOException; +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/FeatureDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/FeatureDAO.java index 08cd091c9d..37ac430c83 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/FeatureDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/FeatureDAO.java @@ -11,7 +11,8 @@ import java.util.List; public interface FeatureDAO { /** - * Add a new feature to plugin feature table. + * Add a new feature to feature table. + * * @param feature Feature object that holds data related to the feature to be inserted. * @return The status of the operation. If the insert was successful or not. * @throws MobileDeviceManagementDAOException @@ -20,6 +21,7 @@ public interface FeatureDAO { /** * Update a feature in the feature table. + * * @param feature Feature object that holds data has to be updated. * @return The status of the operation. If the update was successful or not. * @throws MobileDeviceManagementDAOException @@ -27,23 +29,44 @@ public interface FeatureDAO { boolean updateFeature(Feature feature) throws MobileDeviceManagementDAOException; /** - * Delete a given feature from plugin database. + * Delete a feature from feature table when the feature id is given. + * + * @param featureId Feature id of the feature to be deleted. + * @return The status of the operation. If the operationId was successful or not. + * @throws MobileDeviceManagementDAOException + */ + boolean deleteFeatureById(String featureId) throws MobileDeviceManagementDAOException; + + /** + * Delete a feature from feature table when the feature code is given. + * * @param featureCode Feature code of the feature to be deleted. - * @return The status of the operation. If the delete was successful or not. + * @return The status of the operation. If the operationId was successful or not. + * @throws MobileDeviceManagementDAOException + */ + boolean deleteFeatureByCode(String featureCode) throws MobileDeviceManagementDAOException; + + /** + * Retrieve a given feature from feature table when the feature id is given. + * + * @param featureId Feature id of the feature to be retrieved. + * @return Feature object that holds data of the feature represented by featureId. * @throws MobileDeviceManagementDAOException */ - boolean deleteFeature(String featureCode) throws MobileDeviceManagementDAOException; + Feature getFeatureById(String featureId) throws MobileDeviceManagementDAOException; /** - * Retrieve a given feature from plugin database. + * Retrieve a given feature from feature table when the feature code is given. + * * @param featureCode Feature code of the feature to be retrieved. * @return Feature object that holds data of the feature represented by featureCode. * @throws MobileDeviceManagementDAOException */ - Feature getFeature(String featureCode) throws MobileDeviceManagementDAOException; + Feature getFeatureByCode(String featureCode) throws MobileDeviceManagementDAOException; /** - * Retrieve all the features from plugin database. + * Retrieve all the features from plugin specific database. + * * @return Feature object list. * @throws MobileDeviceManagementDAOException */ diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/FeaturePropertyDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/FeaturePropertyDAO.java index aacfac3eaa..e0d60ddc16 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/FeaturePropertyDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/FeaturePropertyDAO.java @@ -2,49 +2,59 @@ package org.wso2.carbon.device.mgt.mobile.dao; import org.wso2.carbon.device.mgt.mobile.dto.FeatureProperty; +import java.util.List; + /** * This class represents the key operations associated with persisting feature property related * information. */ public interface FeaturePropertyDAO { /** - * Add a new feature property to plugin feature property table. + * Add a new feature property to feature property table. + * * @param featureProperty Feature property object that holds data related to the feature property to be inserted. * @return The status of the operation. If the insert was successful or not. * @throws MobileDeviceManagementDAOException */ - boolean addFeatureProperty(FeatureProperty featureProperty) throws MobileDeviceManagementDAOException; + boolean addFeatureProperty(FeatureProperty featureProperty) + throws MobileDeviceManagementDAOException; /** * Update a feature property in the feature property table. + * * @param featureProperty Feature property object that holds data has to be updated. * @return The status of the operation. If the update was successful or not. * @throws MobileDeviceManagementDAOException */ - boolean updateFeatureProperty(FeatureProperty featureProperty) throws MobileDeviceManagementDAOException; + boolean updateFeatureProperty(FeatureProperty featureProperty) + throws MobileDeviceManagementDAOException; /** - * Delete a given feature property from plugin database. + * Delete a given feature property from feature property table. + * * @param propertyId Id of the feature property to be deleted. - * @return The status of the operation. If the delete was successful or not. + * @return The status of the operation. If the operationId was successful or not. * @throws MobileDeviceManagementDAOException */ - boolean deleteDeviceProperty(String propertyId) throws MobileDeviceManagementDAOException; + boolean deleteFeatureProperty(int propertyId) throws MobileDeviceManagementDAOException; /** - * Retrieve a given feature property from plugin database. + * Retrieve a given feature property from feature property table. + * * @param propertyId Id of the feature property to be retrieved. * @return Feature property object that holds data of the feature property represented by propertyId. * @throws MobileDeviceManagementDAOException */ - FeatureProperty getFeatureProperty(String propertyId) throws MobileDeviceManagementDAOException; + FeatureProperty getFeatureProperty(int propertyId) throws MobileDeviceManagementDAOException; /** - * Retrieve a list of feature property corresponds to a feature code . - * @param featureCode feature code of the feature property to be retrieved. + * Retrieve a list of feature property corresponds to a feature id . + * + * @param featureId feature id of the feature property to be retrieved. * @return Feature property object that holds data of the feature property represented by propertyId. * @throws MobileDeviceManagementDAOException */ - FeatureProperty[] getFeaturePropertyOfFeature(String featureCode) throws MobileDeviceManagementDAOException; + List getFeaturePropertyOfFeature(String featureId) + throws MobileDeviceManagementDAOException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/OperationDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/OperationDAO.java new file mode 100644 index 0000000000..8fba815ace --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/OperationDAO.java @@ -0,0 +1,45 @@ +package org.wso2.carbon.device.mgt.mobile.dao; + +import org.wso2.carbon.device.mgt.mobile.dto.Operation; + +import java.util.List; + +/** + * This class represents the key operations associated with persisting operation related + * information. + */ +public interface OperationDAO { + + /** + * Add a new operation to plugin operation table. + * @param operation Operation object that holds data related to the operation to be inserted. + * @return The status of the operation. If the insert was successful or not. + * @throws MobileDeviceManagementDAOException + */ + boolean addOperation(Operation operation) throws MobileDeviceManagementDAOException; + + /** + * Update a operation in the operation table. + * @param operation Operation object that holds data has to be updated. + * @return The status of the operation. If the update was successful or not. + * @throws MobileDeviceManagementDAOException + */ + boolean updateOperation(Operation operation) throws MobileDeviceManagementDAOException; + + /** + * Delete a given operation from plugin database. + * @param operationId Operation code of the operation to be deleted. + * @return The status of the operation. If the operationId was successful or not. + * @throws MobileDeviceManagementDAOException + */ + boolean deleteOperation(int operationId) throws MobileDeviceManagementDAOException; + + /** + * Retrieve a given operation from plugin database. + * @param operationId Operation id of the operation to be retrieved. + * @return Operation object that holds data of the feature represented by operationId. + * @throws MobileDeviceManagementDAOException + */ + Operation getOperation(int operationId) throws MobileDeviceManagementDAOException; + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/OperationPropertyDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/OperationPropertyDAO.java new file mode 100644 index 0000000000..de012babb8 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/OperationPropertyDAO.java @@ -0,0 +1,61 @@ +package org.wso2.carbon.device.mgt.mobile.dao; + +import org.wso2.carbon.device.mgt.mobile.dto.OperationProperty; + +import java.util.List; + +/** + * This class represents the key operations associated with persisting operation property related + * information. + */ +public interface OperationPropertyDAO { + /** + * Add a new mapping to plugin operation property table. + * + * @param operationProperty OperationProperty object that holds data related to the operation property to be inserted. + * @return The status of the operation. If the insert was successful or not. + * @throws MobileDeviceManagementDAOException + */ + boolean addOperationProperty(OperationProperty operationProperty) + throws MobileDeviceManagementDAOException; + + /** + * Update a feature in the feature table. + * + * @param operationProperty DeviceOperation object that holds data has to be updated. + * @return The status of the operation. If the update was successful or not. + * @throws MobileDeviceManagementDAOException + */ + boolean updateOperationProperty(OperationProperty operationProperty) + throws MobileDeviceManagementDAOException; + + /** + * Delete a given device operation from plugin database. + * + * @param operationPropertyId Device id of the mapping to be deleted. + * @return The status of the operation. If the deletion was successful or not. + * @throws MobileDeviceManagementDAOException + */ + boolean deleteOperationProperty(int operationPropertyId) + throws MobileDeviceManagementDAOException; + + /** + * Retrieve a given device operation from plugin database. + * + * @param deviceId Device id of the mapping to be retrieved. + * @param operationId Operation id of the mapping to be retrieved. + * @return DeviceOperation object that holds data of the device operation mapping represented by deviceId and operationId. + * @throws MobileDeviceManagementDAOException + */ + OperationProperty getOperationProperty(String deviceId, int operationId) + throws MobileDeviceManagementDAOException; + + /** + * Retrieve all the device operation mapping from plugin database. + * + * @return Device operation mapping object list. + * @throws MobileDeviceManagementDAOException + */ + List getAllDeviceOperationOfDevice(String deviceId) + throws MobileDeviceManagementDAOException; +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/impl/DeviceOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/impl/DeviceOperationDAOImpl.java new file mode 100644 index 0000000000..26c5884d99 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/impl/DeviceOperationDAOImpl.java @@ -0,0 +1,196 @@ +package org.wso2.carbon.device.mgt.mobile.dao.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.mobile.dao.DeviceOperationDAO; +import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOException; +import org.wso2.carbon.device.mgt.mobile.dao.util.MobileDeviceManagementDAOUtil; +import org.wso2.carbon.device.mgt.mobile.dto.DeviceOperation; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +/** + * Implementation of DeviceOperationDAO + */ +public class DeviceOperationDAOImpl implements DeviceOperationDAO { + + private DataSource dataSource; + private static final Log log = LogFactory.getLog(DeviceOperationDAOImpl.class); + + public DeviceOperationDAOImpl(DataSource dataSource) { + this.dataSource = dataSource; + } + + @Override + public boolean addDeviceOperation(DeviceOperation deviceOperation) + throws MobileDeviceManagementDAOException { + boolean status = false; + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String createDBQuery = + "INSERT INTO MBL_DEVICE_OPERATION(DEVICE_ID, OPERATION_ID, SENT_DATE, RECEIVED_DATE) VALUES (?, ?, ?, ?)"; + + stmt = conn.prepareStatement(createDBQuery); + stmt.setString(1, deviceOperation.getDeviceId()); + stmt.setInt(2, deviceOperation.getOperationId()); + stmt.setInt(3, deviceOperation.getSentDate()); + stmt.setInt(4, deviceOperation.getReceivedDate()); + int rows = stmt.executeUpdate(); + if (rows > 0) { + status = true; + } + } catch (SQLException e) { + String msg = "Error occurred while adding device id - '" + + deviceOperation.getDeviceId() + " and operation id - " + deviceOperation.getOperationId() + "of mapping table MBL_DEVICE_OPERATION";; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + return status; + } + + @Override + public boolean updateDeviceOperation(DeviceOperation deviceOperation) + throws MobileDeviceManagementDAOException { + boolean status = false; + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String updateDBQuery = + "UPDATE MBL_DEVICE_OPERATION SET SENT_DATE = ?, RECEIVED_DATE = ? WHERE DEVICE_ID = ? and OPERATION_ID=?"; + stmt = conn.prepareStatement(updateDBQuery); + stmt.setInt(1, deviceOperation.getSentDate()); + stmt.setInt(2, deviceOperation.getReceivedDate()); + stmt.setString(3, deviceOperation.getDeviceId()); + stmt.setInt(4, deviceOperation.getOperationId()); + int rows = stmt.executeUpdate(); + if (rows > 0) { + status = true; + } + } catch (SQLException e) { + String msg = "Error occurred while updating device id - '" + + deviceOperation.getDeviceId() + " and operation id - " + deviceOperation.getOperationId() + "of mapping table MBL_DEVICE_OPERATION"; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + return status; + } + + @Override + public boolean deleteDeviceOperation(String deviceId, int operationId) + throws MobileDeviceManagementDAOException { + boolean status = false; + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String deleteDBQuery = + "DELETE FROM MBL_DEVICE_OPERATION WHERE DEVICE_ID = ? and OPERATION_ID=?"; + stmt = conn.prepareStatement(deleteDBQuery); + stmt.setString(1, deviceId); + stmt.setInt(2, operationId); + int rows = stmt.executeUpdate(); + if(rows>0){ + status = true; + } + } catch (SQLException e) { + String msg = "Error occurred while deleting mapping table MBL_DEVICE_OPERATION with device id - '" + + deviceId + " and operation id - " + operationId; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + return status; + } + + @Override + public DeviceOperation getDeviceOperation(String deviceId, int operationId) + throws MobileDeviceManagementDAOException { + Connection conn = null; + PreparedStatement stmt = null; + DeviceOperation deviceOperation = null; + try { + conn = this.getConnection(); + String selectDBQuery = + "SELECT DEVICE_ID, OPERATION_ID, SENT_DATE, RECEIVED_DATE FROM MBL_DEVICE_OPERATION WHERE DEVICE_ID = ? and OPERATION_ID=?"; + stmt = conn.prepareStatement(selectDBQuery); + stmt.setString(1, deviceId); + stmt.setInt(2, operationId); + ResultSet resultSet = stmt.executeQuery(); + while (resultSet.next()) { + deviceOperation = new DeviceOperation(); + deviceOperation.setDeviceId(resultSet.getString(1)); + deviceOperation.setOperationId(resultSet.getInt(2)); + deviceOperation.setSentDate(resultSet.getInt(3)); + deviceOperation.setReceivedDate(resultSet.getInt(4)); + break; + } + } catch (SQLException e) { + String msg = "Error occurred while fetching mapping table MBL_DEVICE_OPERATION entry with device id - '" + + deviceId + " and operation id - " + operationId; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + return deviceOperation; + } + + @Override + public List getAllDeviceOperationOfDevice(String deviceId) + throws MobileDeviceManagementDAOException { + Connection conn = null; + PreparedStatement stmt = null; + DeviceOperation deviceOperation = null; + List deviceOperations=new ArrayList(); + try { + conn = this.getConnection(); + String selectDBQuery = + "SELECT DEVICE_ID, OPERATION_ID, SENT_DATE, RECEIVED_DATE FROM MBL_DEVICE_OPERATION WHERE DEVICE_ID = ?"; + stmt = conn.prepareStatement(selectDBQuery); + stmt.setString(1, deviceId); + ResultSet resultSet = stmt.executeQuery(); + while (resultSet.next()) { + deviceOperation = new DeviceOperation(); + deviceOperation.setDeviceId(resultSet.getString(1)); + deviceOperation.setOperationId(resultSet.getInt(2)); + deviceOperation.setSentDate(resultSet.getInt(3)); + deviceOperation.setReceivedDate(resultSet.getInt(4)); + deviceOperations.add(deviceOperation); + break; + } + } catch (SQLException e) { + String msg = "Error occurred while fetching mapping table MBL_DEVICE_OPERATION entry with device id - '" + + deviceId; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + return deviceOperations; + } + + private Connection getConnection() throws MobileDeviceManagementDAOException { + try { + return dataSource.getConnection(); + } catch (SQLException e) { + String msg = "Error occurred while obtaining a connection from the mobile device " + + "management metadata repository datasource."; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/impl/FeatureDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/impl/FeatureDAOImpl.java index 301828a120..ba82b9d5b6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/impl/FeatureDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/impl/FeatureDAOImpl.java @@ -35,7 +35,7 @@ public class FeatureDAOImpl implements FeatureDAO { try { conn = this.getConnection(); String createDBQuery = - "INSERT INTO MBL_FEATURES(CODE, NAME, DESCRIPTION) VALUES (?, ?, ?)"; + "INSERT INTO MBL_FEATURE(CODE, NAME, DESCRIPTION) VALUES (?, ?, ?)"; stmt = conn.prepareStatement(createDBQuery); stmt.setString(1, feature.getCode()); @@ -47,7 +47,7 @@ public class FeatureDAOImpl implements FeatureDAO { } } catch (SQLException e) { String msg = "Error occurred while adding feature code - '" + - feature.getCode() + "'"; + feature.getCode() + "' to feature table"; log.error(msg, e); throw new MobileDeviceManagementDAOException(msg, e); } finally { @@ -65,7 +65,7 @@ public class FeatureDAOImpl implements FeatureDAO { try { conn = this.getConnection(); String updateDBQuery = - "UPDATE MBL_FEATURES SET CODE = ?, NAME = ?, DESCRIPTION = ? WHERE FEATURE_ID = ?"; + "UPDATE MBL_FEATURE SET CODE = ?, NAME = ?, DESCRIPTION = ? WHERE FEATURE_ID = ?"; stmt = conn.prepareStatement(updateDBQuery); stmt.setString(1, feature.getCode()); stmt.setString(2, feature.getName()); @@ -87,7 +87,7 @@ public class FeatureDAOImpl implements FeatureDAO { } @Override - public boolean deleteFeature(String featureCode) + public boolean deleteFeatureByCode(String featureCode) throws MobileDeviceManagementDAOException { boolean status = false; Connection conn = null; @@ -95,11 +95,11 @@ public class FeatureDAOImpl implements FeatureDAO { try { conn = this.getConnection(); String deleteDBQuery = - "DELETE FROM MBL_FEATURES WHERE FEATURE_ID = ?"; + "DELETE FROM MBL_FEATURE WHERE CODE = ?"; stmt = conn.prepareStatement(deleteDBQuery); stmt.setString(1, featureCode); int rows = stmt.executeUpdate(); - if(rows>0){ + if (rows > 0) { status = true; } } catch (SQLException e) { @@ -113,7 +113,33 @@ public class FeatureDAOImpl implements FeatureDAO { } @Override - public Feature getFeature(String featureCode) + public boolean deleteFeatureById(String featureId) + throws MobileDeviceManagementDAOException { + boolean status = false; + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String deleteDBQuery = + "DELETE FROM MBL_FEATURE WHERE FEATURE_ID = ?"; + stmt = conn.prepareStatement(deleteDBQuery); + stmt.setString(1, featureId); + int rows = stmt.executeUpdate(); + if (rows > 0) { + status = true; + } + } catch (SQLException e) { + String msg = "Error occurred while deleting feature with id - " + featureId; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + return status; + } + + @Override + public Feature getFeatureByCode(String featureCode) throws MobileDeviceManagementDAOException { Connection conn = null; PreparedStatement stmt = null; @@ -121,7 +147,7 @@ public class FeatureDAOImpl implements FeatureDAO { try { conn = this.getConnection(); String selectDBQuery = - "SELECT FEATURE_ID, CODE, NAME, DESCRIPTION FROM MBL_FEATURE WHERE FEATURE_ID = ?"; + "SELECT FEATURE_ID, CODE, NAME, DESCRIPTION FROM MBL_FEATURE WHERE CODE = ?"; stmt = conn.prepareStatement(selectDBQuery); stmt.setString(1, featureCode); ResultSet resultSet = stmt.executeQuery(); @@ -144,12 +170,44 @@ public class FeatureDAOImpl implements FeatureDAO { return feature; } + @Override + public Feature getFeatureById(String featureID) + throws MobileDeviceManagementDAOException { + Connection conn = null; + PreparedStatement stmt = null; + Feature feature = null; + try { + conn = this.getConnection(); + String selectDBQuery = + "SELECT FEATURE_ID, CODE, NAME, DESCRIPTION FROM MBL_FEATURE WHERE FEATURE_ID = ?"; + stmt = conn.prepareStatement(selectDBQuery); + stmt.setString(1, featureID); + ResultSet resultSet = stmt.executeQuery(); + while (resultSet.next()) { + feature = new Feature(); + feature.setId(resultSet.getInt(1)); + feature.setCode(resultSet.getString(2)); + feature.setName(resultSet.getString(3)); + feature.setDescription(resultSet.getString(4)); + break; + } + } catch (SQLException e) { + String msg = "Error occurred while fetching feature id - '" + + featureID + "'"; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + return feature; + } + @Override public List getAllFeatures() throws MobileDeviceManagementDAOException { Connection conn = null; PreparedStatement stmt = null; Feature feature; - List features=new ArrayList(); + List features = new ArrayList(); try { conn = this.getConnection(); String selectDBQuery = @@ -178,8 +236,8 @@ public class FeatureDAOImpl implements FeatureDAO { try { return dataSource.getConnection(); } catch (SQLException e) { - String msg = "Error occurred while obtaining a connection from the mobile device " + - "management metadata repository datasource."; + String msg = "Error occurred while obtaining a connection from the mobile specific " + + "datasource."; log.error(msg, e); throw new MobileDeviceManagementDAOException(msg, e); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/impl/FeaturePropertyDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/impl/FeaturePropertyDAOImpl.java new file mode 100644 index 0000000000..64473f0bff --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/impl/FeaturePropertyDAOImpl.java @@ -0,0 +1,186 @@ +package org.wso2.carbon.device.mgt.mobile.dao.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.mobile.dao.FeaturePropertyDAO; +import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOException; +import org.wso2.carbon.device.mgt.mobile.dao.util.MobileDeviceManagementDAOUtil; +import org.wso2.carbon.device.mgt.mobile.dto.FeatureProperty; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +/** + * Implementation of FeaturePropertyDAO + */ +public class FeaturePropertyDAOImpl implements FeaturePropertyDAO { + + private DataSource dataSource; + private static final Log log = LogFactory.getLog(FeaturePropertyDAOImpl.class); + + public FeaturePropertyDAOImpl(DataSource dataSource) { + this.dataSource = dataSource; + } + + @Override + public boolean addFeatureProperty(FeatureProperty featureProperty) + throws MobileDeviceManagementDAOException { + boolean status = false; + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String createDBQuery = + "INSERT INTO MBL_FEATURE_PROPERTY(PROPERTY, FEATURE_ID) VALUES (?, ?)"; + + stmt = conn.prepareStatement(createDBQuery); + stmt.setString(1, featureProperty.getProperty()); + stmt.setString(2, featureProperty.getFeatureID()); + int rows = stmt.executeUpdate(); + if (rows > 0) { + status = true; + } + } catch (SQLException e) { + String msg = "Error occurred while adding property id - '" + + featureProperty.getFeatureID() + "'"; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + return status; + } + + @Override + public boolean updateFeatureProperty(FeatureProperty featureProperty) + throws MobileDeviceManagementDAOException { + boolean status = false; + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String updateDBQuery = + "UPDATE MBL_FEATURE_PROPERTY SET PROPERTY = ?, FEATURE_ID = ? WHERE PROPERTY_ID = ?"; + stmt = conn.prepareStatement(updateDBQuery); + stmt.setString(1, featureProperty.getProperty()); + stmt.setString(2, featureProperty.getFeatureID()); + stmt.setInt(3, featureProperty.getPropertyId()); + int rows = stmt.executeUpdate(); + if (rows > 0) { + status = true; + } + } catch (SQLException e) { + String msg = "Error occurred while updating the feature property with property id - '" + + featureProperty.getPropertyId() + "'"; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + return status; + } + + @Override + public boolean deleteFeatureProperty(int propertyId) + throws MobileDeviceManagementDAOException { + boolean status = false; + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String deleteDBQuery = + "DELETE FROM MBL_FEATURE_PROPERTY WHERE PROPERTY_ID = ?"; + stmt = conn.prepareStatement(deleteDBQuery); + stmt.setInt(1, propertyId); + int rows = stmt.executeUpdate(); + if (rows > 0) { + status = true; + } + } catch (SQLException e) { + String msg = "Error occurred while deleting feature property with property Id - " + + propertyId; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + return status; + } + + @Override + public FeatureProperty getFeatureProperty(int propertyId) + throws MobileDeviceManagementDAOException { + Connection conn = null; + PreparedStatement stmt = null; + FeatureProperty featureProperty = null; + try { + conn = this.getConnection(); + String selectDBQuery = + "SELECT PROPERTY, FEATURE_ID FROM MBL_FEATURE_PROPERTY WHERE PROPERTY_ID = ?"; + stmt = conn.prepareStatement(selectDBQuery); + stmt.setInt(1, propertyId); + ResultSet resultSet = stmt.executeQuery(); + while (resultSet.next()) { + featureProperty = new FeatureProperty(); + featureProperty.setProperty(resultSet.getString(1)); + featureProperty.setFeatureID(resultSet.getString(2)); + break; + } + } catch (SQLException e) { + String msg = "Error occurred while fetching property Id - '" + + propertyId + "'"; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + return featureProperty; + } + + @Override + public List getFeaturePropertyOfFeature(String featureId) + throws MobileDeviceManagementDAOException { + Connection conn = null; + PreparedStatement stmt = null; + FeatureProperty featureProperty = null; + List FeatureProperties = new ArrayList(); + try { + conn = this.getConnection(); + String selectDBQuery = + "SELECT PROPERTY_ID,PROPERTY, FEATURE_ID FROM MBL_FEATURE_PROPERTY WHERE FEATURE_ID = ?"; + stmt = conn.prepareStatement(selectDBQuery); + stmt.setString(1, featureId); + ResultSet resultSet = stmt.executeQuery(); + while (resultSet.next()) { + featureProperty = new FeatureProperty(); + featureProperty.setPropertyId(resultSet.getInt(1)); + featureProperty.setProperty(resultSet.getString(2)); + featureProperty.setFeatureID(resultSet.getString(3)); + FeatureProperties.add(featureProperty); + } + return FeatureProperties; + } catch (SQLException e) { + String msg = "Error occurred while fetching all feature property.'"; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + } + + private Connection getConnection() throws MobileDeviceManagementDAOException { + try { + return dataSource.getConnection(); + } catch (SQLException e) { + String msg = "Error occurred while obtaining a connection from the mobile device " + + "management metadata repository datasource."; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/impl/OperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/impl/OperationDAOImpl.java new file mode 100644 index 0000000000..3e57869052 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/impl/OperationDAOImpl.java @@ -0,0 +1,151 @@ +package org.wso2.carbon.device.mgt.mobile.dao.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOException; +import org.wso2.carbon.device.mgt.mobile.dao.OperationDAO; +import org.wso2.carbon.device.mgt.mobile.dao.util.MobileDeviceManagementDAOUtil; +import org.wso2.carbon.device.mgt.mobile.dto.Operation; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +/** + * Implementation of OperationDAO + */ +public class OperationDAOImpl implements OperationDAO { + + private DataSource dataSource; + private static final Log log = LogFactory.getLog(OperationDAOImpl.class); + + public OperationDAOImpl(DataSource dataSource) { + this.dataSource = dataSource; + } + + @Override + public boolean addOperation(Operation operation) + throws MobileDeviceManagementDAOException { + boolean status = false; + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String createDBQuery = + "INSERT INTO MBL_OPERATION(FEATURE_CODE, CREATED_DATE) VALUES ( ?, ?)"; + + stmt = conn.prepareStatement(createDBQuery); + stmt.setString(1, operation.getFeatureCode()); + stmt.setInt(2, operation.getCreatedDate()); + int rows = stmt.executeUpdate(); + if (rows > 0) { + status = true; + } + } catch (SQLException e) { + String msg = "Error occurred while adding feature code - '" + + operation.getFeatureCode() + "' to operations table"; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + return status; + } + + @Override + public boolean updateOperation(Operation operation) + throws MobileDeviceManagementDAOException { + boolean status = false; + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String updateDBQuery = + "UPDATE MBL_OPERATION SET FEATURE_CODE = ?, CREATED_DATE = ? WHERE OPERATION_ID = ?"; + stmt = conn.prepareStatement(updateDBQuery); + stmt.setString(1, operation.getFeatureCode()); + stmt.setInt(2, operation.getCreatedDate()); + stmt.setInt(3, operation.getOperationId()); + int rows = stmt.executeUpdate(); + if (rows > 0) { + status = true; + } + } catch (SQLException e) { + String msg = "Error occurred while updating the operation with operation id - '" + + operation.getOperationId() + "'"; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + return status; + } + + @Override + public boolean deleteOperation(int operationId) + throws MobileDeviceManagementDAOException { + boolean status = false; + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String deleteDBQuery = + "DELETE FROM MBL_OPERATION WHERE OPERATION_ID = ?"; + stmt = conn.prepareStatement(deleteDBQuery); + stmt.setInt(1, operationId); + int rows = stmt.executeUpdate(); + if (rows > 0) { + status = true; + } + } catch (SQLException e) { + String msg = "Error occurred while deleting operation with operation Id - "; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + return status; + } + + @Override + public Operation getOperation(int operationId) + throws MobileDeviceManagementDAOException { + Connection conn = null; + PreparedStatement stmt = null; + Operation operation = null; + try { + conn = this.getConnection(); + String selectDBQuery = + "SELECT OPERATION_ID, FEATURE_CODE, CREATED_DATE FROM MBL_OPERATION WHERE OPERATION_ID = ?"; + stmt = conn.prepareStatement(selectDBQuery); + stmt.setInt(1, operation.getOperationId()); + ResultSet resultSet = stmt.executeQuery(); + while (resultSet.next()) { + operation = new Operation(); + operation.setOperationId(resultSet.getInt(1)); + break; + } + } catch (SQLException e) { + String msg = "Error occurred while fetching operationId - '" + + operationId + "'"; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + return operation; + } + + private Connection getConnection() throws MobileDeviceManagementDAOException { + try { + return dataSource.getConnection(); + } catch (SQLException e) { + String msg = "Error occurred while obtaining a connection from the mobile device " + + "management metadata repository datasource."; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/impl/OperationPropertyDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/impl/OperationPropertyDAOImpl.java new file mode 100644 index 0000000000..42ed4604b8 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/impl/OperationPropertyDAOImpl.java @@ -0,0 +1,87 @@ +package org.wso2.carbon.device.mgt.mobile.dao.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOException; +import org.wso2.carbon.device.mgt.mobile.dao.OperationPropertyDAO; +import org.wso2.carbon.device.mgt.mobile.dao.util.MobileDeviceManagementDAOUtil; +import org.wso2.carbon.device.mgt.mobile.dto.OperationProperty; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.List; + +/** + * Implementation of OperationPropertyDAO + */ +public class OperationPropertyDAOImpl implements OperationPropertyDAO { + + private DataSource dataSource; + private static final Log log = LogFactory.getLog(OperationPropertyDAOImpl.class); + + public OperationPropertyDAOImpl(DataSource dataSource) { + this.dataSource = dataSource; + } + + @Override + public boolean addOperationProperty(OperationProperty operationProperty) + throws MobileDeviceManagementDAOException { + boolean status = false; + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String createDBQuery = + "INSERT INTO MBL_OPERATION_PROPERTY(OPERATION_ID, PROPERTY_ID, VALUE) VALUES ( ?, ?, ?)"; + + stmt = conn.prepareStatement(createDBQuery); + stmt.setInt(1, operationProperty.getOperationId()); + stmt.setInt(2, operationProperty.getPropertyId()); + stmt.setString(3, operationProperty.getValue()); + int rows = stmt.executeUpdate(); + if (rows > 0) { + status = true; + } + } catch (SQLException e) { + String msg = "Error occurred while adding feature property to operation property table"; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + return status; + } + + @Override public boolean updateOperationProperty(OperationProperty operationProperty) + throws MobileDeviceManagementDAOException { + return false; + } + + @Override public boolean deleteOperationProperty(int operationPropertyId) + throws MobileDeviceManagementDAOException { + return false; + } + + @Override public OperationProperty getOperationProperty(String deviceId, int operationId) + throws MobileDeviceManagementDAOException { + return null; + } + + @Override public List getAllDeviceOperationOfDevice(String deviceId) + throws MobileDeviceManagementDAOException { + return null; + } + + private Connection getConnection() throws MobileDeviceManagementDAOException { + try { + return dataSource.getConnection(); + } catch (SQLException e) { + String msg = "Error occurred while obtaining a connection from the mobile device " + + "management metadata repository datasource."; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dto/DeviceOperation.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dto/DeviceOperation.java new file mode 100644 index 0000000000..84e5503742 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dto/DeviceOperation.java @@ -0,0 +1,44 @@ +package org.wso2.carbon.device.mgt.mobile.dto; + +/** + * DTO of Operations. + */ +public class DeviceOperation { + String deviceId; + int operationId; + int sentDate; + int receivedDate; + + public String getDeviceId() { + return deviceId; + } + + public void setDeviceId(String deviceId) { + this.deviceId = deviceId; + } + + public int getOperationId() { + return operationId; + } + + public void setOperationId(int operationId) { + this.operationId = operationId; + } + + public int getSentDate() { + return sentDate; + } + + public void setSentDate(int sentDate) { + this.sentDate = sentDate; + } + + public int getReceivedDate() { + return receivedDate; + } + + public void setReceivedDate(int receivedDate) { + this.receivedDate = receivedDate; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dto/FeatureProperty.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dto/FeatureProperty.java index f1cdf154ef..4f72e878e9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dto/FeatureProperty.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dto/FeatureProperty.java @@ -1,20 +1,19 @@ package org.wso2.carbon.device.mgt.mobile.dto; - /** * DTO of feature property. Represents a property of a feature. */ public class FeatureProperty { int propertyId; String property; - String featureCode; + String featureID; - public String getFeatureCode() { - return featureCode; + public String getFeatureID() { + return featureID; } - public void setFeatureCode(String featureCode) { - this.featureCode = featureCode; + public void setFeatureID(String featureID) { + this.featureID = featureID; } public int getPropertyId() { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dto/Operation.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dto/Operation.java new file mode 100644 index 0000000000..855ba6c606 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dto/Operation.java @@ -0,0 +1,36 @@ +package org.wso2.carbon.device.mgt.mobile.dto; + +/** + * DTO of operation. + */ +public class Operation { + int operationId; + String featureCode; + int createdDate; + + public int getOperationId() { + return operationId; + } + + public void setOperationId(int operationId) { + this.operationId = operationId; + } + + public String getFeatureCode() { + return featureCode; + } + + public void setFeatureCode(String featureCode) { + this.featureCode = featureCode; + } + + public int getCreatedDate() { + return createdDate; + } + + public void setCreatedDate(int createdDate) { + this.createdDate = createdDate; + } + + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dto/OperationProperty.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dto/OperationProperty.java new file mode 100644 index 0000000000..2800807979 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dto/OperationProperty.java @@ -0,0 +1,44 @@ +package org.wso2.carbon.device.mgt.mobile.dto; + +/** + * DTO of operation property. + */ +public class OperationProperty { + int operationPropertyId; + int getOperationId; + int propertyId; + String value; + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + public int getOperationPropertyId() { + return operationPropertyId; + } + + public void setOperationPropertyId(int operationPropertyId) { + this.operationPropertyId = operationPropertyId; + } + + public int getOperationId() { + return getOperationId; + } + + public void setOperationId(int getOperationId) { + this.getOperationId = getOperationId; + } + + public int getPropertyId() { + return propertyId; + } + + public void setPropertyId(int propertyId) { + this.propertyId = propertyId; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/org/wso2/carbon/device/mgt/mobile/impl/dao/FeatureDAOTestSuite.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/org/wso2/carbon/device/mgt/mobile/impl/dao/FeatureDAOTestSuite.java index 6411406b2c..8bc07619ba 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/org/wso2/carbon/device/mgt/mobile/impl/dao/FeatureDAOTestSuite.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/org/wso2/carbon/device/mgt/mobile/impl/dao/FeatureDAOTestSuite.java @@ -26,20 +26,12 @@ import org.testng.annotations.Parameters; import org.testng.annotations.Test; import org.w3c.dom.Document; import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.core.common.DBTypes; -import org.wso2.carbon.device.mgt.core.common.TestDBConfiguration; -import org.wso2.carbon.device.mgt.core.common.TestDBConfigurations; -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.OwnerShip; -import org.wso2.carbon.device.mgt.core.dto.Status; -import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; -import org.wso2.carbon.device.mgt.mobile.dao.FeatureDAO; -import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOException; -import org.wso2.carbon.device.mgt.mobile.dao.impl.FeatureDAOImpl; import org.wso2.carbon.device.mgt.mobile.impl.common.DBTypes; import org.wso2.carbon.device.mgt.mobile.impl.common.TestDBConfiguration; import org.wso2.carbon.device.mgt.mobile.impl.common.TestDBConfigurations; +import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; +import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOException; +import org.wso2.carbon.device.mgt.mobile.dao.impl.FeatureDAOImpl; import org.wso2.carbon.device.mgt.mobile.dto.*; import javax.xml.bind.JAXBContext; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/resources/testng.xml b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/resources/testng.xml index 1bf0f08f81..8bdba93ad7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/resources/testng.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/test/resources/testng.xml @@ -22,7 +22,7 @@ - + \ No newline at end of file