use of org.wso2.carbon.apimgt.core.models.Subscription in project carbon-apimgt by wso2.
the class PolicyDAOImpl method getSubscriptionPolicies.
@Override
public List<SubscriptionPolicy> getSubscriptionPolicies() throws APIMgtDAOException {
try {
List<SubscriptionPolicy> policyList = new ArrayList<>();
String sqlQuery = "SELECT UUID, NAME, QUOTA_TYPE, TIME_UNIT, UNIT_TIME, QUOTA, QUOTA_UNIT, DESCRIPTION, " + "DISPLAY_NAME, CUSTOM_ATTRIBUTES, IS_DEPLOYED,RATE_LIMIT_COUNT, RATE_LIMIT_TIME_UNIT, " + "STOP_ON_QUOTA_REACH, BILLING_PLAN FROM AM_SUBSCRIPTION_POLICY";
try (Connection connection = DAOUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
policyList.add(createSubscriptionPolicyFromResultSet(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_NAME), resultSet));
}
}
}
return policyList;
} catch (SQLException e) {
String errorMsg = "Error in retrieving Subscription policies";
log.error(errorMsg, e);
throw new APIMgtDAOException(errorMsg, e);
}
}
use of org.wso2.carbon.apimgt.core.models.Subscription in project carbon-apimgt by wso2.
the class PolicyDAOImpl method getSubscriptionPolicyById.
/**
* Retrieves {@link SubscriptionPolicy} with policy uuid <code>uuid</code>
* <p>This will retrieve complete details about the ApplicationPolicy with all pipelins and conditions.</p>
*
* @param uuid uuid of the policy to retrieve from the database
* @return {@link SubscriptionPolicy}
*/
private SubscriptionPolicy getSubscriptionPolicyById(String uuid) throws SQLException, APIMgtDAOException {
final String query = "SELECT NAME, UUID, QUOTA_TYPE, TIME_UNIT, UNIT_TIME, QUOTA, QUOTA_UNIT, DESCRIPTION, " + "DISPLAY_NAME, CUSTOM_ATTRIBUTES, IS_DEPLOYED, RATE_LIMIT_COUNT, RATE_LIMIT_TIME_UNIT, " + "STOP_ON_QUOTA_REACH, BILLING_PLAN FROM AM_SUBSCRIPTION_POLICY WHERE UUID = ?";
try (Connection conn = DAOUtil.getConnection();
PreparedStatement statement = conn.prepareStatement(query)) {
statement.setString(1, uuid);
statement.execute();
try (ResultSet rs = statement.getResultSet()) {
if (rs.next()) {
return createSubscriptionPolicyFromResultSet(uuid, rs);
} else {
// not found
String msg = "Subscription Policy not found for id: " + uuid;
log.warn(msg);
throw new APIMgtDAOException(msg, ExceptionCodes.POLICY_NOT_FOUND);
}
}
}
}
use of org.wso2.carbon.apimgt.core.models.Subscription 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.core.models.Subscription 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.core.models.Subscription 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;
}
Aggregations