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