use of org.wso2.carbon.apimgt.api.model.subscription.SubscriptionPolicy in project carbon-apimgt by wso2.
the class PolicyDAOImpl method addSubscriptionPolicy.
/**
* Adds an Subscription policy
*
* @param policy {@link SubscriptionPolicy} instance
* @param connection DB Connection instance
* @throws SQLException if any error occurs while setting default throttle policy related information
*/
private static void addSubscriptionPolicy(SubscriptionPolicy policy, Connection connection) throws SQLException {
String query;
query = "INSERT INTO AM_SUBSCRIPTION_POLICY (UUID, NAME, DISPLAY_NAME, DESCRIPTION, QUOTA_TYPE, QUOTA, " + "QUOTA_UNIT, UNIT_TIME, RATE_LIMIT_COUNT, RATE_LIMIT_TIME_UNIT, CUSTOM_ATTRIBUTES, " + "STOP_ON_QUOTA_REACH, BILLING_PLAN, TIME_UNIT, IS_DEPLOYED) " + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
Limit limit = policy.getDefaultQuotaPolicy().getLimit();
try (PreparedStatement statement = connection.prepareStatement(query)) {
statement.setString(1, policy.getUuid());
statement.setString(2, policy.getPolicyName());
statement.setString(3, policy.getDisplayName());
statement.setString(4, policy.getDescription());
statement.setString(5, policy.getDefaultQuotaPolicy().getType());
setDefaultThrottlePolicyDetailsPreparedStmt(limit, statement);
statement.setInt(8, policy.getDefaultQuotaPolicy().getLimit().getUnitTime());
policy.populateDataInPreparedStatement(statement);
statement.setString(14, policy.getDefaultQuotaPolicy().getLimit().getTimeUnit());
statement.setBoolean(15, policy.isDeployed());
statement.execute();
}
}
use of org.wso2.carbon.apimgt.api.model.subscription.SubscriptionPolicy in project carbon-apimgt by wso2.
the class PolicyDAOImpl method getSimplifiedPolicyByLevelAndName.
@Override
public Policy getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel policyLevel, String policyName) throws APIMgtDAOException, APIMgtResourceNotFoundException {
Policy policy = null;
final String apiPolicyQuery = "SELECT UUID,NAME FROM AM_API_POLICY WHERE NAME = ?";
final String applicationPolicyQuery = "SELECT UUID,NAME FROM AM_APPLICATION_POLICY WHERE NAME = ?";
final String subscriptionPolicyQuery = "SELECT UUID,NAME FROM AM_SUBSCRIPTION_POLICY WHERE NAME = ?";
try (Connection connection = DAOUtil.getConnection()) {
if (policyLevel.equals(APIMgtAdminService.PolicyLevel.api)) {
try (PreparedStatement statement = connection.prepareStatement(apiPolicyQuery)) {
statement.setString(1, policyName);
try (ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) {
policy = new APIPolicy(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_UUID), resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_NAME));
}
}
}
} else if (policyLevel.equals(APIMgtAdminService.PolicyLevel.application)) {
try (PreparedStatement statement = connection.prepareStatement(applicationPolicyQuery)) {
statement.setString(1, policyName);
try (ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) {
policy = new ApplicationPolicy(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_UUID), resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_NAME));
}
}
}
} else {
try (PreparedStatement statement = connection.prepareStatement(subscriptionPolicyQuery)) {
statement.setString(1, policyName);
try (ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) {
policy = new SubscriptionPolicy(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_UUID), resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_NAME));
}
}
}
}
} catch (SQLException e) {
String msg = "Error while retrieving policies";
log.error(msg, e);
throw new APIMgtDAOException(msg, ExceptionCodes.APIMGT_DAO_EXCEPTION);
}
if (policy == null) {
throw new APIMgtResourceNotFoundException("Policy " + policyLevel + "Couldn't found " + policyName, ExceptionCodes.POLICY_NOT_FOUND);
}
return policy;
}
use of org.wso2.carbon.apimgt.api.model.subscription.SubscriptionPolicy in project carbon-apimgt by wso2.
the class PolicyDAOImpl method updateSubscriptionPolicy.
/**
* updates an existing Subscription Policy
*
* @param subscriptionPolicy {@link Policy} instance
* @param connection DB Connection instance
* @throws APIMgtDAOException if an error occurs while updating the Subscription policy
*/
private void updateSubscriptionPolicy(Policy subscriptionPolicy, Connection connection) throws SQLException {
final String query = "UPDATE AM_SUBSCRIPTION_POLICY SET NAME = ?, DISPLAY_NAME = ?, DESCRIPTION = ?, QUOTA_TYPE = ?, " + "UNIT_TIME = ?, QUOTA = ?, QUOTA_UNIT = ?, TIME_UNIT = ?, RATE_LIMIT_COUNT = ?, " + "RATE_LIMIT_TIME_UNIT = ?, CUSTOM_ATTRIBUTES = ?, STOP_ON_QUOTA_REACH = ?, " + "BILLING_PLAN = ?, IS_DEPLOYED = ?, LAST_UPDATED_TIME = ? WHERE UUID = ?";
Limit limit = subscriptionPolicy.getDefaultQuotaPolicy().getLimit();
try (PreparedStatement statement = connection.prepareStatement(query)) {
statement.setString(1, subscriptionPolicy.getPolicyName());
statement.setString(2, subscriptionPolicy.getDisplayName());
statement.setString(3, subscriptionPolicy.getDescription());
statement.setString(4, subscriptionPolicy.getDefaultQuotaPolicy().getType());
statement.setInt(5, subscriptionPolicy.getDefaultQuotaPolicy().getLimit().getUnitTime());
setDefaultThrottlePolicyDetailsPreparedStmt(limit, statement);
statement.setString(8, subscriptionPolicy.getDefaultQuotaPolicy().getLimit().getTimeUnit());
subscriptionPolicy.populateDataInPreparedStatement(statement);
statement.setBoolean(14, subscriptionPolicy.isDeployed());
statement.setTimestamp(15, Timestamp.valueOf(LocalDateTime.now()));
statement.setString(16, subscriptionPolicy.getUuid());
statement.execute();
}
}
use of org.wso2.carbon.apimgt.api.model.subscription.SubscriptionPolicy in project carbon-apimgt by wso2.
the class PolicyDAOImpl method createSubscriptionPolicyFromResultSet.
/**
* Creates a Subscription Policy from the results set
*
* @param identifier policy id
* @param rs {@link ResultSet} instance
* @return {@link SubscriptionPolicy} instance
* @throws SQLException if any error occurs while creating the Subscription Policy from the result set
* @throws APIMgtDAOException if any error occurs while retrieving custom attributes for this Subscription Policy
*/
private SubscriptionPolicy createSubscriptionPolicyFromResultSet(String identifier, ResultSet rs) throws SQLException, APIMgtDAOException {
SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy(rs.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_NAME));
setCommonPolicyDetails(subscriptionPolicy, rs);
setSubscriptionPolicyDetals(subscriptionPolicy, rs);
InputStream binary = rs.getBinaryStream(APIMgtConstants.ThrottlePolicyConstants.COLUMN_CUSTOM_ATTRIB);
if (binary != null) {
byte[] customAttrib;
try {
customAttrib = IOUtils.toByteArray(binary);
if (customAttrib.length > 0) {
subscriptionPolicy.setCustomAttributes(customAttrib);
}
} catch (IOException e) {
String errorMsg = "An Error occurred while retrieving custom attributes for subscription policy with " + "identifier: " + identifier;
log.error(errorMsg, e);
throw new APIMgtDAOException(errorMsg, e);
}
}
return subscriptionPolicy;
}
use of org.wso2.carbon.apimgt.api.model.subscription.SubscriptionPolicy in project carbon-apimgt by wso2.
the class PolicyDAOImpl method addSubscriptionPolicy.
@Override
public void addSubscriptionPolicy(SubscriptionPolicy policy) throws APIMgtDAOException {
try (Connection connection = DAOUtil.getConnection()) {
try {
connection.setAutoCommit(false);
addSubscriptionPolicy(policy, connection);
connection.commit();
} catch (SQLException e) {
connection.rollback();
String errorMessage = "Error in adding Subscription policy, policy name: " + policy.getPolicyName();
log.error(errorMessage, e);
throw new APIMgtDAOException(errorMessage, e);
} finally {
connection.setAutoCommit(DAOUtil.isAutoCommit());
}
} catch (SQLException e) {
String errorMsg = "Error in obtaining DB connection";
log.error(errorMsg, e);
throw new APIMgtDAOException(errorMsg, e);
}
}
Aggregations