use of org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy 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.policy.ApplicationPolicy in project carbon-apimgt by wso2.
the class PolicyDAOImpl method updateApplicationPolicy.
/**
* updates an existing Application Policy
*
* @param applicationPolicy {@link Policy} instance
* @param connection DB Connection instance
* @throws SQLException if an error occurs while updating the API policy
* @throws APIMgtDAOException if the uuid of the policy is not available
*/
private void updateApplicationPolicy(Policy applicationPolicy, Connection connection) throws SQLException {
final String query = "UPDATE AM_APPLICATION_POLICY SET NAME = ?, DISPLAY_NAME = ?, DESCRIPTION = ?, " + "QUOTA_TYPE = ?, UNIT_TIME = ?, QUOTA = ?, QUOTA_UNIT = ?, TIME_UNIT = ?, LAST_UPDATED_TIME = ? " + "WHERE UUID = ?";
Limit limit = applicationPolicy.getDefaultQuotaPolicy().getLimit();
try (PreparedStatement statement = connection.prepareStatement(query)) {
statement.setString(1, applicationPolicy.getPolicyName());
statement.setString(2, applicationPolicy.getDisplayName());
statement.setString(3, applicationPolicy.getDescription());
statement.setString(4, applicationPolicy.getDefaultQuotaPolicy().getType());
statement.setInt(5, applicationPolicy.getDefaultQuotaPolicy().getLimit().getUnitTime());
setDefaultThrottlePolicyDetailsPreparedStmt(limit, statement);
statement.setString(8, applicationPolicy.getDefaultQuotaPolicy().getLimit().getTimeUnit());
statement.setTimestamp(9, Timestamp.valueOf(LocalDateTime.now()));
statement.setString(10, applicationPolicy.getUuid());
statement.execute();
}
}
use of org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy in project carbon-apimgt by wso2.
the class PolicyDAOImpl method updateApplicationPolicy.
@Override
public void updateApplicationPolicy(ApplicationPolicy policy) throws APIMgtDAOException {
try (Connection connection = DAOUtil.getConnection()) {
try {
connection.setAutoCommit(false);
updateApplicationPolicy(policy, connection);
connection.commit();
} catch (SQLException e) {
connection.rollback();
String errorMessage = "Error in updating Application policy with 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);
}
}
use of org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy 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.core.models.policy.ApplicationPolicy in project carbon-apimgt by wso2.
the class PolicyDAOImpl method getApplicationPolicies.
@Override
public List<ApplicationPolicy> getApplicationPolicies() throws APIMgtDAOException {
try {
List<ApplicationPolicy> policyList = new ArrayList<>();
String sqlQuery = "SELECT UUID, NAME, QUOTA_TYPE, TIME_UNIT, UNIT_TIME, QUOTA, QUOTA_UNIT, DESCRIPTION, " + "DISPLAY_NAME, IS_DEPLOYED from AM_APPLICATION_POLICY";
try (Connection connection = DAOUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
ApplicationPolicy applicationPolicy = new ApplicationPolicy(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_NAME));
setCommonPolicyDetails(applicationPolicy, resultSet);
policyList.add(applicationPolicy);
}
}
}
return policyList;
} catch (SQLException e) {
String errorMsg = "Error in retrieving Application policies";
log.error(errorMsg, e);
throw new APIMgtDAOException(errorMsg, e);
}
}
Aggregations