Skip to content

Commit

Permalink
Refactor subcription visibility db
Browse files Browse the repository at this point in the history
  • Loading branch information
chamilaadhi committed Oct 4, 2024
1 parent 8a875bc commit 9477c98
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11542,25 +11542,9 @@ public void addSubscriptionPolicy(SubscriptionPolicy policy) throws APIManagemen
policyStatement.setInt(15, policy.getGraphQLMaxDepth());
policyStatement.setInt(16, policy.getGraphQLMaxComplexity());
policyStatement.setString(17, policy.getBillingPlan());
List<String> list = policy.getAllowedOrganizations();
String allowedOrganizations = APIConstants.ALLOWED_ORGANIZATIONS_DEFAULT; //Default
if (list != null && !list.isEmpty()) {
allowedOrganizations = String.join(",", list);
}
policyStatement.setString(18, allowedOrganizations);

if (hasCustomAttrib) {
policyStatement.setBytes(19, policy.getCustomAttributes());
policyStatement.setString(20, policy.getMonetizationPlan());
policyStatement.setString(21,
policy.getMonetizationPlanProperties().get(APIConstants.Monetization.FIXED_PRICE));
policyStatement.setString(22,
policy.getMonetizationPlanProperties().get(APIConstants.Monetization.BILLING_CYCLE));
policyStatement.setString(23,
policy.getMonetizationPlanProperties().get(APIConstants.Monetization.PRICE_PER_REQUEST));
policyStatement.setString(24,
policy.getMonetizationPlanProperties().get(APIConstants.Monetization.CURRENCY));
policyStatement.setInt(25, policy.getSubscriberCount());
} else {
policyStatement.setBytes(18, policy.getCustomAttributes());
policyStatement.setString(19, policy.getMonetizationPlan());
policyStatement.setString(20,
policy.getMonetizationPlanProperties().get(APIConstants.Monetization.FIXED_PRICE));
Expand All @@ -11571,6 +11555,30 @@ public void addSubscriptionPolicy(SubscriptionPolicy policy) throws APIManagemen
policyStatement.setString(23,
policy.getMonetizationPlanProperties().get(APIConstants.Monetization.CURRENCY));
policyStatement.setInt(24, policy.getSubscriberCount());
} else {
policyStatement.setString(18, policy.getMonetizationPlan());
policyStatement.setString(19,
policy.getMonetizationPlanProperties().get(APIConstants.Monetization.FIXED_PRICE));
policyStatement.setString(20,
policy.getMonetizationPlanProperties().get(APIConstants.Monetization.BILLING_CYCLE));
policyStatement.setString(21,
policy.getMonetizationPlanProperties().get(APIConstants.Monetization.PRICE_PER_REQUEST));
policyStatement.setString(22,
policy.getMonetizationPlanProperties().get(APIConstants.Monetization.CURRENCY));
policyStatement.setInt(23, policy.getSubscriberCount());
}

List<String> allowedOrgs = policy.getAllowedOrganizations();
if (allowedOrgs != null && !allowedOrgs.isEmpty()) {
try (PreparedStatement addVisibleOrgsStatement = conn
.prepareStatement(SQLConstants.PolicyOrgVisibilitySqlConstants.ADD_POLICY_ORG_VISIBILITY_SQL)) {
for (String org : allowedOrgs) {
addVisibleOrgsStatement.setString(1, policy.getUUID());
addVisibleOrgsStatement.setString(2, org);
addVisibleOrgsStatement.addBatch();
}
addVisibleOrgsStatement.executeBatch();
}
}
policyStatement.executeUpdate();
conn.commit();
Expand Down Expand Up @@ -12285,13 +12293,7 @@ public SubscriptionPolicy[] getSubscriptionPolicies(int tenantID) throws APIMana
monetizationPlanProperties.put(APIConstants.Monetization.CURRENCY,
rs.getString(ThrottlePolicyConstants.COLUMN_CURRENCY));
subPolicy.setMonetizationPlanProperties(monetizationPlanProperties);
String allowedOrgs = rs.getString(ThrottlePolicyConstants.COLUMN_ALLOWED_ORGANIZATIONS);
if(allowedOrgs == null) {
allowedOrgs = APIConstants.ALLOWED_ORGANIZATIONS_DEFAULT; // set default if null
}
String[] items = allowedOrgs.split(",");
ArrayList<String> allowedOrglist = new ArrayList<>(Arrays.asList(items));
subPolicy.setAllowedOrganizations(allowedOrglist);
subPolicy.setAllowedOrganizations(getPolicyVisibleOrgs(subPolicy.getUUID()));
InputStream binary = rs.getBinaryStream(ThrottlePolicyConstants.COLUMN_CUSTOM_ATTRIB);
if (binary != null) {
byte[] customAttrib = APIUtil.toByteArray(binary);
Expand Down Expand Up @@ -12719,13 +12721,7 @@ public SubscriptionPolicy getSubscriptionPolicy(String policyName, int tenantId)
policy.setGraphQLMaxDepth(resultSet.getInt(ThrottlePolicyConstants.COLUMN_MAX_DEPTH));
policy.setGraphQLMaxComplexity(resultSet.getInt(ThrottlePolicyConstants.COLUMN_MAX_COMPLEXITY));
policy.setSubscriberCount(resultSet.getInt(ThrottlePolicyConstants.COLUMN_CONNECTION_COUNT));
String allowedOrgs = resultSet.getString(ThrottlePolicyConstants.COLUMN_ALLOWED_ORGANIZATIONS);
if(allowedOrgs == null) {
allowedOrgs = APIConstants.ALLOWED_ORGANIZATIONS_DEFAULT; // set default if null
}
String[] items = allowedOrgs.split(",");
ArrayList<String> allowedOrglist = new ArrayList<>(Arrays.asList(items));
policy.setAllowedOrganizations(allowedOrglist);
policy.setAllowedOrganizations(getPolicyVisibleOrgs(policy.getUUID()));
InputStream binary = resultSet.getBinaryStream(ThrottlePolicyConstants.COLUMN_CUSTOM_ATTRIB);
if (binary != null) {
byte[] customAttrib = APIUtil.toByteArray(binary);
Expand Down Expand Up @@ -12778,14 +12774,7 @@ public SubscriptionPolicy getSubscriptionPolicyByUUID(String uuid) throws APIMan
policy.setGraphQLMaxDepth(resultSet.getInt(ThrottlePolicyConstants.COLUMN_MAX_DEPTH));
policy.setGraphQLMaxComplexity(resultSet.getInt(ThrottlePolicyConstants.COLUMN_MAX_COMPLEXITY));
policy.setSubscriberCount(resultSet.getInt(ThrottlePolicyConstants.COLUMN_CONNECTION_COUNT));

String allowedOrgs = resultSet.getString(ThrottlePolicyConstants.COLUMN_ALLOWED_ORGANIZATIONS);
if(allowedOrgs == null) {
allowedOrgs = APIConstants.ALLOWED_ORGANIZATIONS_DEFAULT; // set default if null
}
String[] items = allowedOrgs.split(",");
ArrayList<String> allowedOrglist = new ArrayList<>(Arrays.asList(items));
policy.setAllowedOrganizations(allowedOrglist);
policy.setAllowedOrganizations(getPolicyVisibleOrgs(policy.getUUID()));

InputStream binary = resultSet.getBinaryStream(ThrottlePolicyConstants.COLUMN_CUSTOM_ATTRIB);
if (binary != null) {
Expand Down Expand Up @@ -12816,6 +12805,29 @@ public SubscriptionPolicy getSubscriptionPolicyByUUID(String uuid) throws APIMan
}
return policy;
}

public List<String> getPolicyVisibleOrgs(String policyUUID) throws APIManagementException {
List<String> orgList = new ArrayList<String>();
try (Connection conn = APIMgtDBUtil.getConnection()) {
try {
String getPolicyPermissionQuery = SQLConstants.PolicyOrgVisibilitySqlConstants.GET_POLICY_ORG_VISIBILITY_SQL;
PreparedStatement ps = conn.prepareStatement(getPolicyPermissionQuery);
ps.setString(1, policyUUID);
ResultSet resultSet = ps.executeQuery();
while (resultSet.next()) {
orgList.add(resultSet.getString(ThrottlePolicyConstants.COLUMN_ALLOWED_ORGANIZATIONS));
}
} catch (SQLException e) {
conn.rollback();
handleException("Failed to get policy allowed organizations " + policyUUID,
e);
}
} catch (SQLException e) {
throw new APIManagementException(
"Error while retrieving policy organizations with id " + policyUUID, e);
}
return orgList;
}

/**
* Retrieves list of pipelines for the policy with policy Id: <code>policyId</code>
Expand Down Expand Up @@ -13225,44 +13237,10 @@ public void updateSubscriptionPolicy(SubscriptionPolicy policy) throws APIManage
updateStatement.setInt(12, policy.getGraphQLMaxComplexity());
updateStatement.setString(13, policy.getBillingPlan());

List<String> list = policy.getAllowedOrganizations();
String allowedOrganizations = APIConstants.ALLOWED_ORGANIZATIONS_DEFAULT; //Default
if (list != null && !list.isEmpty()) {
allowedOrganizations = String.join(",", list);
}
updateStatement.setString(14, allowedOrganizations);

if (hasCustomAttrib) {
long lengthOfStream = policy.getCustomAttributes().length;
updateStatement.setBinaryStream(15, new ByteArrayInputStream(policy.getCustomAttributes()),
updateStatement.setBinaryStream(14, new ByteArrayInputStream(policy.getCustomAttributes()),
lengthOfStream);
if (!StringUtils.isBlank(policy.getPolicyName()) && policy.getTenantId() != -1) {
updateStatement.setString(16, policy.getMonetizationPlan());
updateStatement.setString(17,
policy.getMonetizationPlanProperties().get(APIConstants.Monetization.FIXED_PRICE));
updateStatement.setString(18,
policy.getMonetizationPlanProperties().get(APIConstants.Monetization.BILLING_CYCLE));
updateStatement.setString(19,
policy.getMonetizationPlanProperties().get(APIConstants.Monetization.PRICE_PER_REQUEST));
updateStatement.setString(20,
policy.getMonetizationPlanProperties().get(APIConstants.Monetization.CURRENCY));
updateStatement.setInt(21, policy.getSubscriberCount());
updateStatement.setString(22, policy.getPolicyName());
updateStatement.setInt(23, policy.getTenantId());
} else if (!StringUtils.isBlank(policy.getUUID())) {
updateStatement.setString(16, policy.getMonetizationPlan());
updateStatement.setString(17,
policy.getMonetizationPlanProperties().get(APIConstants.Monetization.FIXED_PRICE));
updateStatement.setString(18,
policy.getMonetizationPlanProperties().get(APIConstants.Monetization.BILLING_CYCLE));
updateStatement.setString(19,
policy.getMonetizationPlanProperties().get(APIConstants.Monetization.PRICE_PER_REQUEST));
updateStatement.setString(20,
policy.getMonetizationPlanProperties().get(APIConstants.Monetization.CURRENCY));
updateStatement.setInt(21, policy.getSubscriberCount());
updateStatement.setString(22, policy.getUUID());
}
} else {
if (!StringUtils.isBlank(policy.getPolicyName()) && policy.getTenantId() != -1) {
updateStatement.setString(15, policy.getMonetizationPlan());
updateStatement.setString(16,
Expand All @@ -13276,7 +13254,6 @@ public void updateSubscriptionPolicy(SubscriptionPolicy policy) throws APIManage
updateStatement.setInt(20, policy.getSubscriberCount());
updateStatement.setString(21, policy.getPolicyName());
updateStatement.setInt(22, policy.getTenantId());

} else if (!StringUtils.isBlank(policy.getUUID())) {
updateStatement.setString(15, policy.getMonetizationPlan());
updateStatement.setString(16,
Expand All @@ -13290,8 +13267,54 @@ public void updateSubscriptionPolicy(SubscriptionPolicy policy) throws APIManage
updateStatement.setInt(20, policy.getSubscriberCount());
updateStatement.setString(21, policy.getUUID());
}
} else {
if (!StringUtils.isBlank(policy.getPolicyName()) && policy.getTenantId() != -1) {
updateStatement.setString(14, policy.getMonetizationPlan());
updateStatement.setString(15,
policy.getMonetizationPlanProperties().get(APIConstants.Monetization.FIXED_PRICE));
updateStatement.setString(16,
policy.getMonetizationPlanProperties().get(APIConstants.Monetization.BILLING_CYCLE));
updateStatement.setString(17,
policy.getMonetizationPlanProperties().get(APIConstants.Monetization.PRICE_PER_REQUEST));
updateStatement.setString(18,
policy.getMonetizationPlanProperties().get(APIConstants.Monetization.CURRENCY));
updateStatement.setInt(19, policy.getSubscriberCount());
updateStatement.setString(20, policy.getPolicyName());
updateStatement.setInt(21, policy.getTenantId());

} else if (!StringUtils.isBlank(policy.getUUID())) {
updateStatement.setString(14, policy.getMonetizationPlan());
updateStatement.setString(15,
policy.getMonetizationPlanProperties().get(APIConstants.Monetization.FIXED_PRICE));
updateStatement.setString(16,
policy.getMonetizationPlanProperties().get(APIConstants.Monetization.BILLING_CYCLE));
updateStatement.setString(17,
policy.getMonetizationPlanProperties().get(APIConstants.Monetization.PRICE_PER_REQUEST));
updateStatement.setString(18,
policy.getMonetizationPlanProperties().get(APIConstants.Monetization.CURRENCY));
updateStatement.setInt(19, policy.getSubscriberCount());
updateStatement.setString(20, policy.getUUID());
}
}
updateStatement.executeUpdate();

try (PreparedStatement deleteOrgsStatement = connection.prepareStatement(SQLConstants
.PolicyOrgVisibilitySqlConstants.DELETE_ALL_POLICY_ORG_VISIBILITY_SQL)) {
deleteOrgsStatement.setString(1, policy.getUUID());
deleteOrgsStatement.executeUpdate();
}
List<String> allowedOrgs = policy.getAllowedOrganizations();
if (allowedOrgs != null && !allowedOrgs.isEmpty()) {
try (PreparedStatement addOrgsStatement = connection.prepareStatement(SQLConstants
.PolicyOrgVisibilitySqlConstants.ADD_POLICY_ORG_VISIBILITY_SQL)) {
for (String org : allowedOrgs) {
addOrgsStatement.setString(1, policy.getUUID());
addOrgsStatement.setString(2, org);
addOrgsStatement.addBatch();
}
addOrgsStatement.executeBatch();
}
}
connection.commit();
} catch (SQLException e) {
if (connection != null) {
Expand Down Expand Up @@ -13507,7 +13530,9 @@ private void setCommonParametersForPolicy(PreparedStatement policyStatement, Pol
if (!StringUtils.isBlank(policy.getUUID())) {
policyStatement.setString(11, policy.getUUID());
} else {
policyStatement.setString(11, UUID.randomUUID().toString());
String uuid = UUID.randomUUID().toString();
policy.setUUID(uuid);
policyStatement.setString(11, uuid);
}

}
Expand Down
Loading

0 comments on commit 9477c98

Please sign in to comment.