From 94d3f1b3b1ac0d74a31bdfb61e3be6c2c5e2794a Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Wed, 26 Sep 2018 18:02:03 +0530 Subject: [PATCH] Fix SQL issues --- .../device/application/mgt/common/Filter.java | 10 +++++++ .../GenericApplicationDAOImpl.java | 17 ++++++++--- .../mgt/core/impl/ApplicationManagerImpl.java | 2 +- .../services/ApplicationManagementAPI.java | 29 +++++++++++++++---- .../impl/ApplicationManagementAPIImpl.java | 23 +++++++++++++-- .../dbscripts/cdm/application-mgt/h2.sql | 15 +++++----- .../dbscripts/cdm/application-mgt/mysql.sql | 15 +++++----- 7 files changed, 83 insertions(+), 28 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 144abba320f..96ee8e49bd7 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 @@ -40,6 +40,8 @@ public class Filter { required = false) private String appType; + private String appCategory; + @ApiModelProperty( name = "isFullMatch", value = "Checking the application name matches fully with given name", @@ -111,4 +113,12 @@ public class Filter { public void setAppType(String appType) { this.appType = appType; } + + public String getAppCategory() { + return appCategory; + } + + public void setAppCategory(String appCategory) { + this.appCategory = appCategory; + } } 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 d66c7e7dc16..5847eda9b81 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 @@ -147,11 +147,17 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic throw new ApplicationManagementDAOException("Filter need to be instantiated"); } - if (filter.getAppType() != null) { + if (filter.getAppType() != null && !filter.getAppType().isEmpty()) { sql += " AND AP_APP.TYPE "; sql += "= ?"; } - if (filter.getAppName() != null) { + + if (filter.getAppCategory() != null && !filter.getAppCategory().isEmpty()) { + sql += " AND AP_APP.APP_CATEGORY "; + sql += "= ?"; + } + + if (filter.getAppName() != null && !filter.getAppName().isEmpty()) { sql += " AND LOWER (AP_APP.NAME) "; if (filter.isFullMatch()) { sql += "= ?"; @@ -175,10 +181,13 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic stmt.setInt(paramIndex++, tenantId); stmt.setString(paramIndex++, AppLifecycleState.REMOVED.toString()); - if (filter.getAppType() != null) { + if (filter.getAppType() != null && !filter.getAppType().isEmpty()) { stmt.setString(paramIndex++, filter.getAppType()); } - if (filter.getAppName() != null) { + if (filter.getAppCategory() != null && !filter.getAppCategory().isEmpty()) { + stmt.setString(paramIndex++, filter.getAppCategory()); + } + if (filter.getAppName() != null && !filter.getAppName().isEmpty()) { if (filter.isFullMatch()) { stmt.setString(paramIndex++, filter.getAppName().toLowerCase()); } else { 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 d8c7706972b..52073bdbaed 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 @@ -215,7 +215,7 @@ public class ApplicationManagerImpl implements ApplicationManager { ConnectionManagerUtil.getDBConnection(); applicationList = applicationDAO.getApplications(filter, tenantId); if(applicationList != null && applicationList.getApplications() != null && applicationList - .getApplications().size() > 0) { + .getApplications().isEmpty()) { if (!isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { applicationList = getRoleRestrictedApplicationList(applicationList, userName); } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/ApplicationManagementAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/ApplicationManagementAPI.java index 35b70eeba57..3459bd7d816 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/ApplicationManagementAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/ApplicationManagementAPI.java @@ -39,6 +39,7 @@ import java.util.List; import javax.validation.Valid; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; +import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; @@ -125,10 +126,21 @@ public interface ApplicationManagementAPI { }) Response getApplications( @ApiParam( - name = "filter", - value = "Filter to get application list", - required = true) - @Valid Filter filter, + name = "name", + value = "Name of the application") + @QueryParam("name") String appName, + @ApiParam( + name = "type", + value = "Type of the application") + @QueryParam("type") String appType, + @ApiParam( + name = "category", + value = "Category of the application") + @QueryParam("category") String appCategory, + @ApiParam( + name = "exact-match", + value = "Is it requesting exactly matching application or partially matching application.") + @QueryParam("exact-match") boolean isFullMatch, @ApiParam( name = "offset", value = "offset", @@ -138,11 +150,16 @@ public interface ApplicationManagementAPI { name = "limit", value = "limit", defaultValue = "20") - @QueryParam("limit") int limit + @QueryParam("limit") int limit, + @ApiParam( + name = "sort", + value = "Sorting type", + defaultValue = "AES") + @QueryParam("sort") String sortBy ); @GET - @Path("/{appType}") + @Path("/{appId}") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @ApiOperation( diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementAPIImpl.java index 6f16e1dc0fe..eec6282e856 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementAPIImpl.java @@ -41,6 +41,7 @@ import java.util.UUID; import javax.validation.Valid; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; +import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; @@ -64,14 +65,30 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { @Override @Consumes("application/json") public Response getApplications( - @Valid Filter filter, - @QueryParam("offset") int offset, - @QueryParam("limit") int limit) { + @QueryParam("name") String appName, + @QueryParam("type") String appType, + @QueryParam("category") String appCategory, + @QueryParam("exact-match") boolean isFullMatch, + @DefaultValue("0") @QueryParam("offset") int offset, + @DefaultValue("20") @QueryParam("limit") int limit, + @DefaultValue("ASC") @QueryParam("sort") String sortBy) { ApplicationManager applicationManager = APIUtil.getApplicationManager(); try { + Filter filter = new Filter(); filter.setOffset(offset); filter.setLimit(limit); + filter.setSortBy(sortBy); + filter.setFullMatch(isFullMatch); + if (appName != null && !appName.isEmpty()) { + filter.setAppName(appName); + } + if (appType != null && !appType.isEmpty()) { + filter.setAppType(appType); + } + if (appCategory != null && !appCategory.isEmpty()) { + filter.setAppCategory(appCategory); + } ApplicationList applications = applicationManager.getApplications(filter); if (applications.getApplications().isEmpty()) { return Response.status(Response.Status.NOT_FOUND).entity diff --git a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/h2.sql b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/h2.sql index 54fe2f09605..6bada31a2a2 100644 --- a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/h2.sql +++ b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/h2.sql @@ -20,17 +20,18 @@ CREATE TABLE IF NOT EXISTS AP_APP ( -- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS AP_APP_RELEASE ( ID INT(11) NOT NULL AUTO_INCREMENT, - VERSION VARCHAR(10) NOT NULL, + VERSION VARCHAR(25) NOT NULL, TENANT_ID VARCHAR(45) NOT NULL, UUID VARCHAR(200) NOT NULL, RELEASE_TYPE VARCHAR(45) NOT NULL, + PACKAGE_NAME VARCHAR(45) NOT NULL, APP_PRICE DECIMAL(6,2) NULL DEFAULT NULL, - STORED_LOCATION VARCHAR(45) NOT NULL, - BANNER_LOCATION VARCHAR(45) NOT NULL, - SC_1_LOCATION VARCHAR(45) NOT NULL, - SC_2_LOCATION VARCHAR(45) NULL DEFAULT NULL, - SC_3_LOCATION VARCHAR(45) NULL DEFAULT NULL, - APP_HASH_VALUE VARCHAR(1000) NOT NULL, + STORED_LOCATION VARCHAR(100) NOT NULL, + BANNER_LOCATION VARCHAR(100) NOT NULL, + SC_1_LOCATION VARCHAR(100) NOT NULL, + SC_2_LOCATION VARCHAR(100) NULL DEFAULT NULL, + SC_3_LOCATION VARCHAR(100) NULL DEFAULT NULL, + APP_HASH_VALUE VARCHAR(100) NOT NULL, SHARED_WITH_ALL_TENANTS INT(11) NULL DEFAULT NULL, APP_META_INFO VARCHAR(20000) NULL DEFAULT NULL, RATING DOUBLE NULL DEFAULT NULL, diff --git a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql index 058feaaa5f9..eb5d1d5f3fa 100644 --- a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql +++ b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql @@ -32,17 +32,18 @@ CREATE TABLE IF NOT EXISTS `AP_APP` ( -- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS `AP_APP_RELEASE` ( `ID` INT(11) NOT NULL AUTO_INCREMENT, - `VERSION` VARCHAR(10) NOT NULL, + `VERSION` VARCHAR(25) NOT NULL, `TENANT_ID` VARCHAR(45) NOT NULL, `UUID` VARCHAR(200) NOT NULL, `RELEASE_TYPE` VARCHAR(45) NOT NULL, + `PACKAGE_NAME` VARCHAR(45) NOT NULL, `APP_PRICE` DECIMAL(6,2) NULL DEFAULT NULL, - `STORED_LOCATION` VARCHAR(45) NOT NULL, - `BANNER_LOCATION` VARCHAR(45) NOT NULL, - `SC_1_LOCATION` VARCHAR(45) NOT NULL, - `SC_2_LOCATION` VARCHAR(45) NULL DEFAULT NULL, - `SC_3_LOCATION` VARCHAR(45) NULL DEFAULT NULL, - `APP_HASH_VALUE` VARCHAR(1000) NOT NULL, + `STORED_LOCATION` VARCHAR(100) NOT NULL, + `BANNER_LOCATION` VARCHAR(100) NOT NULL, + `SC_1_LOCATION` VARCHAR(100) NOT NULL, + `SC_2_LOCATION` VARCHAR(100) NULL DEFAULT NULL, + `SC_3_LOCATION` VARCHAR(100) NULL DEFAULT NULL, + `APP_HASH_VALUE` VARCHAR(100) NOT NULL, `SHARED_WITH_ALL_TENANTS` INT(11) NULL DEFAULT NULL, `APP_META_INFO` VARCHAR(20000) NULL DEFAULT NULL, `RATING` DOUBLE NULL DEFAULT NULL,