// global authentication method. depends on the ClientType public CouponClientFacade login(String name, String password, ClientType clientType) { Utils.logMessage(this, Severity.DEBUG, clientType + " login invoked with pass = "******"db hash = " + _adminHash); Utils.logMessage(this, Severity.DEBUG, "calculated hash = " + md5HashOfPassword); if (md5HashOfPassword.equals(_adminHash)) { Utils.logMessage(this, Severity.DEBUG, "admin authenticated by md5."); // admin auth success, init AdminFacade and passing the DAO's AdminFacade adminFacade = new AdminFacade(customerDao, companyDao, couponDao); return adminFacade; } Utils.logMessage(this, Severity.ERROR, "admin authentication failed. md5 not match !"); } // login of Company if (clientType == ClientType.COMPANY) { // success auth will return the customer ID Long companyId = companyDao.login(name, password); if (companyId != null && companyId != 0) { // construct a customer with those params Company company = new Company(); company.setId(companyId); company.setCompName(name); company.setPassword(password); // create a CustomerFacade, referring to this company CompanyFacade companyFacade = new CompanyFacade(company); companyFacade.setCompanyDao(companyDao); companyFacade.setCouponDao(couponDao); return companyFacade; } } // login of Customer if (clientType == ClientType.CUSTOMER) { // success auth will return the customer ID Long customerId = customerDao.login(name, password); if (customerId != null && customerId != 0) { // construct a customer with those params Customer customer = new Customer(); customer.setId(customerId); customer.setCustName(name); customer.setPassword(password); // create a CustomerFacade CustomerFacade customerFacade = new CustomerFacade(); // customerFacade need to ref that specific customer ( 1 per client ) customerFacade.setCustomer(customer); // and ref the DAOs ( customer + coupons ) customerFacade.setCustomerDao(customerDao); customerFacade.setCouponDao(couponDao); return customerFacade; } } return null; }
// in this implementation, there is only one order per customer // the data model however allows for multiple orders per customer public CustomerOrder findByCustomer(int customerID) { Customer customer = customerFacade.find(customerID); CustomerOrder order = new CustomerOrder(); Connection con = util.getConnection(); String sql = "select * from customer_order where customer_id=?"; PreparedStatement statement = null; try { statement = con.prepareStatement(sql); statement.setInt(1, customerID); ResultSet rs = statement.executeQuery(); while (rs.next()) { order.setId(rs.getInt(1)); order.setAmount(rs.getBigDecimal(2)); order.setDateCreated(rs.getDate(3)); order.setConfirmationNumber(rs.getInt(4)); order.setCustomer(customer); } } catch (SQLException e) { e.printStackTrace(); } finally { if (statement != null) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } } return order; // return (CustomerOrder) // em.createNamedQuery("CustomerOrder.findByCustomer").setParameter("customer", // customer).getSingleResult(); }
@Override public void actionPerformed(ActionEvent e) { String validateCheckResult = null; if (e.getActionCommand().equals(AccountManager.EXIT)) { System.exit(0); } if (e.getActionCommand().equals(AccountManager.VALIDATE_SAVE)) { // get input values String firstName = accountManager.getTextFName(); String lastName = accountManager.getTextLName(); String address = accountManager.getTextAddress(); String city = accountManager.getTextCity(); String state = accountManager.getTextState(); String cardType = accountManager.getCbbType(); String cardNumber = accountManager.getTextCardNumber(); String cardExpDate = accountManager.getTextExpDate(); CustomerFacade facade = new CustomerFacade(); facade.setFname(firstName); facade.setLname(lastName); facade.setAddress(address); facade.setCity(city); facade.setState(state); facade.setCardType(cardType); facade.setCardNumber(cardNumber); facade.setCardExpDate(cardExpDate); // Client is not required to access subsystem components. boolean result = facade.saveCustomerData(); if (result) { validateCheckResult = " Valid Customer Data: Data Saved Successfully "; } else { validateCheckResult = " Invalid Customer Data: Data Could Not Be Saved "; } accountManager.setTxtResult(validateCheckResult); } }
// overridden - refresh method called to retrieve order id from database public CustomerOrder find(int id) { // throw new RuntimeException(); CustomerOrder order = new CustomerOrder(); Connection con = util.getConnection(); String sql = "select * from customer_order where ID=?"; PreparedStatement statement = null; try { statement = con.prepareStatement(sql); statement.setInt(1, id); ResultSet rs = statement.executeQuery(); while (rs.next()) { order.setId(id); order.setAmount(rs.getBigDecimal(2)); order.setDateCreated(rs.getDate(3)); order.setConfirmationNumber(rs.getInt(4)); int customerId = rs.getInt(5); CustomerFacade customerFacade = new CustomerFacade(); Customer customer = customerFacade.find(customerId); order.setCustomer(customer); } } catch (SQLException e) { e.printStackTrace(); } finally { if (statement != null) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } } return order; /*CustomerOrder order = em.find(CustomerOrder.class, id); em.refresh(order); return order;*/ }
public static void main(String[] args) { ///// Admin Section ////// /// Company management /// ////////////////////////// ////////////////////////// // Normal run (without exception pops) // ----------------------------------- try // Replace try scope for each declaration, for example purpose only... { /*CouponSystem system = CouponSystem.getInstance(); AdminFacade admin = (AdminFacade)system.login("admin", "1234", ClientType.ADMIN); //create new company Company comp = new Company("Nike","12345","*****@*****.**"); //add company to database. admin.createCompany(comp); //print all companies (without coupons). System.out.println(admin.getAllComapnies().toString()); //updates company details and view details of specific company: System.out.println("Before email's update: "+ admin.getCompany(comp.getId()).toString()); //set different email address. comp.setEmail("*****@*****.**"); //update in db. admin.updateComapny(comp); //print details from db. System.out.println("After email's update: "+ admin.getCompany(comp.getId()).toString()); //delete company - delete nike's company. admin.removeComapny(comp); System.out.println("After Nike Company Deletion: " + admin.getAllComapnies().toString()); ///////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////// ///// Admin Section /////// /// customer management /// /////////////////////////// /////////////////////////// //Create new customer Customer cust = new Customer("Arik", "1234", "*****@*****.**"); //add customer to database. admin.createCustomer(cust); //print all customer without coupons System.out.println(admin.getAllCustomers().toString()); //updates customer details and view details of specific customer: System.out.println("Before password change: " + admin.getCustomer(cust.getId()).toString()); cust.setPassword("123456"); admin.updateCustomer(cust); System.out.println("After password change: " + admin.getCustomer(cust.getId()).toString()); //delete customer admin.removeCustomer(cust); System.out.println("After arik's deletion: " + admin.getAllCustomers()); ///// Company Section ///// /////////////////////////// /////////////////////////// /////////////////////////// //Login as company CompanyFacade company = (CompanyFacade)system.login("Groupon", "12345", ClientType.COMPANY); Calendar cal = Calendar.getInstance(); //Add new coupon cal.set(2015, 1, 1); Date startDate = cal.getTime(); Date endDate = cal.getTime(); Coupon cpn = new Coupon ("Burger X2",startDate,endDate,15,CouponType.FOOD,"Get double size burger and pay for one.",55.00,"null.jpg"); //Add to company company.createCoupon(cpn); //update price of a coupon cpn.setPrice(70.00); company.updateCoupon(cpn); //delete coupon company.removeCoupon(cpn); //watch all coupons of the company System.out.println(company.getAllCoupons().toString()); //watch all coupons of the company by type = traveling System.out.println(company.getAllCompanysCoponsByType(CouponType.TRAVELING)); //watch all coupons cheaper than a given price System.out.println(company.getAllCompanysCoponsByPrice(120.00)); //watch all coupons earlier than a given date. cal = Calendar.getInstance(); cal.set(2015, 8, 10); java.util.Date date = cal.getTime(); System.out.println(company.getAllCompanysCoponsByDate(date)); //// Customer Section ///// /////////////////////////// /////////////////////////// /////////////////////////// //Connect as a customer CustomerFacade customer = (CustomerFacade) system.login("Tali", "1234", ClientType.CUSTOMER); //get coupon from db, in future it will be done from a list. CouponDBDAO cpnDB = new CouponDBDAO(); Coupon cpn2 = cpnDB.getCoupon(10000003); //purchase a coupon customer.purchaseCoupon(cpn2); //print customer's purchase history System.out.println("Customer purchased history:"); System.out.println(customer.getAllPurchasedCopons()); //print customer's purchases by type System.out.println("Customer purchases by Traveling Type:"); System.out.println(customer.getAllCoponsByType(CouponType.TRAVELING)); //print customer's purchases by type (<=500) System.out.println("Customer purchases until 500 NIS:"); System.out.println(customer.getAllCoponsByPrice(500));*/ CouponSystem system = CouponSystem.getInstance(); CustomerFacade cust = (CustomerFacade) system.login("Tali", "1234", ClientType.CUSTOMER); Coupon c = cust.getCouponbyId(10000001); cust.purchaseCoupon(c); System.out.println(cust.getAllPurchasedCopons()); } catch (CouponsException e) { System.out.println(e.getMessage() + e.getCause()); } }