diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationDAO.java index 6ff8e3651ef..a77767d054c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationDAO.java @@ -97,4 +97,22 @@ public interface ApplicationDAO { * @throws DeviceManagementDAOException If any database error occurred */ void deleteApplicationIcon(String packageName) throws DeviceManagementDAOException; + + /** + * This method is used to get the installed application list of a specific device + * @param deviceId ID of the device + * @param enrolmentId Enrolment ID of the device + * @param tenantId tenant ID + * @throws DeviceManagementDAOException If any database error occurred + */ + List getInstalledApplicationListOnDevice(int deviceId, int enrolmentId, int offset, int limit, int tenantId) + throws DeviceManagementDAOException; + + /** + * This method is used to retrieve the icon info of an installed app in device. + * @param applicationIdentifier application identifier. + * @return returns the application icon path. + * @throws DeviceManagementDAOException + */ + String getIconPath(String applicationIdentifier) throws DeviceManagementDAOException; } 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 f07d667b43a..e9c4e1bf9e4 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 @@ -855,13 +855,4 @@ public interface DeviceDAO { * @throws DeviceManagementDAOException */ List getAgentVersions(int tenantId) throws DeviceManagementDAOException; - - /** - * This method is used to retrieve the icon info of an installed app in device. - * - * @param applicationIdentifier application identifier. - * @return returns the application icon path. - * @throws DeviceManagementDAOException - */ - String getIconPath(String applicationIdentifier) 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/AbstractDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index 4bc3cd43264..08725aeddbe 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -3236,27 +3236,4 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } return agentVersions; } - - @Override - public String getIconPath(String applicationIdentifier) throws DeviceManagementDAOException{ - Connection conn; - PreparedStatement stmt = null; - String iconPath = null; - try{ - conn = this.getConnection(); - stmt = conn.prepareStatement("SELECT ICON_PATH FROM DM_APP_ICONS" + - " WHERE PACKAGE_NAME = ?"); - stmt.setString(1,applicationIdentifier); - try (ResultSet rs = stmt.executeQuery()) { - if (rs.next()) { - iconPath = rs.getString("ICON_PATH"); - } - return iconPath; - } - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while retrieving app icon path"); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, null); - } - } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java index 14ab224b1f6..292f25aa860 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java @@ -476,4 +476,59 @@ public class ApplicationDAOImpl implements ApplicationDAO { DeviceManagementDAOUtil.cleanupResources(stmt, null); } } + + @Override + public String getIconPath(String applicationIdentifier) throws DeviceManagementDAOException{ + Connection conn; + PreparedStatement stmt = null; + String iconPath = null; + try{ + conn = this.getConnection(); + stmt = conn.prepareStatement("SELECT ICON_PATH FROM DM_APP_ICONS" + + " WHERE PACKAGE_NAME = ?"); + stmt.setString(1,applicationIdentifier); + try (ResultSet rs = stmt.executeQuery()) { + if (rs.next()) { + iconPath = rs.getString("ICON_PATH"); + } + return iconPath; + } + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving app icon path"); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, null); + } + } + + @Override + public List getInstalledApplicationListOnDevice(int deviceId, int enrolmentId, int offset, int limit, int tenantId) + throws DeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + List applicationList = new ArrayList<>(); + Application application; + ResultSet rs = null; + try { + conn = this.getConnection(); + stmt = conn.prepareStatement("SELECT ID, NAME, APP_IDENTIFIER, PLATFORM, CATEGORY, VERSION, TYPE, " + + "LOCATION_URL, IMAGE_URL, APP_PROPERTIES, MEMORY_USAGE, IS_ACTIVE, TENANT_ID FROM DM_APPLICATION " + + "WHERE DEVICE_ID = ? AND ENROLMENT_ID = ? AND TENANT_ID = ? LIMIT ? OFFSET ?"); + stmt.setInt(1, deviceId); + stmt.setInt(2, enrolmentId); + stmt.setInt(3, tenantId); + stmt.setInt(4, limit); + stmt.setInt(5, offset); + rs = stmt.executeQuery(); + while (rs.next()) { + application = loadApplication(rs); + applicationList.add(application); + } + } catch (SQLException e) { + throw new DeviceManagementDAOException("SQL Error occurred while retrieving the list of Applications " + + "installed in device id '" + deviceId, e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + return applicationList; + } } 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 4ca4eafaeae..ee1c5fa4a00 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 @@ -37,6 +37,7 @@ package org.wso2.carbon.device.mgt.core.service; import org.apache.commons.collections.map.SingletonMap; import org.wso2.carbon.device.mgt.common.*; +import org.wso2.carbon.device.mgt.common.app.mgt.Application; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException; import org.wso2.carbon.device.mgt.common.configuration.mgt.AmbiguousConfigurationException; import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException; @@ -1063,4 +1064,7 @@ public interface DeviceManagementProviderService { * @throws DeviceManagementException if any service level or DAO level error occurs */ void deleteApplicationIcon(String packageName) throws DeviceManagementException; + + List getInstalledApplicationsOnDevice(Device device, int offset, int limit) + 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 87e3452a4df..202fa59e5a9 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 @@ -52,25 +52,7 @@ import org.opensaml.xmlsec.signature.P; import org.wso2.carbon.CarbonConstants; import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.device.mgt.common.ActivityPaginationRequest; -import org.wso2.carbon.device.mgt.common.Billing; -import org.wso2.carbon.device.mgt.common.Device; -import org.wso2.carbon.device.mgt.common.DeviceEnrollmentInfoNotification; -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.device.mgt.common.DeviceManager; -import org.wso2.carbon.device.mgt.common.DeviceNotification; -import org.wso2.carbon.device.mgt.common.DevicePropertyNotification; -import org.wso2.carbon.device.mgt.common.DeviceTransferRequest; -import org.wso2.carbon.device.mgt.common.DynamicTaskContext; -import org.wso2.carbon.device.mgt.common.EnrolmentInfo; -import org.wso2.carbon.device.mgt.common.FeatureManager; -import org.wso2.carbon.device.mgt.common.InitialOperationConfig; -import org.wso2.carbon.device.mgt.common.MonitoringOperation; -import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig; -import org.wso2.carbon.device.mgt.common.PaginationRequest; -import org.wso2.carbon.device.mgt.common.PaginationResult; -import org.wso2.carbon.device.mgt.common.StartupOperationConfig; -import org.wso2.carbon.device.mgt.common.BillingResponse; +import org.wso2.carbon.device.mgt.common.*; import org.wso2.carbon.device.mgt.common.app.mgt.Application; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException; import org.wso2.carbon.device.mgt.common.app.mgt.MobileAppTypes; @@ -171,17 +153,7 @@ import java.lang.reflect.Type; import java.sql.SQLException; import java.sql.Timestamp; import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Collection; -import java.util.Collections; -import java.util.Date; -import java.util.Enumeration; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Properties; +import java.util.*; import java.util.concurrent.TimeUnit; import java.util.concurrent.ExecutionException; import java.util.stream.Collectors; @@ -3707,9 +3679,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } finally { DeviceManagementDAOFactory.closeConnection(); } - if (applications != null){ - this.getInstalledAppIconInfo(device); - } return applications; } @@ -4988,16 +4957,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.closeConnection(); } } - private void getInstalledAppIconInfo(Device device) throws DeviceManagementException { - List applications = device.getApplications(); + + private List getInstalledAppIconInfo(List applications) throws DeviceManagementException { String iconPath; try { DeviceManagementDAOFactory.openConnection(); for (Application app : applications) { - iconPath = deviceDAO.getIconPath(app.getApplicationIdentifier()); + iconPath = applicationDAO.getIconPath(app.getApplicationIdentifier()); app.setImageUrl(iconPath); } - DeviceManagementDAOFactory.commitTransaction(); } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); String msg = "Error occurred while retrieving installed app icon info"; @@ -5011,5 +4979,41 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } finally { DeviceManagementDAOFactory.closeConnection(); } + return applications; + } + + @Override + public List getInstalledApplicationsOnDevice(Device device, int offset, int limit) throws DeviceManagementException { + List applications; + try { + DeviceManagementDAOFactory.openConnection(); + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + applications = applicationDAO.getInstalledApplicationListOnDevice(device.getId(), + device.getEnrolmentInfo().getId(), offset, limit, tenantId); + if (applications == null) { + String msg = "Couldn't found applications for device identifier '" + device.getId() + "'"; + log.error(msg); + throw new DeviceManagementException(msg); + } + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while retrieving the application list of android device, " + + "which carries the id '" + device.getId() + "'"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + List newApplicationList; + newApplicationList = this.getInstalledAppIconInfo(applications); + if (newApplicationList == null) { + String msg = "Error occurred while getting app icon info for device identifier '" + device.getId() + "'"; + log.error(msg); + throw new DeviceManagementException(msg); + } + return newApplicationList; } }