Search in sources :

Example 1 with ContentSearchResultNameComparator

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

the class AbstractAPIManager method searchPaginatedAPIsByContent.

/**
 * Search api resources by their content
 *
 * @param registry
 * @param searchQuery
 * @param start
 * @param end
 * @return
 * @throws APIManagementException
 */
public Map<String, Object> searchPaginatedAPIsByContent(Registry registry, int tenantId, String searchQuery, int start, int end, boolean limitAttributes) throws APIManagementException {
    SortedSet<API> apiSet = new TreeSet<API>(new APINameComparator());
    SortedSet<APIProduct> apiProductSet = new TreeSet<APIProduct>(new APIProductNameComparator());
    Map<Documentation, API> docMap = new HashMap<Documentation, API>();
    Map<Documentation, APIProduct> productDocMap = new HashMap<Documentation, APIProduct>();
    Map<String, Object> result = new HashMap<String, Object>();
    int totalLength = 0;
    boolean isMore = false;
    // SortedSet<Object> compoundResult = new TreeSet<Object>(new ContentSearchResultNameComparator());
    ArrayList<Object> compoundResult = new ArrayList<Object>();
    try {
        GenericArtifactManager apiArtifactManager = APIUtil.getArtifactManager(registry, APIConstants.API_KEY);
        GenericArtifactManager docArtifactManager = APIUtil.getArtifactManager(registry, APIConstants.DOCUMENTATION_KEY);
        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);
        if (tenantId == -1) {
            tenantId = MultitenantConstants.SUPER_TENANT_ID;
        }
        UserRegistry systemUserRegistry = ServiceReferenceHolder.getInstance().getRegistryService().getRegistry(CarbonConstants.REGISTRY_SYSTEM_USERNAME, tenantId);
        ContentBasedSearchService contentBasedSearchService = new ContentBasedSearchService();
        String newSearchQuery = getSearchQuery(searchQuery);
        String[] searchQueries = newSearchQuery.split("&");
        String apiState = "";
        String publisherRoles = "";
        Map<String, String> attributes = new HashMap<String, String>();
        for (String searchCriterea : searchQueries) {
            String[] keyVal = searchCriterea.split("=");
            if (APIConstants.STORE_VIEW_ROLES.equals(keyVal[0])) {
                attributes.put("propertyName", keyVal[0]);
                attributes.put("rightPropertyValue", keyVal[1]);
                attributes.put("rightOp", "eq");
            } else if (APIConstants.PUBLISHER_ROLES.equals(keyVal[0])) {
                publisherRoles = keyVal[1];
            } else {
                if (APIConstants.LCSTATE_SEARCH_KEY.equals(keyVal[0])) {
                    apiState = keyVal[1];
                    continue;
                }
                attributes.put(keyVal[0], keyVal[1]);
            }
        }
        // check whether the new document indexer is engaged
        RegistryConfigLoader registryConfig = RegistryConfigLoader.getInstance();
        Map<String, Indexer> indexerMap = registryConfig.getIndexerMap();
        Indexer documentIndexer = indexerMap.get(APIConstants.DOCUMENT_MEDIA_TYPE_KEY);
        String complexAttribute;
        if (documentIndexer != null && documentIndexer instanceof DocumentIndexer) {
            // field check on document_indexed was added to prevent unindexed(by new DocumentIndexer) from coming up as search results
            // on indexed documents this property is always set to true
            complexAttribute = ClientUtils.escapeQueryChars(APIConstants.API_RXT_MEDIA_TYPE) + " OR mediaType_s:(" + ClientUtils.escapeQueryChars(APIConstants.DOCUMENT_RXT_MEDIA_TYPE) + " AND document_indexed_s:true)";
            // this was designed this way so that content search can be fully functional if registry is re-indexed after engaging DocumentIndexer
            if (!StringUtils.isEmpty(publisherRoles)) {
                complexAttribute = "(" + ClientUtils.escapeQueryChars(APIConstants.API_RXT_MEDIA_TYPE) + " AND publisher_roles_ss:" + publisherRoles + ") OR mediaType_s:(" + ClientUtils.escapeQueryChars(APIConstants.DOCUMENT_RXT_MEDIA_TYPE) + " AND publisher_roles_s:" + publisherRoles + ")";
            }
        } else {
            // document indexer required for document content search is not engaged, therefore carry out the search only for api artifact contents
            complexAttribute = ClientUtils.escapeQueryChars(APIConstants.API_RXT_MEDIA_TYPE);
            if (!StringUtils.isEmpty(publisherRoles)) {
                complexAttribute = "(" + ClientUtils.escapeQueryChars(APIConstants.API_RXT_MEDIA_TYPE) + " AND publisher_roles_ss:" + publisherRoles + ")";
            }
        }
        attributes.put(APIConstants.DOCUMENTATION_SEARCH_MEDIA_TYPE_FIELD, complexAttribute);
        attributes.put(APIConstants.API_OVERVIEW_STATUS, apiState);
        SearchResultsBean resultsBean = contentBasedSearchService.searchByAttribute(attributes, systemUserRegistry);
        String errorMsg = resultsBean.getErrorMessage();
        if (errorMsg != null) {
            handleException(errorMsg);
        }
        ResourceData[] resourceData = resultsBean.getResourceDataList();
        if (resourceData == null || resourceData.length == 0) {
            result.put("apis", compoundResult);
            result.put("length", 0);
            result.put("isMore", isMore);
        }
        totalLength = PaginationContext.getInstance().getLength();
        // 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;
        }
        for (ResourceData data : resourceData) {
            String resourcePath = data.getResourcePath();
            if (resourcePath.contains(APIConstants.APIMGT_REGISTRY_LOCATION)) {
                int index = resourcePath.indexOf(APIConstants.APIMGT_REGISTRY_LOCATION);
                resourcePath = resourcePath.substring(index);
                Resource resource = registry.get(resourcePath);
                if (APIConstants.DOCUMENT_RXT_MEDIA_TYPE.equals(resource.getMediaType()) || APIConstants.DOCUMENTATION_INLINE_CONTENT_TYPE.equals(resource.getMediaType())) {
                    if (resourcePath.contains(APIConstants.INLINE_DOCUMENT_CONTENT_DIR)) {
                        int indexOfContents = resourcePath.indexOf(APIConstants.INLINE_DOCUMENT_CONTENT_DIR);
                        resourcePath = resourcePath.substring(0, indexOfContents) + data.getName();
                    }
                    Resource docResource = registry.get(resourcePath);
                    String docArtifactId = docResource.getUUID();
                    GenericArtifact docArtifact = docArtifactManager.getGenericArtifact(docArtifactId);
                    Documentation doc = APIUtil.getDocumentation(docArtifact);
                    API associatedAPI = null;
                    APIProduct associatedAPIProduct = null;
                    int indexOfDocumentation = resourcePath.indexOf(APIConstants.DOCUMENTATION_KEY);
                    String apiPath = resourcePath.substring(0, indexOfDocumentation) + APIConstants.API_KEY;
                    Resource apiResource = registry.get(apiPath);
                    String apiArtifactId = apiResource.getUUID();
                    if (apiArtifactId != null) {
                        GenericArtifact apiArtifact = apiArtifactManager.getGenericArtifact(apiArtifactId);
                        if (apiArtifact.getAttribute(APIConstants.API_OVERVIEW_TYPE).equals(APIConstants.AuditLogConstants.API_PRODUCT)) {
                            associatedAPIProduct = APIUtil.getAPIProduct(apiArtifact, registry);
                        } else {
                            associatedAPI = APIUtil.getAPI(apiArtifact, registry);
                        }
                    } else {
                        throw new GovernanceException("artifact id is null of " + apiPath);
                    }
                    if (associatedAPI != null && doc != null) {
                        docMap.put(doc, associatedAPI);
                    }
                    if (associatedAPIProduct != null && doc != null) {
                        productDocMap.put(doc, associatedAPIProduct);
                    }
                } else {
                    String apiArtifactId = resource.getUUID();
                    API api;
                    APIProduct apiProduct;
                    if (apiArtifactId != null) {
                        GenericArtifact apiArtifact = apiArtifactManager.getGenericArtifact(apiArtifactId);
                        if (apiArtifact.getAttribute(APIConstants.API_OVERVIEW_TYPE).equals(APIConstants.API_PRODUCT)) {
                            apiProduct = APIUtil.getAPIProduct(apiArtifact, registry);
                            apiProductSet.add(apiProduct);
                        } else {
                            api = APIUtil.getAPI(apiArtifact, registry);
                            apiSet.add(api);
                        }
                    } else {
                        throw new GovernanceException("artifact id is null for " + resourcePath);
                    }
                }
            }
        }
        compoundResult.addAll(apiSet);
        compoundResult.addAll(apiProductSet);
        compoundResult.addAll(docMap.entrySet());
        compoundResult.addAll(productDocMap.entrySet());
        compoundResult.sort(new ContentSearchResultNameComparator());
    } catch (RegistryException e) {
        handleException("Failed to search APIs by content", e);
    } catch (IndexerException e) {
        handleException("Failed to search APIs by content", e);
    }
    result.put("apis", compoundResult);
    result.put("length", totalLength);
    result.put("isMore", isMore);
    return result;
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) APINameComparator(org.wso2.carbon.apimgt.impl.utils.APINameComparator) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) DocumentIndexer(org.wso2.carbon.apimgt.impl.indexing.indexer.DocumentIndexer) Indexer(org.wso2.carbon.registry.indexing.indexer.Indexer) TreeSet(java.util.TreeSet) SearchResultsBean(org.wso2.carbon.registry.indexing.service.SearchResultsBean) IndexerException(org.wso2.carbon.registry.indexing.indexer.IndexerException) DocumentIndexer(org.wso2.carbon.apimgt.impl.indexing.indexer.DocumentIndexer) GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) ResourceData(org.wso2.carbon.registry.common.ResourceData) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException) ContentBasedSearchService(org.wso2.carbon.registry.indexing.service.ContentBasedSearchService) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) RegistryConfigLoader(org.wso2.carbon.registry.indexing.RegistryConfigLoader) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) API(org.wso2.carbon.apimgt.api.model.API) JSONObject(org.json.simple.JSONObject) ContentSearchResultNameComparator(org.wso2.carbon.apimgt.impl.utils.ContentSearchResultNameComparator) APIAPIProductNameComparator(org.wso2.carbon.apimgt.impl.utils.APIAPIProductNameComparator) APIProductNameComparator(org.wso2.carbon.apimgt.impl.utils.APIProductNameComparator)

