Skip to main content

Adding a Customer to the Database

In this document, we will explain the process of adding a customer to the database. The process involves creating a customer resource, setting customer details, and writing the customer data to the database.

The flow starts by creating a customer resource and setting the customer's details such as address, name, date of birth, and sort code. These details are then passed to the method that initiates the customer creation process. The process includes several validations, writing the customer data to the VSAM database, and finally writing the customer details to the DB2 database. If all operations are successful, the customer is added to the database.

Flow drill down


addToDB Function

Diving into the addToDB function, we start by creating a CustomerResource and a CustomerJSON object. The customer's details are set into the CustomerJSON object, which is then passed to the createCustomerExternal method. This method initiates the customer creation process.

	public String addToDB()
{
CustomerResource myCustomerResource = new CustomerResource();

CustomerJSON myCustomerJSON = new CustomerJSON();

myCustomerJSON.setCustomerAddress(this.getAddress());
myCustomerJSON.setCustomerName(this.getName());
myCustomerJSON.setDateOfBirth(this.getDob());
myCustomerJSON.setSortCode(this.getSortcode());
Response myCustomerResponse = myCustomerResource
.createCustomerExternal(myCustomerJSON);

String myCustomerString = null;
JSONObject myCustomer = null;

if (myCustomerResponse.getStatus() == 201)
{
myCustomerString = myCustomerResponse.getEntity().toString();
try
{


createCustomerExternal Function

Moving to the createCustomerExternal function, it logs the entry and calls the createCustomerInternal function. After the internal creation process, it terminates the HBankDataAccess and logs the exit.

	@POST
@Produces(MediaType.APPLICATION_JSON)
public Response createCustomerExternal(CustomerJSON customer)
{
logger.entering(this.getClass().getName(),
CREATE_CUSTOMER_EXTERNAL + customer.toString());
Response myResponse = createCustomerInternal(customer);
HBankDataAccess myHBankDataAccess = new HBankDataAccess();
myHBankDataAccess.terminate();
logger.exiting(this.getClass().getName(), CREATE_CUSTOMER_EXTERNAL_EXIT,
myResponse);
return myResponse;
}


createCustomerInternal Function

Next, the createCustomerInternal function performs several validations on the customer data, such as checking if the customer name, sort code, address, and date of birth are not null. It then creates a Customer object and sets the sort code. The customer is created in the VSAM database, and if successful, the customer details are written to the PROCTRAN data store.

	public Response createCustomerInternal(CustomerJSON customer)
{
logger.entering(this.getClass().getName(),
CREATE_CUSTOMER_INTERNAL + customer.toString());
JSONObject response = new JSONObject();


if(customer.getCustomerName() == null)
{
JSONObject error = new JSONObject();
error.put(JSON_ERROR_MSG,
"Customer name is null");
Response myResponse = Response.status(400).entity(error.toString())
.build();
logger.log(Level.WARNING,
() -> "Customer name is null in CustomerResource.createCustomerInternal(), "
+ customer.toString());
logger.exiting(this.getClass().getName(),
CREATE_CUSTOMER_INTERNAL_EXIT, myResponse);
return myResponse;
}


writeCreateCustomerInternal Function

Then, the writeCreateCustomerInternal function writes the created customer details to the DB2 database. If the write operation is successful, it returns an OK response; otherwise, it returns a server error.

	public Response writeCreateCustomerInternal(
ProcessedTransactionCreateCustomerJSON myCreatedCustomer)
{
com.ibm.cics.cip.bankliberty.web.db2.ProcessedTransaction myProcessedTransactionDB2 = new com.ibm.cics.cip.bankliberty.web.db2.ProcessedTransaction();


if (myProcessedTransactionDB2.writeCreateCustomer(
myCreatedCustomer.getSortCode(),
myCreatedCustomer.getAccountNumber(), 0.00,
myCreatedCustomer.getCustomerDOB(),
myCreatedCustomer.getCustomerName(),
myCreatedCustomer.getCustomerNumber()))
{
return Response.ok().build();
}
else
{
return Response.serverError().build();
}

}


createCustomer Function

Going into the createCustomer function, it initializes a Customer object and sets various customer details, including the address, name, date of birth, and sort code. It also populates the credit score and review date for the customer. The customer data is then written to the VSAM file.

	public Customer createCustomer(CustomerJSON customer,
Integer sortCodeInteger)
{
logger.entering(this.getClass().getName(), CREATE_CUSTOMER);

Customer temp = null;

customerFile.setName(FILENAME);
myCustomer = new CUSTOMER();

String sortCodeString = sortCodeInteger.toString();
long customerNumberAsPrimitive = getNextCustomerNumber(sortCodeString);

Long customerNumberLong = Long.valueOf(customerNumberAsPrimitive);
if (customerNumberLong == -1)
{
return null;
}

byte[] key = buildKey(sortCodeInteger, customerNumberLong);



populateCreditScoreAndReviewDate Function

Finally, the populateCreditScoreAndReviewDate function sets a random credit score review date within the next 21 days and calculates the average credit score from multiple credit agencies. This information is then set into the customer object.

	public static CustomerJSON populateCreditScoreAndReviewDate(
CustomerJSON customer)
{
sortOutLogging();

int creditAgencyCount = 5;

/* Set up a random CS review date within the next 21 days */

Calendar calendar = Calendar.getInstance();
long nowMs = calendar.getTimeInMillis();
int next21Days = new Random(calendar.getTimeInMillis()).nextInt(20);
next21Days++;
nowMs = nowMs + (1000L * 60L * 60L * 24L * next21Days);
customer.setReviewDate(new Date(nowMs));
Channel myCreditScoreChannel = null;

AsyncService asService = new AsyncServiceImpl();
List<Future<ChildResponse>> children = new ArrayList<>();
String[] containerID = new String[creditAgencyCount];
int creditScoreTotal = 0;

 

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