Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Review: Packages Not Specified
- ///Classes
- //Change this class method if your database changes
- class ConnectionGenerator
- public static Connection generateConnection()
- Create Connection sqliteConnection
- Load JDBC Driver ex: "org.sqlite.JDBC"
- set sqliteConnection by calling DriverManager.getConnection('Connection URL')
- return sqliteConnection
- //User class for User object
- public class User
- //MEMBER VARIABLES
- private String mstrUsername;
- private String mstrPassword;
- //CONSTRUCTOR
- public User(String lUsername, String lPassword)
- set mstrUsername <- lUsername
- set mstrPassword <- lPassword
- //GETTER METHODS
- public String getMstrUsername()
- return mstrUsername;
- public String getMstrPassword()
- return mstrPassword;
- public class Customer
- private int miCustomerID;
- private String mstrCustomerName;
- private String mstrCustomerType;
- private boolean mbIsActive;
- private double mdCreditLimit;
- //CONSTRUCTOR
- public Customer(int pCustomerID, String pName, String pType, boolean pIsActive, double pCreditLimit)
- miCustomerID <- pCustomerID
- mstrCustomerName <- pName
- mstrCustomerType <- pType
- mbIsActive <- pIsActive
- mdCreditLimit <- pCreditLimit
- // Public GETTERS FOR ALL MEMBERS
- public int getMiCustomerID()
- return miCustomerID
- public String getMstrCustomerName()
- return mstrCustomerName
- public String getMstrCustomerType()
- return mstrCustomerType
- public boolean getMbIsActive()
- return mbIsActive
- public double getMdCreditLimit()
- return mdCreditLimit
- public class Invoice
- //MEMBER VARIABLES
- private String mstrCustomerName;
- private int miInvoiceID;
- private double mdInvoiceAmount;
- private Date mInvoiceDate;
- private int miStatus;
- private int miCustomerID;
- private Date mPaidDate;
- private Date mDueDate;
- //CONSTRUCTOR
- public Invoice(String pCustomerName, int pInvoiceID, double pInvoiceAmount, Date pInvoiceDate, int pStatus, int pCustomerID, Date pPaidDate, Date pDueDate)
- mstrCustomerName <- pCustomerName
- miInvoiceID <- pInvoiceID
- mdInvoiceAmount <- pInvoiceAmount
- mInvoiceDate <- pInvoiceDate
- miStatus <- pStatus
- miCustomerID <- pCustomerID
- mPaidDate <- pPaidDate
- mDueDate <- pDueDate
- public String getMstrCustomerName()
- return mstrCustomerName
- public int getMiInvoiceId()
- return miInvoiceID
- public double getMdInvoiceAmount()
- return mdInvoiceAmount
- public Date mInvoiceDate()
- return mInvoiceDate
- public int getMiStatus()
- return miStatus
- public int getMiCustomerID()
- return miCustomerID
- public Date getMPaidDate()
- return mPaidDate
- public Date getMDueDate()
- return mDueDate
- ///DAO INTERFACES :
- public interface LoginDAO
- public boolean doLogin(User pUser);
- public interface CustomerDAO
- public boolean putCustomer(Customer pCustomer);
- public ArrayList<Customer> getAllCustomerList();
- public Customer getCustomer(int pCustomerID);
- public interface InvoiceDAO
- public Invoice getInvoice(int pInvoiceID);
- public ArrayList<Invoice> getAllInvoiceList();
- public ArrayList<Invoice> getCustomerInvoiceList(int pCustomerID);
- ///DAO IMPLEMENTATIONS:
- public class MyLoginDAO implements LoginDAO
- public boolean doLogin(User pUser)
- Create Connection loginConnection <- ConnectionGenerator.generateConnection()
- Create a prepared Statement lLoginStatement ("select * from users where username=? and password=?")
- Set String 1 by pUser.getMstrUsername()
- Set . 2 by pUser.getMstrPassword()
- ResultSet lLoginResult <- executeQuery preparedStatement
- if (lLoginResult.next())
- return true
- else
- return false
- public class MyCustomerDAO implements CustomerDAO
- public boolean putCustomer(Customer pCustomer)
- Create Connection putterConnection <- ConnectionGenerator.generateConnection()
- Create a prepared Statement putterStatement (insert into customers values(?,?,?,?,?))
- Set int 1 by pCustomer.getMiCustomerID()
- Set String 2 by pCustomer.getMstrCustomerName()
- Set String 3 by pCustomer.getMstrCustomerType()
- Set boolean 4 by pCustomer.getMbIsActive()
- Set double 5 by pCustomer.getMdCreditLimit()
- int liRowsAffected = putterStatement . executeUpdate()
- if(liRowsAffected == 1)
- return true
- else
- return false
- public boolean updateCustomer(Customer pCustomer)
- Create Connection putterConnection <- ConnectionGenerator.generateConnection()
- Create a prepared Statement putterStatement (update customers set customerName=?, customerType=?, isActive=?, creditLimit=? where customerID=?)
- Set String 1 by pCustomer.getMstrCustomerName()
- Set String 2 by pCustomer.getMstrCustomerType()
- Set boolean 3 by pCustomer.getMbIsActive()
- Set double 4 by pCustomer.getMdCreditLimit()
- set int 5 by pCustomer.getMiCustomerID
- int liRowsAffected = putterStatement . executeUpdate()
- if(liRowsAffected == 1)
- return true
- else
- return false
- public ArrayList<Customer> getAllCustomerList()
- ArrayList<Customer> lCustomerList
- Create Connection getAllCustomerConnection <- ConnectionGenerator.generateConnection()
- Create a preparedStatement getAllCustomerStatement (select * from customers)
- ResultSet customerSet = executeQuery
- while (customer.next())
- lCustomerList.add(new Customer(getInt(1), getString(2), getString(3), getBoolean(4), getDouble(5)))
- return lCustomerList
- public Customer getCustomer(int pCustomerID)
- Create customerGetConnection <- ConnectionGenerator.generateConnection()
- Create preparedStatement customerGetStatement (select * from customers where customerID=?)
- set int 1 as pCustomerID
- ResultSet customerResult <- executeQuery
- if(customerResult.next())
- return new Customer(getInt(1), getString(2), getString(3), getBoolean(4), getDouble(5))
- else
- return null
- public class MyInvoiceDAO implements InvoiceDAO
- // METHOD TO GET INVOICE OBJECT FROM InvoiceID
- public Invoice getInvoice(int pInvoiceID)
- Create invoiceGetConnection <- ConnectionGenerator.generateConnection()
- Create preparedStatement invoiceGetStatement (select * from invoices where invoiceID=?)
- set int 1 as pInvoiceID
- ResultSet invoiceResult <- executeQuery
- if(invoiceResult.next())
- return new Invoice(getString(1), getInt(2), getDouble(3), getDate(4), getInt(5), getInt(6), getDate(7), getDate(8))
- else
- return null
- //METHOD TO GET ALL LIST OF ALL INVOICES FROM ALL USERS
- public ArrayList<Invoice> getAllInvoiceList()
- ArrayList<Invoice> lInvoiceList
- Create Connection getAllInvoicesConnection <- ConnectionGenerator.generateConnection()
- Create a preparedStatement getAllInvoicesStatement(select * from invoices)
- ResultSet invoiceSet = getAllInvoicesStatement.executeQuery()
- while (invoiceSet.next())
- lInvoiceList.add( new Invoice(getString(1), getInt(2), getDouble(3), getDate(4), getInt(5), getInt(6), getDate(7), getDate(8)))
- return lInvoiceList
- //METHOD TO GET LIST OF ALL INVOICES FROM A PARTICULAR CUSTOMER
- public ArrayList<Invoice> getCustomerInvoiceList(int pCustomerID);
- ArrayList<Invoice> lInvoiceList = new ArrayList<Invoice>();
- Create Connection getCustomerInvoiceConnection <- ConnectionGenerator.generateConnection()
- Create a preparedStatement getCustomerInvoiceStatement(select * from invoices where customerID=?)
- getCustomerInvoiceConnection.setInt()
- ResultSet invoiceSet = getAllInvoicesStatement.executeQuery()
- while (invoiceSet.next())
- lInvoiceList.add( new Invoice(getString(1), getInt(2), getDouble(3), getDate(4), getInt(5), getInt(6), getDate(7), getDate(8)))
- return lInvoiceList
- public boolean updateInvoice(Invoice pInvoice)
- Create Connection updateConnection <- ConnectionGenerator.generateConnection()
- Create a prepared Statement updaterStatement (update invoices set status=?, paidDate=?, dueDate=? where invoiceID=?)
- set Boolean 1 as getStatus
- set Date 2 as getpaidDate
- set Date 3 as getDueDate
- set int 4 as invoiceID
- int liRowsAffected = putterStatement . executeUpdate()
- if(liRowsAffected == 1)
- return true
- else
- return false
- public boolean putInvoice(Invoice pInvoice)
- Create Connection updateConnection <- ConnectionGenerator.generateConnection()
- Create a prepared Statement updaterStatement (insert into invoices values(?,?,?,?,?,?,?,?))
- set 1 as getMstrCustomerName
- set 2 as miInvoiceID
- set 3 as mdInvoiceAmount
- set 4 as mInvoiceDate
- set 5 as miStatus
- set 6 as miCustomerID
- set 7 as mPaidDate
- set 8 as mDueDate
- int liRowsAffected = putterStatement . executeUpdate()
- if(liRowsAffected == 1)
- return true
- else
- return false
- /// BASE SERVLET CLASS FOR VALIDATION IN ALL SERVLETS
- public class MyHttpServlet extends HttpServlet
- protected void service(HttpServletRequest request, HttpServletResponse response)
- if(request.getSession.getAttribute("activeUser") is null)
- redirect to PreLogin servlet
- else
- super.service(request,response)
- /// PRELOGIN SERVLET
- public class PreLogin extends HttpServlet
- protected void service(HttpServletRequest request, HttpServletResponse response)
- if(request.getSession.getAttribute("activeUser") not null)
- redirect to AdminPanel servlet
- else
- super.service(request,response)
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- forward request to login.jsp
- /// SUBMITLOGIN Servlet accessed after login.jsp
- public class SubmitLogin extends HttpServlet
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- Get username and password from request objects.
- Create a User object and getLoginDAOInstance() from DAO Factory
- if(ldaoInstance.doLogin() is true)
- request.getSession().setAttribute "activeUser" with User object
- redirect to AdminPanel servlet
- else
- print fail on response object
- /// AdminPanel Servlet forwads to admin-panel.jsp
- public class AdminPanel extends MyHttpServlet
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- forward request to admin-panel.jsp
- /// CustomerListServlet forwards to customer-list-maker.jsp
- public class CustomerListServlet extends MyHttpServlet
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- get customerDAOInstance from DAO Factory
- and retrieve Arraylist of Customer...
- Set the arraylist as request Attribute
- forward request to cutomer-list-maker.jsp
- /// AddUpdateCustomer can add or update customer details
- public class AddUpdateCustomer extends MyHttpServlet
- //DOPOST TO HANDLE Add / Update request
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- check if request parameter customerID set
- call editMode(request,response)
- else
- call newMode(request,response)
- private void editMode(HttpServletRequest request, HttpServletResponse response)
- get request parameter CustomerID
- Create CustomerDAO object customerdao by getCustomerDAOInstance()
- Get Customer object by - customerdao.getCustomer(customerID)
- set the cutomer object as Attribute in request
- forward the request to cutomer-form-maker.jsp
- private void newMode(HttpServletRequest request, HttpServletResponse response)
- Generate a random int CustomerID
- Create Customer object by using CustomerID as newly generated int and rest of the fields empty
- set the cutomer object as Attribute in request
- forward the request to cutomer-form-maker.jsp
- /// Submits the customer details to commit to database
- // Review : servlet Name?
- public class SubmitCustomerForm extends MyHttpServlet
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- get mstrCustomerID mstrCustomerName mstrCustomerType mbIsActive mdCreditLimit from getParameter()
- Create a new Customer Object
- customerdao getCustomerDAOInstance from DAO Factory
- if parameter mode = new
- Write into response, result of === > customerdao.putCustomer(customer object)
- else
- Write into response, result of === > customerdao.updateCustomer(Customer object)
- /// InvoiceList servlet forwards the request to invoice-list-maker.jsp
- public class InvoiceList extends MyHttpServlet
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- create InvoiceDAO object - invoicedao from Daofactory
- call the invoicedao.getAllInvoiceList() and store it in ArrayList<Invoice>
- set the request attribute to ArrayList<Invoice>
- forward request to invoice-list-maker.jsp
- /// InvoicePanel forwards the request to invoice-manager.jsp
- public class InvoicePanel extends MyHttpServlet
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- forward request to invoice-manager.jsp
- /// AddUpdateInvoice displays invoice form in add or edit mode
- public class AddUpdateInvoice extends MyHttpServlet
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- check if request parameter invoiceID set
- call editMode(request,response)
- else
- call newMode(request,response)
- private void editMode(HttpServletRequest request, HttpServletResponse response)
- get request parameter invoiceID
- Create InvoiceDAO object invoicedao by getInvoiceDAOInstance()
- Get Invoice object by - invoice.getInvoice(invoiceID)
- set the invoice object as Attribute in request
- forward the request to invoice-form-maker.jsp
- private void newMode(HttpServletRequest request, HttpServletResponse response)
- Generate a random int invoiceID
- Create Invoice object by using invoiceID as newly generated int and rest of the fields empty
- set the invoice object as Attribute in request
- forward the request to invoice-form-maker.jsp
- /// COMMIT THE CHANGES IN INVOICES TO THE DATABASE
- public class SubmitInvoice extends MyHttpServlet
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- get mstrCustomerName miInvoiceID mdInvoiceAmount mInvoiceDate miStatus miCustomerID mPaidDate mDueDate from getParameter()
- Create a new Invoice Object
- invoicedao getInvoiceDAOInstance from DAO Factory
- if parameter mode = new
- Write into response, result of === > invoicedao.putInvoice(invoice object)
- else
- Write into response, result of === > invoice.updateInvoice(invoice object)
- ///JSPs
- //login.jsp
- login form submit takes you to SubmitLogin and on success it redirects to AdminServlet which forwards to admin-panel.jsp
- if failed on SubmitLogin ...... we go back to login.jsp
- //admin-panel.jsp
- Add Customer button - calls the AddUpdateCustomer Servlet with mode=new and shows the output customer-form-maker.jsp in bottom div
- View customer list calls the CustomerManager servlet which prints the customer list table which is customer-list-maker.jsp in bottom div
- View Invoice List button - calls the InvoiceList which prints the invoice list table which is invoice-list-maker.jsp in bottom div
- //invoice-panel.jsp
- AddInvoiceButton: calls the AddUpdateInvoice Servlet with mode=new and show output invoice-form-maker.jsp in bottom div
- View Invoice List calls InvoiceManager servlet which prints the output of invoice-list-maker.jsp in bottom div
- // customer-list-maker.jsp
- Prints the list of customers..
- Takes the ArrayList<Customer> from the request Attribute
- print the table of Customers one by one
- 1. Clicking on customer name will open customer-form-maker.jsp in edit mode
- 2. Clicking on Add invoice against ever customer will open invoice-form-maker.jsp in new mode
- // customer-form-maker
- Take the Customer object from the request
- Print the customer form in edit/new mode
- submit will call the SubmitCustomerForm servlet
- // invoice-list-maker
- Take the ArrayList<Invoice> attribute from the request
- print the table of Invoice one by one
- 1. Clicking on Checkbox for unpaid invoices and submit will call InvoiceManager and update the invoices
- 2. Clicking on individual invoice IDs will open invoice-form-maker in edit mode
- // invoice-form-maker
- Take the Invoice object from the request
- Print the Invoice form in edit/new mode
- submit will call the SubmitInvoice Servlet and send the form
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement