Search in sources :

Example 36 with Monetization

use of org.wso2.carbon.apimgt.api.model.Monetization in project carbon-apimgt by wso2.

the class MonetizationAPIMappinUtil method fromUsageStateToDTO.

/**
 * Set the info of monetization usage publisher to DTO of the monetization usage status
 *
 * @param info info about the monetization usage publisher job
 * @return the DTO for monetization usage publish status
 */
public static MonetizationUsagePublishInfoDTO fromUsageStateToDTO(MonetizationUsagePublishInfo info) {
    MonetizationUsagePublishInfoDTO monetizationUsagePublishInfo = new MonetizationUsagePublishInfoDTO();
    monetizationUsagePublishInfo.setState(info.getState());
    monetizationUsagePublishInfo.setStatus(info.getStatus());
    monetizationUsagePublishInfo.setStartedTime(Long.toString(info.getStartedTime()));
    monetizationUsagePublishInfo.setLastPublsihedTime(Long.toString(info.getLastPublishTime()));
    return monetizationUsagePublishInfo;
}
Also used : MonetizationUsagePublishInfoDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.MonetizationUsagePublishInfoDTO)

Example 37 with Monetization

use of org.wso2.carbon.apimgt.api.model.Monetization in project carbon-apimgt by wso2.

the class MonetizationApiServiceImpl method monetizationPublishUsagePost.

/**
 * Run the monetization usage publish job
 *
 * @return Response of the server
 */
@Override
public Response monetizationPublishUsagePost(MessageContext messageContext) {
    MonetizationUsagePublishInfo monetizationUsagePublishInfo;
    try {
        APIAdmin apiAdmin = new APIAdminImpl();
        monetizationUsagePublishInfo = apiAdmin.getMonetizationUsagePublishInfo();
        if (monetizationUsagePublishInfo == null) {
            monetizationUsagePublishInfo = new MonetizationUsagePublishInfo();
            monetizationUsagePublishInfo.setId(APIConstants.Monetization.USAGE_PUBLISHER_JOB_NAME);
            monetizationUsagePublishInfo.setState(APIConstants.Monetization.INITIATED);
            monetizationUsagePublishInfo.setStatus(APIConstants.Monetization.INPROGRESS);
            // read the number of days to reduce from the current time to derive the from / last publish time
            // when there is no record of the last publish time
            APIManagerConfiguration configuration = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration();
            String gap = configuration.getFirstProperty(APIConstants.Monetization.FROM_TIME_CONFIGURATION_PROPERTY);
            // if the from time / last publish time is not set , set it to default
            if (gap == null) {
                gap = APIConstants.Monetization.USAGE_PUBLISH_DEFAULT_TIME_GAP_IN_DAYS;
            }
            DateFormat df = new SimpleDateFormat(APIConstants.Monetization.USAGE_PUBLISH_TIME_FORMAT);
            df.setTimeZone(TimeZone.getTimeZone(APIConstants.Monetization.USAGE_PUBLISH_TIME_ZONE));
            Calendar cal = Calendar.getInstance();
            Date currentDate = cal.getTime();
            String formattedCurrentDate = df.format(currentDate);
            long currentTimestamp = apiAdmin.getTimestamp(formattedCurrentDate);
            monetizationUsagePublishInfo.setStartedTime(currentTimestamp);
            // reducing the number of days set to get the last published time when there is no record of
            // the last published time
            cal.add(Calendar.DATE, -Integer.parseInt(gap));
            Date fromDate = cal.getTime();
            String formattedFromDate = df.format(fromDate);
            long lastPublishedTimeStamp = apiAdmin.getTimestamp(formattedFromDate);
            monetizationUsagePublishInfo.setLastPublishTime(lastPublishedTimeStamp);
            apiAdmin.addMonetizationUsagePublishInfo(monetizationUsagePublishInfo);
        }
        if (!monetizationUsagePublishInfo.getState().equals(APIConstants.Monetization.RUNNING)) {
            executor = Executors.newSingleThreadExecutor();
            MonetizationUsagePublishAgent agent = new MonetizationUsagePublishAgent(monetizationUsagePublishInfo);
            executor.execute(agent);
            return Response.accepted().entity(MonetizationAPIMappinUtil.fromStatusToDTO("Request Accepted", "Server is running the usage publisher")).build();
        } else {
            return Response.serverError().entity(MonetizationAPIMappinUtil.fromStatusToDTO("Server could not " + "accept the request", "A job is already running")).build();
        }
    } catch (APIManagementException ex) {
        RestApiUtil.handleInternalServerError("Could not add or derive monetization usage publish info", ex, log);
    }
    return null;
}
Also used : APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) MonetizationUsagePublishAgent(org.wso2.carbon.apimgt.impl.monetization.MonetizationUsagePublishAgent) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) MonetizationUsagePublishInfo(org.wso2.carbon.apimgt.api.model.MonetizationUsagePublishInfo) APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Calendar(java.util.Calendar) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)24 JSONObject (org.json.simple.JSONObject)14 Tier (org.wso2.carbon.apimgt.api.model.Tier)14 API (org.wso2.carbon.apimgt.api.model.API)13 ArrayList (java.util.ArrayList)9 ParseException (org.json.simple.parser.ParseException)9 Monetization (org.wso2.carbon.apimgt.api.model.Monetization)9 GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)9 JSONParser (org.json.simple.parser.JSONParser)8 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)8 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)8 Gson (com.google.gson.Gson)7 MonetizationException (org.wso2.carbon.apimgt.api.MonetizationException)7 APIProduct (org.wso2.carbon.apimgt.api.model.APIProduct)7 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)7 HashMap (java.util.HashMap)6 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)6 UserStoreException (org.wso2.carbon.user.api.UserStoreException)6 HashSet (java.util.HashSet)5 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)5