<repo_name>javadev/jsf-jdbc-sample-application<file_sep>/README.md
jsf-jdbc-sample-application
===========================

JSF 2 + jdbc sample application
<file_sep>/src/main/java/QBank/DAL/DataModel/AccountStatement.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package QBank.DAL.DataModel;

/**
 *
 * @author GilC
 */
public class AccountStatement {
    private String Description;
    private int Factor;

    public AccountStatement()
    {
    }
    
    public AccountStatement(String desc, int fac)
    {
        setDescription(desc);
        setFactor(fac);
    }
    
    /**
     * @return the Description
     */
    public String getDescription() {
        return Description;
    }

    /**
     * @param Description the Description to set
     */
    public void setDescription(String Description) {
        this.Description = Description;
    }

    /**
     * @return the Factor
     */
    public int getFactor() {
        return Factor;
    }

    /**
     * @param Factor the Factor to set
     */
    public void setFactor(int Factor) {
        this.Factor = Factor;
    }
}
<file_sep>/src/main/java/QBank/BL/AccountDetailsBean.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package QBank.BL;

import QBank.DAL.AccessMethods;
import QBank.DAL.DataModel.*;
import java.io.IOException;
import java.io.Serializable;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.ServletContext;
import java.util.List;
import java.util.Random;

/**
 *
 * @author GilC
 */
@Named
@SessionScoped
public class AccountDetailsBean implements Serializable 
{
    final int NUM_OF_DAYS = 7;
    final int MAX_TRANS_COUNT = 3;
    final int MAX_TRANS_AMOUNT = 2000;
    final int MIN_TRANS_AMOUNT = 100;
    ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();

    private String AccountNum;
    private String Owner;
    private AccountData CurrentAccountData;

    public void initAccountData()
            throws IOException, SQLException
    {            
        int intAccountNum = 0;
        String query = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("accountnum");
        try
        {
            intAccountNum = Integer.parseInt(query);
        }
        catch (Exception ex)
        {
            FacesContext.getCurrentInstance().getExternalContext().redirect("./index.xhtml");
        }

        Account accountDetails = AccessMethods.GetAccountDetails(query);
        if (accountDetails == null)
        {
            FacesContext.getCurrentInstance().getExternalContext().redirect("./index.xhtml");
        }
        
        setCurrentAccountData(GetFakeAccountData(intAccountNum));
        setAccountNum(Integer.toString(intAccountNum));
        setOwner(accountDetails.getName());


        /*
        foreach (GridViewRow curr in grvData.Rows)
        {
            if (int.Parse(curr.Cells[3].Text) > 0)
            {
                curr.Cells[3].CssClass = "PositiveBalance";
            }
            else
            {
                curr.Cells[3].CssClass = "NegativeBalance";
            }
        }*/
    }

    /**
     * @return the CurrentAccountData
     */
    public AccountData getCurrentAccountData() {
        return CurrentAccountData;
    }

    /**
     * @param CurrentAccountData the CurrentAccountData to set
     */
    private void setCurrentAccountData(AccountData CurrentAccountData) {
        this.CurrentAccountData = CurrentAccountData;
    }
        

    /**
     * @return the NumOfDays
     */
    public int getNumOfDays() {
        return NUM_OF_DAYS;
    }

    /**
     * @return the AccountNum
     */
    public String getAccountNum() {
        return AccountNum;
    }

    /**
     * @param AccountNum the AccountNum to set
     */
    public void setAccountNum(String AccountNum) {
        this.AccountNum = AccountNum;
    }

    /**
     * @return the Owner
     */
    public String getOwner() {
        return Owner;
    }

    /**
     * @param Owner the Owner to set
     */
    public void setOwner(String Owner) {
        this.Owner = Owner;
    }
       
    public void setContextVariable(String Name, Object Value)
    {
        servletContext.setAttribute(Name, Value);
    }
    
    public Object getContextVariable(String Name)
    {
        return servletContext.getAttribute(Name);
    }
    
