Search in sources :

Example 1 with APIAPIProductNameComparator

use of org.wso2.carbon.apimgt.impl.utils.APIAPIProductNameComparator in project carbon-apimgt by wso2.

the class APIConsumerImpl method searchPaginatedAPIs.

@Override
public Map<String, Object> searchPaginatedAPIs(String searchQuery, String organization, int start, int end, String sortBy, String sortOrder) throws APIManagementException {
    Map<String, Object> result = new HashMap<String, Object>();
    if (log.isDebugEnabled()) {
        log.debug("Original search query received : " + searchQuery);
    }
    Organization org = new Organization(organization);
    String userName = (userNameWithoutChange != null) ? userNameWithoutChange : username;
    String[] roles = APIUtil.getListOfRoles(userName);
    Map<String, Object> properties = APIUtil.getUserProperties(userName);
    UserContext userCtx = new UserContext(userNameWithoutChange, org, properties, roles);
    try {
        DevPortalAPISearchResult searchAPIs = apiPersistenceInstance.searchAPIsForDevPortal(org, searchQuery, start, end, userCtx);
        if (log.isDebugEnabled()) {
            log.debug("searched Devportal APIs for query : " + searchQuery + " :-->: " + searchAPIs.toString());
        }
        SortedSet<Object> apiSet = new TreeSet<>(new APIAPIProductNameComparator());
        if (searchAPIs != null) {
            List<DevPortalAPIInfo> list = searchAPIs.getDevPortalAPIInfoList();
            List<Object> apiList = new ArrayList<>();
            for (DevPortalAPIInfo devPortalAPIInfo : list) {
                API mappedAPI = APIMapper.INSTANCE.toApi(devPortalAPIInfo);
                mappedAPI.setRating(APIUtil.getAverageRating(mappedAPI.getUuid()));
                apiList.add(mappedAPI);
            }
            apiSet.addAll(apiList);
            result.put("apis", apiSet);
            result.put("length", searchAPIs.getTotalAPIsCount());
            result.put("isMore", true);
        } else {
            result.put("apis", apiSet);
            result.put("length", 0);
            result.put("isMore", false);
        }
    } catch (APIPersistenceException e) {
        throw new APIManagementException("Error while searching the api ", e);
    }
    return result;
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) UserContext(org.wso2.carbon.apimgt.persistence.dto.UserContext) DevPortalAPIInfo(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPIInfo) ArrayList(java.util.ArrayList) DevPortalAPISearchResult(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPISearchResult) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) TreeSet(java.util.TreeSet) JSONObject(org.json.simple.JSONObject) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) API(org.wso2.carbon.apimgt.api.model.API) APIAPIProductNameComparator(org.wso2.carbon.apimgt.impl.utils.APIAPIProductNameComparator)

Example 2 with APIAPIProductNameComparator

use of org.wso2.carbon.apimgt.impl.utils.APIAPIProductNameComparator in project carbon-apimgt by wso2.

the class AbstractAPIManager method searchPaginatedAPIs.

/**
 * Returns API Search result based on the provided query. This search method supports '&' based concatenate
 * search in multiple fields.
 *
 * @param registry
 * @param tenantId
 * @param searchQuery Ex: provider=*admin*&version=*1*
 * @return API result
 * @throws APIManagementException
 */
