Search in sources :

Example 1 with SubscriptionValidationResult

use of org.wso2.carbon.apimgt.core.models.SubscriptionValidationResult in project carbon-apimgt by wso2.

the class APISubscriptionDAOImpl method validateSubscription.

/**
 * Validates a subscription
 *
 * @param apiContext  Context of the API
 * @param apiVersion  Version of the API
 * @param clientId    Client id of the application
 * @return Subscription Validation Information
 * @throws APIMgtDAOException   If failed to get subscription validation results.
 */
public SubscriptionValidationResult validateSubscription(String apiContext, String apiVersion, String clientId) throws APIMgtDAOException {
    final String validateSubscriptionSql = "SELECT SUBS.API_ID AS API_ID, SUBS.APPLICATION_ID AS APP_ID, " + "SUBS.SUB_STATUS AS SUB_STATUS, API.PROVIDER AS API_PROVIDER, API.NAME AS API_NAME, " + "APP.NAME AS APP_NAME, APP.CREATED_BY AS APP_OWNER, POLICY.NAME AS SUBS_POLICY " + "FROM AM_SUBSCRIPTION SUBS, AM_API API, AM_APPLICATION APP, AM_SUBSCRIPTION_POLICY POLICY, " + "AM_APP_KEY_MAPPING KEYS " + "WHERE API.CONTEXT = ? AND API.VERSION = ? AND KEYS.CLIENT_ID= ? " + "AND APP.AND SUBS.API_ID = API.UUID AND SUBS.APPLICATION_ID = APP.UUID " + "AND SUBS.TIER_ID = POLICY.UUID AND KEYS.APPLICATION_ID = APP.UUID";
    SubscriptionValidationResult validationInfo = new SubscriptionValidationResult(false);
    try (Connection conn = DAOUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(validateSubscriptionSql)) {
        ps.setString(1, apiContext);
        ps.setString(2, apiVersion);
        ps.setString(3, clientId);
        try (ResultSet rs = ps.executeQuery()) {
            if (rs.next()) {
                validationInfo.setValid(true);
                validationInfo.setApiId(rs.getString("API_ID"));
                validationInfo.setApplicationId(rs.getString("APP_ID"));
                validationInfo.setSubscriptionStatus(APIMgtConstants.SubscriptionStatus.valueOf(rs.getString("SUB_STATUS")));
                validationInfo.setApiProvider(rs.getString("API_PROVIDER"));
                validationInfo.setApiName(rs.getString("API_NAME"));
                validationInfo.setApplicationName(rs.getString("APP_NAME"));
                validationInfo.setApplicationOwner(rs.getString("APP_OWNER"));
                validationInfo.setSubscriptionStatus(APIMgtConstants.SubscriptionStatus.valueOf(rs.getString("SUBS_POLICY")));
            }
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "validating subscription(apiContext: " + apiContext + ", apiVersion: " + apiVersion + ", clientId: " + clientId + ")", e);
    }
    return validationInfo;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) SubscriptionValidationResult(org.wso2.carbon.apimgt.core.models.SubscriptionValidationResult) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)1 SubscriptionValidationResult (org.wso2.carbon.apimgt.core.models.SubscriptionValidationResult)1