    protected AccountData GetFakeAccountData(int intAccountNum)
    {            
        if (getContextVariable("Accounts") == null)
        { 
            setContextVariable("Accounts", new ArrayList<AccountData>());
        }

        for (AccountData curr : (List<AccountData>)getContextVariable("Accounts"))
        {
            if (curr.getAccountNum() == intAccountNum)
            {
                return curr;
            }
        }

        List<AccountData> temp = (List<AccountData>)getContextVariable("Accounts");
        AccountData newcurr = new AccountData();
        newcurr.setAccountNum(intAccountNum);
        newcurr.setData(GenerateFakeAccountData(intAccountNum, NUM_OF_DAYS));
        temp.add(newcurr);
        setContextVariable("Accounts", temp);
        return newcurr;
    }

    private String CurrentDateAddDays(int daysToAdd)
    {
        try
        {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            c.setTime(sdf.parse(new Date().toString()));
            c.add(Calendar.DATE, daysToAdd);  // number of days to add
            return sdf.format(c.getTime());
        }
        catch (ParseException ex)
        {
            return new Date().toString();
        }
    }
    private ArrayList<AccountDataRow> GenerateFakeAccountData(int intAccountNum, int intNumOfDays)
    {
        Random rand = new Random();
        ArrayList<AccountStatement> tabAccountStatementDemo = new ArrayList<>();        
        tabAccountStatementDemo.add(new AccountStatement("INSTITUTION CREDIT", 1));
        tabAccountStatementDemo.add(new AccountStatement("SAVE A-C CHARGE", -1));
        tabAccountStatementDemo.add(new AccountStatement("STANDING ORDER", -1));
        tabAccountStatementDemo.add(new AccountStatement("CREDITING OF BANKS", -1));
        tabAccountStatementDemo.add(new AccountStatement("MASTERCARD CORPORATE", -1));
        tabAccountStatementDemo.add(new AccountStatement("INTERNATIONAL VISA", -1));
        tabAccountStatementDemo.add(new AccountStatement("ATM WITHDRAW", -1));
        tabAccountStatementDemo.add(new AccountStatement("ATM WITHDRAW", -1));
        tabAccountStatementDemo.add(new AccountStatement("ATM WITHDRAW", -1));
        tabAccountStatementDemo.add(new AccountStatement("ATM WITHDRAW", -1));
        tabAccountStatementDemo.add(new AccountStatement("LOANS", -1));
        tabAccountStatementDemo.add(new AccountStatement("SALARY", 1));
        tabAccountStatementDemo.add(new AccountStatement("STOCK REVENIEWS", 1));
        tabAccountStatementDemo.add(new AccountStatement("CASH DEPOSIT", 1));
        tabAccountStatementDemo.add(new AccountStatement("CASH DEPOSIT", 1));
        tabAccountStatementDemo.add(new AccountStatement("CASH DEPOSIT", 1));
        tabAccountStatementDemo.add(new AccountStatement("DIRECT DEBIT", -1));
        tabAccountStatementDemo.add(new AccountStatement("CHECK DEPOSIT", 1));
        tabAccountStatementDemo.add(new AccountStatement("CHECK WITHDRAW", -1));
        tabAccountStatementDemo.add(new AccountStatement("MORTAGE PAYMENT", -1));

        ArrayList<AccountDataRow> ret = new ArrayList<>();
        int intBalance = rand.nextInt(10000) - 5000;
        int intTransNum = rand.nextInt(100000);

        for (int i = 0; i < intNumOfDays; i++)
        { 
            int intTransCount = rand.nextInt(MAX_TRANS_COUNT);
            for (int j = 0; j < intTransCount; j++)
            {
                int k = rand.nextInt(tabAccountStatementDemo.size() - 1);
                int creditdebit = tabAccountStatementDemo.get(k).getFactor() * (rand.nextInt(MAX_TRANS_AMOUNT - MIN_TRANS_AMOUNT) + MIN_TRANS_AMOUNT);
                intBalance = intBalance + creditdebit;
                ret.add(new AccountDataRow(CurrentDateAddDays(intNumOfDays * (-1) + i), 
                    tabAccountStatementDemo.get(k).getDescription(),
                    creditdebit,
                    intBalance,
                    Integer.toString(intTransNum++)));
            }
        }

        return ret;
    }
}<file_sep>/src/main/java/QBank/DAL/DataModel/Account.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package QBank.DAL.DataModel;

