Search in sources :

Example 1 with SearchResultDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.SearchResultDTO in project carbon-apimgt by wso2.

the class SearchApiServiceImpl method searchGet.

@Override
public Response searchGet(Integer limit, Integer offset, String xWSO2Tenant, String query, String ifNoneMatch, MessageContext messageContext) {
    SearchResultListDTO resultListDTO = new SearchResultListDTO();
    List<SearchResultDTO> allmatchedResults = new ArrayList<>();
    limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    query = query == null ? "*" : query;
    String organization = RestApiUtil.getOrganization(messageContext);
    try {
        if (!query.contains(":")) {
            query = (APIConstants.CONTENT_SEARCH_TYPE_PREFIX + ":" + query);
        }
        String username = RestApiCommonUtil.getLoggedInUsername();
        APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
        Map<String, Object> result = null;
        // Extracting search queries for the recommendation system
        apiConsumer.publishSearchQuery(query, username);
        if (query.startsWith(APIConstants.CONTENT_SEARCH_TYPE_PREFIX)) {
            result = apiConsumer.searchPaginatedContent(query, organization, offset, limit);
        } else {
            result = apiConsumer.searchPaginatedAPIs(query, organization, offset, limit, null, null);
        }
        ArrayList<Object> apis;
        /* Above searchPaginatedAPIs method underneath calls searchPaginatedAPIsByContent method,searchPaginatedAPIs
            method and searchAPIDoc method in AbstractApiManager. And those methods respectively returns ArrayList,
            TreeSet and a HashMap.
            Hence the below logic.
            */
        Object apiSearchResults = result.get("apis");
        if (apiSearchResults instanceof List<?>) {
            apis = (ArrayList<Object>) apiSearchResults;
        } else if (apiSearchResults instanceof HashMap) {
            Collection<String> values = ((HashMap) apiSearchResults).values();
            apis = new ArrayList<Object>(values);
        } else {
            apis = new ArrayList<Object>();
            apis.addAll((Collection<?>) apiSearchResults);
        }
        for (Object searchResult : apis) {
            if (searchResult instanceof API) {
                API api = (API) searchResult;
                SearchResultDTO apiResult = SearchResultMappingUtil.fromAPIToAPIResultDTO(api);
                allmatchedResults.add(apiResult);
            } else if (searchResult instanceof Map.Entry) {
                Map.Entry pair = (Map.Entry) searchResult;
                SearchResultDTO docResult = SearchResultMappingUtil.fromDocumentationToDocumentResultDTO((Documentation) pair.getKey(), (API) pair.getValue());
                allmatchedResults.add(docResult);
            } else if (searchResult instanceof APIProduct) {
                APIProduct apiProduct = (APIProduct) searchResult;
                SearchResultDTO apiResult = SearchResultMappingUtil.fromAPIToAPIResultDTO(apiProduct);
                allmatchedResults.add(apiResult);
            }
        }
        Object totalLength = result.get("length");
        Integer length = 0;
        if (totalLength != null) {
            length = (Integer) totalLength;
        }
        List<Object> allmatchedObjectResults = new ArrayList<>(allmatchedResults);
        resultListDTO.setList(allmatchedObjectResults);
        resultListDTO.setCount(allmatchedResults.size());
        SearchResultMappingUtil.setPaginationParams(resultListDTO, query, offset, limit, length);
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving search results";
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return Response.ok().entity(resultListDTO).build();
}
Also used : HashMap(java.util.HashMap) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) ArrayList(java.util.ArrayList) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) SearchResultDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.SearchResultDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SearchResultListDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.SearchResultListDTO) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) API(org.wso2.carbon.apimgt.api.model.API) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with SearchResultDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.SearchResultDTO in project carbon-apimgt by wso2.

the class SearchApiServiceImpl method search.

public Response search(Integer limit, Integer offset, String query, String ifNoneMatch, MessageContext messageContext) throws APIManagementException {
    SearchResultListDTO resultListDTO = new SearchResultListDTO();
    List<SearchResultDTO> allmatchedResults = new ArrayList<>();
    limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    query = query == null ? "*" : query;
    if (!query.contains(":")) {
        query = (APIConstants.CONTENT_SEARCH_TYPE_PREFIX + ":" + query);
    }
    APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
    String organization = RestApiUtil.getOrganization(messageContext);
    Map<String, Object> result = null;
    if (query.startsWith(APIConstants.CONTENT_SEARCH_TYPE_PREFIX)) {
        result = apiProvider.searchPaginatedContent(query, organization, offset, limit);
    } else {
        result = apiProvider.searchPaginatedAPIs(query, organization, offset, limit, null, null);
    }
    ArrayList<Object> apis;
    /* Above searchPaginatedAPIs method underneath calls searchPaginatedAPIsByContent method,searchPaginatedAPIs
        method and searchAPIDoc method in AbstractApiManager. And those methods respectively returns ArrayList,
        TreeSet and a HashMap.
        Hence the below logic.
        */
    Object apiSearchResults = result.get("apis");
    if (apiSearchResults instanceof List<?>) {
        apis = (ArrayList<Object>) apiSearchResults;
    } else if (apiSearchResults instanceof HashMap) {
        Collection<String> values = ((HashMap) apiSearchResults).values();
        apis = new ArrayList<Object>(values);
    } else {
        apis = new ArrayList<Object>();
        apis.addAll((Collection<?>) apiSearchResults);
    }
    for (Object searchResult : apis) {
        if (searchResult instanceof API) {
            API api = (API) searchResult;
            SearchResultDTO apiResult = SearchResultMappingUtil.fromAPIToAPIResultDTO(api);
            allmatchedResults.add(apiResult);
        } else if (searchResult instanceof APIProduct) {
            APIProduct apiproduct = (APIProduct) searchResult;
            SearchResultDTO apiResult = SearchResultMappingUtil.fromAPIProductToAPIResultDTO(apiproduct);
            allmatchedResults.add(apiResult);
        } else if (searchResult instanceof Map.Entry) {
            Map.Entry pair = (Map.Entry) searchResult;
            SearchResultDTO docResult;
            if (pair.getValue() instanceof API) {
                docResult = SearchResultMappingUtil.fromDocumentationToDocumentResultDTO((Documentation) pair.getKey(), (API) pair.getValue());
            } else {
                docResult = SearchResultMappingUtil.fromDocumentationToProductDocumentResultDTO((Documentation) pair.getKey(), (APIProduct) pair.getValue());
            }
            allmatchedResults.add(docResult);
        }
    }
    Object totalLength = result.get("length");
    Integer length = 0;
    if (totalLength != null) {
        length = (Integer) totalLength;
    }
    List<Object> allmatchedObjectResults = new ArrayList<>(allmatchedResults);
    resultListDTO.setList(allmatchedObjectResults);
    resultListDTO.setCount(allmatchedResults.size());
    SearchResultMappingUtil.setPaginationParams(resultListDTO, query, offset, limit, length);
    return Response.ok().entity(resultListDTO).build();
}
Also used : HashMap(java.util.HashMap) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) ArrayList(java.util.ArrayList) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) SearchResultDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SearchResultDTO) SearchResultListDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SearchResultListDTO) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) API(org.wso2.carbon.apimgt.api.model.API) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 API (org.wso2.carbon.apimgt.api.model.API)2 APIProduct (org.wso2.carbon.apimgt.api.model.APIProduct)2 Documentation (org.wso2.carbon.apimgt.api.model.Documentation)2 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)1 SearchResultDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SearchResultDTO)1 SearchResultListDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SearchResultListDTO)1 SearchResultDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.SearchResultDTO)1 SearchResultListDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.SearchResultListDTO)1