From 20a9d1222f3224b3433744d1f2c446e279b33e88 Mon Sep 17 00:00:00 2001 From: harshanl Date: Fri, 3 Jul 2015 14:35:26 +0530 Subject: [PATCH] Added a method to retrieve list of applications installed on a device --- .../carbon/device/mgt/core/dao/DeviceDAO.java | 17 ++++++ .../mgt/core/dao/impl/DeviceDAOImpl.java | 52 +++++++++++++++++++ .../DeviceManagementProviderService.java | 2 +- .../DeviceManagementProviderServiceImpl.java | 12 ++++- 4 files changed, 80 insertions(+), 3 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index 4e13a34f89..fc4e34dfd8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -75,5 +75,22 @@ public interface DeviceDAO { */ List getDevicesByName(String deviceName, int tenantId) throws DeviceManagementDAOException; + /** + * Get the list of devices that matches with the given device name. + * + * @param id Name of the device + * @param applications List of applications + * @throws DeviceManagementDAOException + */ void addDeviceApplications(int id, Object applications) throws DeviceManagementDAOException; + + /** + * Get the list of devices that matches with the given device name. + * + * @param deviceId device id of the device + * @return List of Applications that are installed on the given device. + * @throws DeviceManagementDAOException + */ + List getInstalledApplications(int deviceId) + throws DeviceManagementDAOException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java index ad447730e1..9a2d11528f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java @@ -18,6 +18,8 @@ package org.wso2.carbon.device.mgt.core.dao.impl; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; @@ -29,7 +31,11 @@ import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dto.DeviceType; +import org.wso2.carbon.device.mgt.core.dto.operation.mgt.ProfileOperation; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.ObjectInputStream; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -40,6 +46,8 @@ import java.util.List; public class DeviceDAOImpl implements DeviceDAO { + private static final Log log = LogFactory.getLog(DeviceDAOImpl.class); + @Override public void addDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException { Connection conn; @@ -317,6 +325,50 @@ public class DeviceDAOImpl implements DeviceDAO { } } + @Override + public List getInstalledApplications(int deviceId) + throws DeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + List applications = new ArrayList(); + Application application; + ByteArrayInputStream bais; + ObjectInputStream ois; + + try { + conn = this.getConnection(); + stmt = conn.prepareStatement( + "SELECT DEVICE_ID, APPLICATIONS FROM DM_DEVICE_APPLICATIONS WHERE DEVICE_ID = ?"); + stmt.setInt(1, deviceId); + ResultSet rs = stmt.executeQuery(); + + while (rs.next()) { + byte[] applicationDetails = rs.getBytes("APPLICATIONS"); + bais = new ByteArrayInputStream(applicationDetails); + ois = new ObjectInputStream(bais); + application = (Application) ois.readObject(); + applications.add(application); + } + + }catch (IOException e) { + String errorMsg = "IO Error occurred while de serialize the Application object"; + log.error(errorMsg, e); + throw new DeviceManagementDAOException(errorMsg, e); + } catch (ClassNotFoundException e) { + String errorMsg = "Class not found error occurred while de serialize the Application object"; + log.error(errorMsg, e); + throw new DeviceManagementDAOException(errorMsg, e); + } catch (SQLException e) { + String errorMsg = "SQL Error occurred while retrieving the list of Applications installed in device id '" + + deviceId; + log.error(errorMsg, e); + throw new DeviceManagementDAOException(errorMsg, e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, null); + } + return applications; + } + private Device loadDevice(ResultSet rs) throws SQLException { Device device = new Device(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index 9602ad390b..ba8fd26d59 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -82,7 +82,7 @@ public interface DeviceManagementProviderService extends DeviceManager, LicenseM * The method to get application list installed for the device. * * @param deviceIdentifier - * @return + * @return List of applications installed on the device * @throws DeviceManagementException */ List getApplicationListForDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index ad002b76cd..09c090e117 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -648,7 +648,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getApplicationListForDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementException { - return null; + Device device = null; + try { + device = this.getDevice(deviceIdentifier); + return deviceDAO.getInstalledApplications(device.getId()); + }catch (DeviceManagementDAOException deviceDaoEx){ + String errorMsg = "Error occured while fetching the Application List of device : " + device.getId(); + log.error(errorMsg, deviceDaoEx); + throw new DeviceManagementException(errorMsg, deviceDaoEx); + } } @Override @@ -661,7 +669,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv device.getEnrolmentInfo().setStatus(status); deviceDAO.updateDevice(deviceType.getId(), device, tenantId); }catch (DeviceManagementDAOException deviceDaoEx){ - String errorMsg = "Error occured update device enrolment status:"+device.getId(); + String errorMsg = "Error occured update device enrolment status : "+device.getId(); log.error(errorMsg, deviceDaoEx); throw new DeviceManagementException(errorMsg, deviceDaoEx); }