/**
 *
 * @author GilC
 */
public class Account {
    private String AccountNumber = "";
    private String Password = "";
    private String Name = "";

    public Account(String strAccountNumber, String strPassword, String strName)
    {
        AccountNumber = strAccountNumber;
        Password = <PASSWORD>;
        Name = strName;
    }
    
    /**
     * @return the AccountNumber
     */
    public String getAccountNumber() {
        return AccountNumber;
    }

    /**
     * @param AccountNumber the AccountNumber to set
     */
    public void setAccountNumber(String AccountNumber) {
        this.AccountNumber = AccountNumber;
    }

    /**
     * @return the Password
     */
    public String getPassword() {
        return Password;
    }

    /**
     * @param Password the Password to set
     */
    public void setPassword(String Password) {
        this.Password = Password;
    }

    /**
     * @return the Name
     */
    public String getName() {
        return Name;
    }

    /**
     * @param Name the Name to set
     */
    public void setName(String Name) {
        this.Name = Name;
    }
}
<file_sep>/src/main/webapp/accountdetails.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:body> 
    	<ui:composition template="./commonLayout.xhtml"> 
            <ui:define name="content">
                <f:event listener="#{accountDetailsBean.initAccountData()}" type="preRenderView" />
                Account number: #{accountDetailsBean.accountNum}<br />Owner: #{accountDetailsBean.owner}<br />
                <b>&lt;Last #{accountDetailsBean.numOfDays} days&gt;</b>&nbsp;&nbsp;&nbsp;<a href="">Last 30 days</a>&nbsp;&nbsp;&nbsp;<a href="">Last 60 days</a>
                
                <h:dataTable value="#{accountDetailsBean.currentAccountData.data}" var="o" class="accountDetailsTable" border="1"> 
                    <h:column>
                            <!-- column header -->
                            <f:facet name="header">Transaction date</f:facet>
                            <!-- row record -->
                            #{o.date.toString()}
                    </h:column>
                    <h:column>
                            <!-- column header -->
                            <f:facet name="header">Transaction description</f:facet>
                            <!-- row record -->
                            #{o.description}
                    </h:column>
                    <h:column>
                            <!-- column header -->
                            <f:facet name="header">Credit/Debit</f:facet>
                            <!-- row record -->
                            #{o.creditDebit}
                    </h:column>
                    <h:column>
                            <!-- column header -->
                            <f:facet name="header">Balance</f:facet>
                            <!-- row record -->
                            <span class="#{0 gt o.balance ? 'NegativeBalance' : 'PositiveBalance'}">#{o.balance}</span>
                    </h:column>
                    <h:column>
                            <!-- column header -->
                            <f:facet name="header">Transaction No.</f:facet>
                            <!-- row record -->
                            #{o.transactionNumber}
                    </h:column>
                </h:dataTable>
            </ui:define>		
    	</ui:composition>
    </h:body>
</html>

<file_sep>/src/main/java/QBank/DAL/DataModel/AccountData.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package QBank.DAL.DataModel;

import java.util.List;

/**
 *
 * @author GilC
 */
public class AccountData 
{
    private int AccountNum;
    private List<AccountDataRow> Data;

    /**
     * @return the AccountNum
     */
    public int getAccountNum() {
        return AccountNum;
    }

    /**
     * @param AccountNum the AccountNum to set
     */
    public void setAccountNum(int AccountNum) {
        this.AccountNum = AccountNum;
    }

    /**
     * @return the Data
     */
    public List<AccountDataRow> getData() {
        return Data;
    }

    /**
     * @param Data the Data to set
     */
    public void setData(List<AccountDataRow> Data) {
        this.Data = Data;
    }        
}
<file_sep>/src/main/java/QBank/BL/SearcherBean.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package QBank.BL;
import java.io.Serializable;
import java.util.Random;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import QBank.DAL.DataModel.*;
import QBank.DAL.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.faces.context.FacesContext;