Example 2 with ContentSearchResultNameComparator

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

the class APIProviderImpl method searchPaginatedContent.

@Override
public Map<String, Object> searchPaginatedContent(String searchQuery, String organization, int start, int end) throws APIManagementException {
    ArrayList<Object> compoundResult = new ArrayList<Object>();
    Map<Documentation, API> docMap = new HashMap<Documentation, API>();
    Map<Documentation, APIProduct> productDocMap = new HashMap<Documentation, APIProduct>();
    Map<String, Object> result = new HashMap<String, Object>();
    SortedSet<API> apiSet = new TreeSet<API>(new APINameComparator());
    SortedSet<APIProduct> apiProductSet = new TreeSet<APIProduct>(new APIProductNameComparator());
    String userame = userNameWithoutChange;
    Organization org = new Organization(organization);
    Map<String, Object> properties = APIUtil.getUserProperties(userame);
    String[] roles = APIUtil.getFilteredUserRoles(userame);
    UserContext ctx = new UserContext(userame, org, properties, roles);
    try {
        PublisherContentSearchResult results = apiPersistenceInstance.searchContentForPublisher(org, searchQuery, start, end, ctx);
        if (results != null) {
            List<SearchContent> resultList = results.getResults();
            for (SearchContent item : resultList) {
                if ("API".equals(item.getType())) {
                    PublisherSearchContent publiserAPI = (PublisherSearchContent) item;
                    API api = new API(new APIIdentifier(publiserAPI.getProvider(), publiserAPI.getName(), publiserAPI.getVersion()));
                    api.setUuid(publiserAPI.getId());
                    api.setContext(publiserAPI.getContext());
                    api.setContextTemplate(publiserAPI.getContext());
                    api.setStatus(publiserAPI.getStatus());
                    apiSet.add(api);
                } else if ("APIProduct".equals(item.getType())) {
                    PublisherSearchContent publiserAPI = (PublisherSearchContent) item;
                    APIProduct api = new APIProduct(new APIProductIdentifier(publiserAPI.getProvider(), publiserAPI.getName(), publiserAPI.getVersion()));
                    api.setUuid(publiserAPI.getId());
                    api.setContextTemplate(publiserAPI.getContext());
                    api.setState(publiserAPI.getStatus());
                    apiProductSet.add(api);
                } else if (item instanceof DocumentSearchContent) {
                    // doc item
                    DocumentSearchContent docItem = (DocumentSearchContent) item;
                    Documentation doc = new Documentation(DocumentationType.valueOf(docItem.getDocType().toString()), docItem.getName());
                    doc.setSourceType(DocumentSourceType.valueOf(docItem.getSourceType().toString()));
                    doc.setVisibility(DocumentVisibility.valueOf(docItem.getVisibility().toString()));
                    doc.setId(docItem.getId());
                    if ("API".equals(docItem.getAssociatedType())) {
                        API api = new API(new APIIdentifier(docItem.getApiProvider(), docItem.getApiName(), docItem.getApiVersion()));
                        api.setUuid(docItem.getApiUUID());
                        docMap.put(doc, api);
                    } else if ("APIProduct".equals(docItem.getAssociatedType())) {
                        APIProduct api = new APIProduct(new APIProductIdentifier(docItem.getApiProvider(), docItem.getApiName(), docItem.getApiVersion()));
                        api.setUuid(docItem.getApiUUID());
                        productDocMap.put(doc, api);
                    }
                }
            }
            compoundResult.addAll(apiSet);
            compoundResult.addAll(apiProductSet);
            compoundResult.addAll(docMap.entrySet());
            compoundResult.addAll(productDocMap.entrySet());
            compoundResult.sort(new ContentSearchResultNameComparator());
            result.put("length", results.getTotalCount());
        } else {
            result.put("length", compoundResult.size());
        }
    } catch (APIPersistenceException e) {
        throw new APIManagementException("Error while searching content ", e);
    }
    result.put("apis", compoundResult);
    return result;
}
Also used : Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) APINameComparator(org.wso2.carbon.apimgt.impl.utils.APINameComparator) PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) TreeSet(java.util.TreeSet) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) SearchContent(org.wso2.carbon.apimgt.persistence.dto.SearchContent) DocumentSearchContent(org.wso2.carbon.apimgt.persistence.dto.DocumentSearchContent) PublisherSearchContent(org.wso2.carbon.apimgt.persistence.dto.PublisherSearchContent) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) PublisherSearchContent(org.wso2.carbon.apimgt.persistence.dto.PublisherSearchContent) DocumentSearchContent(org.wso2.carbon.apimgt.persistence.dto.DocumentSearchContent) UserContext(org.wso2.carbon.apimgt.persistence.dto.UserContext) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) PublisherContentSearchResult(org.wso2.carbon.apimgt.persistence.dto.PublisherContentSearchResult) JSONObject(org.json.simple.JSONObject) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) ContentSearchResultNameComparator(org.wso2.carbon.apimgt.impl.utils.ContentSearchResultNameComparator) APIProductNameComparator(org.wso2.carbon.apimgt.impl.utils.APIProductNameComparator)

