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;
}
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);
}
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);
}
}
Aggregations