Give it a search

Tuesday, March 23, 2010

Development – Database Connection and Transaction Management

The system uses MySQL as the DBMS to store data regarding the fixed Deposit management process as well as some fundamental system information.

So a php script was needed to access and manipulate the database. There also was the need to manage transactions in the database so that the database will be consistent.

These two requirements were addressed by the DBAdapter class. It was developed to allow the system to access the database and to do transactions to the database in a centralized manner. In other words all other scripts that require database access will use this class to gain it.

This way allows better control of database management because of several reasons.

  • Since the system is using one class to access the database DB access scripts won’t scatter all around the source code, thereby giving the programmer a much easier time when it comes to reconfigure the DB connection.
  • This will also reduce the amount of code because of code reusability.

Actually DB access is achieved using 3 class files (DBAdapter, FileAdapter, XMLAdapter) and 1 XML file (dbconfig.xml).


Why so many classes?

Well, first of all let me explain how a connection is made to the database.

All credentials for accessing the database such as Host, User, Password and Database Name are stored in the dbconfig.xml.

So to get those values from the XML first you need to read the file (FileAdapter is used). Then you need to read each XML element (XMLAdapter is used) then you can make the connection (DBAdapter).


Then why can’t you write one class to do all these?

Well, you can. But then, what if you wanted to read a different kind of file to do something else? Then you will have to write code to open and read files all over again. When you create large systems you have to consider the big picture. That is why these functionalities are divided into several classes. Code reusability can be achieved. And that will definitely improve the clarity of the code.

The code looks something like this for accessing the database.

Note: Only the necessary methods of each class are given below.



class FileAdapter
{

/**
*Open the file
*/
function openFile($fileName, $accessMode)
{
$fp = fopen($fileName, $accessMode) or die("Error opening file");
return $fp;
}

/**
*Close the file
*/
function closeFile($pointer)
{
if($pointer)
{
fclose($pointer);
}
}

/**
*Read the file and store data in a variable
*/
function readFile($fileName)
{
$fp = $this -> openFile($fileName, "r"); //file mode “r” means read

$data;

if($fp)
{
while(!feof($fp))
{
$data .= fgets($fp, 4096); //get one line at a time
}
}

$this -> closeFile($fp);
return $data;
}
}



//imports___________________________
require_once('FileAdapter.php');
//_________________________________

class XMLAdapter
{
/**
*Open the xml file and get the content
*/
function getXMLContent($fileName)
{
if($fileName != "" && substr($fileName, -3) == "xml")
{
$FileAdapter = new FileAdapter;
$xml = $FileAdapter -> readFile($fileName);
return $xml;
}
else
{
return false;
}
}

/**
*Get the content into an array of XML tags
*/
function getXMLTagArray($fileName)
{
$FileAdapter = new FileAdapter;
$xml = $FileAdapter -> readFile($fileName);

//Create an XML parser
$xmlparser = xml_parser_create();
xml_parser_set_option($xmlparser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($xmlparser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($xmlparser, $xml, $arrValues);
xml_parser_free($xmlparser);

return $arrValues;
}


/**
*Get values in XML tags to array variables
*/
function getXMLData($fileName)
{
$arrTemp = $this -> getXMLTagArray($fileName);
$arrValues = array();

foreach($arrTemp as $arrSub)
{
if($arrSub['type'] == "complete")
{
$arrValues[] = $arrSub['value'];
}
}

return $arrValues;
}
}



//imports__________________________________
require_once('XMLAdapter.php');
//_______________________________________

class DBAdapter
{

/**
*Connect to the database
*/
function connect()
{
//Get connection info from the config XML
$XMLAdapter = new XMLAdapter;
$db = $XMLAdapter -> getXMLData($_SERVER['DOCUMENT_ROOT']."/FixedDeposit/config/dbconfig.xml");

$server = $db[0];
$user = $db[1];
$password = $db[2];
$dbName = $db[3];

//Establish connection
$connection = mysql_connect($server, $user, $password) or die(mysql_error());
mysql_select_db($dbName,$connection) or die(mysql_error());

return $connection;
}


/**
*Close connection
*/
function close($connection)
{
if($connection)
{
mysql_close($connection);
}
}

/**
*Start a new transaction
*/
function begin($connection)
{
mysql_query("BEGIN", $connection);
}

/**
*Commit a transaction
*/
function commit($connection)
{
mysql_query("COMMIT", $connection);
}


/**
*Rollback a transaction
*/
function rollback($connection, $terminate = false)
{
mysql_query("ROLLBACK", $connection);
if($terminate)
{
$this -> close($connection);
}
}


/**
*Run query without transaction control
*/
function runQueryDirect($query)
{
//connect
$connection = $this -> connect();

//run query
$result = mysql_query($query, $connection) or die(mysql_error());

//close connection
$this -> close($connection);

return $result;
}


/**
*Run query with transaction control
*/
function runQueryTrans($query)
{
//connect
$connection = $this -> connect();

//start the transaction
$this->begin($connection);

//run query
$result = mysql_query($query, $connection);

//check for success
if($result)
{
//commit
$this->commit($connection);
}
else
{
//rollback
$this->rollback($connection);
}

$this -> close($connection);

return $result;
}
}

Wednesday, March 17, 2010

The Design of the System


The Fixed Deposit Management System was designed as a web application. Following technologies will be used in the development of the system.
  • php
  • HTML
  • CSS
  • JavaScript
  • XML

When designing the system several important decisions were taken. They are listed below.

1. The system was divided into three fundamental layers and each type of functionality was allocated in those separate layers. All the interfaces and interface driving logic was allocated to the “Presentation Tier”. Fundamental logical operations were placed in the “Logic Tier” and the database was placed in the “Data Tier”.
This is technically known as the three-tier architecture and is widely used in web applications.

2. System was to be designed as separate modules. And a base on which all of those modules can be put together. Each module needed to be as separate as possible with other modules. This was to enable the system to be developed and implemented in sections rather than to develop everything in one go. And also to ease future modifications.

3. It was seen that implementation would be much easier if an Object Oriented approach was used. The business process then could be easily simulated inside the computer. Additionally, by using object orientation code reusability and abstraction could be achieved.

4. It was decided to divide the system logically into two parts. These two parts were named as "System" and "Business" The "System" portion of the system comprised of fundamental functionality that was essential to the system. These included.

  • User authentication
  • Privilege granting for users
  • Module handling
  • Accessing resources beyond the system boundary
  • Database connection handling
  • File handling
  • XML parsing

On the other hand the "Business" portion contained all the business logic that was needed in maintaining fixed deposits.

5. A minimum amount of business logic was to be allocated inside user interfaces. This gave the ability to completely change the user interface without changing the actual business logic.


The overall design of the Fixed Deposit Management System can be illustrated as follows.





The system comprised of modules and a base that connects all modules together.
Logic was distributed all over the system. This was to achieve distributed computing and to reduce the computational load of the server by doing so. The scattering around of logic all over the system was done adhering to several fundamental conditions.

They are as follows.

1. System and Business logic that are vital to the system was kept in the server
2. Validations and low priority operations were moved to the client side.



Adapters

Adapters are a collection of php classes. The objectives of these are to facilitate access to resources such as the Database, Files, XML documents and third party libraries that reside outside of the system boundary. All other logic classes in the system are given access to outside resources only through an adapter class. As an example a logic class needs to instantiate the adapter class that facilitate database operations in order to connect and run transactions in the DBMS.

Adapter classes facilitate abstraction, code reusability and ease of maintenance. Since these classes provide centralized access to a resource the system gains the distinct advantage of portability.


Common JavaScript Files

These files contain common operations shared by the system’s client side.


CSS

These are a set of Cascading Style Sheet (CSS) files. Their objective is to keep the styling data of the interfaces separate from interfaces themselves. This was done to avoid spending too much time designing user interfaces and also to gain the ability to change the appearance of these interfaces depending on user feedbacks.


Module

A Module accomplishes a unique task within the system. As an example the Client module handles insertion of new clients and updating and deletion of existing clients.
A single Module consists of four files. Two at server side (Logic and Link) and two at client side (Module specific JavaScript and HTML).


HTML Page

This is the user interface for the module


Module Specific JavaScript File

This file handles all of the user operations done on the HTML page. Allow the server side of the module to communicate with the client side and handles data transfer between the server and the client asynchronously. On top of all that this file takes on the job of modifying the interface according to response from the server.


Link File

Link file is a php script file that acts as the bridge between the client side JavaScript file and the server side logic file. Addition of this portion to a module was needed to overcome the heterogenic situation that arises when two different programming languages try to communicate with each other.


Logic File

The Logic file of the module is a php class. All of the vital module specific logic resides within this class.


XMLHTTPRequest

This is a JavaScript file that creates the XMLHTTPRequest object. This object is solely responsible for the asynchronous communication between the client and the server. This is more commonly known as a popular form of Ajax implementation.

Monday, March 15, 2010

The Scope of the Project

The project was to create a Fixed Deposit Management System.

Before starting to describe how the problem was addressed I will briefly discuss what a Fixed Deposit is.


Fixed Deposit

A Fixed Deposit is an investing mechanism where an individual can invest his or her money in a financial company for a fixed period of time. An interest is given by the company to the investor depending on the amount of money and the period of investment.

There is a formal procedure that is followed during this process. The process involves lot of referencing and cross validating. Hence carrying it out manually can sometimes be a burden on employees. Especially, when there are a large number of customers to be managed.
The fixed deposit management process varies by country. This is due to the fact that laws and regulations for financial transactions differ according to the country. And further more each financial institute has their own variation when doing fixed deposits.

1. An application form is given to an investor to be filled and then handed over to the corresponding officer. This application consists of following details.
  • The details of the investor who is known as the “Holder” in Fixed Deposits. There can be up to 3 Holders for a Fixed Deposit.
  • The details of the “Nominee”
  • Details of “Payee”. There can be up to 3 Payees.
  • The deposit amount
  • The deposit period
  • Deposit type (“Monthly” deposits pay interest by month and “Maturity” deposits pay interest at the end of the period)
2. The application is then checked by that officer. If relevant conditions are satisfied then a draft version of the Fixed Deposit is started by that officer.

3. The agreed amount of money is paid by the customer to the company and the company issues a receipt

4. If the money was paid via a cheque, the cheque is checked for validity and if valid the fixed deposit is “Initiated”. Or if the payment was made by cash then the Fixed Deposit is “Initiated” as for validation is not required.

5. A Fixed Deposit Certificate is printed and handed over to the Holder.

6. All initiated applications are forwarded to a superior officer and is again checked.

7. If conditions are satisfied the Fixed Deposit is “Approved”.

8. Monthly interest is calculated and paid to the investor appropriately.

9. When a Fixed Deposit is “Matured” (time period for the Fixed Deposit completed) the investor is given the opportunity either to “Withdraw” the deposit or to “Renew” it.

10. In some cases the investor might want to withdraw the money before the Fixed
Deposit matures. This is known as a “Pre-Mature” withdrawal and handled according to a different process. Then the interest is recalculated using a different formula that yields interest at a much lower rate to the time the money staid with the company.

  • If the FD is of “Maturity” type then the deposit amount plus the newly calculated interest is given to the investor
  • If the FD is of “Monthly” type where the investor has claimed the interests of previous months, that amount is deducted from the combination of the deposit amount and the newly calculated interest amount.

For better understanding the whole process can be illustrated as follows





Objectives of the Project

Having explained what a Fixed Deposit is, following were the main objectives of the project.

1. Customer details and details regarding Fixed Deposits needed to be held in digital format for easy access and maintainability

2. Interest that should be given to an investor needed to be calculated along with the interest payment date. These interest calculations needed to be done considering several factors
  • The deposit amount
  • The period of deposit

3. Fixed Deposit certificates and receipts needed to be printed

4. The entire process of managing fixed deposits must run as a smooth work flow

5. Detailed reports and statistics were needed for the management to take vital management decisions.

6. Management of Fixed Deposits needed to be faster. Hence the company can deal with more customers.

7. The staff needed to be relieved of burdensome operations such as constant cross validating and the need to go through files upon files of records.

Sunday, March 7, 2010

Additional Tools Used

Apache HTTP Server, php and MySQL created the fundamental environment for the project. Apart from these several other tools were used to streamline and ease the development of the system.


NetBeans IDE


The NetBeans IDE is a free integrated development environment that supports an exquisite range of programming languages and development environments. It is one of my favourite IDEs and it helped me a lot in the development process. Especially the code completion and the ability to refer the php manual from within the IDE came very helpful.

Official Website http://www.netbeans.org/



phpMyAdmin

To manage the MySQL database I used phpMyAdmin. It too is free and open sourced and great for managing MySQL.



Firefox

I used this popular free web browser as the client side for my system. The system was developed targeting only Firefox. Why? It is to avoid coding JavaScript separately for each browser. This was not seen as a weakness of the system but as one of the strong points (coding JavaScript for just one browser actually helped me in time management for the project). Since the system will only be used in a contained environment all the computers in the company could be equipped with Firefox to access the system.



Firebug

Firebug is a JavaScript debugging plugin that comes for Firefox. Since the system uses JavaScript and Ajax a lot, I needed a way to debug JavaScript and Firebug was the solution. It can be downloaded from its web site or it comes attached with NetBeans. This is a great tool that can really ease the pain when it comes to JavaScript debugging. And not only that, you can use FireBug to monitor asynchronous calls to the server.