From a277fd53c22806c2b405c82e2889849ce5db57cc Mon Sep 17 00:00:00 2001 From: hasuniea Date: Wed, 21 Oct 2015 19:29:34 +0530 Subject: [PATCH] Implemented WindowsFeatureDAOImpl --- .../WindowsFeatureManagementDAOException.java | 80 ++++++ .../dao/impl/WindowsFeatureDAOImpl.java | 258 ++++++++++++++++++ .../windows/util/WindowsPluginConstants.java | 7 + 3 files changed, 345 insertions(+) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/dao/WindowsFeatureManagementDAOException.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/dao/impl/WindowsFeatureDAOImpl.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/dao/WindowsFeatureManagementDAOException.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/dao/WindowsFeatureManagementDAOException.java new file mode 100644 index 0000000000..5444fd2239 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/dao/WindowsFeatureManagementDAOException.java @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.device.mgt.mobile.impl.windows.dao; + +import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOException; + +/** + * Implement Exception class for Windows Device Features. + */ +public class WindowsFeatureManagementDAOException extends MobileDeviceManagementDAOException { + + private String message; + private static final long serialVersionUID = 2021891706072918865L; + + /** + * Constructs a new MobileDeviceManagementDAOException with the specified detail message and + * nested exception. + * + * @param message error message + * @param nestedException exception + */ + public WindowsFeatureManagementDAOException(String message, Exception nestedException) { + super(message, nestedException); + } + + /** + * Constructs a new MobileDeviceManagementDAOException with the specified detail message + * and cause. + * + * @param message the detail message. + * @param cause the cause of this exception. + */ + public WindowsFeatureManagementDAOException(String message, Throwable cause) { + super(message, cause); + setErrorMessage(message); + } + + /** + * Constructs a new MobileDeviceManagementDAOException with the specified detail message. + * + * @param message the detail message. + */ + public WindowsFeatureManagementDAOException(String message) { + super(message); + setErrorMessage(message); + } + + /** + * Constructs a new MobileDeviceManagementDAOException with the specified and cause. + * + * @param cause the cause of this exception. + */ + public WindowsFeatureManagementDAOException(Throwable cause) { + super(cause); + } + + public String getMessage() { + return message; + } + + public void setErrorMessage(String errorMessage) { + this.message = errorMessage; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/dao/impl/WindowsFeatureDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/dao/impl/WindowsFeatureDAOImpl.java new file mode 100644 index 0000000000..e69e209974 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/dao/impl/WindowsFeatureDAOImpl.java @@ -0,0 +1,258 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.device.mgt.mobile.impl.windows.dao.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; +import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOException; +import org.wso2.carbon.device.mgt.mobile.dao.MobileFeatureDAO; +import org.wso2.carbon.device.mgt.mobile.dao.util.MobileDeviceManagementDAOUtil; +import org.wso2.carbon.device.mgt.mobile.dto.MobileFeature; +import org.wso2.carbon.device.mgt.mobile.impl.windows.dao.WindowsDAOFactory; +import org.wso2.carbon.device.mgt.mobile.impl.windows.dao.WindowsFeatureManagementDAOException; +import org.wso2.carbon.device.mgt.mobile.impl.windows.util.WindowsPluginConstants; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +/** + * Implement MobileFeatureDAO for Windows devices. + */ +public class WindowsFeatureDAOImpl implements MobileFeatureDAO { + + private static final Log log = LogFactory.getLog(WindowsFeatureDAOImpl.class); + + public WindowsFeatureDAOImpl() { + + } + @Override + public boolean addFeature(MobileFeature mobileFeature) throws MobileDeviceManagementDAOException { + PreparedStatement stmt = null; + boolean status; + Connection conn; + try { + conn = WindowsDAOFactory.getConnection(); + String sql = "INSERT INTO WINDOWS_FEATURE(CODE, NAME, DESCRIPTION) VALUES (?, ?, ?)"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, mobileFeature.getCode()); + stmt.setString(2, mobileFeature.getName()); + stmt.setString(3, mobileFeature.getDescription()); + stmt.executeUpdate(); + status = true; + status = true; + } catch (SQLException e) { + throw new WindowsFeatureManagementDAOException( + "Error occurred while adding windows feature '" + + mobileFeature.getName() + "' into the metadata repository", e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(stmt, null); + } + return status; + } + + @Override + public boolean updateFeature(MobileFeature mobileFeature) throws MobileDeviceManagementDAOException { + boolean status = false; + Connection conn; + PreparedStatement stmt = null; + try { + conn = WindowsDAOFactory.getConnection(); + String updateDBQuery = + "UPDATE WINDOWS_FEATURE SET NAME = ?, DESCRIPTION = ?" + + "WHERE CODE = ?"; + + stmt = conn.prepareStatement(updateDBQuery); + stmt.setString(1, mobileFeature.getName()); + stmt.setString(2, mobileFeature.getDescription()); + stmt.setString(3, mobileFeature.getCode()); + + int rows = stmt.executeUpdate(); + if (rows > 0) { + status = true; + if (log.isDebugEnabled()) { + log.debug("Windows Feature " + mobileFeature.getCode() + " data has been " + + "modified."); + } + } + } catch (SQLException e) { + String msg = "Error occurred while updating the Windows Feature '" + + mobileFeature.getCode() + "' to the Android db."; + log.error(msg, e); + throw new WindowsFeatureManagementDAOException(msg, e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(stmt, null); + } + return status; + } + + @Override + public boolean deleteFeatureById(int mblFeatureId) throws MobileDeviceManagementDAOException { + PreparedStatement stmt = null; + boolean status = false; + Connection conn = null; + try { + conn = WindowsDAOFactory.getConnection(); + String sql = "DELETE FROM WINDOWS_FEATURE WHERE FEATURE_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, mblFeatureId); + stmt.execute(); + status = true; + } catch (SQLException e) { + throw new WindowsFeatureManagementDAOException( + "Error occurred while deleting windows feature '" + + mblFeatureId + "' from Windows database.", e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(stmt, null); + } + return status; + } + + @Override + public boolean deleteFeatureByCode(String mblFeatureCode) throws MobileDeviceManagementDAOException { + PreparedStatement stmt = null; + boolean status = false; + Connection conn = null; + try { + conn = WindowsDAOFactory.getConnection(); + String sql = "DELETE FROM WINDOWS_FEATURE WHERE CODE = ?"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, mblFeatureCode); + stmt.execute(); + status = true; + } catch (SQLException e) { + throw new WindowsFeatureManagementDAOException( + "Error occurred while deleting windows feature '" + + mblFeatureCode + "' from Windows database.", e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(stmt, null); + } + return status; + } + + @Override + public MobileFeature getFeatureById(int mblFeatureId) throws MobileDeviceManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + Connection conn = null; + try { + conn = WindowsDAOFactory.getConnection(); + String sql = "SELECT FEATURE_ID, CODE, NAME, DESCRIPTION FROM WINDOWS_FEATURE WHERE ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, mblFeatureId); + rs = stmt.executeQuery(); + + MobileFeature mobileFeature = null; + if (rs.next()) { + mobileFeature = new MobileFeature(); + mobileFeature.setId(rs.getInt(WindowsPluginConstants.WINDOWS_FEATURE_ID)); + mobileFeature.setCode(rs.getString(WindowsPluginConstants.WINDOWS_FEATURE_CODE)); + mobileFeature.setName(rs.getString(WindowsPluginConstants.WINDOWS_FEATURE_NAME)); + mobileFeature.setDescription(rs.getString(WindowsPluginConstants.WINDOWS_FEATURE_DESCRIPTION)); + mobileFeature.setDeviceType( + DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_WINDOWS); + } + return mobileFeature; + } catch (SQLException e) { + throw new WindowsFeatureManagementDAOException( + "Error occurred while retrieving windows feature '" + + mblFeatureId + "' from the Windows database.", e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs); + WindowsDAOFactory.closeConnection(); + } + } + + @Override + public MobileFeature getFeatureByCode(String mblFeatureCode) throws MobileDeviceManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + Connection conn = null; + + try { + conn = WindowsDAOFactory.getConnection(); + String sql = "SELECT FEATURE_ID, CODE, NAME, DESCRIPTION FROM WINDOWS_FEATURE WHERE CODE = ?"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, mblFeatureCode); + rs = stmt.executeQuery(); + + MobileFeature mobileFeature = null; + if (rs.next()) { + mobileFeature = new MobileFeature(); + mobileFeature.setId(rs.getInt(WindowsPluginConstants.WINDOWS_FEATURE_ID)); + mobileFeature.setCode(rs.getString(WindowsPluginConstants.WINDOWS_FEATURE_CODE)); + mobileFeature.setName(rs.getString(WindowsPluginConstants.WINDOWS_FEATURE_NAME)); + mobileFeature.setDescription(rs.getString(WindowsPluginConstants.WINDOWS_FEATURE_DESCRIPTION)); + mobileFeature.setDeviceType( + DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_WINDOWS); + } + return mobileFeature; + } catch (SQLException e) { + throw new WindowsFeatureManagementDAOException( + "Error occurred while retrieving windows feature '" + + mblFeatureCode + "' from the Windows database.", e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs); + WindowsDAOFactory.closeConnection(); + } + } + + @Override + public List getFeatureByDeviceType(String deviceType) throws MobileDeviceManagementDAOException { + return this.getAllFeatures(); + } + + @Override + public List getAllFeatures() throws MobileDeviceManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + Connection conn = null; + List features = new ArrayList(); + + try { + conn = WindowsDAOFactory.getConnection(); + String sql = "SELECT FEATURE_ID, CODE, NAME, DESCRIPTION FROM WINDOWS_FEATURE"; + stmt = conn.prepareStatement(sql); + rs = stmt.executeQuery(); + MobileFeature mobileFeature = null; + + while (rs.next()) { + mobileFeature = new MobileFeature(); + mobileFeature.setId(rs.getInt(WindowsPluginConstants.WINDOWS_FEATURE_ID)); + mobileFeature.setCode(rs.getString(WindowsPluginConstants.WINDOWS_FEATURE_CODE)); + mobileFeature.setName(rs.getString(WindowsPluginConstants.WINDOWS_FEATURE_NAME)); + mobileFeature.setDescription(rs.getString(WindowsPluginConstants.WINDOWS_FEATURE_DESCRIPTION)); + mobileFeature.setDeviceType( + DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_WINDOWS); + features.add(mobileFeature); + } + return features; + } catch (SQLException e) { + throw new WindowsFeatureManagementDAOException("Error occurred while retrieving all " + + "windows features from the Windows database.", e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs); + WindowsDAOFactory.closeConnection(); + } + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsPluginConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsPluginConstants.java index 50f8f15c3b..53515ccf05 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsPluginConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsPluginConstants.java @@ -38,4 +38,11 @@ public class WindowsPluginConstants { public static final String MAC_ADDRESS = "MAC_ADDRESS"; public static final String DEVICE_NAME = "DEVICE_NAME"; + //Properties related to AD_FEATURE table + public static final String WINDOWS_FEATURE_ID = "FEATURE_ID"; + public static final String WINDOWS_FEATURE_CODE = "CODE"; + public static final String WINDOWS_FEATURE_NAME = "NAME"; + public static final String WINDOWS_FEATURE_DESCRIPTION = "DESCRIPTION"; + + }