diff --git a/modules/samples/connectedcup/component/plugin/src/main/java/org/coffeeking/connectedcup/plugin/constants/ConnectedCupConstants.java b/modules/samples/connectedcup/component/plugin/src/main/java/org/coffeeking/connectedcup/plugin/constants/ConnectedCupConstants.java index e472d3ab..4121aa30 100644 --- a/modules/samples/connectedcup/component/plugin/src/main/java/org/coffeeking/connectedcup/plugin/constants/ConnectedCupConstants.java +++ b/modules/samples/connectedcup/component/plugin/src/main/java/org/coffeeking/connectedcup/plugin/constants/ConnectedCupConstants.java @@ -18,5 +18,6 @@ public class ConnectedCupConstants { public static final String SENSOR_TEMPERATURE = "temperature"; public static final String SENSOR_LEVEL = "level"; + public static final String DATA_SOURCE_NAME = "jdbc/ConnectedCupDM_DB"; } diff --git a/modules/samples/connectedcup/component/plugin/src/main/java/org/coffeeking/connectedcup/plugin/exception/ConnectedCupDeviceMgtPluginException.java b/modules/samples/connectedcup/component/plugin/src/main/java/org/coffeeking/connectedcup/plugin/exception/ConnectedCupDeviceMgtPluginException.java new file mode 100644 index 00000000..296ea59b --- /dev/null +++ b/modules/samples/connectedcup/component/plugin/src/main/java/org/coffeeking/connectedcup/plugin/exception/ConnectedCupDeviceMgtPluginException.java @@ -0,0 +1,56 @@ +/* + * 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.coffeeking.connectedcup.plugin.exception; + + +public class ConnectedCupDeviceMgtPluginException extends Exception{ + + private String errorMessage; + + public String getErrorMessage() { + return errorMessage; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + + public ConnectedCupDeviceMgtPluginException(String msg, Exception nestedEx) { + super(msg, nestedEx); + setErrorMessage(msg); + } + + public ConnectedCupDeviceMgtPluginException(String message, Throwable cause) { + super(message, cause); + setErrorMessage(message); + } + + public ConnectedCupDeviceMgtPluginException(String msg) { + super(msg); + setErrorMessage(msg); + } + + public ConnectedCupDeviceMgtPluginException() { + super(); + } + + public ConnectedCupDeviceMgtPluginException(Throwable cause) { + super(cause); + } + +} diff --git a/modules/samples/connectedcup/component/plugin/src/main/java/org/coffeeking/connectedcup/plugin/impl/ConnectedCupManager.java b/modules/samples/connectedcup/component/plugin/src/main/java/org/coffeeking/connectedcup/plugin/impl/ConnectedCupManager.java index da3e4e40..a2bc2f90 100644 --- a/modules/samples/connectedcup/component/plugin/src/main/java/org/coffeeking/connectedcup/plugin/impl/ConnectedCupManager.java +++ b/modules/samples/connectedcup/component/plugin/src/main/java/org/coffeeking/connectedcup/plugin/impl/ConnectedCupManager.java @@ -21,6 +21,7 @@ package org.coffeeking.connectedcup.plugin.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.coffeeking.connectedcup.plugin.exception.ConnectedCupDeviceMgtPluginException; import org.coffeeking.connectedcup.plugin.impl.dao.ConnectedCupDAO; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; @@ -31,12 +32,6 @@ import org.wso2.carbon.device.mgt.common.FeatureManager; import org.wso2.carbon.device.mgt.common.configuration.mgt.TenantConfiguration; import org.wso2.carbon.device.mgt.common.license.mgt.License; import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; -import org.wso2.carbon.device.mgt.iot.util.iotdevice.dao.IotDeviceManagementDAOException; -import org.wso2.carbon.device.mgt.iot.util.iotdevice.dao.IotDeviceManagementDAOFactory; -import org.wso2.carbon.device.mgt.iot.util.iotdevice.dto.IotDevice; -import org.wso2.carbon.device.mgt.iot.util.iotdevice.util.IotDeviceManagementUtil; - -import java.util.ArrayList; import java.util.List; @@ -47,7 +42,7 @@ public class ConnectedCupManager implements DeviceManager { private static final Log log = LogFactory.getLog(ConnectedCupManager.class); - private static final IotDeviceManagementDAOFactory iotDeviceManagementDAOFactory = new ConnectedCupDAO(); + private static final ConnectedCupDAO connectedCupDAO = new ConnectedCupDAO(); @Override public FeatureManager getFeatureManager() { @@ -70,19 +65,17 @@ public class ConnectedCupManager implements DeviceManager { @Override public boolean enrollDevice(Device device) throws DeviceManagementException { boolean status; - IotDevice iotDevice = IotDeviceManagementUtil.convertToIotDevice(device); try { if (log.isDebugEnabled()) { log.debug("Enrolling a new Connected Cup device : " + device.getDeviceIdentifier()); } ConnectedCupDAO.beginTransaction(); - status = iotDeviceManagementDAOFactory.getIotDeviceDAO().addIotDevice( - iotDevice); + status = connectedCupDAO.getConnectedCupDeviceDAO().addDevice(device); ConnectedCupDAO.commitTransaction(); - } catch (IotDeviceManagementDAOException e) { + } catch (ConnectedCupDeviceMgtPluginException e) { try { ConnectedCupDAO.rollbackTransaction(); - } catch (IotDeviceManagementDAOException iotDAOEx) { + } catch (ConnectedCupDeviceMgtPluginException iotDAOEx) { String msg = "Error occurred while roll back the device enrol transaction :" + device.toString(); log.warn(msg, iotDAOEx); } @@ -96,19 +89,17 @@ public class ConnectedCupManager implements DeviceManager { @Override public boolean modifyEnrollment(Device device) throws DeviceManagementException { boolean status; - IotDevice iotDevice = IotDeviceManagementUtil.convertToIotDevice(device); try { if (log.isDebugEnabled()) { log.debug("Modifying the Connected Cup device enrollment data"); } ConnectedCupDAO.beginTransaction(); - status = iotDeviceManagementDAOFactory.getIotDeviceDAO() - .updateIotDevice(iotDevice); + status = connectedCupDAO.getConnectedCupDeviceDAO().updateDevice(device); ConnectedCupDAO.commitTransaction(); - } catch (IotDeviceManagementDAOException e) { + } catch (ConnectedCupDeviceMgtPluginException e) { try { ConnectedCupDAO.rollbackTransaction(); - } catch (IotDeviceManagementDAOException iotDAOEx) { + } catch (ConnectedCupDeviceMgtPluginException iotDAOEx) { String msg = "Error occurred while roll back the update device transaction :" + device.toString(); log.warn(msg, iotDAOEx); } @@ -128,13 +119,12 @@ public class ConnectedCupManager implements DeviceManager { log.debug("Dis-enrolling Connected Cup device : " + deviceId); } ConnectedCupDAO.beginTransaction(); - status = iotDeviceManagementDAOFactory.getIotDeviceDAO() - .deleteIotDevice(deviceId.getId()); + status = connectedCupDAO.getConnectedCupDeviceDAO().deleteDevice(deviceId.getId()); ConnectedCupDAO.commitTransaction(); - } catch (IotDeviceManagementDAOException e) { + } catch (ConnectedCupDeviceMgtPluginException e) { try { ConnectedCupDAO.rollbackTransaction(); - } catch (IotDeviceManagementDAOException iotDAOEx) { + } catch (ConnectedCupDeviceMgtPluginException iotDAOEx) { String msg = "Error occurred while roll back the device dis enrol transaction :" + deviceId.toString(); log.warn(msg, iotDAOEx); } @@ -152,13 +142,11 @@ public class ConnectedCupManager implements DeviceManager { if (log.isDebugEnabled()) { log.debug("Checking the enrollment of Connected Cup device : " + deviceId.getId()); } - IotDevice iotDevice = - iotDeviceManagementDAOFactory.getIotDeviceDAO().getIotDevice( - deviceId.getId()); + Device iotDevice = connectedCupDAO.getConnectedCupDeviceDAO().getDevice(deviceId.getId()); if (iotDevice != null) { isEnrolled = true; } - } catch (IotDeviceManagementDAOException e) { + } catch (ConnectedCupDeviceMgtPluginException e) { String msg = "Error while checking the enrollment status of Connected Cup device : " + deviceId.getId(); log.error(msg, e); @@ -184,10 +172,9 @@ public class ConnectedCupManager implements DeviceManager { try {if (log.isDebugEnabled()) { log.debug("Getting the details of Connected Cup device : " + deviceId.getId()); } - IotDevice iotDevice = iotDeviceManagementDAOFactory.getIotDeviceDAO(). - getIotDevice(deviceId.getId()); - device = IotDeviceManagementUtil.convertToDevice(iotDevice); - } catch (IotDeviceManagementDAOException e) { + device = connectedCupDAO.getConnectedCupDeviceDAO().getDevice(deviceId.getId()); + + } catch (ConnectedCupDeviceMgtPluginException e) { String msg = "Error while fetching the Connected Cup device : " + deviceId.getId(); log.error(msg, e); throw new DeviceManagementException(msg, e); @@ -229,20 +216,18 @@ public class ConnectedCupManager implements DeviceManager { @Override public boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, Device device) throws DeviceManagementException { boolean status; - IotDevice iotDevice = IotDeviceManagementUtil.convertToIotDevice(device); try { if (log.isDebugEnabled()) { log.debug( "updating the details of Connected Cup device : " + deviceIdentifier); } ConnectedCupDAO.beginTransaction(); - status = iotDeviceManagementDAOFactory.getIotDeviceDAO() - .updateIotDevice(iotDevice); + status = connectedCupDAO.getConnectedCupDeviceDAO().updateDevice(device); ConnectedCupDAO.commitTransaction(); - } catch (IotDeviceManagementDAOException e) { + } catch (ConnectedCupDeviceMgtPluginException e) { try { ConnectedCupDAO.rollbackTransaction(); - } catch (IotDeviceManagementDAOException iotDAOEx) { + } catch (ConnectedCupDeviceMgtPluginException iotDAOEx) { String msg = "Error occurred while roll back the update device info transaction :" + device.toString(); log.warn(msg, iotDAOEx); } @@ -261,15 +246,8 @@ public class ConnectedCupManager implements DeviceManager { if (log.isDebugEnabled()) { log.debug("Fetching the details of all Connected Cup devices"); } - List iotDevices = - iotDeviceManagementDAOFactory.getIotDeviceDAO().getAllIotDevices(); - if (iotDevices != null) { - devices = new ArrayList(); - for (IotDevice iotDevice : iotDevices) { - devices.add(IotDeviceManagementUtil.convertToDevice(iotDevice)); - } - } - } catch (IotDeviceManagementDAOException e) { + devices = connectedCupDAO.getConnectedCupDeviceDAO().getAllDevices(); + } catch (ConnectedCupDeviceMgtPluginException e) { String msg = "Error while fetching all Connected Cup devices."; log.error(msg, e); throw new DeviceManagementException(msg, e); diff --git a/modules/samples/connectedcup/component/plugin/src/main/java/org/coffeeking/connectedcup/plugin/impl/ConnectedCupManagerService.java b/modules/samples/connectedcup/component/plugin/src/main/java/org/coffeeking/connectedcup/plugin/impl/ConnectedCupManagerService.java index 47bd4b88..89f21f57 100644 --- a/modules/samples/connectedcup/component/plugin/src/main/java/org/coffeeking/connectedcup/plugin/impl/ConnectedCupManagerService.java +++ b/modules/samples/connectedcup/component/plugin/src/main/java/org/coffeeking/connectedcup/plugin/impl/ConnectedCupManagerService.java @@ -18,6 +18,7 @@ package org.coffeeking.connectedcup.plugin.impl; +import org.coffeeking.connectedcup.plugin.constants.ConnectedCupConstants; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManager; @@ -34,8 +35,7 @@ public class ConnectedCupManagerService implements DeviceManagementService{ @Override public String getType() { - return "connectedcup"; - //TODO: Constant + return ConnectedCupConstants.DEVICE_TYPE; } diff --git a/modules/samples/connectedcup/component/plugin/src/main/java/org/coffeeking/connectedcup/plugin/impl/dao/ConnectedCupDAO.java b/modules/samples/connectedcup/component/plugin/src/main/java/org/coffeeking/connectedcup/plugin/impl/dao/ConnectedCupDAO.java index a4d782dc..acefa4d3 100644 --- a/modules/samples/connectedcup/component/plugin/src/main/java/org/coffeeking/connectedcup/plugin/impl/dao/ConnectedCupDAO.java +++ b/modules/samples/connectedcup/component/plugin/src/main/java/org/coffeeking/connectedcup/plugin/impl/dao/ConnectedCupDAO.java @@ -21,102 +21,112 @@ package org.coffeeking.connectedcup.plugin.impl.dao; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.coffeeking.connectedcup.plugin.constants.ConnectedCupConstants; +import org.coffeeking.connectedcup.plugin.exception.ConnectedCupDeviceMgtPluginException; import org.coffeeking.connectedcup.plugin.impl.dao.impl.ConnectedCupDAOImpl; -import org.wso2.carbon.device.mgt.iot.util.iotdevice.dao.IotDeviceDAO; -import org.wso2.carbon.device.mgt.iot.util.iotdevice.dao.IotDeviceManagementDAOException; -import org.wso2.carbon.device.mgt.iot.util.iotdevice.dao.IotDeviceManagementDAOFactory; -import org.wso2.carbon.device.mgt.iot.util.iotdevice.dao.IotDeviceManagementDAOFactoryInterface; - +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; -public class ConnectedCupDAO extends IotDeviceManagementDAOFactory - implements IotDeviceManagementDAOFactoryInterface { +public class ConnectedCupDAO { + + private static final Log log = LogFactory.getLog(ConnectedCupDAO.class); + static DataSource dataSource; + private static ThreadLocal currentConnection = new ThreadLocal(); + + public ConnectedCupDAO() { + initConnectedCupDAO(); + } - private static final Log log = LogFactory.getLog(ConnectedCupDAO.class); - static DataSource dataSource; - private static ThreadLocal currentConnection = new ThreadLocal(); + public static void initConnectedCupDAO() { + try { + Context ctx = new InitialContext(); + dataSource = (DataSource) ctx.lookup(ConnectedCupConstants.DATA_SOURCE_NAME); + } catch (NamingException e) { + log.error("Error while looking up the data source: " + + ConnectedCupConstants.DATA_SOURCE_NAME); + } - public ConnectedCupDAO() { - initFireAlarmDAO(); - } + } - public static void initFireAlarmDAO() { - dataSource = getDataSourceMap().get(ConnectedCupConstants.DEVICE_TYPE); - } - @Override public IotDeviceDAO getIotDeviceDAO() { - return new ConnectedCupDAOImpl(); - } + public ConnectedCupDAOImpl getConnectedCupDeviceDAO() { + return new ConnectedCupDAOImpl(); + } - public static void beginTransaction() throws IotDeviceManagementDAOException { - try { - Connection conn = dataSource.getConnection(); - conn.setAutoCommit(false); - currentConnection.set(conn); - } catch (SQLException e) { - throw new IotDeviceManagementDAOException("Error occurred while retrieving datasource connection", e); - } - } + public static void beginTransaction() throws ConnectedCupDeviceMgtPluginException { + try { + Connection conn = dataSource.getConnection(); + conn.setAutoCommit(false); + currentConnection.set(conn); + } catch (SQLException e) { + throw new ConnectedCupDeviceMgtPluginException( + "Error occurred while retrieving datasource connection", e); + } + } - public static Connection getConnection() throws IotDeviceManagementDAOException { - if (currentConnection.get() == null) { - try { - currentConnection.set(dataSource.getConnection()); - } catch (SQLException e) { - throw new IotDeviceManagementDAOException("Error occurred while retrieving data source connection", e); - } - } - return currentConnection.get(); - } + public static Connection getConnection() throws ConnectedCupDeviceMgtPluginException { + if (currentConnection.get() == null) { + try { + currentConnection.set(dataSource.getConnection()); + } catch (SQLException e) { + throw new ConnectedCupDeviceMgtPluginException( + "Error occurred while retrieving data source connection", e); + } + } + return currentConnection.get(); + } - public static void commitTransaction() throws IotDeviceManagementDAOException { - try { - Connection conn = currentConnection.get(); - if (conn != null) { - conn.commit(); - } else { - if (log.isDebugEnabled()) { - log.debug("Datasource connection associated with the current thread is null, hence commit " - + "has not been attempted"); - } - } - } catch (SQLException e) { - throw new IotDeviceManagementDAOException("Error occurred while committing the transaction", e); - } finally { - closeConnection(); - } - } + public static void commitTransaction() throws ConnectedCupDeviceMgtPluginException { + try { + Connection conn = currentConnection.get(); + if (conn != null) { + conn.commit(); + } else { + if (log.isDebugEnabled()) { + log.debug("Datasource connection associated with the current thread is null, " + + "hence commit has not been attempted"); + } + } + } catch (SQLException e) { + throw new ConnectedCupDeviceMgtPluginException( + "Error occurred while committing the transaction", e); + } finally { + closeConnection(); + } + } - public static void closeConnection() throws IotDeviceManagementDAOException { + public static void closeConnection() throws ConnectedCupDeviceMgtPluginException { - Connection con = currentConnection.get(); - if (con != null) { - try { - con.close(); - } catch (SQLException e) { - log.error("Error occurred while close the connection"); - } - } - currentConnection.remove(); - } + Connection con = currentConnection.get(); + if (con != null) { + try { + con.close(); + } catch (SQLException e) { + log.error("Error occurred while close the connection"); + } + } + currentConnection.remove(); + } - public static void rollbackTransaction() throws IotDeviceManagementDAOException { - try { - Connection conn = currentConnection.get(); - if (conn != null) { - conn.rollback(); - } else { - if (log.isDebugEnabled()) { - log.debug("Datasource connection associated with the current thread is null, hence rollback " - + "has not been attempted"); - } - } - } catch (SQLException e) { - throw new IotDeviceManagementDAOException("Error occurred while rollback the transaction", e); - } finally { - closeConnection(); - } - } + public static void rollbackTransaction() throws ConnectedCupDeviceMgtPluginException { + try { + Connection conn = currentConnection.get(); + if (conn != null) { + conn.rollback(); + } else { + if (log.isDebugEnabled()) { + log.debug( + "Datasource connection associated with the current thread is null, " + + "hence rollback has not been attempted"); + } + } + } catch (SQLException e) { + throw new ConnectedCupDeviceMgtPluginException("Error occurred while rollback the transaction", e); + } finally { + closeConnection(); + } + } } \ No newline at end of file diff --git a/modules/samples/connectedcup/component/plugin/src/main/java/org/coffeeking/connectedcup/plugin/impl/dao/impl/ConnectedCupDAOImpl.java b/modules/samples/connectedcup/component/plugin/src/main/java/org/coffeeking/connectedcup/plugin/impl/dao/impl/ConnectedCupDAOImpl.java index 3dd17be4..3f74077c 100644 --- a/modules/samples/connectedcup/component/plugin/src/main/java/org/coffeeking/connectedcup/plugin/impl/dao/impl/ConnectedCupDAOImpl.java +++ b/modules/samples/connectedcup/component/plugin/src/main/java/org/coffeeking/connectedcup/plugin/impl/dao/impl/ConnectedCupDAOImpl.java @@ -21,222 +21,223 @@ package org.coffeeking.connectedcup.plugin.impl.dao.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.coffeeking.connectedcup.plugin.constants.ConnectedCupConstants; +import org.coffeeking.connectedcup.plugin.exception.ConnectedCupDeviceMgtPluginException; import org.coffeeking.connectedcup.plugin.impl.dao.ConnectedCupDAO; -import org.wso2.carbon.device.mgt.iot.util.iotdevice.dao.IotDeviceDAO; -import org.wso2.carbon.device.mgt.iot.util.iotdevice.dao.IotDeviceManagementDAOException; -import org.wso2.carbon.device.mgt.iot.util.iotdevice.dao.util.IotDeviceManagementDAOUtil; -import org.wso2.carbon.device.mgt.iot.util.iotdevice.dto.IotDevice; - +import org.coffeeking.connectedcup.plugin.impl.dao.util.ConnectedCupUtils; +import org.wso2.carbon.device.mgt.common.Device; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; /** - * Implements IotDeviceDAO for virtual firealarm Devices. + * Device Dao for connected cup Devices. */ -public class ConnectedCupDAOImpl implements IotDeviceDAO { - - - private static final Log log = LogFactory.getLog(ConnectedCupDAOImpl.class); - - @Override - public IotDevice getIotDevice(String iotDeviceId) - throws IotDeviceManagementDAOException { - Connection conn = null; - PreparedStatement stmt = null; - IotDevice iotDevice = null; - ResultSet resultSet = null; - try { - conn = ConnectedCupDAO.getConnection(); - String selectDBQuery = - "SELECT CONNECTED_CUP_DEVICE_ID, DEVICE_NAME, ACCESS_TOKEN, REFRESH_TOKEN" + - " FROM CONNECTED_CUP_DEVICE WHERE CONNECTED_CUP_DEVICE_ID = ?"; - stmt = conn.prepareStatement(selectDBQuery); - stmt.setString(1, iotDeviceId); - resultSet = stmt.executeQuery(); - - if (resultSet.next()) { - iotDevice = new IotDevice(); - iotDevice.setIotDeviceName(resultSet.getString( - ConnectedCupConstants.DEVICE_PLUGIN_DEVICE_NAME)); - Map propertyMap = new HashMap(); - propertyMap.put(ConnectedCupConstants.DEVICE_PLUGIN_PROPERTY_ACCESS_TOKEN, resultSet.getString("ACCESS_TOKEN")); - propertyMap.put(ConnectedCupConstants.DEVICE_PLUGIN_PROPERTY_REFRESH_TOKEN, resultSet.getString("REFRESH_TOKEN")); - iotDevice.setDeviceProperties(propertyMap); - - if (log.isDebugEnabled()) { - log.debug("Connected Cup service " + iotDeviceId + " data has been fetched from " + - "Connected Cup database."); - } - } - } catch (SQLException e) { - String msg = "Error occurred while fetching Connected Cup device : '" + iotDeviceId + "'"; - log.error(msg, e); - throw new IotDeviceManagementDAOException(msg, e); - } finally { - IotDeviceManagementDAOUtil.cleanupResources(stmt, resultSet); - ConnectedCupDAO.closeConnection(); - } - - return iotDevice; - } - - @Override - public boolean addIotDevice(IotDevice iotDevice) - throws IotDeviceManagementDAOException { - boolean status = false; - Connection conn = null; - PreparedStatement stmt = null; - try { - conn = ConnectedCupDAO.getConnection(); - String createDBQuery = - "INSERT INTO CONNECTED_CUP_DEVICE(CONNECTED_CUP_DEVICE_ID, DEVICE_NAME, ACCESS_TOKEN, REFRESH_TOKEN) VALUES (?, ?, ?, ?)"; - - stmt = conn.prepareStatement(createDBQuery); - stmt.setString(1, iotDevice.getIotDeviceId()); - stmt.setString(2, iotDevice.getIotDeviceName()); - stmt.setString(3, iotDevice.getDeviceProperties().get(ConnectedCupConstants.DEVICE_PLUGIN_PROPERTY_ACCESS_TOKEN)); - stmt.setString(4, iotDevice.getDeviceProperties().get(ConnectedCupConstants.DEVICE_PLUGIN_PROPERTY_REFRESH_TOKEN)); - - - int rows = stmt.executeUpdate(); - if (rows > 0) { - status = true; - if (log.isDebugEnabled()) { - log.debug("Connected Cup device " + iotDevice.getIotDeviceId() + " data has been" + - " added to the Connected Cup database."); - } - } - } catch (SQLException e) { - String msg = "Error occurred while adding the Connected Cup device '" + - iotDevice.getIotDeviceId() + "' to the Connected Cup db."; - log.error(msg, e); - throw new IotDeviceManagementDAOException(msg, e); - } finally { - IotDeviceManagementDAOUtil.cleanupResources(stmt, null); - } - return status; - } - - @Override - public boolean updateIotDevice(IotDevice iotDevice) - throws IotDeviceManagementDAOException { - boolean status = false; - Connection conn = null; - PreparedStatement stmt = null; - try { - conn = ConnectedCupDAO.getConnection(); - String updateDBQuery = - "UPDATE CONNECTED_CUP_DEVICE SET DEVICE_NAME = ?, ACCESS_TOKEN=?, REFRESH_TOKEN=? WHERE CONNECTED_CUP_DEVICE_ID = ?"; - - stmt = conn.prepareStatement(updateDBQuery); - - if (iotDevice.getDeviceProperties() == null) { - iotDevice.setDeviceProperties(new HashMap()); +public class ConnectedCupDAOImpl { + + + private static final Log log = LogFactory.getLog(ConnectedCupDAOImpl.class); + + public Device getDevice(String deviceId) throws ConnectedCupDeviceMgtPluginException { + Connection conn = null; + PreparedStatement stmt = null; + Device connectedCupDevice = null; + ResultSet resultSet = null; + try { + conn = ConnectedCupDAO.getConnection(); + String selectDBQuery = + "SELECT CONNECTED_CUP_DEVICE_ID, DEVICE_NAME, ACCESS_TOKEN, REFRESH_TOKEN" + + " FROM CONNECTED_CUP_DEVICE WHERE CONNECTED_CUP_DEVICE_ID = ?"; + stmt = conn.prepareStatement(selectDBQuery); + stmt.setString(1, deviceId); + resultSet = stmt.executeQuery(); + + if (resultSet.next()) { + connectedCupDevice = new Device(); + connectedCupDevice.setName(resultSet.getString( + ConnectedCupConstants.DEVICE_PLUGIN_DEVICE_NAME)); + List propertyList = new ArrayList(); + propertyList.add(ConnectedCupUtils.getProperty( + ConnectedCupConstants.DEVICE_PLUGIN_PROPERTY_ACCESS_TOKEN, + resultSet.getString("ACCESS_TOKEN"))); + propertyList.add(ConnectedCupUtils.getProperty( + ConnectedCupConstants.DEVICE_PLUGIN_PROPERTY_REFRESH_TOKEN, + resultSet.getString("REFRESH_TOKEN"))); + connectedCupDevice.setProperties(propertyList); + + if (log.isDebugEnabled()) { + log.debug("Connected Cup service " + deviceId + " data has been fetched from" + + "Connected Cup database."); } - stmt.setString(1, iotDevice.getIotDeviceName()); - stmt.setString(2, iotDevice.getDeviceProperties().get( - ConnectedCupConstants.DEVICE_PLUGIN_PROPERTY_ACCESS_TOKEN)); - stmt.setString(3, iotDevice.getDeviceProperties().get( - ConnectedCupConstants.DEVICE_PLUGIN_PROPERTY_ACCESS_TOKEN)); - stmt.setString(4, iotDevice.getIotDeviceId()); - int rows = stmt.executeUpdate(); - if (rows > 0) { - status = true; - if (log.isDebugEnabled()) { - log.debug("Connected Cup device " + iotDevice.getIotDeviceId() + " data has been" + - " modified."); - } + } + } catch (SQLException e) { + String msg = "Error occurred while fetching Connected Cup device : '" + deviceId + "'"; + log.error(msg, e); + throw new ConnectedCupDeviceMgtPluginException(msg, e); + } finally { + ConnectedCupUtils.cleanupResources(stmt, resultSet); + ConnectedCupDAO.closeConnection(); + } + return connectedCupDevice; + } + + + public boolean addDevice(Device connectedCupDevice) throws ConnectedCupDeviceMgtPluginException { + boolean status = false; + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = ConnectedCupDAO.getConnection(); + String createDBQuery = + "INSERT INTO CONNECTED_CUP_DEVICE(CONNECTED_CUP_DEVICE_ID, DEVICE_NAME, " + + "ACCESS_TOKEN, REFRESH_TOKEN) VALUES (?, ?, ?, ?)"; + + stmt = conn.prepareStatement(createDBQuery); + stmt.setString(1, connectedCupDevice.getDeviceIdentifier()); + stmt.setString(2, connectedCupDevice.getName()); + stmt.setString(3, ConnectedCupUtils.getDeviceProperty( + connectedCupDevice.getProperties(), + ConnectedCupConstants.DEVICE_PLUGIN_PROPERTY_ACCESS_TOKEN)); + stmt.setString(4, ConnectedCupUtils.getDeviceProperty( + connectedCupDevice.getProperties(), + ConnectedCupConstants.DEVICE_PLUGIN_PROPERTY_REFRESH_TOKEN)); + + int rows = stmt.executeUpdate(); + if (rows > 0) { + status = true; + if (log.isDebugEnabled()) { + log.debug("Connected Cup device " + connectedCupDevice.getDeviceIdentifier() + + " data has been added to the Connected Cup database."); } - } catch (SQLException e) { - String msg = "Error occurred while modifying the Connected Cup device '" + - iotDevice.getIotDeviceId() + "' data."; - log.error(msg, e); - throw new IotDeviceManagementDAOException(msg, e); - } finally { - IotDeviceManagementDAOUtil.cleanupResources(stmt, null); - } - return status; - } - - @Override - public boolean deleteIotDevice(String iotDeviceId) - throws IotDeviceManagementDAOException { - boolean status = false; - Connection conn = null; - PreparedStatement stmt = null; - try { - conn = ConnectedCupDAO.getConnection(); - String deleteDBQuery = - "DELETE FROM CONNECTED_CUP_DEVICE WHERE CONNECTED_CUP_DEVICE_ID = ?"; - stmt = conn.prepareStatement(deleteDBQuery); - stmt.setString(1, iotDeviceId); - int rows = stmt.executeUpdate(); - if (rows > 0) { - status = true; - if (log.isDebugEnabled()) { - log.debug("Connected Cup device " + iotDeviceId + " data has deleted" + - " from the Connected Cup database."); - } + } + } catch (SQLException e) { + String msg = "Error occurred while adding the Connected Cup device '" + + connectedCupDevice.getDeviceIdentifier() + "' to the Connected Cup db."; + log.error(msg, e); + throw new ConnectedCupDeviceMgtPluginException(msg, e); + } finally { + ConnectedCupUtils.cleanupResources(stmt, null); + } + return status; + } + + public boolean updateDevice(Device connectedCupDevice) throws ConnectedCupDeviceMgtPluginException { + boolean status = false; + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = ConnectedCupDAO.getConnection(); + String updateDBQuery = + "UPDATE CONNECTED_CUP_DEVICE SET DEVICE_NAME = ?, ACCESS_TOKEN=?, " + + "REFRESH_TOKEN=? WHERE CONNECTED_CUP_DEVICE_ID = ?"; + + stmt = conn.prepareStatement(updateDBQuery); + + if (connectedCupDevice.getProperties() == null) { + connectedCupDevice.setProperties(new ArrayList()); + } + stmt.setString(1, connectedCupDevice.getName()); + stmt.setString(2, ConnectedCupUtils.getDeviceProperty( + connectedCupDevice.getProperties(), + ConnectedCupConstants.DEVICE_PLUGIN_PROPERTY_ACCESS_TOKEN)); + stmt.setString(3, ConnectedCupUtils.getDeviceProperty( + connectedCupDevice.getProperties(), + ConnectedCupConstants.DEVICE_PLUGIN_PROPERTY_REFRESH_TOKEN)); + stmt.setString(4, connectedCupDevice.getDeviceIdentifier()); + int rows = stmt.executeUpdate(); + if (rows > 0) { + status = true; + if (log.isDebugEnabled()) { + log.debug("Connected Cup device " + connectedCupDevice.getDeviceIdentifier() + + " data has been modified."); } - } catch (SQLException e) { - String msg = "Error occurred while deleting Connected Cup device " + iotDeviceId; - log.error(msg, e); - throw new IotDeviceManagementDAOException(msg, e); - } finally { - IotDeviceManagementDAOUtil.cleanupResources(stmt, null); - } - return status; - } - - @Override - public List getAllIotDevices() - throws IotDeviceManagementDAOException { - - Connection conn = null; - PreparedStatement stmt = null; - ResultSet resultSet = null; - IotDevice iotDevice; - List iotDevices = new ArrayList<>(); - - try { - conn = ConnectedCupDAO.getConnection(); - String selectDBQuery = - "SELECT CONNECTED_CUP_DEVICE_ID, DEVICE_NAME, ACCESS_TOKEN, REFRESH_TOKEN" + - "FROM CONNECTED_CUP_DEVICE"; - stmt = conn.prepareStatement(selectDBQuery); - resultSet = stmt.executeQuery(); - while (resultSet.next()) { - iotDevice = new IotDevice(); - iotDevice.setIotDeviceId(resultSet.getString(ConnectedCupConstants.DEVICE_PLUGIN_DEVICE_ID)); - iotDevice.setIotDeviceName(resultSet.getString(ConnectedCupConstants.DEVICE_PLUGIN_DEVICE_NAME)); - - Map propertyMap = new HashMap<>(); - propertyMap.put(ConnectedCupConstants.DEVICE_PLUGIN_PROPERTY_ACCESS_TOKEN, - resultSet.getString("ACCESS_TOKEN")); - propertyMap.put(ConnectedCupConstants.DEVICE_PLUGIN_PROPERTY_REFRESH_TOKEN, - resultSet.getString("REFRESH_TOKEN")); - iotDevice.setDeviceProperties(propertyMap); - iotDevices.add(iotDevice); + } + } catch (SQLException e) { + String msg = "Error occurred while modifying the Connected Cup device '" + + connectedCupDevice.getDeviceIdentifier() + "' data."; + log.error(msg, e); + throw new ConnectedCupDeviceMgtPluginException(msg, e); + } finally { + ConnectedCupUtils.cleanupResources(stmt, null); + } + return status; + } + + public boolean deleteDevice(String deviceId) throws ConnectedCupDeviceMgtPluginException { + boolean status = false; + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = ConnectedCupDAO.getConnection(); + String deleteDBQuery = + "DELETE FROM CONNECTED_CUP_DEVICE WHERE CONNECTED_CUP_DEVICE_ID = ?"; + stmt = conn.prepareStatement(deleteDBQuery); + stmt.setString(1, deviceId); + int rows = stmt.executeUpdate(); + if (rows > 0) { + status = true; + if (log.isDebugEnabled()) { + log.debug("Connected Cup device " + deviceId + " data has deleted" + + " from the Connected Cup database."); } - if (log.isDebugEnabled()) { - log.debug("All Connected Cup device details have fetched from Connected Cup database."); - } - return iotDevices; - } catch (SQLException e) { - String msg = "Error occurred while fetching all Connected Cup device data'"; - log.error(msg, e); - throw new IotDeviceManagementDAOException(msg, e); - } finally { - IotDeviceManagementDAOUtil.cleanupResources(stmt, resultSet); - ConnectedCupDAO.closeConnection(); - } - - } - - } \ No newline at end of file + } + } catch (SQLException e) { + String msg = "Error occurred while deleting Connected Cup device " + deviceId; + log.error(msg, e); + throw new ConnectedCupDeviceMgtPluginException(msg, e); + } finally { + ConnectedCupUtils.cleanupResources(stmt, null); + } + return status; + } + + public List getAllDevices() throws ConnectedCupDeviceMgtPluginException { + + Connection conn = null; + PreparedStatement stmt = null; + ResultSet resultSet = null; + Device connectedCupDevice; + List iotDevices = new ArrayList<>(); + + try { + conn = ConnectedCupDAO.getConnection(); + String selectDBQuery = + "SELECT CONNECTED_CUP_DEVICE_ID, DEVICE_NAME, ACCESS_TOKEN, REFRESH_TOKEN" + + "FROM CONNECTED_CUP_DEVICE"; + stmt = conn.prepareStatement(selectDBQuery); + resultSet = stmt.executeQuery(); + while (resultSet.next()) { + connectedCupDevice = new Device(); + connectedCupDevice.setDeviceIdentifier(resultSet.getString( + ConnectedCupConstants.DEVICE_PLUGIN_DEVICE_ID)); + connectedCupDevice.setName(resultSet.getString( + ConnectedCupConstants.DEVICE_PLUGIN_DEVICE_NAME)); + + List propertyList = new ArrayList(); + propertyList.add(ConnectedCupUtils.getProperty( + ConnectedCupConstants.DEVICE_PLUGIN_PROPERTY_ACCESS_TOKEN, + resultSet.getString("ACCESS_TOKEN"))); + propertyList.add(ConnectedCupUtils.getProperty( + ConnectedCupConstants.DEVICE_PLUGIN_PROPERTY_REFRESH_TOKEN, + resultSet.getString("REFRESH_TOKEN"))); + connectedCupDevice.setProperties(propertyList); + } + if (log.isDebugEnabled()) { + log.debug("All Connected Cup device details have fetched from Connected Cup database" + + "."); + } + return iotDevices; + } catch (SQLException e) { + String msg = "Error occurred while fetching all Connected Cup device data'"; + log.error(msg, e); + throw new ConnectedCupDeviceMgtPluginException(msg, e); + } finally { + ConnectedCupUtils.cleanupResources(stmt, resultSet); + ConnectedCupDAO.closeConnection(); + } + + } + +} \ No newline at end of file diff --git a/modules/samples/connectedcup/component/plugin/src/main/java/org/coffeeking/connectedcup/plugin/impl/dao/util/ConnectedCupUtils.java b/modules/samples/connectedcup/component/plugin/src/main/java/org/coffeeking/connectedcup/plugin/impl/dao/util/ConnectedCupUtils.java index a2e298cb..fce7c509 100644 --- a/modules/samples/connectedcup/component/plugin/src/main/java/org/coffeeking/connectedcup/plugin/impl/dao/util/ConnectedCupUtils.java +++ b/modules/samples/connectedcup/component/plugin/src/main/java/org/coffeeking/connectedcup/plugin/impl/dao/util/ConnectedCupUtils.java @@ -20,7 +20,13 @@ package org.coffeeking.connectedcup.plugin.impl.dao.util; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.Device; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.List; import java.util.Map; /** @@ -30,15 +36,52 @@ public class ConnectedCupUtils { private static Log log = LogFactory.getLog(ConnectedCupUtils.class); - public static String getDeviceProperty(Map deviceProperties, String property) { + public static String getDeviceProperty(List deviceProperties, String propertyKey) { + String deviceProperty = ""; + for(Device.Property property :deviceProperties){ + if(propertyKey.equals(property.getName())){ + deviceProperty = property.getValue(); + } + } + return deviceProperty; + } - String deviceProperty = deviceProperties.get(property); + public static Device.Property getProperty(String property, String value) { + if (property != null) { + Device.Property prop = new Device.Property(); + prop.setName(property); + prop.setValue(value); + return prop; + } + return null; + } - if (deviceProperty == null) { - return ""; + public static void cleanupResources(Connection conn, PreparedStatement stmt, ResultSet rs) { + if (rs != null) { + try { + rs.close(); + } catch (SQLException e) { + log.warn("Error occurred while closing result set", e); + } } + if (stmt != null) { + try { + stmt.close(); + } catch (SQLException e) { + log.warn("Error occurred while closing prepared statement", e); + } + } + if (conn != null) { + try { + conn.close(); + } catch (SQLException e) { + log.warn("Error occurred while closing database connection", e); + } + } + } - return deviceProperty; + public static void cleanupResources(PreparedStatement stmt, ResultSet rs) { + cleanupResources(null, stmt, rs); } diff --git a/modules/samples/connectedcup/component/plugin/src/main/java/org/coffeeking/connectedcup/plugin/internal/ConnectedCupServiceComponent.java b/modules/samples/connectedcup/component/plugin/src/main/java/org/coffeeking/connectedcup/plugin/internal/ConnectedCupServiceComponent.java index f14f3300..d224d979 100644 --- a/modules/samples/connectedcup/component/plugin/src/main/java/org/coffeeking/connectedcup/plugin/internal/ConnectedCupServiceComponent.java +++ b/modules/samples/connectedcup/component/plugin/src/main/java/org/coffeeking/connectedcup/plugin/internal/ConnectedCupServiceComponent.java @@ -25,18 +25,11 @@ import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; import org.osgi.service.component.ComponentContext; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; -import org.wso2.carbon.device.mgt.iot.service.DeviceTypeService; /** * @scr.component name="org.coffeeking.connectedcup.plugin.internal * .ConnectedCupServiceComponent" * immediate="true" - * @scr.reference name="org.wso2.carbon.device.mgt.iot.service.DeviceTypeServiceImpl" - * interface="org.wso2.carbon.device.mgt.iot.service.DeviceTypeService" - * cardinality="1..1" - * policy="dynamic" - * bind="setDeviceTypeService" - * unbind="unsetDeviceTypeService" */ public class ConnectedCupServiceComponent { @@ -77,15 +70,4 @@ public class ConnectedCupServiceComponent { log.error("Error occurred while de-activating Connected Cup Service Component", e); } } - - protected void setDeviceTypeService(DeviceTypeService deviceTypeService) { - /* This is to avoid this component getting initialized before the - common registered */ - if (log.isDebugEnabled()) { - log.debug("Data source service set to mobile service component"); - } - } - - protected void unsetDeviceTypeService(DeviceTypeService deviceTypeService) { - } }