public Map<String, Object> searchPaginatedAPIs(Registry registry, int tenantId, String searchQuery, int start, int end, boolean limitAttributes, boolean reducedPublisherAPIInfo) throws APIManagementException {
    SortedSet<Object> apiSet = new TreeSet<>(new APIAPIProductNameComparator());
    List<Object> apiList = new ArrayList<>();
    Map<String, Object> result = new HashMap<String, Object>();
    int totalLength = 0;
    boolean isMore = false;
    try {
        String paginationLimit = getAPIManagerConfiguration().getFirstProperty(APIConstants.API_STORE_APIS_PER_PAGE);
        // If the Config exists use it to set the pagination limit
        final int maxPaginationLimit;
        if (paginationLimit != null) {
            // The additional 1 added to the maxPaginationLimit is to help us determine if more
            // APIs may exist so that we know that we are unable to determine the actual total
            // API count. We will subtract this 1 later on so that it does not interfere with
            // the logic of the rest of the application
            int pagination = Integer.parseInt(paginationLimit);
            // leading to some of the APIs not being displayed
            if (pagination < 11) {
                pagination = 11;
                log.warn("Value of '" + APIConstants.API_STORE_APIS_PER_PAGE + "' is too low, defaulting to 11");
            }
            maxPaginationLimit = start + pagination + 1;
        } else // Else if the config is not specified we go with default functionality and load all
        {
            maxPaginationLimit = Integer.MAX_VALUE;
        }
        PaginationContext.init(start, end, "ASC", APIConstants.API_OVERVIEW_NAME, maxPaginationLimit);
        List<GovernanceArtifact> governanceArtifacts = GovernanceUtils.findGovernanceArtifacts(getSearchQuery(searchQuery), registry, APIConstants.API_RXT_MEDIA_TYPE, true);
        totalLength = PaginationContext.getInstance().getLength();
        boolean isFound = true;
        if (governanceArtifacts == null || governanceArtifacts.size() == 0) {
            if (searchQuery.contains(APIConstants.API_OVERVIEW_PROVIDER)) {
                searchQuery = searchQuery.replaceAll(APIConstants.API_OVERVIEW_PROVIDER, APIConstants.API_OVERVIEW_OWNER);
                governanceArtifacts = GovernanceUtils.findGovernanceArtifacts(getSearchQuery(searchQuery), registry, APIConstants.API_RXT_MEDIA_TYPE, true);
                if (governanceArtifacts == null || governanceArtifacts.size() == 0) {
                    isFound = false;
                }
            } else {
                isFound = false;
            }
        }
        if (!isFound) {
            result.put("apis", apiSet);
            result.put("length", 0);
            result.put("isMore", isMore);
            return result;
        }
        // Check to see if we can speculate that there are more APIs to be loaded
        if (maxPaginationLimit == totalLength) {
            // More APIs exist, cannot determine total API count without incurring perf hit
            isMore = true;
            // Remove the additional 1 added earlier when setting max pagination limit
            --totalLength;
        }
        int tempLength = 0;
        for (GovernanceArtifact artifact : governanceArtifacts) {
            String type = artifact.getAttribute(APIConstants.API_OVERVIEW_TYPE);
            if (APIConstants.API_PRODUCT.equals(type)) {
                APIProduct resultAPI = APIUtil.getAPIProduct(artifact, registry);
                if (resultAPI != null) {
                    apiList.add(resultAPI);
                }
            } else {
                API resultAPI;
                if (APIUtil.getAPIIdentifierFromUUID(artifact.getId()) != null) {
                    if (limitAttributes) {
                        resultAPI = APIUtil.getAPI(artifact);
                    } else {
                        if (reducedPublisherAPIInfo) {
                            resultAPI = APIUtil.getReducedPublisherAPIForListing(artifact, registry);
                        } else {
                            resultAPI = APIUtil.getAPI(artifact, registry);
                        }
                    }
                    if (resultAPI != null) {
                        apiList.add(resultAPI);
                    }
                }
            }
            // Ensure the APIs returned matches the length, there could be an additional API
            // returned due incrementing the pagination limit when getting from registry
            tempLength++;
            if (tempLength >= totalLength) {
                break;
            }
        }
        // Creating a apiIds string
        String apiIdsString = "";
        int apiCount = apiList.size();
        if (!reducedPublisherAPIInfo) {
            for (int i = 0; i < apiCount; i++) {
                Object api = apiList.get(i);
                String apiId = "";
                if (api instanceof API) {
                    apiId = ((API) api).getId().getApplicationId();
                } else if (api instanceof APIProduct) {
                    apiId = ((APIProduct) api).getId().getApplicationId();
                }
                if (apiId != null && !apiId.isEmpty()) {
                    if (apiIdsString.isEmpty()) {
                        apiIdsString = apiId;
                    } else {
                        apiIdsString = apiIdsString + "," + apiId;
                    }
                }
            }
        }
        apiSet.addAll(apiList);
    } catch (RegistryException e) {
        String msg = "Failed to search APIs with type";
        throw new APIManagementException(msg, e);
    } finally {
        PaginationContext.destroy();
    }
    result.put("apis", apiSet);
    result.put("length", totalLength);
    result.put("isMore", isMore);
    return result;
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) GovernanceArtifact(org.wso2.carbon.governance.api.common.dataobjects.GovernanceArtifact) ArrayList(java.util.ArrayList) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) TreeSet(java.util.TreeSet) JSONObject(org.json.simple.JSONObject) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) API(org.wso2.carbon.apimgt.api.model.API) APIAPIProductNameComparator(org.wso2.carbon.apimgt.impl.utils.APIAPIProductNameComparator)

Aggregations

ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 TreeSet (java.util.TreeSet)2 JSONObject (org.json.simple.JSONObject)2 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 API (org.wso2.carbon.apimgt.api.model.API)2 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)2 APIAPIProductNameComparator (org.wso2.carbon.apimgt.impl.utils.APIAPIProductNameComparator)2 LinkedHashMap (java.util.LinkedHashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 APIProduct (org.wso2.carbon.apimgt.api.model.APIProduct)1 DevPortalAPI (org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI)1 DevPortalAPIInfo (org.wso2.carbon.apimgt.persistence.dto.DevPortalAPIInfo)1 DevPortalAPISearchResult (org.wso2.carbon.apimgt.persistence.dto.DevPortalAPISearchResult)1 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)1 PublisherAPI (org.wso2.carbon.apimgt.persistence.dto.PublisherAPI)1 UserContext (org.wso2.carbon.apimgt.persistence.dto.UserContext)1 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)1 GovernanceArtifact (org.wso2.carbon.governance.api.common.dataobjects.GovernanceArtifact)1 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)1