From 1ac18c80b345be8dcacde6c3a9ae0f105021860f Mon Sep 17 00:00:00 2001 From: Amanda Randombage <74random.amanda@gmail.com> Date: Wed, 15 Apr 2020 00:48:26 +0000 Subject: [PATCH] Fix issues in GET /applications API --- .../device/application/mgt/common/Filter.java | 4 +- .../GenericApplicationDAOImpl.java | 81 +++++++++--------- .../application/OracleApplicationDAOImpl.java | 83 +++++++++---------- .../SQLServerApplicationDAOImpl.java | 83 +++++++++---------- .../mgt/core/impl/ApplicationManagerImpl.java | 3 - 5 files changed, 118 insertions(+), 136 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Filter.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Filter.java index 942f2cf309..6e45015c41 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Filter.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Filter.java @@ -21,6 +21,7 @@ import java.util.List; /** * Filter represents a criteria that can be used for searching applications. + * The default value for limit is 20 */ public class Filter { @@ -87,8 +88,9 @@ public class Filter { /** * Limit of the applications + * default: 20 */ - private int limit; + private int limit = 20; /** * Started from diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java index b0d4f7f381..a4155d8ba2 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java @@ -94,7 +94,13 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } @Override - public List getApplications(Filter filter,int deviceTypeId, int tenantId) throws ApplicationManagementDAOException { + public List getApplications(Filter filter, int deviceTypeId, int tenantId) throws + ApplicationManagementDAOException { + if (filter == null) { + String msg = "Filter is not instantiated for tenant "+tenantId; + log.error(msg); + throw new ApplicationManagementDAOException(msg); + } if (log.isDebugEnabled()) { log.debug("Getting application data from the database"); log.debug(String.format("Filter: limit=%s, offset=%s", filter.getLimit(), filter.getOffset())); @@ -131,74 +137,60 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic + "FROM AP_APP " + "INNER JOIN AP_APP_RELEASE ON " + "AP_APP.ID = AP_APP_RELEASE.AP_APP_ID " - + "INNER JOIN (SELECT ID FROM AP_APP WHERE AP_APP.TENANT_ID = ? LIMIT ? OFFSET ? ) AS app_data ON app_data.ID = AP_APP.ID " - + "WHERE AP_APP.TENANT_ID = ?"; - - if (filter == null) { - String msg = "Filter is not instantiated."; - log.error(msg); - throw new ApplicationManagementDAOException(msg); + + "INNER JOIN (SELECT AP_APP.ID FROM AP_APP "; + if (!StringUtils.isEmpty(filter.getVersion()) || !StringUtils.isEmpty(filter.getAppReleaseState()) + || !StringUtils.isEmpty(filter.getAppReleaseType())) { + sql += "INNER JOIN AP_APP_RELEASE ON AP_APP.ID = AP_APP_RELEASE.AP_APP_ID "; } + sql += "WHERE AP_APP.TENANT_ID = ? "; if (!StringUtils.isEmpty(filter.getAppType())) { - sql += " AND AP_APP.TYPE = ?"; + sql += "AND AP_APP.TYPE = ? "; } if (!StringUtils.isEmpty(filter.getAppName())) { sql += " AND LOWER (AP_APP.NAME) "; if (filter.isFullMatch()) { - sql += "= ?"; + sql += "= ? "; } else { - sql += "LIKE ?"; + sql += "LIKE ? "; } } if (!StringUtils.isEmpty(filter.getSubscriptionType())) { - sql += " AND AP_APP.SUB_TYPE = ?"; + sql += "AND AP_APP.SUB_TYPE = ? "; } if (filter.getMinimumRating() > 0) { - sql += " AND AP_APP.RATING >= ?"; + sql += "AND AP_APP.RATING >= ? "; } if (!StringUtils.isEmpty(filter.getVersion())) { - sql += " AND AP_APP_RELEASE.VERSION = ?"; + sql += "AND AP_APP_RELEASE.VERSION = ? "; } if (!StringUtils.isEmpty(filter.getAppReleaseType())) { - sql += " AND AP_APP_RELEASE.RELEASE_TYPE = ?"; + sql += "AND AP_APP_RELEASE.RELEASE_TYPE = ? "; } if (!StringUtils.isEmpty(filter.getAppReleaseState())) { - sql += " AND AP_APP_RELEASE.CURRENT_STATE = ?"; + sql += "AND AP_APP_RELEASE.CURRENT_STATE = ? "; } if (deviceTypeId != -1) { - sql += " AND AP_APP.DEVICE_TYPE_ID = ?"; + sql += "AND AP_APP.DEVICE_TYPE_ID = ? "; } - - if (filter.getLimit() == -1) { - sql = sql.replace("LIMIT ? OFFSET ?", ""); + sql += "GROUP BY AP_APP.ID "; + if (!StringUtils.isEmpty(filter.getSortBy())) { + sql += "ORDER BY ID " + filter.getSortBy() +" "; } - - String sortingOrder = "ASC"; - if (!StringUtils.isEmpty(filter.getSortBy() )) { - sortingOrder = filter.getSortBy(); + if (filter.getLimit() != -1) { + sql += " LIMIT ? OFFSET ? "; } - sql += " ORDER BY APP_ID " + sortingOrder; - + sql += ") AS app_data ON app_data.ID = AP_APP.ID " + + "WHERE AP_APP.TENANT_ID = ?"; try { Connection conn = this.getDBConnection(); - try (PreparedStatement stmt = conn.prepareStatement(sql)){ + try (PreparedStatement stmt = conn.prepareStatement(sql)) { int paramIndex = 1; stmt.setInt(paramIndex++, tenantId); - if (filter.getLimit() != -1) { - if (filter.getLimit() == 0) { - stmt.setInt(paramIndex++, 100); - } else { - stmt.setInt(paramIndex++, filter.getLimit()); - } - stmt.setInt(paramIndex++, filter.getOffset()); - } - stmt.setInt(paramIndex++, tenantId); - - if (filter.getAppType() != null && !filter.getAppType().isEmpty()) { + if (!StringUtils.isEmpty(filter.getAppType())) { stmt.setString(paramIndex++, filter.getAppType()); } - if (filter.getAppName() != null && !filter.getAppName().isEmpty()) { + if (!StringUtils.isEmpty(filter.getAppName())) { if (filter.isFullMatch()) { stmt.setString(paramIndex++, filter.getAppName().toLowerCase()); } else { @@ -220,10 +212,15 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic if (!StringUtils.isEmpty(filter.getAppReleaseState())) { stmt.setString(paramIndex++, filter.getAppReleaseState()); } - if (deviceTypeId > 0 ) { - stmt.setInt(paramIndex, deviceTypeId); + if (deviceTypeId > 0) { + stmt.setInt(paramIndex++, deviceTypeId); } - try (ResultSet rs = stmt.executeQuery() ) { + if (filter.getLimit() != -1) { + stmt.setInt(paramIndex++, filter.getLimit()); + stmt.setInt(paramIndex++, filter.getOffset()); + } + stmt.setInt(paramIndex, tenantId); + try (ResultSet rs = stmt.executeQuery()) { return DAOUtil.loadApplications(rs); } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/OracleApplicationDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/OracleApplicationDAOImpl.java index b9de2a1ac3..d39469eedf 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/OracleApplicationDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/OracleApplicationDAOImpl.java @@ -41,13 +41,17 @@ public class OracleApplicationDAOImpl extends GenericApplicationDAOImpl { private static final Log log = LogFactory.getLog(OracleApplicationDAOImpl.class); @Override - public List getApplications(Filter filter,int deviceTypeId, int tenantId) throws + public List getApplications(Filter filter, int deviceTypeId, int tenantId) throws ApplicationManagementDAOException { + if (filter == null) { + String msg = "Filter is not instantiated for tenant " + tenantId; + log.error(msg); + throw new ApplicationManagementDAOException(msg); + } if (log.isDebugEnabled()) { log.debug("Getting application data from the database"); log.debug(String.format("Filter: limit=%s, offset=%s", filter.getLimit(), filter.getOffset())); } - int paramIndex = 1; String sql = "SELECT " + "AP_APP.ID AS APP_ID, " + "AP_APP.NAME AS APP_NAME, " @@ -80,73 +84,57 @@ public class OracleApplicationDAOImpl extends GenericApplicationDAOImpl { + "FROM AP_APP " + "INNER JOIN AP_APP_RELEASE ON " + "AP_APP.ID = AP_APP_RELEASE.AP_APP_ID " - + "INNER JOIN (SELECT ID FROM AP_APP ORDER BY ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY) AS app_data ON app_data.ID = AP_APP.ID " - + "WHERE AP_APP.TENANT_ID = ?"; - - if (filter == null) { - String msg = "Filter is not instantiated."; - log.error(msg); - throw new ApplicationManagementDAOException(msg); + + "INNER JOIN (SELECT AP_APP.ID FROM AP_APP ORDER BY ID "; + if (!StringUtils.isEmpty(filter.getVersion()) || !StringUtils.isEmpty(filter.getAppReleaseState()) + || !StringUtils.isEmpty(filter.getAppReleaseType())) { + sql += "INNER JOIN AP_APP_RELEASE ON AP_APP.ID = AP_APP_RELEASE.AP_APP_ID "; } - if (!StringUtils.isEmpty(filter.getAppType())) { - sql += " AND AP_APP.TYPE = ?"; + sql += "AND AP_APP.TYPE = ? "; } if (!StringUtils.isEmpty(filter.getAppName())) { sql += " AND LOWER (AP_APP.NAME) "; if (filter.isFullMatch()) { - sql += "= ?"; + sql += "= ? "; } else { - sql += "LIKE ?"; + sql += "LIKE ? "; } } if (!StringUtils.isEmpty(filter.getSubscriptionType())) { - sql += " AND AP_APP.SUB_TYPE = ?"; + sql += "AND AP_APP.SUB_TYPE = ? "; } if (filter.getMinimumRating() > 0) { - sql += " AND AP_APP.RATING >= ?"; + sql += "AND AP_APP.RATING >= ? "; } if (!StringUtils.isEmpty(filter.getVersion())) { - sql += " AND AP_APP_RELEASE.VERSION = ?"; + sql += "AND AP_APP_RELEASE.VERSION = ? "; } if (!StringUtils.isEmpty(filter.getAppReleaseType())) { - sql += " AND AP_APP_RELEASE.RELEASE_TYPE = ?"; + sql += "AND AP_APP_RELEASE.RELEASE_TYPE = ? "; } if (!StringUtils.isEmpty(filter.getAppReleaseState())) { - sql += " AND AP_APP_RELEASE.CURRENT_STATE = ?"; + sql += "AND AP_APP_RELEASE.CURRENT_STATE = ? "; } if (deviceTypeId != -1) { - sql += " AND AP_APP.DEVICE_TYPE_ID = ?"; + sql += "AND AP_APP.DEVICE_TYPE_ID = ? "; } - - if (filter.getLimit() == -1) { - sql = sql.replace("OFFSET ? ROWS FETCH NEXT ? ROWS ONLY", ""); + sql += "GROUP BY AP_APP.ID "; + if (!StringUtils.isEmpty(filter.getSortBy())) { + sql += "ORDER BY ID " + filter.getSortBy() + " "; } - - String sortingOrder = "ASC"; - if (!StringUtils.isEmpty(filter.getSortBy() )) { - sortingOrder = filter.getSortBy(); + if (filter.getLimit() != -1) { + sql += "OFFSET ? ROWS FETCH NEXT ? ROWS ONLY "; } - sql += " ORDER BY APP_ID " + sortingOrder; - + sql += ") AS app_data ON app_data.ID = AP_APP.ID " + + "WHERE AP_APP.TENANT_ID = ?"; try { Connection conn = this.getDBConnection(); - try (PreparedStatement stmt = conn.prepareStatement(sql); - ){ - if (filter.getLimit() != -1) { - stmt.setInt(paramIndex++, filter.getOffset()); - if (filter.getLimit() == 0) { - stmt.setInt(paramIndex++, 100); - } else { - stmt.setInt(paramIndex++, filter.getLimit()); - } - } - stmt.setInt(paramIndex++, tenantId); - - if (filter.getAppType() != null && !filter.getAppType().isEmpty()) { + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + int paramIndex = 1; + if (!StringUtils.isEmpty(filter.getAppType())) { stmt.setString(paramIndex++, filter.getAppType()); } - if (filter.getAppName() != null && !filter.getAppName().isEmpty()) { + if (!StringUtils.isEmpty(filter.getAppName())) { if (filter.isFullMatch()) { stmt.setString(paramIndex++, filter.getAppName().toLowerCase()); } else { @@ -168,10 +156,15 @@ public class OracleApplicationDAOImpl extends GenericApplicationDAOImpl { if (!StringUtils.isEmpty(filter.getAppReleaseState())) { stmt.setString(paramIndex++, filter.getAppReleaseState()); } - if (deviceTypeId > 0 ) { - stmt.setInt(paramIndex, deviceTypeId); + if (deviceTypeId > 0) { + stmt.setInt(paramIndex++, deviceTypeId); + } + if (filter.getLimit() != -1) { + stmt.setInt(paramIndex++, filter.getOffset()); + stmt.setInt(paramIndex++, filter.getLimit()); } - try (ResultSet rs = stmt.executeQuery() ) { + stmt.setInt(paramIndex, tenantId); + try (ResultSet rs = stmt.executeQuery()) { return DAOUtil.loadApplications(rs); } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/SQLServerApplicationDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/SQLServerApplicationDAOImpl.java index 7ca05d12a6..9e07d8ccea 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/SQLServerApplicationDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/SQLServerApplicationDAOImpl.java @@ -40,13 +40,17 @@ public class SQLServerApplicationDAOImpl extends GenericApplicationDAOImpl { private static final Log log = LogFactory.getLog(SQLServerApplicationDAOImpl.class); @Override - public List getApplications(Filter filter,int deviceTypeId, int tenantId) throws + public List getApplications(Filter filter, int deviceTypeId, int tenantId) throws ApplicationManagementDAOException { + if (filter == null) { + String msg = "Filter is not instantiated for tenant " + tenantId; + log.error(msg); + throw new ApplicationManagementDAOException(msg); + } if (log.isDebugEnabled()) { log.debug("Getting application data from the database"); log.debug(String.format("Filter: limit=%s, offset=%s", filter.getLimit(), filter.getOffset())); } - int paramIndex = 1; String sql = "SELECT " + "AP_APP.ID AS APP_ID, " + "AP_APP.NAME AS APP_NAME, " @@ -79,73 +83,57 @@ public class SQLServerApplicationDAOImpl extends GenericApplicationDAOImpl { + "FROM AP_APP " + "INNER JOIN AP_APP_RELEASE ON " + "AP_APP.ID = AP_APP_RELEASE.AP_APP_ID " - + "INNER JOIN (SELECT ID FROM AP_APP ORDER BY ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY) AS app_data ON app_data.ID = AP_APP.ID " - + "WHERE AP_APP.TENANT_ID = ?"; - - if (filter == null) { - String msg = "Filter is not instantiated."; - log.error(msg); - throw new ApplicationManagementDAOException(msg); + + "INNER JOIN (SELECT AP_APP.ID FROM AP_APP "; + if (!StringUtils.isEmpty(filter.getVersion()) || !StringUtils.isEmpty(filter.getAppReleaseState()) + || !StringUtils.isEmpty(filter.getAppReleaseType())) { + sql += "INNER JOIN AP_APP_RELEASE ON AP_APP.ID = AP_APP_RELEASE.AP_APP_ID "; } - if (!StringUtils.isEmpty(filter.getAppType())) { - sql += " AND AP_APP.TYPE = ?"; + sql += "AND AP_APP.TYPE = ? "; } if (!StringUtils.isEmpty(filter.getAppName())) { sql += " AND LOWER (AP_APP.NAME) "; if (filter.isFullMatch()) { - sql += "= ?"; + sql += "= ? "; } else { - sql += "LIKE ?"; + sql += "LIKE ? "; } } if (!StringUtils.isEmpty(filter.getSubscriptionType())) { - sql += " AND AP_APP.SUB_TYPE = ?"; + sql += "AND AP_APP.SUB_TYPE = ? "; } if (filter.getMinimumRating() > 0) { - sql += " AND AP_APP.RATING >= ?"; + sql += "AND AP_APP.RATING >= ? "; } if (!StringUtils.isEmpty(filter.getVersion())) { - sql += " AND AP_APP_RELEASE.VERSION = ?"; + sql += "AND AP_APP_RELEASE.VERSION = ? "; } if (!StringUtils.isEmpty(filter.getAppReleaseType())) { - sql += " AND AP_APP_RELEASE.RELEASE_TYPE = ?"; + sql += "AND AP_APP_RELEASE.RELEASE_TYPE = ? "; } if (!StringUtils.isEmpty(filter.getAppReleaseState())) { - sql += " AND AP_APP_RELEASE.CURRENT_STATE = ?"; + sql += "AND AP_APP_RELEASE.CURRENT_STATE = ? "; } if (deviceTypeId != -1) { - sql += " AND AP_APP.DEVICE_TYPE_ID = ?"; + sql += "AND AP_APP.DEVICE_TYPE_ID = ? "; } - - if (filter.getLimit() == -1) { - sql = sql.replace("ORDER BY ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY", ""); - } - - String sortingOrder = "ASC"; + sql += "GROUP BY AP_APP.ID "; if (!StringUtils.isEmpty(filter.getSortBy())) { - sortingOrder = filter.getSortBy(); + sql += "ORDER BY ID " + filter.getSortBy() + " "; } - sql += " ORDER BY APP_ID " + sortingOrder; - + if (filter.getLimit() != -1) { + sql += "ORDER BY ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY "; + } + sql += ") AS app_data ON app_data.ID = AP_APP.ID " + + "WHERE AP_APP.TENANT_ID = ?"; try { Connection conn = this.getDBConnection(); - try (PreparedStatement stmt = conn.prepareStatement(sql); - ){ - if (filter.getLimit() != -1) { - stmt.setInt(paramIndex++, filter.getOffset()); - if (filter.getLimit() == 0) { - stmt.setInt(paramIndex++, 100); - } else { - stmt.setInt(paramIndex++, filter.getLimit()); - } - } - stmt.setInt(paramIndex++, tenantId); - - if (filter.getAppType() != null && !filter.getAppType().isEmpty()) { + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + int paramIndex = 1; + if (!StringUtils.isEmpty(filter.getAppType())) { stmt.setString(paramIndex++, filter.getAppType()); } - if (filter.getAppName() != null && !filter.getAppName().isEmpty()) { + if (!StringUtils.isEmpty(filter.getAppName())) { if (filter.isFullMatch()) { stmt.setString(paramIndex++, filter.getAppName().toLowerCase()); } else { @@ -167,10 +155,15 @@ public class SQLServerApplicationDAOImpl extends GenericApplicationDAOImpl { if (!StringUtils.isEmpty(filter.getAppReleaseState())) { stmt.setString(paramIndex++, filter.getAppReleaseState()); } - if (deviceTypeId > 0 ) { - stmt.setInt(paramIndex, deviceTypeId); + if (deviceTypeId > 0) { + stmt.setInt(paramIndex++, deviceTypeId); + } + if (filter.getLimit() != -1) { + stmt.setInt(paramIndex++, filter.getOffset()); + stmt.setInt(paramIndex++, filter.getLimit()); } - try (ResultSet rs = stmt.executeQuery() ) { + stmt.setInt(paramIndex, tenantId); + try (ResultSet rs = stmt.executeQuery()) { return DAOUtil.loadApplications(rs); } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java index 142ee4dfe5..021037ede3 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java @@ -608,9 +608,6 @@ public class ApplicationManagerImpl implements ApplicationManager { if (!StringUtils.isEmpty(filter.getDeviceType())) { deviceType = APIUtil.getDeviceTypeData(filter.getDeviceType()); } - if (filter.getLimit() == 0) { - filter.setLimit(20); - } if (deviceType == null) { deviceType = new DeviceType();