Skip to content

Commit

Permalink
Update dao
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaminduR committed Sep 4, 2023
1 parent 4ac82af commit a563ef1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,9 @@ public Integer getAPIResourcesCount(Integer tenantId, List<ExpressionNode> expre
appendFilterQuery(expressionNodesCopy, filterQueryBuilder, false);

Map<Integer, String> filterAttributeValue = filterQueryBuilder.getFilterAttributeValue();
String sqlStmt;
String getAPIResourcesCountSqlStmtTail = SQLConstants.GET_API_RESOURCES_COUNT_TAIL;

sqlStmt = SQLConstants.GET_API_RESOURCES_COUNT + filterQueryBuilder.getFilterQuery() +
String sqlStmt = SQLConstants.GET_API_RESOURCES_COUNT + filterQueryBuilder.getFilterQuery() +
getAPIResourcesCountSqlStmtTail;

try (Connection dbConnection = IdentityDatabaseUtil.getDBConnection(false)) {
Expand All @@ -95,8 +94,7 @@ public Integer getAPIResourcesCount(Integer tenantId, List<ExpressionNode> expre
if (rs.next()) {
return rs.getInt(1);
}
} catch (
SQLException e) {
} catch (SQLException e) {
throw new APIResourceMgtServerException(
APIResourceManagementConstants.ErrorMessages.ERROR_CODE_ERROR_WHILE_RETRIEVING_API_RESOURCES
.getCode(),
Expand All @@ -110,11 +108,9 @@ public Integer getAPIResourcesCount(Integer tenantId, List<ExpressionNode> expre
public APIResource addAPIResource(APIResource apiResource, Integer tenantId) throws APIResourceMgtException {

String generatedAPIId = UUID.randomUUID().toString();
PreparedStatement prepStmt = null;
try (Connection dbConnection = IdentityDatabaseUtil.getDBConnection(true)) {
try {
dbConnection.setAutoCommit(false);
prepStmt = dbConnection.prepareStatement(SQLConstants.ADD_API_RESOURCE);
PreparedStatement prepStmt = dbConnection.prepareStatement(SQLConstants.ADD_API_RESOURCE);
prepStmt.setString(1, generatedAPIId);
prepStmt.setString(2, apiResource.getType());
prepStmt.setString(3, apiResource.getName());
Expand Down Expand Up @@ -310,11 +306,9 @@ public APIResource getAPIResourceByIdentifier(String identifier, Integer tenantI
public void updateAPIResource(APIResource apiResource, List<Scope> addedScopes, List<String> removedScopes,
Integer tenantId) throws APIResourceMgtException {


try (Connection dbConnection = IdentityDatabaseUtil.getDBConnection(true);
PreparedStatement preparedStatement = dbConnection.prepareStatement(SQLConstants.UPDATE_API_RESOURCE)) {
try {
dbConnection.setAutoCommit(false);
preparedStatement.setString(1, apiResource.getName());
preparedStatement.setString(2, apiResource.getDescription());
preparedStatement.setString(3, apiResource.getId());
Expand All @@ -341,7 +335,6 @@ public void deleteAPIResourceById(String apiId, Integer tenantId) throws APIReso

try (Connection dbConnection = IdentityDatabaseUtil.getDBConnection(true)) {
try {
dbConnection.setAutoCommit(false);
PreparedStatement prepStmt = dbConnection.prepareStatement(SQLConstants.DELETE_SCOPES_BY_API);
prepStmt.setString(1, apiId);
prepStmt.setInt(2, tenantId);
Expand Down Expand Up @@ -371,14 +364,11 @@ public boolean isScopeExistByName(String name, Integer tenantId) throws APIResou
preparedStatement.setString(1, name);
preparedStatement.setInt(2, tenantId);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
return true;
}
return resultSet.next();
} catch (SQLException e) {
throw APIResourceManagementUtil.handleServerException(
APIResourceManagementConstants.ErrorMessages.ERROR_CODE_ERROR_WHILE_CHECKING_EXISTENCE_OF_SCOPE, e);
}
return false;
}

@Override
Expand Down Expand Up @@ -408,6 +398,7 @@ public Scope getScopeByNameAndTenantId(String name, Integer tenantId) throws API
preparedStatement.setInt(2, tenantId);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
// TODO: refactor to private method.
return new Scope(
resultSet.getString(SQLConstants.ID_COLUMN_NAME),
resultSet.getString(SQLConstants.NAME_COLUMN_NAME),
Expand Down Expand Up @@ -570,14 +561,13 @@ public void putScopes(String apiId, List<Scope> currentScopes, List<Scope> scope

try (Connection dbConnection = IdentityDatabaseUtil.getDBConnection(true)) {
try {
dbConnection.setAutoCommit(false);
// Delete the existing scopes and commit.
deleteScopeByAPIId(dbConnection, apiId, tenantId);
IdentityDatabaseUtil.commitTransaction(dbConnection);
// Add the new scopes and commit.
addScopes(dbConnection, apiId, scopes, tenantId);
IdentityDatabaseUtil.commitTransaction(dbConnection);
} catch (SQLException | APIResourceMgtException e) {
} catch (APIResourceMgtException e) {

// Rollback the transaction if any error occurred and add back the previous scopes.
IdentityDatabaseUtil.rollbackTransaction(dbConnection);
Expand Down Expand Up @@ -611,9 +601,9 @@ private String buildQuery(List<String> ids) {
/**
* Get API resources list.
*
* @param limit Limit.
* @param limit API resources limit.
* @param tenantId Tenant ID.
* @param sortOrder Sort order.
* @param sortOrder Order to sort the results.
* @param expressionNodes Expression nodes.
* @return API resources list.
* @throws APIResourceMgtException If an error occurs while retrieving API resources.
Expand Down Expand Up @@ -673,19 +663,8 @@ private List<APIResource> getAPIResourcesList(Integer limit, Integer tenantId, S
private String buildAPIResourcesSqlStatement(String databaseName, Integer tenantId, String filterQuery,
String sortOrder, Integer limit) {

String sqlStmtTail;
String sqlStmtHead;

sqlStmtHead = SQLConstants.GET_API_RESOURCES;
sqlStmtTail = SQLConstants.GET_API_RESOURCES_TAIL;

// if (databaseName.contains(SQLConstants.DB2)) {
// sqlStmtHead = SQLConstants.GET_API_RESOURCES_DB2;
// sqlStmtTail = SQLConstants.GET_API_RESOURCES_DB2_TAIL;
// } else if (databaseName.contains(SQLConstants.ORACLE)) {
// sqlStmtHead = SQLConstants.GET_API_RESOURCES_ORACLE;
// sqlStmtTail = SQLConstants.GET_API_RESOURCES_ORACLE_TAIL;
// } else
String sqlStmtHead = SQLConstants.GET_API_RESOURCES;
String sqlStmtTail = SQLConstants.GET_API_RESOURCES_TAIL;

if (databaseName.contains(SQLConstants.MICROSOFT)) {
sqlStmtHead = SQLConstants.GET_API_RESOURCES_MSSQL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
@WithCarbonHome
@WithRegistry
@WithRealmService
@WithH2Database(jndiName = "jdbc/WSO2IdentityDB", files = {"dbScripts/h2.sql"})
@WithH2Database(jndiName = "jdbc/WSO2IdentityDB", files = {"dbscripts/h2.sql"})
public class APIResourceManagerTest extends PowerMockTestCase {

private String tenantDomain;
Expand Down

0 comments on commit a563ef1

Please sign in to comment.