From e7b0255a957d3542708a876b20ce668ee9c2ab31 Mon Sep 17 00:00:00 2001 From: osh Date: Tue, 18 Jul 2023 19:34:11 +0530 Subject: [PATCH 1/2] Add get device apps by id service --- .../common/services/ApplicationManager.java | 11 +++ .../mgt/core/dao/ApplicationDAO.java | 10 +++ .../GenericApplicationDAOImpl.java | 70 +++++++++++++++++++ .../mgt/core/impl/ApplicationManagerImpl.java | 29 ++++++++ .../application/mgt/core/util/DAOUtil.java | 20 ++++++ 5 files changed, 140 insertions(+) diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/ApplicationManager.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/ApplicationManager.java index 0c75b3b284..7451b8469f 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/ApplicationManager.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/ApplicationManager.java @@ -273,6 +273,17 @@ public interface ApplicationManager { */ ApplicationDTO getApplication(int applicationId) throws ApplicationManagementException; + /** + * This method is responsible to provide application data for given deviceId. + * + * @param deviceId id of the device + * @return {@link ApplicationDTO} + * @throws ApplicationManagementException + * if an error occurred while getting subscribed app details for relevant device id, + */ + ApplicationList getSubscribedAppsOfDevice(int deviceId) + throws ApplicationManagementException; + /** * To get the Application for given Id. * diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/ApplicationDAO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/ApplicationDAO.java index 6886e824f2..d46ed6336b 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/ApplicationDAO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/ApplicationDAO.java @@ -150,6 +150,16 @@ public interface ApplicationDAO { */ ApplicationDTO getApplication(int applicationId, int tenantId) throws ApplicationManagementDAOException; + /** + * To get the application with the given id + * + * @param deviceId ID of the device which the apps are installed on. + * @param tenantId ID of the tenant. + * @return the application + * @throws ApplicationManagementDAOException ApplicationDTO Management DAO Exception. + */ + List getSubscribedAppsOfDevice(int deviceId, int tenantId) throws ApplicationManagementDAOException; + /** * To get the application with the given uuid * diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java index a6abaef4cb..361746f623 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java @@ -656,6 +656,76 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } } + @Override + public List getSubscribedAppsOfDevice(int deviceId, int tenantId) throws + ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Getting all installed apps of device " + deviceId + + " from the database"); + } + List appList = null; + + String sql = "SELECT " + + "AP_APP.ID AS APP_ID, " + + "AP_APP.NAME AS APP_NAME, " + + "AP_APP.DESCRIPTION AS APP_DESCRIPTION, " + + "AP_APP.TYPE AS APP_TYPE, " + + "AP_APP.STATUS AS APP_STATUS, " + + "AP_APP.SUB_TYPE AS APP_SUB_TYPE, " + + "AP_APP.CURRENCY AS APP_CURRENCY, " + + "AP_APP.RATING AS APP_RATING, " + + "AP_APP.DEVICE_TYPE_ID AS APP_DEVICE_TYPE_ID, " + + "AP_APP_RELEASE.ID AS RELEASE_ID, " + + "AP_APP_RELEASE.DESCRIPTION AS RELEASE_DESCRIPTION, " + + "AP_APP_RELEASE.VERSION AS RELEASE_VERSION, " + + "AP_APP_RELEASE.UUID AS RELEASE_UUID, " + + "AP_APP_RELEASE.RELEASE_TYPE AS RELEASE_TYPE, " + + "AP_APP_RELEASE.INSTALLER_LOCATION AS AP_RELEASE_STORED_LOC, " + + "AP_APP_RELEASE.ICON_LOCATION AS AP_RELEASE_ICON_LOC, " + + "AP_APP_RELEASE.BANNER_LOCATION AS AP_RELEASE_BANNER_LOC, " + + "AP_APP_RELEASE.SC_1_LOCATION AS AP_RELEASE_SC1, " + + "AP_APP_RELEASE.SC_2_LOCATION AS AP_RELEASE_SC2, " + + "AP_APP_RELEASE.SC_3_LOCATION AS AP_RELEASE_SC3, " + + "AP_APP_RELEASE.APP_HASH_VALUE AS RELEASE_HASH_VALUE, " + + "AP_APP_RELEASE.APP_PRICE AS RELEASE_PRICE, " + + "AP_APP_RELEASE.APP_META_INFO AS RELEASE_META_INFO, " + + "AP_APP_RELEASE.PACKAGE_NAME AS PACKAGE_NAME, " + + "AP_APP_RELEASE.SUPPORTED_OS_VERSIONS AS RELEASE_SUP_OS_VERSIONS, " + + "AP_APP_RELEASE.RATING AS RELEASE_RATING, " + + "AP_APP_RELEASE.CURRENT_STATE AS RELEASE_CURRENT_STATE, " + + "AP_APP_RELEASE.RATED_USERS AS RATED_USER_COUNT " + + "FROM AP_APP " + + "JOIN AP_APP_RELEASE ON AP_APP.ID = AP_APP_RELEASE.AP_APP_ID " + + "JOIN AP_DEVICE_SUBSCRIPTION ON AP_APP_RELEASE.ID = AP_DEVICE_SUBSCRIPTION.AP_APP_RELEASE_ID " + + "WHERE AP_DEVICE_SUBSCRIPTION.DM_DEVICE_ID = ? AND AP_DEVICE_SUBSCRIPTION.TENANT_ID= ? " + +"AND AP_DEVICE_SUBSCRIPTION.STATUS= 'COMPLETED'"; + + try { + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, deviceId); + stmt.setInt(2, tenantId); + try (ResultSet rs = stmt.executeQuery()) { + appList = new ArrayList<>(); + while (rs.next()) { + ApplicationDTO app = DAOUtil.loadDeviceApp(rs); + appList.add(app); + } + return appList; + } + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection for getting all apps installed on the device of " + + "device Id: " + deviceId + "."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while while running SQL to get all installed apps of device with device Id: " + deviceId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } + @Override public boolean updateApplication(ApplicationDTO applicationDTO, int tenantId) throws ApplicationManagementDAOException { diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/ApplicationManagerImpl.java index 3bb8ba8260..257994d0a9 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/ApplicationManagerImpl.java @@ -1444,6 +1444,35 @@ public class ApplicationManagerImpl implements ApplicationManager { } } + @Override + public ApplicationList getSubscribedAppsOfDevice(int deviceId) throws ApplicationManagementException { + ApplicationList applicationList = new ApplicationList(); + List applications = new ArrayList<>(); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + try { + ConnectionManagerUtil.openDBConnection(); + List applicationDTOS = this.applicationDAO.getSubscribedAppsOfDevice(deviceId, tenantId); + for (ApplicationDTO applicationDTO: applicationDTOS) { + applicationDTO.setTags(this.applicationDAO.getAppTags(applicationDTO.getId(), tenantId)); + applicationDTO.setAppCategories(this.applicationDAO.getAppCategories(applicationDTO.getId(), tenantId)); + applications.add(APIUtil.appDtoToAppResponse(applicationDTO)); + } + applicationList.setApplications(applications); + return applicationList; + } catch (ApplicationManagementDAOException e) { + String msg = "Error occurred when getting installed apps of device with device id: " + + deviceId; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (DBConnectionException e) { + String msg = "DB Connection error occurred while getting installed apps of device with device id: " + deviceId; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + /** * Check whether given OS range is valid or invalid * diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/DAOUtil.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/DAOUtil.java index 1960230759..5538cbfa05 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/DAOUtil.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/DAOUtil.java @@ -241,6 +241,26 @@ public class DAOUtil { return applicationDTOs.get(0); } + public static ApplicationDTO loadDeviceApp(ResultSet rs) throws SQLException { + ApplicationDTO application = new ApplicationDTO(); + application.setId( rs.getInt("APP_ID")); + application.setName(rs.getString("APP_NAME")); + application.setDescription(rs.getString("APP_DESCRIPTION")); + application.setType(rs.getString("APP_TYPE")); + application.setSubType(rs.getString("APP_SUB_TYPE")); + application.setPaymentCurrency(rs.getString("APP_CURRENCY")); + application.setStatus(rs.getString("APP_STATUS")); + application.setAppRating(rs.getDouble("APP_RATING")); + application.setDeviceTypeId(rs.getInt("APP_DEVICE_TYPE_ID")); + ApplicationReleaseDTO releaseDTO = constructAppReleaseDTO(rs); + List releaseDtoList = new ArrayList<>(); + if (releaseDTO != null) { + releaseDtoList.add(constructAppReleaseDTO(rs)); + application.setApplicationReleaseDTOs(releaseDtoList); + } + return application; + } + /** * Populates {@link ApplicationReleaseDTO} object with the result obtained from the database. * From 77b68af5d0a80d194fdd4a4f5061efe734f21b44 Mon Sep 17 00:00:00 2001 From: osh Date: Tue, 18 Jul 2023 23:10:23 +0530 Subject: [PATCH 2/2] Add pagination --- .../mgt/common/services/ApplicationManager.java | 3 ++- .../application/mgt/core/dao/ApplicationDAO.java | 3 ++- .../impl/application/GenericApplicationDAOImpl.java | 11 ++++++++++- .../mgt/core/impl/ApplicationManagerImpl.java | 13 +++++++++++-- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/ApplicationManager.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/ApplicationManager.java index 7451b8469f..57890f53eb 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/ApplicationManager.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/ApplicationManager.java @@ -27,6 +27,7 @@ import io.entgra.device.mgt.core.application.mgt.common.response.Category; import io.entgra.device.mgt.core.application.mgt.common.response.Tag; import io.entgra.device.mgt.core.device.mgt.common.Base64File; import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationDTO; +import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest; import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException; import org.apache.cxf.jaxrs.ext.multipart.Attachment; import io.entgra.device.mgt.core.application.mgt.common.ApplicationArtifact; @@ -281,7 +282,7 @@ public interface ApplicationManager { * @throws ApplicationManagementException * if an error occurred while getting subscribed app details for relevant device id, */ - ApplicationList getSubscribedAppsOfDevice(int deviceId) + ApplicationList getSubscribedAppsOfDevice(int deviceId, PaginationRequest request) throws ApplicationManagementException; /** diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/ApplicationDAO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/ApplicationDAO.java index d46ed6336b..ffa85b260d 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/ApplicationDAO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/ApplicationDAO.java @@ -22,6 +22,7 @@ import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.CategoryDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.TagDTO; import io.entgra.device.mgt.core.application.mgt.core.exception.ApplicationManagementDAOException; +import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest; import java.util.List; @@ -158,7 +159,7 @@ public interface ApplicationDAO { * @return the application * @throws ApplicationManagementDAOException ApplicationDTO Management DAO Exception. */ - List getSubscribedAppsOfDevice(int deviceId, int tenantId) throws ApplicationManagementDAOException; + List getSubscribedAppsOfDevice(int deviceId, int tenantId, PaginationRequest request) throws ApplicationManagementDAOException; /** * To get the application with the given uuid diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java index 361746f623..1e403e5848 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java @@ -22,6 +22,7 @@ import io.entgra.device.mgt.core.application.mgt.core.dao.impl.AbstractDAOImpl; import io.entgra.device.mgt.core.application.mgt.core.exception.UnexpectedServerErrorException; import io.entgra.device.mgt.core.application.mgt.core.util.Constants; import io.entgra.device.mgt.core.application.mgt.core.util.DAOUtil; +import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -657,7 +658,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } @Override - public List getSubscribedAppsOfDevice(int deviceId, int tenantId) throws + public List getSubscribedAppsOfDevice(int deviceId, int tenantId, PaginationRequest request) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Getting all installed apps of device " + deviceId @@ -700,11 +701,19 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic + "WHERE AP_DEVICE_SUBSCRIPTION.DM_DEVICE_ID = ? AND AP_DEVICE_SUBSCRIPTION.TENANT_ID= ? " +"AND AP_DEVICE_SUBSCRIPTION.STATUS= 'COMPLETED'"; + if (request != null) { + sql = sql + " LIMIT ?,?"; + } + try { Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, deviceId); stmt.setInt(2, tenantId); + if (request != null) { + stmt.setInt(3, request.getStartIndex()); + stmt.setInt(4, request.getRowCount()); + } try (ResultSet rs = stmt.executeQuery()) { appList = new ArrayList<>(); while (rs.next()) { diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/ApplicationManagerImpl.java index 257994d0a9..22f5688b07 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/ApplicationManagerImpl.java @@ -22,6 +22,7 @@ import io.entgra.device.mgt.core.application.mgt.core.exception.BadRequestExcept import io.entgra.device.mgt.core.device.mgt.common.Base64File; import io.entgra.device.mgt.core.application.mgt.core.dao.SPApplicationDAO; import io.entgra.device.mgt.core.application.mgt.core.util.ApplicationManagementUtil; +import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest; import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.Metadata; import org.apache.commons.codec.digest.DigestUtils; @@ -1445,19 +1446,27 @@ public class ApplicationManagerImpl implements ApplicationManager { } @Override - public ApplicationList getSubscribedAppsOfDevice(int deviceId) throws ApplicationManagementException { + public ApplicationList getSubscribedAppsOfDevice(int deviceId, PaginationRequest request) throws ApplicationManagementException { ApplicationList applicationList = new ApplicationList(); List applications = new ArrayList<>(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); try { ConnectionManagerUtil.openDBConnection(); - List applicationDTOS = this.applicationDAO.getSubscribedAppsOfDevice(deviceId, tenantId); + List applicationDTOS = this.applicationDAO.getSubscribedAppsOfDevice(deviceId, tenantId, request); for (ApplicationDTO applicationDTO: applicationDTOS) { applicationDTO.setTags(this.applicationDAO.getAppTags(applicationDTO.getId(), tenantId)); applicationDTO.setAppCategories(this.applicationDAO.getAppCategories(applicationDTO.getId(), tenantId)); applications.add(APIUtil.appDtoToAppResponse(applicationDTO)); } + + List totalApplications = this.applicationDAO.getSubscribedAppsOfDevice(deviceId, tenantId, null); + Pagination pagination = new Pagination(); + pagination.setCount(totalApplications.size()); + pagination.setSize(applications.size()); + pagination.setOffset(request.getStartIndex()); + pagination.setLimit(request.getRowCount()); applicationList.setApplications(applications); + applicationList.setPagination(pagination); return applicationList; } catch (ApplicationManagementDAOException e) { String msg = "Error occurred when getting installed apps of device with device id: "