use of org.wso2.carbon.apimgt.api.model.Monetization in project carbon-apimgt by wso2.
the class MonetizationAPIMappinUtil method fromStatusToDTO.
/**
* Set value to DTO of the monetization usage publisher
*
* @param status status of the request
* @param msg description of the the status
* @return the DTO for monetization usage publish API
*/
public static PublishStatusDTO fromStatusToDTO(String status, String msg) {
PublishStatusDTO publishStatusDTO = new PublishStatusDTO();
publishStatusDTO.setStatus(status);
publishStatusDTO.setMessage(msg);
return publishStatusDTO;
}
use of org.wso2.carbon.apimgt.api.model.Monetization in project carbon-apimgt by wso2.
the class PublisherCommonUtils method updateApiProduct.
/**
* Update an API Product.
*
* @param originalAPIProduct Existing API Product
* @param apiProductDtoToUpdate New API Product DTO to update
* @param apiProvider API Provider
* @param username Username
* @throws APIManagementException If an error occurs while retrieving and updating an existing API Product
* @throws FaultGatewaysException If an error occurs while updating an existing API Product
*/
public static APIProduct updateApiProduct(APIProduct originalAPIProduct, APIProductDTO apiProductDtoToUpdate, APIProvider apiProvider, String username, String orgId) throws APIManagementException, FaultGatewaysException {
List<String> apiSecurity = apiProductDtoToUpdate.getSecurityScheme();
// validation for tiers
List<String> tiersFromDTO = apiProductDtoToUpdate.getPolicies();
if (apiSecurity.contains(APIConstants.DEFAULT_API_SECURITY_OAUTH2) || apiSecurity.contains(APIConstants.API_SECURITY_API_KEY)) {
if (tiersFromDTO == null || tiersFromDTO.isEmpty()) {
throw new APIManagementException("No tier defined for the API Product", ExceptionCodes.TIER_CANNOT_BE_NULL);
}
}
// check whether the added API Products's tiers are all valid
Set<Tier> definedTiers = apiProvider.getTiers();
List<String> invalidTiers = PublisherCommonUtils.getInvalidTierNames(definedTiers, tiersFromDTO);
if (!invalidTiers.isEmpty()) {
throw new APIManagementException("Specified tier(s) " + Arrays.toString(invalidTiers.toArray()) + " are invalid", ExceptionCodes.TIER_NAME_INVALID);
}
if (apiProductDtoToUpdate.getAdditionalProperties() != null) {
String errorMessage = PublisherCommonUtils.validateAdditionalProperties(apiProductDtoToUpdate.getAdditionalProperties());
if (!errorMessage.isEmpty()) {
throw new APIManagementException(errorMessage, ExceptionCodes.from(ExceptionCodes.INVALID_ADDITIONAL_PROPERTIES, originalAPIProduct.getId().getName(), originalAPIProduct.getId().getVersion()));
}
}
APIProduct product = APIMappingUtil.fromDTOtoAPIProduct(apiProductDtoToUpdate, username);
product.setState(originalAPIProduct.getState());
// We do not allow to modify provider,name,version and uuid. Set the origial value
APIProductIdentifier productIdentifier = originalAPIProduct.getId();
product.setID(productIdentifier);
product.setUuid(originalAPIProduct.getUuid());
product.setOrganization(orgId);
Map<API, List<APIProductResource>> apiToProductResourceMapping = apiProvider.updateAPIProduct(product);
apiProvider.updateAPIProductSwagger(originalAPIProduct.getUuid(), apiToProductResourceMapping, product, orgId);
// preserve monetization status in the update flow
apiProvider.configureMonetizationInAPIProductArtifact(product);
return apiProvider.getAPIProduct(productIdentifier);
}
use of org.wso2.carbon.apimgt.api.model.Monetization in project carbon-apimgt by wso2.
the class SettingsMappingUtil method getMonetizationAttributes.
/**
* This method returns the monetization properties from configuration.
*
* @return List<String> monetization properties
* @throws APIManagementException
*/
private List<MonetizationAttributeDTO> getMonetizationAttributes() {
List<MonetizationAttributeDTO> monetizationAttributeDTOSList = new ArrayList<MonetizationAttributeDTO>();
JSONArray monetizationAttributes = APIUtil.getMonetizationAttributes();
for (int i = 0; i < monetizationAttributes.size(); i++) {
JSONObject monetizationAttribute = (JSONObject) monetizationAttributes.get(i);
MonetizationAttributeDTO monetizationAttributeDTO = new MonetizationAttributeDTO();
monetizationAttributeDTO.setName((String) monetizationAttribute.get(APIConstants.Monetization.ATTRIBUTE));
monetizationAttributeDTO.setDisplayName((String) monetizationAttribute.get(APIConstants.Monetization.ATTRIBUTE_DISPLAY_NAME));
monetizationAttributeDTO.setDescription((String) monetizationAttribute.get(APIConstants.Monetization.ATTRIBUTE_DESCRIPTION));
monetizationAttributeDTO.setRequired((Boolean) monetizationAttribute.get(APIConstants.Monetization.IS_ATTRIBITE_REQUIRED));
monetizationAttributeDTO.setHidden((Boolean) monetizationAttribute.get(APIConstants.Monetization.IS_ATTRIBUTE_HIDDEN));
monetizationAttributeDTOSList.add(monetizationAttributeDTO);
}
return monetizationAttributeDTOSList;
}
use of org.wso2.carbon.apimgt.api.model.Monetization in project carbon-apimgt by wso2.
the class DefaultMonetizationImpl method publishMonetizationUsageRecords.
/**
* Update info about monetization usage publish job
*
* @param monetizationUsagePublishInfo
* @return boolean always return true if there is no exception
* @throws MonetizationException
*/
@Override
public boolean publishMonetizationUsageRecords(MonetizationUsagePublishInfo monetizationUsagePublishInfo) throws MonetizationException {
APIAdmin apiAdmin = new APIAdminImpl();
monetizationUsagePublishInfo.setState(APIConstants.Monetization.COMPLETED);
monetizationUsagePublishInfo.setStatus(APIConstants.Monetization.SUCCESSFULL);
DateFormat df = new SimpleDateFormat(APIConstants.Monetization.USAGE_PUBLISH_TIME_FORMAT);
Date dateobj = new Date();
// get the time in UTC format
df.setTimeZone(TimeZone.getTimeZone(APIConstants.Monetization.USAGE_PUBLISH_TIME_ZONE));
String currentDate = df.format(dateobj);
long currentTimestamp = apiAdmin.getTimestamp(currentDate);
monetizationUsagePublishInfo.setLastPublishTime(currentTimestamp);
try {
apiAdmin.updateMonetizationUsagePublishInfo(monetizationUsagePublishInfo);
} catch (APIManagementException e) {
throw new MonetizationException("Failed to update the monetization usage publish info", e);
}
return true;
}
use of org.wso2.carbon.apimgt.api.model.Monetization in project carbon-apimgt by wso2.
the class MonetizationUsagePublishAgent method run.
@Override
public void run() {
Monetization monetizationImpl = null;
APIAdmin apiAdmin = null;
try {
apiAdmin = new APIAdminImpl();
monetizationImpl = apiAdmin.getMonetizationImplClass();
monetizationUsagePublishInfo.setState(APIConstants.Monetization.RUNNING);
monetizationUsagePublishInfo.setStatus(APIConstants.Monetization.INPROGRESS);
DateFormat df = new SimpleDateFormat(APIConstants.Monetization.USAGE_PUBLISH_TIME_FORMAT);
Date dateobj = new Date();
df.setTimeZone(TimeZone.getTimeZone(APIConstants.Monetization.USAGE_PUBLISH_TIME_ZONE));
String currentDate = df.format(dateobj);
long currentTimestamp = apiAdmin.getTimestamp(currentDate);
// set the current time as starting time of the job
monetizationUsagePublishInfo.setStartedTime(currentTimestamp);
apiAdmin.updateMonetizationUsagePublishInfo(monetizationUsagePublishInfo);
monetizationImpl.publishMonetizationUsageRecords(monetizationUsagePublishInfo);
} catch (Exception e) {
try {
// update the state and status of the job incase of any execptions
monetizationUsagePublishInfo.setState(APIConstants.Monetization.COMPLETED);
monetizationUsagePublishInfo.setStatus(APIConstants.Monetization.FAILED);
apiAdmin.updateMonetizationUsagePublishInfo(monetizationUsagePublishInfo);
} catch (APIManagementException ex) {
String errorMsg = "Failed to update the state of monetization ussge publisher";
log.error(errorMsg, ex);
}
String errorMsg = "Failed to publish monetization usage to billing Engine";
log.error(errorMsg, e);
}
}
Aggregations