Search in sources :

Example 1 with ServiceFilterParams

use of org.wso2.carbon.apimgt.api.model.ServiceFilterParams in project carbon-apimgt by wso2.

the class ServiceEntryMappingUtil method setPaginationParams.

public static void setPaginationParams(ServiceListDTO serviceListDTO, int offset, int limit, int size, ServiceFilterParams filterParams) {
    Map<String, Integer> paginatedParams = RestApiCommonUtil.getPaginationParams(offset, limit, size);
    String paginatedPrevious = "";
    String paginatedNext = "";
    if (paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_OFFSET) != null) {
        paginatedPrevious = getServicesPaginatedUrl(paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_OFFSET), paginatedParams.get(RestApiConstants.PAGINATION_PREVIOUS_LIMIT), filterParams);
    }
    if (paginatedParams.get(RestApiConstants.PAGINATION_NEXT_OFFSET) != null) {
        paginatedNext = getServicesPaginatedUrl(paginatedParams.get(RestApiConstants.PAGINATION_NEXT_OFFSET), paginatedParams.get(RestApiConstants.PAGINATION_NEXT_LIMIT), filterParams);
    }
    PaginationDTO paginationDTO = getPaginationDTO(limit, offset, size, paginatedNext, paginatedPrevious);
    serviceListDTO.setPagination(paginationDTO);
}
Also used : PaginationDTO(org.wso2.carbon.apimgt.rest.api.service.catalog.dto.PaginationDTO)

Example 2 with ServiceFilterParams

use of org.wso2.carbon.apimgt.api.model.ServiceFilterParams in project carbon-apimgt by wso2.

the class ServiceCatalogDAO method getServices.

/**
 * Get services
 * @param filterParams Service Filter parameters
 * @param tenantId Tenant ID of the logged in user
 * @param shrink Whether to shrink the response or not
 * @return List of Services
 * @throws APIManagementException
 */
