Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

INTGW-797 | Code improvement. #198

Open
wants to merge 3 commits into
base: igw-dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@
******************************************************************************/
package com.wso2telco.core.dbutils;

import java.sql.*;
import java.util.*;
import java.util.Date;

import javax.persistence.PersistenceException;

import com.wso2telco.core.dbutils.dao.SpendLimitDAO;
import com.wso2telco.core.dbutils.model.FederatedIdpMappingDTO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.persistence.PersistenceException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
Comment on lines +23 to +28
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these related to change for - INTGW-797 | Sonar issues fixing. Added try-with-resources

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes


// TODO: Auto-generated Javadoc

/**
Expand Down Expand Up @@ -379,37 +379,24 @@ public boolean UpdateApplicationOp(int appID, int operatorid, boolean opstat) th
*/
public Integer applicationEntry(int appID, Integer[] operators) throws Exception {

Connection con = null;
PreparedStatement statement = null;
Integer newid = 0;

try {
con = DbUtils.getDBConnection();
if (con == null) {
throw new Exception("Connection not found");
}
StringBuilder queryString = new StringBuilder("INSERT INTO ");
queryString.append("operatorapps ");
queryString.append("(applicationid,operatorid) ");
queryString.append("VALUES (?, ? ) ");

try (Connection con = DbUtils.getDBConnection();
PreparedStatement statement = con.prepareStatement(queryString.toString())) {
for (Integer d : operators) {
StringBuilder queryString = new StringBuilder("INSERT INTO ");
queryString.append("operatorapps ");
queryString.append("(applicationid,operatorid) ");
queryString.append("VALUES (?, ? ) ");

statement = con.prepareStatement(queryString.toString());

statement.setInt(1, appID);
statement.setInt(2, d);

statement.executeUpdate();

}

} catch (PersistenceException e) {
log.error("database operation error in inserting operatorapps", e);
} catch (Exception e) {
log.error("database operation error in inserting operatorapps", e);
} finally {
DbUtils.closeAllConnections(statement, con, null);
}

return newid;
Expand Down Expand Up @@ -707,39 +694,31 @@ public boolean insertValidatorForSubscription(int appID, int apiID, int validato
public boolean removeMerchantProvision(Integer appID, String subscriber, String operator, String[] merchants)
throws Exception {

Connection con = null;
PreparedStatement selectStatement = null;
PreparedStatement deleteStatement = null;
ResultSet rs = null;

try {
con = DbUtils.getDBConnection();
if (con == null) {
throw new Exception("Connection not found.");
}
int operatorid = 0;

StringBuilder selectQueryString = new StringBuilder("SELECT ");
selectQueryString.append("id ");
selectQueryString.append("FROM ");
selectQueryString.append("operators ");
selectQueryString.append("WHERE ");
selectQueryString.append("operatorname = ? ");
StringBuilder selectQueryString = new StringBuilder("SELECT ");
selectQueryString.append("id ");
selectQueryString.append("FROM ");
selectQueryString.append("operators ");
selectQueryString.append("WHERE ");
selectQueryString.append("operatorname = ? ");

selectStatement = con.prepareStatement(selectQueryString.toString());
try (final Connection con = DbUtils.getDBConnection();
final PreparedStatement selectStatement = con.prepareStatement(selectQueryString.toString())) {

selectStatement.setString(1, operator);
rs = selectStatement.executeQuery();

int operatorid = 0;
if (rs.next()) {
operatorid = rs.getInt("id");
} else {
throw new Exception("Operator Not Found");
try (ResultSet rs = selectStatement.executeQuery()) {
if (rs.next()) {
operatorid = rs.getInt("id");
} else {
throw new Exception("Operator Not Found");
}
}

for (int i = 0; i < merchants.length; i++) {

if (appID == null) {

StringBuilder deleteQueryString = new StringBuilder("DELETE ");
deleteQueryString.append("FROM ");
deleteQueryString.append("merchantopco_blacklist ");
Expand All @@ -752,12 +731,12 @@ public boolean removeMerchantProvision(Integer appID, String subscriber, String
deleteQueryString.append("AND ");
deleteQueryString.append("merchant = ? ");

deleteStatement = con.prepareStatement(deleteQueryString.toString());

deleteStatement.setInt(1, operatorid);
deleteStatement.setString(2, subscriber);
deleteStatement.setString(3, merchants[i]);
deleteStatement.executeUpdate();
try (final PreparedStatement deleteStatement = con.prepareStatement(deleteQueryString.toString())) {
deleteStatement.setInt(1, operatorid);
deleteStatement.setString(2, subscriber);
deleteStatement.setString(3, merchants[i]);
deleteStatement.executeUpdate();
}

} else {

Expand All @@ -773,24 +752,19 @@ public boolean removeMerchantProvision(Integer appID, String subscriber, String
queryString.append("AND ");
queryString.append("merchant = ? ");

deleteStatement = con.prepareStatement(queryString.toString());

deleteStatement.setInt(1, appID);
deleteStatement.setInt(2, operatorid);
deleteStatement.setString(3, subscriber);
deleteStatement.setString(4, merchants[i]);
deleteStatement.executeUpdate();

try (final PreparedStatement deleteStatement = con.prepareStatement(queryString.toString())) {
deleteStatement.setInt(1, appID);
deleteStatement.setInt(2, operatorid);
deleteStatement.setString(3, subscriber);
deleteStatement.setString(4, merchants[i]);
deleteStatement.executeUpdate();
}
}
}

} catch (PersistenceException e) {
log.error("database operation error while Provisioning Merchant", e);
} catch (Exception e) {
log.error("database operation error while Provisioning Merchant", e);
} finally {
DbUtils.closeAllConnections(selectStatement, con, rs);
DbUtils.closeAllConnections(deleteStatement, null, null);
}
return true;
}
Expand Down Expand Up @@ -1042,60 +1016,54 @@ public SpendLimitDAO getGroupTotalDayAmount(String groupName, String operator, S
public SpendLimitDAO getGroupTotalMonthAmount(String groupName, String operator, String msisdn)
throws DBUtilException {

Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
SpendLimitDAO spendLimitDAO = null;

try {
con = DbUtils.getDBConnection();

Calendar calendarFrom = GregorianCalendar.getInstance();
calendarFrom.set(Calendar.DAY_OF_MONTH, calendarFrom.getActualMinimum(Calendar.DAY_OF_MONTH));

calendarFrom.set(Calendar.HOUR_OF_DAY, 0);
calendarFrom.set(Calendar.MINUTE, 0);
calendarFrom.set(Calendar.SECOND, 0);
calendarFrom.set(Calendar.MILLISECOND, 0);

Calendar calendarTo = GregorianCalendar.getInstance();
calendarTo.setTime(new Date());
calendarTo.set(Calendar.DAY_OF_MONTH,
calendarTo.getActualMaximum(Calendar.DAY_OF_MONTH));
calendarTo.set(Calendar.HOUR_OF_DAY, 23);
calendarTo.set(Calendar.MINUTE, 59);
calendarTo.set(Calendar.SECOND, 59);
calendarTo.set(Calendar.MILLISECOND, 999);
Calendar calendarFrom = GregorianCalendar.getInstance();
calendarFrom.set(Calendar.DAY_OF_MONTH, calendarFrom.getActualMinimum(Calendar.DAY_OF_MONTH));

calendarFrom.set(Calendar.HOUR_OF_DAY, 0);
calendarFrom.set(Calendar.MINUTE, 0);
calendarFrom.set(Calendar.SECOND, 0);
calendarFrom.set(Calendar.MILLISECOND, 0);

Calendar calendarTo = GregorianCalendar.getInstance();
calendarTo.setTime(new Date());
calendarTo.set(Calendar.DAY_OF_MONTH,
calendarTo.getActualMaximum(Calendar.DAY_OF_MONTH));
calendarTo.set(Calendar.HOUR_OF_DAY, 23);
calendarTo.set(Calendar.MINUTE, 59);
calendarTo.set(Calendar.SECOND, 59);
calendarTo.set(Calendar.MILLISECOND, 999);

String sql = "SELECT SUM(amount) AS amount "
+ "FROM spendlimitdata "
+ "where effectiveTime between ? and ? "
+ "and groupName=? "
+ "and operatorId=? "
+ "and msisdn=? "
+ "group by groupName, operatorId, msisdn";

try (final Connection con = DbUtils.getDBConnection();
final PreparedStatement ps = con.prepareStatement(sql)) {

String sql = "SELECT SUM(amount) AS amount "
+ "FROM spendlimitdata "
+ "where effectiveTime between ? and ? "
+ "and groupName=? "
+ "and operatorId=? "
+ "and msisdn=? "
+ "group by groupName, operatorId, msisdn";

ps = con.prepareStatement(sql);
ps.setLong(1, calendarFrom.getTimeInMillis());
ps.setLong(2, calendarTo.getTimeInMillis());
ps.setString(3, groupName);
ps.setString(4, operator);
ps.setString(5, msisdn);
rs = ps.executeQuery();

rs = ps.executeQuery();
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
spendLimitDAO = new SpendLimitDAO();
spendLimitDAO.setAmount(rs.getDouble("amount"));
}
}

if (rs.next()) {
spendLimitDAO = new SpendLimitDAO();
spendLimitDAO.setAmount(rs.getDouble("amount"));
} catch (Exception e) {
DbUtils.handleException("Error while checking Operator Spend Limit. ", e);
}
} catch (Exception e) {
DbUtils.handleException("Error while checking Operator Spend Limit. ", e);
} finally {
DbUtils.closeAllConnections(ps, con, rs);
return spendLimitDAO;
}
return spendLimitDAO;
}

/**
* Get payment time
Expand Down