Advertisement
Guest User

Untitled

a guest
Apr 15th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.50 KB | None | 0 0
  1. package saitMLS.persistence.clientale;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.sql.*;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import saitMLS.exceptions.clientale.InvalidPhoneNumberException;
  9. import saitMLS.exceptions.clientale.InvalidPostalCodeException;
  10. import saitMLS.persistence.Broker;
  11. import saitMLS.problemDomain.Clientale.Client;
  12.  
  13. /**
  14.  * Implementation of the abstract class Broker which regulates how the client randomAccessFile database is
  15.  * accessed by the ClientWindow.
  16.  * @author Taylor Doud
  17.  *
  18.  */
  19. public class ClientBroker implements Broker
  20. {
  21.     private static ClientBroker cb;
  22.     private ArrayList<Client> searchList;
  23.     private long maxId = 0;
  24.    
  25.     private Statement stmt;
  26.     private Connection conn;
  27.     private ResultSet rs;
  28.    
  29.     /**
  30.      * Constructor to be called when the getBroker method is called. Determines whether a new
  31.      * Binary file is created upon launch.
  32.      * @throws FileNotFoundException
  33.      */
  34.     private ClientBroker() throws FileNotFoundException
  35.     {
  36.         try {
  37.             DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
  38.             String url = "jdbc:oracle:thin:@" + CredentialStore.HOSTNAME + ":" + CredentialStore.PORT;// + ":"
  39.             //              + CredentialStore.SID;
  40.             // jdbc:oracle:thin:@HOSTNAME:PORT:SID
  41.             conn = DriverManager.getConnection(url, CredentialStore.USER, CredentialStore.PASSWORD);
  42.         } catch (SQLException e) {
  43.             e.printStackTrace();
  44.         }
  45.         recordCount();
  46.     }
  47.    
  48.     /**
  49.      * Method to control the generation of ClientBroker.
  50.      * @return ClientBroker constructor
  51.      */
  52.     public static ClientBroker getBroker()
  53.     {
  54.         if(cb == null)
  55.         {
  56.             try
  57.             {
  58.                 cb = new ClientBroker();
  59.             }
  60.             catch (FileNotFoundException e)
  61.             {
  62.                 e.printStackTrace();
  63.             }
  64.         }
  65.         return cb;
  66.     }
  67.     /**
  68.      * Method to write a client object into the binary file.
  69.      * @param client client which will be written into the Binary file;
  70.      * @throws IOException
  71.      */
  72.     private void writeRecord(Client client) throws IOException
  73.     {
  74.         try
  75.         {
  76.             String query = "INSERT INTO client (id, firstname, lastname, address, postalCode, phoneNumber, clientType) values (?,?,?,?,?,?,?)";
  77.             PreparedStatement pStat = conn.prepareStatement(query);
  78.             pStat.setLong(1, client.getId());
  79.             pStat.setString(2, client.getFirstName());
  80.             pStat.setString(3, client.getLastName());
  81.             pStat.setString(4, client.getAddress());
  82.             pStat.setString(5, client.getPostalCode());
  83.             pStat.setString(6, client.getPhoneNum());
  84.             pStat.setString(7, String.valueOf(client.getType()));
  85.            
  86.             int rowCount = pStat.executeUpdate();
  87.             System.out.println("row Count = " + rowCount);
  88.             pStat.close();
  89.         }
  90.         catch (SQLException e)
  91.         {
  92.                 e.printStackTrace();
  93.         }
  94.     }
  95.     /**
  96.      * Method to close Broker and save all information back into the text file.
  97.      */
  98.     @Override
  99.     public void closeBroker()
  100.     {
  101.         try
  102.         {
  103.             rs.close();
  104.             conn.close();
  105.         }
  106.         catch (SQLException e)
  107.         {
  108.             e.printStackTrace();
  109.         }
  110.     }
  111.     /**
  112.      * Method to save an object back into the binary file to later be saved into the text file.
  113.      */
  114.     @Override
  115.     public boolean persist(Object arg0)
  116.     {
  117.         boolean flag = false;
  118.         Client client = (Client)(arg0);
  119.         try
  120.         {
  121.             if(client.getId()==0)
  122.             {
  123.                 client.setId(maxId);
  124.                 maxId++;
  125.                 writeRecord(client);
  126.                 flag = true;
  127.             }
  128.             else
  129.             {
  130.        
  131.                 String query = "UPDATE client SET firstname = ?, lastname = ?, address = ?, postalCode = ?, phoneNumber = ?, clientType = ? WHERE id = ?";
  132.                 PreparedStatement pStat = conn.prepareStatement(query);
  133.                
  134.                 pStat.setString(1, client.getFirstName());
  135.                 pStat.setString(2, client.getLastName());
  136.                 pStat.setString(3, client.getAddress());
  137.                 pStat.setString(4, client.getPostalCode());
  138.                 pStat.setString(5, client.getPhoneNum());
  139.                 pStat.setString(6, String.valueOf(client.getType()));
  140.                 pStat.setLong(7, client.getId());
  141.  
  142.                 int rowCount = pStat.executeUpdate();
  143.                 System.out.println("row Count = " + rowCount);
  144.                 pStat.close();
  145.             }
  146.         }
  147.         catch (SQLException e)
  148.         {
  149.             e.printStackTrace();
  150.         }  
  151.         catch (IOException e)
  152.         {
  153.             e.printStackTrace();
  154.         }
  155.         return flag;
  156.     }
  157.     /**
  158.      * Method to remove the object from the binary file.
  159.      * @param Client object to be removed.
  160.      */
  161.     @Override
  162.     public boolean remove(Object arg0) {
  163.         boolean flag = false;
  164.         Client client = (Client)(arg0);
  165.         long id = client.getId();
  166.        
  167.         try {
  168.             String delete = "DELETE FROM client WHERE id = ?";
  169.             PreparedStatement pStat = conn.prepareStatement(delete);
  170.             pStat.setLong(1, id);
  171.             int rowCount = pStat.executeUpdate();
  172.             System.out.println("row Count = " + rowCount);
  173.             pStat.close();
  174.         }
  175.         catch (SQLException e)
  176.         {
  177.             e.printStackTrace();
  178.         }
  179.         return flag = true;
  180.     }
  181.     /**
  182.      * Method to search determine which search criteria to search the binary file form.
  183.      */
  184.     @Override
  185.     public List search(String item, String type) {
  186.         try
  187.         {
  188.             switch(type)
  189.             {
  190.                 case "id":
  191.                     searchId(item);
  192.                     break;
  193.                 case "type":
  194.                     searchType(item);
  195.                     break;
  196.                 case "name":
  197.                     searchName(item);
  198.                     break;
  199.             }
  200.         }
  201.         catch (IOException e)
  202.         {
  203.             e.printStackTrace();
  204.         }
  205.         return searchList;
  206.     }
  207.     /**
  208.      * Method to search the binary file for an ID number.
  209.      * @param item Id to be search for.
  210.      * @return returns a list of clients with the matching ID number
  211.      * @throws IOException
  212.      */
  213.     private List searchId(String item) throws IOException
  214.     {
  215.         Client client;
  216.         searchList = new ArrayList<Client>();
  217.         long searchedId = Long.parseLong(item);
  218.        
  219.         System.out.println(searchedId);
  220.         try{
  221.         String selectSQL = "SELECT id,firstname,lastname,address,postalCode,phoneNumber,clientType FROM client WHERE id = ?";
  222.         PreparedStatement pStat = conn.prepareStatement(selectSQL);
  223.         pStat.setLong(1, searchedId);
  224.         rs = pStat.executeQuery();
  225.             while (rs.next())
  226.             {
  227.                 client = new Client(rs.getString("id")+";"+rs.getString("firstname")+";"+rs.getString("lastname")+";"+rs.getString("address")+";"+rs.getString("postalCode")+";"+rs.getString("phoneNumber")+";"+rs.getString("clientType"));
  228.                 searchList.add(client);
  229.             }
  230.         pStat.close();
  231.         }
  232.         catch (SQLException e) {
  233.             e.printStackTrace();
  234.         }
  235.         return searchList;
  236.     }
  237.     /**
  238.      * Method to search the binary file for a client type.
  239.      * @param item type to be search for.
  240.      * @return returns a list of clients with the matching client type.
  241.      * @throws IOException
  242.      */
  243.     private List searchType(String item) throws IOException
  244.     {  
  245.         Client client;
  246.         searchList = new ArrayList<Client>();
  247.        
  248.         try{
  249.         String selectSQL = "SELECT id,firstname,lastname,address,postalCode,phoneNumber,clientType FROM client WHERE clientType = ?";
  250.         PreparedStatement pStat = conn.prepareStatement(selectSQL);
  251.         pStat.setString(1, item);
  252.         rs = pStat.executeQuery();
  253.             while (rs.next())
  254.             {
  255.                 client = new Client(rs.getString("id")+";"+rs.getString("firstname")+";"+rs.getString("lastname")+";"+rs.getString("address")+";"+rs.getString("postalCode")+";"+rs.getString("phoneNumber")+";"+rs.getString("clientType"));
  256.                 searchList.add(client);
  257.             }
  258.         pStat.close();
  259.         }
  260.         catch (SQLException e)
  261.         {
  262.             e.printStackTrace();
  263.         }
  264.         return searchList;
  265.     }
  266.     /**
  267.      * Method to search the binary file for a Name.
  268.      * @param item Name to be search for.
  269.      * @return returns a list of clients with the matching Name.
  270.      * @throws IOException
  271.      */
  272.     private List searchName(String item) throws IOException
  273.     {
  274.         System.out.println(item);
  275.         Client client;
  276.         searchList = new ArrayList<Client>();
  277.        
  278.         try{
  279.         String selectSQL = "SELECT id,firstname,lastname,address,postalCode,phoneNumber,clientType FROM client WHERE lastname = ?";
  280.         PreparedStatement pStat = conn.prepareStatement(selectSQL);
  281.         pStat.setString(1, item);
  282.         rs = pStat.executeQuery();
  283.         System.out.println(rs.toString());
  284.             while (rs.next())
  285.             {
  286.                 System.out.println(rs.toString());
  287.                 client = new Client(rs.getString("id")+";"+rs.getString("firstname")+";"+rs.getString("lastname")+";"+rs.getString("address")+";"+rs.getString("postalCode")+";"+rs.getString("phoneNumber")+";"+rs.getString("clientType"));
  288.                 searchList.add(client);
  289.             }
  290.         pStat.close();
  291.         }
  292.         catch (SQLException e) {
  293.             e.printStackTrace();
  294.         }
  295.         return searchList;
  296.     }
  297.    
  298.     private void recordCount()
  299.     {
  300.  
  301.         try {
  302.             stmt = conn.createStatement();
  303.             String query = "SELECT MAX(id) FROM client";
  304.             rs = stmt.executeQuery(query);
  305.             while (rs.next()) {
  306.                 maxId = rs.getLong("MAX(id)");
  307.             }
  308.         } catch (SQLException e) {
  309.             e.printStackTrace();
  310.         }
  311.     }
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement