Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2016
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.07 KB | None | 0 0
  1. // Review: Packages Not Specified
  2. ///Classes
  3.  
  4.  
  5.     //Change this class method if your database changes
  6. class ConnectionGenerator
  7.     public static Connection generateConnection()
  8.         Create Connection sqliteConnection
  9.         Load JDBC Driver ex: "org.sqlite.JDBC"
  10.         set sqliteConnection by calling DriverManager.getConnection('Connection URL')
  11.         return sqliteConnection
  12.  
  13.  
  14.     //User class for User object
  15. public class User
  16.     //MEMBER VARIABLES
  17.     private String mstrUsername;
  18.     private String mstrPassword;
  19.    
  20.     //CONSTRUCTOR
  21.     public User(String lUsername, String lPassword)
  22.         set mstrUsername <- lUsername
  23.         set mstrPassword <- lPassword
  24.    
  25.     //GETTER METHODS
  26.     public String getMstrUsername()
  27.         return mstrUsername;
  28.    
  29.     public String getMstrPassword()
  30.         return mstrPassword;
  31.    
  32.    
  33. public class Customer
  34.     private int miCustomerID;
  35.     private String mstrCustomerName;
  36.     private String mstrCustomerType;
  37.     private boolean mbIsActive;
  38.     private double mdCreditLimit;
  39.    
  40.     //CONSTRUCTOR
  41.     public Customer(int pCustomerID, String pName, String pType, boolean pIsActive, double pCreditLimit)
  42.         miCustomerID <- pCustomerID
  43.         mstrCustomerName <- pName
  44.         mstrCustomerType <- pType
  45.         mbIsActive <- pIsActive
  46.         mdCreditLimit <- pCreditLimit
  47.    
  48.     // Public GETTERS FOR ALL MEMBERS
  49.     public int getMiCustomerID()
  50.         return miCustomerID
  51.     public String getMstrCustomerName()
  52.         return mstrCustomerName
  53.     public String getMstrCustomerType()
  54.         return mstrCustomerType
  55.     public boolean getMbIsActive()
  56.         return mbIsActive
  57.     public double getMdCreditLimit()
  58.         return mdCreditLimit
  59.        
  60.  
  61.  
  62. public class Invoice
  63.     //MEMBER VARIABLES
  64.     private String mstrCustomerName;
  65.     private int miInvoiceID;
  66.     private double mdInvoiceAmount;
  67.     private Date mInvoiceDate;
  68.     private int miStatus;
  69.     private int miCustomerID;
  70.     private Date mPaidDate;
  71.     private Date mDueDate;
  72.    
  73.     //CONSTRUCTOR
  74.     public Invoice(String pCustomerName, int pInvoiceID, double pInvoiceAmount, Date pInvoiceDate, int pStatus, int pCustomerID, Date pPaidDate, Date pDueDate)
  75.         mstrCustomerName <- pCustomerName
  76.         miInvoiceID <- pInvoiceID
  77.         mdInvoiceAmount <- pInvoiceAmount
  78.         mInvoiceDate <- pInvoiceDate
  79.         miStatus <- pStatus
  80.         miCustomerID <- pCustomerID
  81.         mPaidDate <- pPaidDate
  82.         mDueDate <- pDueDate
  83.  
  84.    
  85.     public String getMstrCustomerName()
  86.         return mstrCustomerName
  87.    
  88.     public int getMiInvoiceId()
  89.         return miInvoiceID
  90.    
  91.     public double getMdInvoiceAmount()
  92.         return mdInvoiceAmount
  93.    
  94.     public Date mInvoiceDate()
  95.         return mInvoiceDate
  96.    
  97.     public int getMiStatus()
  98.         return miStatus
  99.        
  100.     public int getMiCustomerID()
  101.         return miCustomerID
  102.        
  103.     public Date getMPaidDate()
  104.         return mPaidDate
  105.    
  106.     public Date getMDueDate()
  107.         return mDueDate
  108.  
  109.  
  110.  
  111.  
  112.  
  113. ///DAO INTERFACES :
  114. public interface LoginDAO
  115.     public boolean doLogin(User pUser);
  116.  
  117.  
  118. public interface CustomerDAO
  119.     public boolean putCustomer(Customer pCustomer);
  120.     public ArrayList<Customer> getAllCustomerList();
  121.     public Customer getCustomer(int pCustomerID);
  122.    
  123.  
  124. public interface InvoiceDAO
  125.     public Invoice getInvoice(int pInvoiceID);
  126.     public ArrayList<Invoice> getAllInvoiceList();
  127.     public ArrayList<Invoice> getCustomerInvoiceList(int pCustomerID);
  128.  
  129.  
  130.  
  131.  
  132.  
  133. ///DAO IMPLEMENTATIONS:
  134. public class MyLoginDAO implements LoginDAO
  135.     public boolean doLogin(User pUser)
  136.         Create Connection loginConnection <- ConnectionGenerator.generateConnection()
  137.         Create a prepared Statement lLoginStatement ("select * from users where username=? and password=?")
  138.         Set String 1 by pUser.getMstrUsername()
  139.         Set . 2 by pUser.getMstrPassword()
  140.         ResultSet lLoginResult <- executeQuery preparedStatement
  141.         if (lLoginResult.next())
  142.             return true
  143.         else
  144.             return false
  145.  
  146.            
  147. public class MyCustomerDAO implements CustomerDAO
  148.     public boolean putCustomer(Customer pCustomer)
  149.         Create Connection putterConnection <- ConnectionGenerator.generateConnection()
  150.         Create a prepared Statement putterStatement (insert into customers values(?,?,?,?,?))
  151.         Set int 1 by pCustomer.getMiCustomerID()
  152.         Set String 2 by pCustomer.getMstrCustomerName()
  153.         Set String 3 by pCustomer.getMstrCustomerType()
  154.         Set boolean 4 by pCustomer.getMbIsActive()
  155.         Set double 5 by pCustomer.getMdCreditLimit()
  156.         int liRowsAffected = putterStatement . executeUpdate()
  157.         if(liRowsAffected == 1)
  158.             return true
  159.         else
  160.             return false
  161.    
  162.     public boolean updateCustomer(Customer pCustomer)
  163.         Create Connection putterConnection <- ConnectionGenerator.generateConnection()
  164.         Create a prepared Statement putterStatement (update customers set customerName=?, customerType=?, isActive=?, creditLimit=? where customerID=?)
  165.        
  166.         Set String 1 by pCustomer.getMstrCustomerName()
  167.         Set String 2 by pCustomer.getMstrCustomerType()
  168.         Set boolean 3 by pCustomer.getMbIsActive()
  169.         Set double 4 by pCustomer.getMdCreditLimit()
  170.         set int 5 by pCustomer.getMiCustomerID
  171.         int liRowsAffected = putterStatement . executeUpdate()
  172.         if(liRowsAffected == 1)
  173.             return true
  174.         else
  175.             return false
  176.        
  177.  
  178.     public ArrayList<Customer> getAllCustomerList()
  179.         ArrayList<Customer> lCustomerList
  180.         Create Connection getAllCustomerConnection <- ConnectionGenerator.generateConnection()
  181.         Create a preparedStatement getAllCustomerStatement (select * from customers)
  182.         ResultSet customerSet = executeQuery
  183.         while (customer.next())
  184.             lCustomerList.add(new Customer(getInt(1), getString(2), getString(3), getBoolean(4), getDouble(5)))
  185.         return lCustomerList
  186.    
  187.     public Customer getCustomer(int pCustomerID)
  188.         Create customerGetConnection <- ConnectionGenerator.generateConnection()
  189.         Create preparedStatement customerGetStatement (select * from customers where customerID=?)
  190.         set int 1 as pCustomerID
  191.         ResultSet customerResult <- executeQuery
  192.         if(customerResult.next())
  193.             return new Customer(getInt(1), getString(2), getString(3), getBoolean(4), getDouble(5))
  194.         else
  195.             return null
  196.  
  197.            
  198.            
  199. public class MyInvoiceDAO implements InvoiceDAO
  200.     // METHOD TO GET INVOICE OBJECT FROM InvoiceID
  201.     public Invoice getInvoice(int pInvoiceID)
  202.         Create invoiceGetConnection <- ConnectionGenerator.generateConnection()
  203.         Create preparedStatement invoiceGetStatement (select * from invoices where invoiceID=?)
  204.         set int 1 as pInvoiceID
  205.         ResultSet invoiceResult <- executeQuery
  206.         if(invoiceResult.next())
  207.             return new Invoice(getString(1), getInt(2), getDouble(3), getDate(4), getInt(5), getInt(6), getDate(7), getDate(8))
  208.         else
  209.             return null
  210.    
  211.     //METHOD TO GET ALL LIST OF ALL INVOICES FROM ALL USERS
  212.     public ArrayList<Invoice> getAllInvoiceList()
  213.         ArrayList<Invoice> lInvoiceList
  214.         Create Connection getAllInvoicesConnection <- ConnectionGenerator.generateConnection()
  215.         Create a preparedStatement getAllInvoicesStatement(select * from invoices)
  216.         ResultSet invoiceSet = getAllInvoicesStatement.executeQuery()
  217.         while (invoiceSet.next())
  218.             lInvoiceList.add( new Invoice(getString(1), getInt(2), getDouble(3), getDate(4), getInt(5), getInt(6), getDate(7), getDate(8)))
  219.         return lInvoiceList
  220.            
  221.            
  222.     //METHOD TO GET LIST OF ALL INVOICES FROM A PARTICULAR CUSTOMER
  223.     public ArrayList<Invoice> getCustomerInvoiceList(int pCustomerID);
  224.         ArrayList<Invoice> lInvoiceList = new ArrayList<Invoice>();
  225.         Create Connection getCustomerInvoiceConnection <- ConnectionGenerator.generateConnection()
  226.         Create a preparedStatement getCustomerInvoiceStatement(select * from invoices where customerID=?)
  227.         getCustomerInvoiceConnection.setInt()
  228.         ResultSet invoiceSet = getAllInvoicesStatement.executeQuery()
  229.         while (invoiceSet.next())
  230.             lInvoiceList.add( new Invoice(getString(1), getInt(2), getDouble(3), getDate(4), getInt(5), getInt(6), getDate(7), getDate(8)))
  231.         return lInvoiceList
  232.    
  233.    
  234.     public boolean updateInvoice(Invoice pInvoice)
  235.         Create Connection updateConnection <- ConnectionGenerator.generateConnection()
  236.         Create a prepared Statement updaterStatement (update invoices set status=?, paidDate=?, dueDate=? where invoiceID=?)
  237.         set Boolean 1 as getStatus
  238.         set Date 2 as getpaidDate
  239.         set Date 3 as getDueDate
  240.         set int 4 as invoiceID
  241.         int liRowsAffected = putterStatement . executeUpdate()
  242.         if(liRowsAffected == 1)
  243.             return true
  244.         else
  245.             return false
  246.    
  247.  
  248.    
  249.     public boolean putInvoice(Invoice pInvoice)
  250.         Create Connection updateConnection <- ConnectionGenerator.generateConnection()
  251.         Create a prepared Statement updaterStatement (insert into invoices values(?,?,?,?,?,?,?,?))
  252.         set 1 as getMstrCustomerName
  253.         set 2 as miInvoiceID
  254.         set 3 as mdInvoiceAmount
  255.         set 4 as mInvoiceDate
  256.         set 5 as miStatus
  257.         set 6 as miCustomerID
  258.         set 7 as mPaidDate
  259.         set 8 as mDueDate
  260.         int liRowsAffected = putterStatement . executeUpdate()
  261.         if(liRowsAffected == 1)
  262.             return true
  263.         else
  264.             return false
  265.        
  266.  
  267.        
  268. /// BASE SERVLET CLASS FOR VALIDATION IN ALL SERVLETS
  269. public class MyHttpServlet extends HttpServlet
  270.     protected void service(HttpServletRequest request, HttpServletResponse response)
  271.         if(request.getSession.getAttribute("activeUser") is null)
  272.             redirect to PreLogin servlet
  273.         else
  274.             super.service(request,response)
  275.            
  276.  
  277. /// PRELOGIN SERVLET
  278. public class PreLogin extends HttpServlet
  279.     protected void service(HttpServletRequest request, HttpServletResponse response)
  280.         if(request.getSession.getAttribute("activeUser") not null)
  281.             redirect to AdminPanel servlet
  282.         else
  283.             super.service(request,response)
  284.        
  285.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  286.         forward request to login.jsp
  287.  
  288.        
  289. /// SUBMITLOGIN Servlet accessed after login.jsp
  290. public class SubmitLogin extends HttpServlet
  291.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  292.         Get username and password from request objects.
  293.         Create a User object and getLoginDAOInstance() from DAO Factory
  294.         if(ldaoInstance.doLogin() is true)
  295.             request.getSession().setAttribute "activeUser" with User object
  296.             redirect to AdminPanel servlet
  297.         else
  298.             print fail on response object
  299.  
  300. /// AdminPanel Servlet forwads to admin-panel.jsp
  301. public class AdminPanel extends MyHttpServlet
  302.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  303.         forward request to admin-panel.jsp
  304.  
  305. /// CustomerListServlet forwards to customer-list-maker.jsp
  306. public class CustomerListServlet extends MyHttpServlet
  307.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  308.         get customerDAOInstance from DAO Factory
  309.         and retrieve Arraylist of Customer...
  310.         Set the arraylist as request Attribute
  311.         forward request to cutomer-list-maker.jsp
  312.  
  313. /// AddUpdateCustomer can add or update customer details
  314. public class AddUpdateCustomer extends MyHttpServlet
  315.     //DOPOST TO HANDLE Add / Update request
  316.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  317.         check if request parameter customerID set
  318.             call editMode(request,response)
  319.         else
  320.             call newMode(request,response)
  321.    
  322.    
  323.     private void editMode(HttpServletRequest request, HttpServletResponse response)
  324.         get request parameter CustomerID
  325.         Create CustomerDAO object customerdao by getCustomerDAOInstance()
  326.         Get Customer object by - customerdao.getCustomer(customerID)
  327.         set the cutomer object as Attribute in request
  328.         forward the request to cutomer-form-maker.jsp
  329.    
  330.     private void newMode(HttpServletRequest request, HttpServletResponse response)
  331.         Generate a random int CustomerID
  332.         Create Customer object by using CustomerID as newly generated int and rest of the fields empty
  333.         set the cutomer object as Attribute in request
  334.         forward the request to cutomer-form-maker.jsp
  335.  
  336.  
  337.        
  338. /// Submits the customer details to commit to database     
  339. // Review : servlet Name?
  340. public class SubmitCustomerForm extends MyHttpServlet
  341.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  342.         get mstrCustomerID mstrCustomerName mstrCustomerType mbIsActive mdCreditLimit from getParameter()
  343.         Create a new Customer Object
  344.         customerdao getCustomerDAOInstance from DAO Factory
  345.         if parameter mode = new
  346.             Write into response, result of === >   customerdao.putCustomer(customer object)
  347.         else
  348.             Write into response, result of === >   customerdao.updateCustomer(Customer object)
  349.    
  350.  
  351. /// InvoiceList servlet forwards the request to invoice-list-maker.jsp
  352. public class InvoiceList extends MyHttpServlet
  353.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  354.         create InvoiceDAO object - invoicedao from Daofactory
  355.         call the invoicedao.getAllInvoiceList() and store it in ArrayList<Invoice>
  356.         set the request attribute to ArrayList<Invoice>
  357.         forward request to invoice-list-maker.jsp
  358.  
  359.  
  360. /// InvoicePanel forwards the request to invoice-manager.jsp
  361. public class InvoicePanel extends MyHttpServlet
  362.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  363.         forward request to invoice-manager.jsp
  364.  
  365.  
  366. /// AddUpdateInvoice displays invoice form in add or edit mode
  367. public class AddUpdateInvoice extends MyHttpServlet
  368.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  369.         check if request parameter invoiceID set
  370.             call editMode(request,response)
  371.         else
  372.             call newMode(request,response)
  373.    
  374.    
  375.     private void editMode(HttpServletRequest request, HttpServletResponse response)
  376.         get request parameter invoiceID
  377.         Create InvoiceDAO object invoicedao by getInvoiceDAOInstance()
  378.         Get Invoice object by - invoice.getInvoice(invoiceID)
  379.         set the invoice object as Attribute in request
  380.         forward the request to invoice-form-maker.jsp
  381.    
  382.     private void newMode(HttpServletRequest request, HttpServletResponse response)
  383.         Generate a random int invoiceID
  384.         Create Invoice object by using invoiceID as newly generated int and rest of the fields empty
  385.         set the invoice object as Attribute in request
  386.         forward the request to invoice-form-maker.jsp
  387.  
  388.  
  389. /// COMMIT THE CHANGES IN INVOICES TO THE DATABASE
  390. public class SubmitInvoice extends MyHttpServlet
  391.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  392.         get mstrCustomerName miInvoiceID mdInvoiceAmount mInvoiceDate miStatus miCustomerID mPaidDate mDueDate from getParameter()
  393.         Create a new Invoice Object
  394.         invoicedao getInvoiceDAOInstance from DAO Factory
  395.         if parameter mode = new
  396.             Write into response, result of === >   invoicedao.putInvoice(invoice object)
  397.         else
  398.             Write into response, result of === >   invoice.updateInvoice(invoice object)
  399.  
  400.  
  401.            
  402.        
  403. ///JSPs
  404. //login.jsp
  405.         login form submit takes you to SubmitLogin and on success it redirects to AdminServlet which forwards to admin-panel.jsp
  406.         if failed on SubmitLogin ...... we go back to login.jsp
  407.    
  408.    
  409. //admin-panel.jsp
  410.         Add Customer button - calls the AddUpdateCustomer Servlet with mode=new and shows the output customer-form-maker.jsp in bottom div
  411.        
  412.         View customer list calls the CustomerManager servlet which prints the customer list table which is customer-list-maker.jsp in bottom div
  413.        
  414.         View Invoice List button - calls the InvoiceList which prints the invoice list table which is invoice-list-maker.jsp in bottom div
  415.        
  416. //invoice-panel.jsp
  417.         AddInvoiceButton: calls the AddUpdateInvoice Servlet with mode=new and show output invoice-form-maker.jsp in bottom div
  418.        
  419.         View Invoice List calls InvoiceManager servlet which prints the output of invoice-list-maker.jsp in bottom div
  420.  
  421.        
  422. // customer-list-maker.jsp
  423.         Prints the list of customers..
  424.             Takes the ArrayList<Customer> from the request Attribute
  425.             print the table of Customers one by one
  426.                 1. Clicking on customer name will open customer-form-maker.jsp in edit mode
  427.                 2. Clicking on Add invoice against ever customer will open invoice-form-maker.jsp in new mode
  428.        
  429. // customer-form-maker
  430.         Take the Customer object from the request
  431.         Print the customer form in edit/new mode
  432.         submit will call the SubmitCustomerForm servlet
  433.        
  434. // invoice-list-maker
  435.         Take the ArrayList<Invoice> attribute from the request
  436.         print the table of Invoice one by one
  437.             1. Clicking on Checkbox for unpaid invoices and submit will call InvoiceManager and update the invoices
  438.             2. Clicking on individual invoice IDs will open invoice-form-maker in edit mode
  439.  
  440. // invoice-form-maker
  441.         Take the Invoice object from the request
  442.         Print the Invoice form in edit/new mode
  443.         submit will call the SubmitInvoice Servlet and send the form
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement