Exemple #1
0
  /**
   * Authenticates a returning customer.
   *
   * @param uname UNAME of the customer
   * @param passwd PASSWD of the customer
   * @return the Big Customer Model object (with address and country name fields), if authentication
   *     is successful, null otherwise.
   */
  public BigCustomerModel authenticate(String uname, String passwd) {
    BigCustomerModel bigModel = null;
    try {

      // find Collection of customers with this C_UNAME
      // it should be actually one or none of them
      Collection coll = custHome.findByC_UNAME(uname);
      if (coll.isEmpty()) return null; // fail if no such customers

      // get the CustomerModel object
      Customer customer = (Customer) coll.iterator().next();
      CustomerModel model = customer.getCustomer();

      // check the password
      if (!model.getC_PASSWD().equals(passwd)) return null; // fail if passwds do not match

      // update C_LOGIN and C_EXPIRATION
      Date now = new Date();
      customer.setC_LOGIN(now);
      customer.setC_EXPIRATION(new Date(now.getTime() + 7200000));

      // create BigCustomerModel object
      // get fields from the Customer table
      bigModel =
          new BigCustomerModel(
              model.getC_ID(),
              model.getC_UNAME(),
              model.getC_PASSWD(),
              model.getC_FNAME(),
              model.getC_LNAME(),
              model.getC_ADDR_ID(),
              model.getC_PHONE(),
              model.getC_EMAIL(),
              model.getC_DISCOUNT(),
              model.getC_BIRTHDATE(),
              model.getC_DATA());

      // get fields from the ADDRESS table
      Integer C_ADDR_ID = model.getC_ADDR_ID();
      AddressModel address = addressHome.findByPrimaryKey(C_ADDR_ID).getAddress();
      bigModel.setADDR_STREET1(address.getADDR_STREET1());
      bigModel.setADDR_STREET2(address.getADDR_STREET2());
      bigModel.setADDR_CITY(address.getADDR_CITY());
      bigModel.setADDR_STATE(address.getADDR_STATE());
      bigModel.setADDR_ZIP(address.getADDR_ZIP());
      bigModel.setCO_ID(address.getADDR_CO_ID());

      // get fields from the Country table
      Integer CO_ID = address.getADDR_CO_ID();
      CountryModel country = countryHome.findByPrimaryKey(CO_ID).getCountry();
      bigModel.setCO_NAME(country.getCO_NAME());
    } catch (Exception re) {
      throw new EJBException(re);
    }
    return bigModel;
  }