Search in sources :

Example 1 with APIVersionStringComparator

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

the class APIProviderImpl method calculateVersionTimestamp.

private String calculateVersionTimestamp(String provider, String name, String version, String org) throws APIManagementException {
    if (StringUtils.isEmpty(provider) || StringUtils.isEmpty(name) || StringUtils.isEmpty(org)) {
        throw new APIManagementException("Invalid API information, name=" + name + " provider=" + provider + " organization=" + org);
    }
    TreeMap<String, API> apiSortedMap = new TreeMap<>();
    List<API> apiList = getAPIVersionsByProviderAndName(provider, name, org);
    for (API mappedAPI : apiList) {
        apiSortedMap.put(mappedAPI.getVersionTimestamp(), mappedAPI);
    }
    APIVersionStringComparator comparator = new APIVersionStringComparator();
    String latestVersion = version;
    long previousTimestamp = 0L;
    String latestTimestamp = "";
    for (API tempAPI : apiSortedMap.values()) {
        if (comparator.compare(tempAPI.getId().getVersion(), latestVersion) > 0) {
            latestTimestamp = String.valueOf((previousTimestamp + Long.valueOf(tempAPI.getVersionTimestamp())) / 2);
            break;
        } else {
            previousTimestamp = Long.valueOf(tempAPI.getVersionTimestamp());
        }
    }
    if (StringUtils.isEmpty(latestTimestamp)) {
        latestTimestamp = String.valueOf(System.currentTimeMillis());
    }
    return latestTimestamp;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) 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) APIVersionStringComparator(org.wso2.carbon.apimgt.impl.utils.APIVersionStringComparator) TreeMap(java.util.TreeMap)

Example 2 with APIVersionStringComparator

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

the class APIProviderImpl method publishToExternalAPIStores.

/**
 * Publish API to external stores given by external store Ids
 *
 * @param api              API which need to published
 * @param externalStoreIds APIStore Ids which need to publish API
 * @throws APIManagementException If failed to publish to external stores
 */
@Override
public boolean publishToExternalAPIStores(API api, List<String> externalStoreIds) throws APIManagementException {
    Set<APIStore> inputStores = new HashSet<>();
    boolean apiOlderVersionExist = false;
    APIIdentifier apiIdentifier = api.getId();
    for (String store : externalStoreIds) {
        if (StringUtils.isNotEmpty(store)) {
            APIStore inputStore = APIUtil.getExternalAPIStore(store, APIUtil.getTenantIdFromTenantDomain(tenantDomain));
            if (inputStore == null) {
                String errorMessage = "Error while publishing to external stores. Invalid External Store Id: " + store;
                log.error(errorMessage);
                ExceptionCodes exceptionCode = ExceptionCodes.EXTERNAL_STORE_ID_NOT_FOUND;
                throw new APIManagementException(errorMessage, new ErrorItem(exceptionCode.getErrorMessage(), errorMessage, exceptionCode.getErrorCode(), exceptionCode.getHttpStatusCode()));
            }
            inputStores.add(inputStore);
        }
    }
    Set<String> versions = getAPIVersions(apiIdentifier.getProviderName(), apiIdentifier.getName(), api.getOrganization());
    APIVersionStringComparator comparator = new APIVersionStringComparator();
    for (String tempVersion : versions) {
        if (comparator.compare(tempVersion, apiIdentifier.getVersion()) < 0) {
            apiOlderVersionExist = true;
            break;
        }
    }
    return updateAPIsInExternalAPIStores(api, inputStores, apiOlderVersionExist);
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) ErrorItem(org.wso2.carbon.apimgt.api.ErrorItem) APIVersionStringComparator(org.wso2.carbon.apimgt.impl.utils.APIVersionStringComparator) ExceptionCodes(org.wso2.carbon.apimgt.api.ExceptionCodes) APIStore(org.wso2.carbon.apimgt.api.model.APIStore) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 3 with APIVersionStringComparator

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

the class PublisherCommonUtils method getLifecycleStateInformation.

/**
 * Get lifecycle state information of API or API Product
 *
 * @param identifier   Unique identifier of API or API Product
 * @param organization Organization of logged-in user
 * @return LifecycleStateDTO object
 * @throws APIManagementException if there is en error while retrieving the lifecycle state information
 */
public static LifecycleStateDTO getLifecycleStateInformation(Identifier identifier, String organization) throws APIManagementException {
    APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
    Map<String, Object> apiLCData = apiProvider.getAPILifeCycleData(identifier.getUUID(), organization);
    if (apiLCData == null) {
        String type;
        if (identifier instanceof APIProductIdentifier) {
            type = APIConstants.API_PRODUCT;
        } else {
            type = APIConstants.API_IDENTIFIER_TYPE;
        }
        throw new APIManagementException("Error while getting lifecycle state for " + type + " with ID " + identifier, ExceptionCodes.from(ExceptionCodes.LIFECYCLE_STATE_INFORMATION_NOT_FOUND, type, identifier.getUUID()));
    } else {
        boolean apiOlderVersionExist = false;
        // check whether other versions of the current API exists
        APIVersionStringComparator comparator = new APIVersionStringComparator();
        Set<String> versions = apiProvider.getAPIVersions(APIUtil.replaceEmailDomain(identifier.getProviderName()), identifier.getName(), organization);
        for (String tempVersion : versions) {
            if (comparator.compare(tempVersion, identifier.getVersion()) < 0) {
                apiOlderVersionExist = true;
                break;
            }
        }
        return APIMappingUtil.fromLifecycleModelToDTO(apiLCData, apiOlderVersionExist);
    }
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) JSONObject(org.json.simple.JSONObject) APIVersionStringComparator(org.wso2.carbon.apimgt.impl.utils.APIVersionStringComparator) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)3 APIVersionStringComparator (org.wso2.carbon.apimgt.impl.utils.APIVersionStringComparator)3 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 TreeMap (java.util.TreeMap)1 JSONObject (org.json.simple.JSONObject)1 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)1 ErrorItem (org.wso2.carbon.apimgt.api.ErrorItem)1 ExceptionCodes (org.wso2.carbon.apimgt.api.ExceptionCodes)1 API (org.wso2.carbon.apimgt.api.model.API)1 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)1 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)1 APIStore (org.wso2.carbon.apimgt.api.model.APIStore)1 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)1 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)1 PublisherAPI (org.wso2.carbon.apimgt.persistence.dto.PublisherAPI)1