Skip to main content

Updating Account Details Flow

In this document, we will explain the process of updating an account's details. The process involves validating the account details, retrieving the account from the database, updating the account details, and handling the database connection.

The flow starts with validating the account details provided by the user. If the details are valid, the system retrieves the account from the database. Once the account is found, the system updates the account details and saves them back to the database. Throughout this process, the system ensures that the database connection is properly managed.

Flow drill down


Validating Account Details

First, the function updateAccountInternal validates the account details provided in the AccountJSON object. It checks if the account type is valid, if the interest rate is within acceptable bounds, and if the sort code matches the bank's sort code. If any of these validations fail, an appropriate error response is returned.

		if (!(account.validateType(account.getAccountType().trim())))
// If account type invalid
{
JSONObject error = new JSONObject();
error.put(JSON_ERROR_MSG,
ACC_TYPE_STRING + account.getAccountType() + NOT_SUPPORTED);
logger.log(Level.WARNING, () -> (ACC_TYPE_STRING
+ account.getAccountType() + NOT_SUPPORTED));
myResponse = Response.status(400).entity(error.toString()).build();
logger.exiting(this.getClass().getName(), UPDATE_ACCOUNT_INTERNAL,
myResponse);
return myResponse;
}

if (account.getInterestRate().doubleValue() < 0.00)
{
// If interest rate < 0
JSONObject error = new JSONObject();
error.put(JSON_ERROR_MSG, INTEREST_RATE_LESS_THAN_ZERO);
logger.log(Level.WARNING, () -> (INTEREST_RATE_LESS_THAN_ZERO));
myResponse = Response.status(400).entity(error.toString()).build();


Retrieving and Updating the Account

Next, the function retrieves the account from the database using the getAccount method. If the account exists, it updates the account details using the updateAccount method. If the update is successful, a response with the updated account details is returned. If the account does not exist or the update fails, an appropriate error response is returned.

		com.ibm.cics.cip.bankliberty.web.db2.Account db2Account = new com.ibm.cics.cip.bankliberty.web.db2.Account();
account.setId(id.toString());
db2Account = db2Account.getAccount(Integer.parseInt(account.getId()),
this.getSortCode().intValue());

if (db2Account != null)
{
db2Account = db2Account.updateAccount(account);
if (db2Account != null)
{
response.put(JSON_SORT_CODE, db2Account.getSortcode().trim());
response.put("id", db2Account.getAccountNumber());
response.put(JSON_CUSTOMER_NUMBER,
db2Account.getCustomerNumber());
response.put(JSON_ACCOUNT_TYPE, db2Account.getType().trim());
response.put(JSON_AVAILABLE_BALANCE,
BigDecimal.valueOf(db2Account.getAvailableBalance()));
response.put(JSON_ACTUAL_BALANCE,
BigDecimal.valueOf(db2Account.getActualBalance()));
response.put(JSON_INTEREST_RATE,
BigDecimal.valueOf(db2Account.getInterestRate()));


Updating Account in Database

The updateAccount method in the Account class is responsible for updating the account details in the database. It first retrieves the account using the getAccount method. If the account is found, it prepares and executes an SQL update statement to update the account type, interest rate, and overdraft limit.

	public Account updateAccount(AccountJSON account)
{
logger.entering(this.getClass().getName(), UPDATE_ACCOUNT);
Account db2Account = this.getAccount(Integer.parseInt(account.getId()),
Integer.parseInt(sortcode));
if (db2Account == null)
{
logger.log(Level.WARNING,
() -> "Unable to access DB2 account " + account.getId());
logger.exiting(this.getClass().getName(), UPDATE_ACCOUNT, null);
return null;
}
Account temp = null;
openConnection();
String accountNumberString = padAccountNumber(
Integer.parseInt(db2Account.getAccountNumber()));

String sortCodeString = padSortCode(Integer.valueOf(sortcode));
String sql1 = SQL_SELECT;
logger.log(Level.FINE,
() -> "About to perform query SQL <" + sql1 + ">");


Retrieving Account from Database

The getAccount method retrieves the account details from the database based on the account number and sort code. It executes an SQL query to fetch the account details and returns an Account object if the account is found.

	public Account getAccount(int accountNumber, int sortCode)
{
logger.entering(this.getClass().getName(), GET_ACCOUNT + accountNumber);
openConnection();
Account temp = null;

String sortCodeString = padSortCode(sortCode);
String sql9999 = "SELECT * from ACCOUNT where ACCOUNT_EYECATCHER LIKE 'ACCT' AND ACCOUNT_SORTCODE like ? order by ACCOUNT_NUMBER DESC";
String sql = SQL_SELECT;
try (PreparedStatement stmt9999 = conn.prepareStatement(sql9999);
PreparedStatement stmt = conn.prepareStatement(sql);)
{
if (accountNumber == 99999999)
{

logger.log(Level.FINE, () -> PRE_SELECT_MSG + sql9999 + ">");

stmt9999.setString(1, sortCodeString);
ResultSet rs = stmt9999.executeQuery();
if (rs.next())
{


Opening Database Connection

The openConnection method opens a connection to the DB2 database. It checks if a connection already exists for the current task. If not, it calls the openConnectionInternal method to create a new connection.

	protected void openConnection()
{
// Open a connection to the DB2 database
logger.entering(this.getClass().getName(), "openConnection()");

Integer taskNumberInteger = Task.getTask().getTaskNumber();
String db2ConnString = DB2CONN.concat(taskNumberInteger.toString());
logger.log(Level.FINE,
() -> "Attempting to get DB2CONN for task number "
+ taskNumberInteger.toString());
this.conn = (Connection) cornedBeef.get(db2ConnString);
if (this.conn == null)
{
HBankDataAccess.incrementConnCount();
logger.log(Level.FINE,
() -> "Attempting to create DB2CONN for task number "
+ taskNumberInteger.toString());
// Attempt to open a connection
openConnectionInternal();
logger.log(Level.FINE,
() -> "Creation succcessful for DB2CONN for task number "


Creating Database Connection

The openConnectionInternal method creates a new connection to the DB2 database using a JNDI lookup. It sets the transaction isolation level and stores the connection in a map for reuse.

	@SuppressWarnings("unchecked")
void openConnectionInternal()
{
logger.entering(this.getClass().getName(), "openConnectionInternal");
String jndiString = "jdbc/defaultCICSDataSource";
Context ctx;

try
{
ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup(jndiString);
logger.log(Level.FINE, () -> "jndi string is " + jndiString);
// If there is no current connection
if (this.conn == null)
{
logger.log(Level.FINE,
() -> "About to attempt to get DB2 connection");
// Try and get a connection
this.conn = ds.getConnection();
this.conn.setTransactionIsolation(
Connection.TRANSACTION_READ_UNCOMMITTED);

Where is this flow used?

This flow is used multiple times in the codebase as represented in the following diagram:

 

This is an auto-generated document by Swimm 🌊 and has not yet been verified by a human