@Named
@SessionScoped
public class SearcherBean implements Serializable {
    private String strSearch = "";
    private List<SiteSearch> arrResults = null;
    
    public SearcherBean() {}
        
    public String getUserSearch() 
    {
        return strSearch;
    }
        
    public void setUserSearch(String user_search)
        throws SQLException 
    {
        strSearch = user_search;
        setResults(AccessMethods.SearchSite(strSearch));
    }          

    /**
     * @return the arrResults
     */
    public List<SiteSearch> getResults() {
        return arrResults;
    }

    /**
     * @param arrResults the arrResults to set
     */
    private void setResults(List<SiteSearch> arrResults) {
        this.arrResults = arrResults;
    }
    
    public void redirectToSearch() throws IOException
    {
        FacesContext.getCurrentInstance().getExternalContext().redirect("./searchresults.xhtml?search=" + this.getUserSearch());
    }
    
    public void initSearch()
        throws SQLException 
    {
        String query = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("search");
        if (query != null && !query.equals(""))
        {
            setUserSearch(query);
        }        
    }
}
<file_sep>/src/main/webapp/about.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:body> 
    	<ui:composition template="./commonLayout.xhtml"> 
            <ui:define name="content">
                    <h2>
                        About
                    </h2>
                    <p>                       
                       Q.Bank has a 150 year history in South Africa and started building a franchise in the Rest of Africa in the early 1990s. We currently operate in 18 countries on the African continent, including South Africa, as well as in other selected emerging markets.
                       Our strategy is to build the leading African financial services organisation using all our competitive advantages to the full. We will focus on delivering superior sustainable shareholder value by serving the needs of our customers through first-class, on-the-ground, operations in chosen countries in Africa. We will also connect other selected emerging markets to Africa and to each other, applying our sector expertise, particularly in natural resources, globally. Our key differentiator is people who are passionate about our strategy wherever in the world they are based.
                       We organise ourselves as three business units but present ourselves as one. Our three main pillars of business are Personal and Business Banking, Corporate and Investment Banking, and Wealth – Liberty.
                       Q.Bank Group is listed on the JSE Limited, share code SBK, and the Namibian Stock Exchange, share code SNB. Normalised headline earnings for 2012 were R15 billion ($1.8 billion), total assets were over R1 557 billion (approximately $184 billion) and we employed approximately 49 000 people (including Liberty) across all geographies. Q.Bank’s market capitalisation at 31 December 2012 was R191 billion (approximately $23 billion). Q.Bank has 1 248 branches, including loan centres, and 8 464 ATMs.
                    </p>
            </ui:define>		
    	</ui:composition>
    </h:body>
</html>

<file_sep>/src/main/java/QBank/DAL/DataModel/CreditCard.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package QBank.DAL.DataModel;
import java.util.*;

/**
 *
 * @author GilC
 */
public class CreditCard {
    private String CC = "";
    private String FirstName = "";
    private String LastName = "";
    private Date ValidTill = null;

    public CreditCard(String strFirstName, String strLastName, String strCC, Date dateValidTill)
    {
        FirstName = strFirstName;
        LastName = strLastName;
        CC = strCC;
        ValidTill = dateValidTill;
    }
    /**
     * @return the CC
     */
    public String getCC() {
        return CC;
    }

    /**
     * @param CC the CC to set
     */
    public void setCC(String CC) {
        this.CC = CC;
    }

    /**
     * @return the FirstName
     */
    public String getFirstName() {
        return FirstName;
    }

    /**
     * @param FirstName the FirstName to set
     */
    public void setFirstName(String FirstName) {
        this.FirstName = FirstName;
    }

    /**
     * @return the LastName
     */
    public String getLastName() {
        return LastName;
    }

    /**
     * @param LastName the LastName to set
     */
    public void setLastName(String LastName) {
        this.LastName = LastName;
    }

    /**
     * @return the ValidTill
     */
    public Date getValidTill() {
        return ValidTill;
    }

    /**
     * @param ValidTill the ValidTill to set
     */
    public void setValidTill(Date ValidTill) {
        this.ValidTill = ValidTill;
    }
}