Example 3 with ContentSearchResultNameComparator

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

the class APIConsumerImpl method searchPaginatedContent.

@Override
public Map<String, Object> searchPaginatedContent(String searchQuery, String organization, int start, int end) throws APIManagementException {
    ArrayList<Object> compoundResult = new ArrayList<Object>();
    Map<Documentation, API> docMap = new HashMap<Documentation, API>();
    Map<String, Object> result = new HashMap<String, Object>();
    SortedSet<API> apiSet = new TreeSet<API>(new APINameComparator());
    int totalLength = 0;
    String userame = (userNameWithoutChange != null) ? userNameWithoutChange : username;
    Organization org = new Organization(organization);
    Map<String, Object> properties = APIUtil.getUserProperties(userame);
    String[] roles = APIUtil.getFilteredUserRoles(userame);
    ;
    UserContext ctx = new UserContext(userame, org, properties, roles);
    try {
        DevPortalContentSearchResult sResults = apiPersistenceInstance.searchContentForDevPortal(org, searchQuery, start, end, ctx);
        if (sResults != null) {
            List<SearchContent> resultList = sResults.getResults();
            for (SearchContent item : resultList) {
                if (item instanceof DocumentSearchContent) {
                    // doc item
                    DocumentSearchContent docItem = (DocumentSearchContent) item;
                    Documentation doc = new Documentation(DocumentationType.valueOf(docItem.getDocType().toString()), docItem.getName());
                    doc.setSourceType(DocumentSourceType.valueOf(docItem.getSourceType().toString()));
                    doc.setVisibility(DocumentVisibility.valueOf(docItem.getVisibility().toString()));
                    doc.setId(docItem.getId());
                    API api = new API(new APIIdentifier(docItem.getApiProvider(), docItem.getApiName(), docItem.getApiVersion()));
                    api.setUuid(docItem.getApiUUID());
                    docMap.put(doc, api);
                } else {
                    DevPortalSearchContent publiserAPI = (DevPortalSearchContent) item;
                    API api = new API(new APIIdentifier(publiserAPI.getProvider(), publiserAPI.getName(), publiserAPI.getVersion()));
                    api.setUuid(publiserAPI.getId());
                    api.setContext(publiserAPI.getContext());
                    api.setContextTemplate(publiserAPI.getContext());
                    api.setStatus(publiserAPI.getStatus());
                    // need to retrieve from db
                    api.setRating(0);
                    apiSet.add(api);
                }
            }
            compoundResult.addAll(apiSet);
            compoundResult.addAll(docMap.entrySet());
            compoundResult.sort(new ContentSearchResultNameComparator());
            result.put("length", sResults.getTotalCount());
        } else {
            result.put("length", compoundResult.size());
        }
    } catch (APIPersistenceException e) {
        throw new APIManagementException("Error while searching content ", e);
    }
    result.put("apis", compoundResult);
    return result;
}
Also used : Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DevPortalSearchContent(org.wso2.carbon.apimgt.persistence.dto.DevPortalSearchContent) ArrayList(java.util.ArrayList) DevPortalContentSearchResult(org.wso2.carbon.apimgt.persistence.dto.DevPortalContentSearchResult) APINameComparator(org.wso2.carbon.apimgt.impl.utils.APINameComparator) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) TreeSet(java.util.TreeSet) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) DevPortalSearchContent(org.wso2.carbon.apimgt.persistence.dto.DevPortalSearchContent) DocumentSearchContent(org.wso2.carbon.apimgt.persistence.dto.DocumentSearchContent) SearchContent(org.wso2.carbon.apimgt.persistence.dto.SearchContent) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) DocumentSearchContent(org.wso2.carbon.apimgt.persistence.dto.DocumentSearchContent) UserContext(org.wso2.carbon.apimgt.persistence.dto.UserContext) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) 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) ContentSearchResultNameComparator(org.wso2.carbon.apimgt.impl.utils.ContentSearchResultNameComparator)

