Added filiter to app list

feature/appm-store/pbac
Chatura Dilan 8 years ago committed by Harshan Liyanage
parent 9d5e912d11
commit 76342ee7e3

@ -51,7 +51,9 @@ public class GsonMessageBodyHandler implements MessageBodyWriter<Object>, 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;

@ -67,11 +67,28 @@ public interface ApplicationManagementService {
response = ErrorResponse.class)
})
Response getApplications(
@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
);
}

@ -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";

@ -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;
}

@ -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;
}

@ -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;
}

@ -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());
}
}

@ -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,26 +43,45 @@ 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<Application> 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;
@ -84,7 +104,7 @@ public class GenericAppManagementDAO implements ApplicationManagementDAO {
length++;
}
pagination.setLength(length);
pagination.setSize(length);
applicationList.setApplications(applications);
applicationList.setPagination(pagination);

@ -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<String, String> 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<String, String> getFilterProperties() {
return filterProperties;
}
public void setFilterProperties(Map<String, String> 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;
}
}

@ -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() {

Loading…
Cancel
Save