Search in sources :

Example 1 with MonetizationUsagePublishInfo

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

the class MonetizationApiServiceImpl method monetizationPublishUsageStatusGet.

/**
 * Retrieves the status of the last monetization usage publishing job
 *
 * @return Retruns the status of the last monetization usage publishing jon
 */
@Override
public Response monetizationPublishUsageStatusGet(MessageContext messageContext) {
    MonetizationUsagePublishInfo monetizationUsagePublishInfo;
    try {
        APIAdmin apiAdmin = new APIAdminImpl();
        monetizationUsagePublishInfo = apiAdmin.getMonetizationUsagePublishInfo();
        return Response.ok().entity(MonetizationAPIMappinUtil.fromUsageStateToDTO(monetizationUsagePublishInfo)).build();
    } catch (APIManagementException ex) {
        RestApiUtil.handleInternalServerError("Could not derive monetization usage publish info", ex, log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) MonetizationUsagePublishInfo(org.wso2.carbon.apimgt.api.model.MonetizationUsagePublishInfo) APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl)

Example 2 with MonetizationUsagePublishInfo

use of org.wso2.carbon.apimgt.api.model.MonetizationUsagePublishInfo 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;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) MonetizationException(org.wso2.carbon.apimgt.api.MonetizationException) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 3 with MonetizationUsagePublishInfo

use of org.wso2.carbon.apimgt.api.model.MonetizationUsagePublishInfo 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);
    }
}
Also used : Monetization(org.wso2.carbon.apimgt.api.model.Monetization) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException)

Example 4 with MonetizationUsagePublishInfo

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

the class ApiMgtDAO method getMonetizationUsagePublishInfo.

/**
 * Derives info about monetization usage publish job
 *
 * @return ifno about the monetization usage publish job
 * @throws APIManagementException
 */
public MonetizationUsagePublishInfo getMonetizationUsagePublishInfo() throws APIManagementException {
    Connection conn = null;
    ResultSet rs = null;
    PreparedStatement ps = null;
    try {
        conn = APIMgtDBUtil.getConnection();
        String query = SQLConstants.GET_MONETIZATION_USAGE_PUBLISH_INFO;
        ps = conn.prepareStatement(query);
        rs = ps.executeQuery();
        if (rs.next()) {
            MonetizationUsagePublishInfo monetizationUsagePublishInfo = new MonetizationUsagePublishInfo();
            monetizationUsagePublishInfo.setId(rs.getString("ID"));
            monetizationUsagePublishInfo.setState(rs.getString("STATE"));
            monetizationUsagePublishInfo.setStatus(rs.getString("STATUS"));
            monetizationUsagePublishInfo.setStartedTime(rs.getLong("STARTED_TIME"));
            monetizationUsagePublishInfo.setLastPublishTime(rs.getLong("PUBLISHED_TIME"));
            return monetizationUsagePublishInfo;
        }
    } catch (SQLException e) {
        handleException("Error while retrieving Monetization Usage Publish Info: ", e);
    } finally {
        APIMgtDBUtil.closeAllConnections(ps, conn, rs);
    }
    return null;
}
Also used : MonetizationUsagePublishInfo(org.wso2.carbon.apimgt.api.model.MonetizationUsagePublishInfo) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 5 with MonetizationUsagePublishInfo

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

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