From 76342ee7e3ea6890cdfa939a4db206f3a5d41643 Mon Sep 17 00:00:00 2001 From: Chatura Dilan Date: Mon, 8 May 2017 15:06:00 +0530 Subject: [PATCH] Added filiter to app list --- .../api/common/GsonMessageBodyHandler.java | 4 +- .../ApplicationManagementService.java | 21 ++++- .../ApplicationManagementServiceImpl.java | 23 ++++-- .../core/components/ApplicationManager.java | 3 +- .../impl/ApplicationManagerImpl.java | 5 +- .../core/dao/ApplicationManagementDAO.java | 3 +- .../ApplicationManagementDAOException.java | 4 + .../dao/impl/GenericAppManagementDAO.java | 36 +++++++-- .../application/mgt/core/dto/Filter.java | 81 +++++++++++++++++++ .../application/mgt/core/dto/Pagination.java | 10 +-- 10 files changed, 164 insertions(+), 26 deletions(-) create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dto/Filter.java diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/common/GsonMessageBodyHandler.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/common/GsonMessageBodyHandler.java index fccfd66b19..8789e25a3d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/common/GsonMessageBodyHandler.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/common/GsonMessageBodyHandler.java @@ -51,7 +51,9 @@ public class GsonMessageBodyHandler implements MessageBodyWriter, Messag private Gson getGson() { if (gson == null) { - final GsonBuilder gsonBuilder = new GsonBuilder().setExclusionStrategies(new AnnotationExclusionStrategy()); + final GsonBuilder gsonBuilder = new GsonBuilder() + .setDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz") + .setExclusionStrategies(new AnnotationExclusionStrategy()); gson = gsonBuilder.create(); } return gson; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ApplicationManagementService.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ApplicationManagementService.java index 48685d76b0..54af6d1cfe 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ApplicationManagementService.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ApplicationManagementService.java @@ -67,11 +67,28 @@ public interface ApplicationManagementService { response = ErrorResponse.class) }) Response getApplications( - @ApiParam( + + @ApiParam( name = "If-Modified-Since", value = "Validates if the requested variant has not been modified since the time specified", required = false) - @HeaderParam("If-Modified-Since") String ifModifiedSince); + @HeaderParam("If-Modified-Since") String ifModifiedSince, + + @ApiParam( + name = "offset", + value = "Provide how many apps it should return", + required = false, + defaultValue = "20") + @QueryParam("offset") int offset, + + @ApiParam( + name = "limit", + value = "Provide from which position apps should return", + required = false, + defaultValue = "0") + @QueryParam("limit") int limit + + ); } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ApplicationManagementServiceImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ApplicationManagementServiceImpl.java index ef89c438c4..87de7714b9 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ApplicationManagementServiceImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ApplicationManagementServiceImpl.java @@ -22,28 +22,39 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.application.mgt.core.components.ApplicationManager; import org.wso2.carbon.device.application.mgt.core.dto.ApplicationList; +import org.wso2.carbon.device.application.mgt.core.dto.Filter; import org.wso2.carbon.device.application.mgt.core.util.ApplicationManagementUtil; -import javax.ws.rs.Consumes; -import javax.ws.rs.GET; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; +import javax.ws.rs.*; import javax.ws.rs.core.Response; @Produces({ "application/json"}) @Consumes({ "application/json"}) public class ApplicationManagementServiceImpl { + public static final int DEFAULT_LIMIT = 20; + private static Log log = LogFactory.getLog(ApplicationManagementServiceImpl.class); @GET @Consumes("application/json") @Path("applications") - public Response getApplications() { + public Response getApplications(@QueryParam("offset") int offset, @QueryParam("limit") int limit, + @QueryParam("q") String searchQuery) { ApplicationManager applicationManager = ApplicationManagementUtil.getApplicationManager(); try { - ApplicationList applications = applicationManager.getApplications(); + + if(limit == 0){ + limit = DEFAULT_LIMIT; + } + + Filter filter = new Filter(); + filter.setOffset(offset); + filter.setLimit(limit); + filter.setSearchQuery(searchQuery); + + ApplicationList applications = applicationManager.getApplications(filter); return Response.status(Response.Status.OK).entity(applications).build(); } catch (Exception e) { String msg = "Error occurred while getting the application list"; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/components/ApplicationManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/components/ApplicationManager.java index 26b58de04e..f14e734317 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/components/ApplicationManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/components/ApplicationManager.java @@ -20,6 +20,7 @@ package org.wso2.carbon.device.application.mgt.core.components; import org.wso2.carbon.device.application.mgt.core.dto.Application; import org.wso2.carbon.device.application.mgt.core.dto.ApplicationList; +import org.wso2.carbon.device.application.mgt.core.dto.Filter; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagerException; import java.util.List; @@ -28,6 +29,6 @@ public interface ApplicationManager { public void createApplication(Application application) throws ApplicationManagerException; - public ApplicationList getApplications() throws ApplicationManagerException; + public ApplicationList getApplications(Filter filter) throws ApplicationManagerException; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/components/impl/ApplicationManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/components/impl/ApplicationManagerImpl.java index 80161047d6..12e4ba519e 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/components/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/components/impl/ApplicationManagerImpl.java @@ -24,6 +24,7 @@ import org.wso2.carbon.device.application.mgt.core.components.ApplicationManager import org.wso2.carbon.device.application.mgt.core.dao.ApplicationManagementDAO; import org.wso2.carbon.device.application.mgt.core.dto.Application; import org.wso2.carbon.device.application.mgt.core.dto.ApplicationList; +import org.wso2.carbon.device.application.mgt.core.dto.Filter; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagerException; import org.wso2.carbon.device.application.mgt.core.internal.ApplicationManagementDataHolder; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; @@ -52,10 +53,10 @@ public class ApplicationManagerImpl implements ApplicationManager { } @Override - public ApplicationList getApplications() throws ApplicationManagerException { + public ApplicationList getApplications(Filter filter) throws ApplicationManagerException { ConnectionManagerUtil.openConnection(); ApplicationManagementDAO applicationManagementDAO = ApplicationManagementDataHolder.getInstance().getApplicationManagementDAO(); - ApplicationList applications = applicationManagementDAO.getApplications(); + ApplicationList applications = applicationManagementDAO.getApplications(filter); ConnectionManagerUtil.closeConnection(); return applications; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationManagementDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationManagementDAO.java index 9ab39c67ff..7020b9b20d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationManagementDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationManagementDAO.java @@ -20,6 +20,7 @@ package org.wso2.carbon.device.application.mgt.core.dao; import org.wso2.carbon.device.application.mgt.core.dto.Application; import org.wso2.carbon.device.application.mgt.core.dto.ApplicationList; +import org.wso2.carbon.device.application.mgt.core.dto.Filter; import java.util.HashMap; import java.util.List; @@ -59,6 +60,6 @@ public interface ApplicationManagementDAO { public void createApplication(Application application) throws ApplicationManagementDAOException; - public ApplicationList getApplications() throws ApplicationManagementDAOException; + public ApplicationList getApplications(Filter filter) throws ApplicationManagementDAOException; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationManagementDAOException.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationManagementDAOException.java index 40874b026d..4759d47817 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationManagementDAOException.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationManagementDAOException.java @@ -25,4 +25,8 @@ public class ApplicationManagementDAOException extends ApplicationManagerExcepti public ApplicationManagementDAOException(String message, Throwable throwable) { super(message, throwable); } + + public ApplicationManagementDAOException(String message) { + super(message, new Exception()); + } } 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/GenericAppManagementDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/GenericAppManagementDAO.java index 01c8455179..bf2b5b6744 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/GenericAppManagementDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/GenericAppManagementDAO.java @@ -24,6 +24,7 @@ import org.wso2.carbon.device.application.mgt.core.dao.ApplicationManagementDAOE import org.wso2.carbon.device.application.mgt.core.dao.ApplicationManagementDAOUtil; import org.wso2.carbon.device.application.mgt.core.dto.Application; import org.wso2.carbon.device.application.mgt.core.dto.ApplicationList; +import org.wso2.carbon.device.application.mgt.core.dto.Filter; import org.wso2.carbon.device.application.mgt.core.dto.Pagination; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; @@ -42,33 +43,52 @@ public class GenericAppManagementDAO implements ApplicationManagementDAO { } @Override - public ApplicationList getApplications() throws ApplicationManagementDAOException { + public ApplicationList getApplications(Filter filter) throws ApplicationManagementDAOException { Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; - String sql = null; + String sql = ""; ApplicationList applicationList = new ApplicationList(); List applications = new ArrayList<>(); Pagination pagination = new Pagination(); + if (filter == null) { + throw new ApplicationManagementDAOException("Filter need to be instantiated"); + } else { + pagination.setLimit(filter.getLimit()); + pagination.setOffset(filter.getOffset()); + } + try { conn = ConnectionManagerUtil.getCurrentConnection().get(); - sql = "SELECT SQL_CALC_FOUND_ROWS AP.*, AT.NAME AS AT_NAME, AT.CODE AS AT_CODE, CT.NAME AS CT_NAME " + - "FROM APPM_APPLICATION AS AP " + - "INNER JOIN APPM_APPLICATION_TYPE AS AT ON AP.APPLICATION_TYPE_ID = AT.ID " + - "INNER JOIN APPM_APPLICATION_CATEGORY AS CT ON AP.CATEGORY_ID = CT.ID;"; + sql += "SELECT SQL_CALC_FOUND_ROWS AP.*, AT.NAME AS AT_NAME, AT.CODE AS AT_CODE, CT.NAME AS CT_NAME "; + sql += "FROM APPM_APPLICATION AS AP "; + sql += "INNER JOIN APPM_APPLICATION_TYPE AS AT ON AP.APPLICATION_TYPE_ID = AT.ID "; + sql += "INNER JOIN APPM_APPLICATION_CATEGORY AS CT ON AP.CATEGORY_ID = CT.ID "; + if (filter.getSearchQuery() != null || "".equals(filter.getSearchQuery())) { + sql += "WHERE AP.NAME LIKE ? "; + } + sql += "LIMIT ? "; + sql += "OFFSET ?;"; stmt = conn.prepareStatement(sql); + int index = 0; + if (filter.getSearchQuery() != null || "".equals(filter.getSearchQuery())) { + stmt.setString(++index, "%" + filter.getSearchQuery() + "%"); + } + stmt.setInt(++index, filter.getLimit()); + stmt.setInt(++index, filter.getOffset()); + rs = stmt.executeQuery(); int length = 0; sql = "SELECT FOUND_ROWS() AS COUNT;"; stmt = conn.prepareStatement(sql); ResultSet rsCount = stmt.executeQuery(); - if(rsCount.next()){ + if (rsCount.next()) { pagination.setCount(rsCount.getInt("COUNT")); } @@ -84,7 +104,7 @@ public class GenericAppManagementDAO implements ApplicationManagementDAO { length++; } - pagination.setLength(length); + pagination.setSize(length); applicationList.setApplications(applications); applicationList.setPagination(pagination); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dto/Filter.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dto/Filter.java new file mode 100644 index 0000000000..95b5c8f183 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dto/Filter.java @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.application.mgt.core.dto; + +import java.util.Map; + +public class Filter { + + private int limit; + + private int offset; + + private String filter; + + private Map filterProperties; + + private String searchQuery; + + public int getLimit() { + return limit; + } + + public void setLimit(int limit) { + this.limit = limit; + } + + public int getOffset() { + return offset; + } + + public void setOffset(int offset) { + this.offset = offset; + } + + public String getFilter() { + return filter; + } + + public void setFilter(String filter) { + this.filter = filter; + } + + public Map getFilterProperties() { + return filterProperties; + } + + public void setFilterProperties(Map filterProperties) { + this.filterProperties = filterProperties; + } + + public String getSearchQuery() { + return searchQuery; + } + + public void setSearchQuery(String searchQuery) { + this.searchQuery = searchQuery; + } + + public boolean hasCondition() { + if (filterProperties != null || searchQuery != null || filter != null) { + return true; + } + return false; + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dto/Pagination.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dto/Pagination.java index 34146acfe2..8ab3590688 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dto/Pagination.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dto/Pagination.java @@ -24,7 +24,7 @@ public class Pagination { private int limit; - private int length; + private int size; private int count; @@ -44,12 +44,12 @@ public class Pagination { this.limit = limit; } - public int getLength() { - return length; + public int getSize() { + return size; } - public void setLength(int length) { - this.length = length; + public void setSize(int size) { + this.size = size; } public int getCount() {