public List<ServiceEntry> getServices(ServiceFilterParams filterParams, int tenantId, boolean shrink) throws APIManagementException {
    List<ServiceEntry> serviceEntryList = new ArrayList<>();
    String query;
    boolean searchByKey = false;
    boolean searchByDefinitionType = false;
    boolean exactNameSearch = false;
    boolean exactVersionSearch = false;
    StringBuilder querySb = new StringBuilder();
    querySb.append("SELECT UUID, SERVICE_KEY, MD5, SERVICE_NAME, SERVICE_VERSION," + "   SERVICE_URL, DEFINITION_TYPE, DEFINITION_URL, DESCRIPTION, SECURITY_TYPE, MUTUAL_SSL_ENABLED," + "   CREATED_TIME, LAST_UPDATED_TIME, CREATED_BY, UPDATED_BY, SERVICE_DEFINITION FROM " + "   AM_SERVICE_CATALOG WHERE TENANT_ID = ? ");
    String whereClauseForExactNameSearch = "AND SERVICE_NAME = ? ";
    String whereClauseForNameSearch = "AND SERVICE_NAME LIKE ? ";
    String whereClauseForExactVersionSearch = "AND SERVICE_VERSION = ? ";
    String whereClauseForVersionSearch = " AND SERVICE_VERSION LIKE ? ";
    String whereClauseWithDefinitionType = " AND DEFINITION_TYPE = ? ";
    String whereClauseWithServiceKey = " AND SERVICE_KEY = ? ";
    if (filterParams.getName().startsWith("\"") && filterParams.getName().endsWith("\"")) {
        exactNameSearch = true;
        filterParams.setName(filterParams.getName().replace("\"", "").trim());
        querySb.append(whereClauseForExactNameSearch);
    } else {
        querySb.append(whereClauseForNameSearch);
    }
    if (filterParams.getVersion().startsWith("\"") && filterParams.getVersion().endsWith("\"")) {
        exactVersionSearch = true;
        filterParams.setVersion(filterParams.getVersion().replace("\"", "").trim());
        querySb.append(whereClauseForExactVersionSearch);
    } else {
        querySb.append(whereClauseForVersionSearch);
    }
    if (StringUtils.isNotEmpty(filterParams.getDefinitionType()) && StringUtils.isEmpty(filterParams.getKey())) {
        searchByDefinitionType = true;
        querySb.append(whereClauseWithDefinitionType);
    } else if (StringUtils.isNotEmpty(filterParams.getKey()) && StringUtils.isEmpty(filterParams.getDefinitionType())) {
        searchByKey = true;
        querySb.append(whereClauseWithServiceKey);
    } else if (StringUtils.isNotEmpty(filterParams.getDefinitionType()) && StringUtils.isNotEmpty(filterParams.getKey())) {
        searchByKey = true;
        searchByDefinitionType = true;
        querySb.append(whereClauseWithDefinitionType).append(whereClauseWithServiceKey);
    }
    querySb.append("ORDER BY ").append(filterParams.getSortBy()).append(" " + filterParams.getSortOrder());
    String[] keyArray = null;
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        String driverName = connection.getMetaData().getDriverName();
        if (driverName.contains("Oracle") || driverName.contains("MS SQL") || driverName.contains("Microsoft")) {
            querySb.append(" OFFSET ? ROWS FETCH NEXT ? ROWS ONLY");
        } else if (driverName.contains("PostgreSQL")) {
            querySb.append(" OFFSET ? LIMIT ? ");
        } else {
            querySb.append(" LIMIT ?, ?");
        }
        query = querySb.toString();
        try (PreparedStatement ps = connection.prepareStatement(query)) {
            keyArray = filterParams.getKey().split(",");
            for (String key : keyArray) {
                ps.setInt(1, tenantId);
                if (exactNameSearch) {
                    ps.setString(2, filterParams.getName());
                } else {
                    ps.setString(2, "%" + filterParams.getName() + "%");
                }
                if (exactVersionSearch) {
                    ps.setString(3, filterParams.getVersion());
                } else {
                    ps.setString(3, "%" + filterParams.getVersion() + "%");
                }
                if (searchByKey && searchByDefinitionType) {
                    ps.setString(4, filterParams.getDefinitionType());
                    ps.setString(5, key);
                    ps.setInt(6, filterParams.getOffset());
                    ps.setInt(7, filterParams.getLimit());
                } else if (searchByKey) {
                    ps.setString(4, key);
                    ps.setInt(5, filterParams.getOffset());
                    ps.setInt(6, filterParams.getLimit());
                } else if (searchByDefinitionType) {
                    ps.setString(4, filterParams.getDefinitionType());
                    ps.setInt(5, filterParams.getOffset());
                    ps.setInt(6, filterParams.getLimit());
                } else {
                    ps.setInt(4, filterParams.getOffset());
                    ps.setInt(5, filterParams.getLimit());
                }
                try (ResultSet resultSet = ps.executeQuery()) {
                    while (resultSet.next()) {
                        ServiceEntry service = getServiceParams(resultSet, shrink);
                        List<API> usedAPIs = getServiceUsage(service.getUuid(), tenantId, connection);
                        int usage = usedAPIs != null ? usedAPIs.size() : 0;
                        service.setUsage(usage);
                        serviceEntryList.add(service);
                    }
                }
            }
        } catch (SQLException e) {
            handleException("Error while retrieving the Services", e);
        }
    } catch (SQLException e) {
        handleException("Error while retrieving the Services", e);
    }
    return serviceEntryList;
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) ServiceEntry(org.wso2.carbon.apimgt.api.model.ServiceEntry) ResultSet(java.sql.ResultSet) API(org.wso2.carbon.apimgt.api.model.API)

Example 3 with ServiceFilterParams

use of org.wso2.carbon.apimgt.api.model.ServiceFilterParams in project carbon-apimgt by wso2.

the class ServicesApiServiceImpl method searchServices.