Example 4 with ContentSearchResultNameComparator

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

the class APIConsumerImpl method filterMultipleVersionedAPIs.

private Map<String, Object> filterMultipleVersionedAPIs(Map<String, Object> searchResults) {
    Object apiObj = searchResults.get("apis");
    ArrayList<Object> apiSet;
    ArrayList<APIProduct> apiProductSet = new ArrayList<>();
    if (apiObj instanceof Set) {
        apiSet = new ArrayList<>(((Set) apiObj));
    } else {
        apiSet = (ArrayList<Object>) apiObj;
    }
    // Store the length of the APIs list with the versioned APIs
    int apiSetLengthWithVersionedApis = apiSet.size();
    int totalLength = Integer.parseInt(searchResults.get("length").toString());
    // filter store results if displayMultipleVersions is set to false
    Boolean displayMultipleVersions = APIUtil.isAllowDisplayMultipleVersions();
    if (!displayMultipleVersions) {
        SortedSet<API> resultApis = new TreeSet<API>(new APINameComparator());
        for (Object result : apiSet) {
            if (result instanceof API) {
                resultApis.add((API) result);
            } else if (result instanceof Map.Entry) {
                Map.Entry<Documentation, API> entry = (Map.Entry<Documentation, API>) result;
                resultApis.add(entry.getValue());
            } else if (result instanceof APIProduct) {
                apiProductSet.add((APIProduct) result);
            }
        }
        Map<String, API> latestPublishedAPIs = new HashMap<String, API>();
        Comparator<API> versionComparator = new APIVersionComparator();
        String key;
        // Run the result api list through API version comparator and filter out multiple versions
        for (API api : resultApis) {
            key = api.getId().getProviderName() + COLON_CHAR + api.getId().getApiName();
            API existingAPI = latestPublishedAPIs.get(key);
            if (existingAPI != null) {
                // this one has a higher version number
                if (versionComparator.compare(api, existingAPI) > 0) {
                    latestPublishedAPIs.put(key, api);
                }
            } else {
                // We haven't seen this API before
                latestPublishedAPIs.put(key, api);
            }
        }
        // filter apiSet
        ArrayList<Object> tempApiSet = new ArrayList<Object>();
        for (Object result : apiSet) {
            API api = null;
            String mapKey;
            API latestAPI;
            if (result instanceof API) {
                api = (API) result;
                mapKey = api.getId().getProviderName() + COLON_CHAR + api.getId().getApiName();
                if (latestPublishedAPIs.containsKey(mapKey)) {
                    latestAPI = latestPublishedAPIs.get(mapKey);
                    if (latestAPI.getId().equals(api.getId())) {
                        tempApiSet.add(api);
                    }
                }
            } else if (result instanceof Map.Entry) {
                Map.Entry<Documentation, API> docEntry = (Map.Entry<Documentation, API>) result;
                api = docEntry.getValue();
                mapKey = api.getId().getProviderName() + COLON_CHAR + api.getId().getApiName();
                if (latestPublishedAPIs.containsKey(mapKey)) {
                    latestAPI = latestPublishedAPIs.get(mapKey);
                    if (latestAPI.getId().equals(api.getId())) {
                        tempApiSet.add(docEntry);
                    }
                }
            }
        }
        // Store the length of the APIs list without the versioned APIs
        int apiSetLengthWithoutVersionedApis = tempApiSet.size();
        apiSet = tempApiSet;
        ArrayList<Object> resultAPIandProductSet = new ArrayList<>();
        resultAPIandProductSet.addAll(apiSet);
        resultAPIandProductSet.addAll(apiProductSet);
        resultAPIandProductSet.sort(new ContentSearchResultNameComparator());
        if (apiObj instanceof Set) {
            searchResults.put("apis", new LinkedHashSet<>(resultAPIandProductSet));
        } else {
            searchResults.put("apis", resultAPIandProductSet);
        }
        searchResults.put("length", totalLength - (apiSetLengthWithVersionedApis - (apiSetLengthWithoutVersionedApis + apiProductSet.size())));
    }
    return searchResults;
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) LinkedHashSet(java.util.LinkedHashSet) SortedSet(java.util.SortedSet) HashSet(java.util.HashSet) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) ArrayList(java.util.ArrayList) APINameComparator(org.wso2.carbon.apimgt.impl.utils.APINameComparator) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIVersionComparator(org.wso2.carbon.apimgt.impl.utils.APIVersionComparator) 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) ContentSearchResultNameComparator(org.wso2.carbon.apimgt.impl.utils.ContentSearchResultNameComparator) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Aggregations

ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 TreeSet (java.util.TreeSet)4 JSONObject (org.json.simple.JSONObject)4 API (org.wso2.carbon.apimgt.api.model.API)4 Documentation (org.wso2.carbon.apimgt.api.model.Documentation)4 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)4 APINameComparator (org.wso2.carbon.apimgt.impl.utils.APINameComparator)4 ContentSearchResultNameComparator (org.wso2.carbon.apimgt.impl.utils.ContentSearchResultNameComparator)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 APIProduct (org.wso2.carbon.apimgt.api.model.APIProduct)3 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)2 APIProductNameComparator (org.wso2.carbon.apimgt.impl.utils.APIProductNameComparator)2 DevPortalAPI (org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI)2 DocumentSearchContent (org.wso2.carbon.apimgt.persistence.dto.DocumentSearchContent)2 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)2 PublisherAPI (org.wso2.carbon.apimgt.persistence.dto.PublisherAPI)2 SearchContent (org.wso2.carbon.apimgt.persistence.dto.SearchContent)2 UserContext (org.wso2.carbon.apimgt.persistence.dto.UserContext)2