Search in sources :

Example 6 with MonetizationUsagePublishInfo

use of org.wso2.carbon.apimgt.api.model.MonetizationUsagePublishInfo 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

APIAdmin (org.wso2.carbon.apimgt.api.APIAdmin)4 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)4 APIAdminImpl (org.wso2.carbon.apimgt.impl.APIAdminImpl)4 DateFormat (java.text.DateFormat)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Date (java.util.Date)3 MonetizationUsagePublishInfo (org.wso2.carbon.apimgt.api.model.MonetizationUsagePublishInfo)3 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Calendar (java.util.Calendar)1 MonetizationException (org.wso2.carbon.apimgt.api.MonetizationException)1 Monetization (org.wso2.carbon.apimgt.api.model.Monetization)1 APIManagerConfiguration (org.wso2.carbon.apimgt.impl.APIManagerConfiguration)1 MonetizationUsagePublishAgent (org.wso2.carbon.apimgt.impl.monetization.MonetizationUsagePublishAgent)1 MonetizationUsagePublishInfoDTO (org.wso2.carbon.apimgt.rest.api.admin.v1.dto.MonetizationUsagePublishInfoDTO)1