@Override
public Response searchServices(String name, String version, String definitionType, String key, Boolean shrink, String sortBy, String sortOrder, Integer limit, Integer offset, MessageContext messageContext) throws APIManagementException {
    String userName = RestApiCommonUtil.getLoggedInUsername();
    int tenantId = APIUtil.getTenantId(userName);
    try {
        List<ServiceDTO> serviceDTOList = new ArrayList<>();
        ServiceFilterParams filterParams = ServiceEntryMappingUtil.getServiceFilterParams(name, version, definitionType, key, sortBy, sortOrder, limit, offset);
        List<ServiceEntry> services = serviceCatalog.getServices(filterParams, tenantId, shrink);
        int totalServices = serviceCatalog.getServicesCount(tenantId, filterParams);
        for (ServiceEntry service : services) {
            serviceDTOList.add(ServiceEntryMappingUtil.fromServiceToDTO(service, shrink));
        }
        ServiceListDTO serviceListDTO = new ServiceListDTO();
        serviceListDTO.setList(serviceDTOList);
        ServiceEntryMappingUtil.setPaginationParams(serviceListDTO, filterParams.getOffset(), filterParams.getLimit(), totalServices, filterParams);
        return Response.ok().entity(serviceListDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving Services";
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : ServiceListDTO(org.wso2.carbon.apimgt.rest.api.service.catalog.dto.ServiceListDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArrayList(java.util.ArrayList) ServiceDTO(org.wso2.carbon.apimgt.rest.api.service.catalog.dto.ServiceDTO) ServiceEntry(org.wso2.carbon.apimgt.api.model.ServiceEntry) ServiceFilterParams(org.wso2.carbon.apimgt.api.model.ServiceFilterParams)

Example 4 with ServiceFilterParams

use of org.wso2.carbon.apimgt.api.model.ServiceFilterParams in project carbon-apimgt by wso2.

the class ServiceEntryMappingUtil method getServiceFilterParams.

/**
 * Create Service Filter Params object based on the parameters
 * @param name Service name
 * @param version Service version
 * @param definitionType Service Definition Type
 * @param key Service key
 * @param sortBy Sort By
 * @param sortOrder Sort Order
 * @param limit
 * @param offset
 * @return
 */
public static ServiceFilterParams getServiceFilterParams(String name, String version, String definitionType, String key, String sortBy, String sortOrder, Integer limit, Integer offset) {
    limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    sortOrder = sortOrder != null ? sortOrder : RestApiConstants.DEFAULT_SORT_ORDER;
    sortBy = getServiceSortByField(sortBy);
    name = name != null ? name : StringUtils.EMPTY;
    version = version != null ? version : StringUtils.EMPTY;
    definitionType = definitionType != null ? definitionType : StringUtils.EMPTY;
    key = key != null ? key : StringUtils.EMPTY;
    ServiceFilterParams filterParams = new ServiceFilterParams();
    filterParams.setName(name);
    filterParams.setVersion(version);
    filterParams.setDefinitionType(definitionType);
    filterParams.setKey(key);
    filterParams.setSortBy(sortBy);
    filterParams.setSortOrder(sortOrder);
    filterParams.setLimit(limit);
    filterParams.setOffset(offset);
    return filterParams;
}
Also used : ServiceFilterParams(org.wso2.carbon.apimgt.api.model.ServiceFilterParams)

Aggregations

ArrayList (java.util.ArrayList)2 ServiceEntry (org.wso2.carbon.apimgt.api.model.ServiceEntry)2 ServiceFilterParams (org.wso2.carbon.apimgt.api.model.ServiceFilterParams)2 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 API (org.wso2.carbon.apimgt.api.model.API)1 PaginationDTO (org.wso2.carbon.apimgt.rest.api.service.catalog.dto.PaginationDTO)1 ServiceDTO (org.wso2.carbon.apimgt.rest.api.service.catalog.dto.ServiceDTO)1 ServiceListDTO (org.wso2.carbon.apimgt.rest.api.service.catalog.dto.ServiceListDTO)1