⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 profilemanagementbean.java

📁 噶额外噶外骨骼感广泛高热感 就 啊啊
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                                      ex.getMessage(), "error.profile.updation" );
    }
  }

  /**
   * This business method emails the password for a specified user.  This method
   * is invoked when the user clicks on "Forgot Password" link.
   * @param <b>userName</b> - userName of the user whose password has to
   *                          be emailed
   * @param <b>langID</b> - Languague used by the user
   * @exception <b>ProfileException</b> if the user cannot be notified of
   *                                    the password
   */
  public void notifyPassword(String userName, String langID) throws ProfileException {

    // Check if the username has been specified
    if( userName == null || userName.trim().equals("") ) {
      throw new ProfileException("User name is null", "error.profile.usernull");
    }
    try {
      // Lookup the customer bean and locate the user record for the specified user
      CustomerLocalHome userHome = getUserLocalHome();
      CustomerLocal user = userHome.findByPrimaryKey(userName);

      Properties emailMessages = Utilities.loadParams("EmailMessages", langID);
      
      // Form the mail content
      StringBuffer mailBody = new StringBuffer();

      // Append the mail body with proper contents.
      mailBody.append(emailMessages.getProperty("mall.email.salutation"));
      mailBody.append( emailMessages.getProperty("mall.email.username" )); 
      mailBody.append( userName );
      mailBody.append("\n");
      mailBody.append( emailMessages.getProperty("mall.email.password" ) );

      mailBody.append( user.getPassword() );
      mailBody.append("\n\n");      
      mailBody.append( emailMessages.getProperty("mall.email.signature" ) );
      
      // Create the mail content object.
      MailContent mailContent = new MailContent();
      mailContent.setTo( user.getEmail() );
      mailContent.setFrom( getAdminEmail() );
      mailContent.setSubject( emailMessages.getProperty("mall.email.notify" ) );
      mailContent.setBody( mailBody.toString() );
      // Send the mail to user
      Mailer.sendMail(mailContent);
      
    } catch( ObjectNotFoundException ex ) {
      throw new ProfileException( "Invalid username specified ", 
                                  "error.profile.invaliduser" );
    } catch( Exception ex ) {
      throw new ProfileException( "Unable to notify user of password because " + 
                                   ex.getMessage(), "error.profile.notify" );
    }
  }

  /**
   * This business method processes user requests for a shop in the Mall
   * @param <b>shop</b> - Information about the shop that the user wishes to 
   *                      set up in the mall
   * @throws <b>ShopException</b> if the shop cannot be created
   */
  public void requestShop( Shop shop ) throws ShopException {
  
    // Check if the shop information has been specified
    if( shop == null)  {
      throw new ShopException("Shop Information is null","error.shop.null");
    }
    try {
      // Lookup the shop bean
      ShopLocalHome shopHome = getShopLocalHome();

      // Check if there are any shops or shop requests for the username specified
      Collection pendingShops = shopHome.findShopForUser(shop.getUserName());
      if( pendingShops.size() > 0 ) {
        // Shop found - raise exception        
        throw new ShopException("Shop/ Shop Request already exists for user",
                                "error.shop.duplicaterequest");
      }
      /* Generate a primary key for the new shop and add the shop information 
       * to the bean */
      String shopID = KeyFactory.getKey();
      ShopLocal shopLocal = shopHome.create( shopID, shop.getCategoryId(),
                                             shop.getUserName(),
                                             shop.getRegDate(), Constants.PENDING );
      // Lookup the ShopDetail entity bean
      ShopDetailLocalHome shopDetailHome = getShopDetailLocalHome();

      // Add shop name and description to the shopdetail bean
      Collection shopDetails = new ArrayList(1);
      shopDetails.add(shopDetailHome.create( shopID, shop.getShopDetail(0).getLangId(),
                                             shop.getShopDetail(0).getShopName(),
                                             shop.getShopDetail(0).getDescription() ));
      shopLocal.setShopDetails(shopDetails);
      // Create the mail body to notify mall administrator of the new shop request

      Properties emailMessages = Utilities.loadParams("EmailMessages", shop.getShopDetail(0).getLangId() );

      StringBuffer mailBody = new StringBuffer();

      // Append the mail body with proper contents.
      mailBody.append( emailMessages.getProperty("user.email.requestmessage" ) );
      mailBody.append( emailMessages.getProperty("user.email.shopname" ) );
      mailBody.append( shop.getShopDetail(0).getShopName() );
      mailBody.append( emailMessages.getProperty("user.email.shopdesc" ) );
      mailBody.append( shop.getShopDetail(0).getDescription() );
      // Look up customer to find the email of the requestor
      CustomerLocalHome home = getUserLocalHome();
      CustomerLocal user = home.findByPrimaryKey(shop.getUserName());
      shopLocal.setOwner(user);
      // Create the mail content object.
      MailContent mailContent = new MailContent();
      mailContent.setTo( getAdminEmail() );
      mailContent.setFrom( user.getEmail() );
      mailContent.setSubject( emailMessages.getProperty("user.email.requestsubject" ) );
      mailContent.setBody( mailBody.toString() );
      
      // Send the mail to the administrator
      Mailer.sendMail(mailContent);
    } catch( Exception ex ) {
      if( ex instanceof ShopException )
        throw new ShopException( "Shop/ Shop Request already exists for user",
                                 "error.shop.duplicaterequest" );
      else
        throw new ShopException( "Unable to process Shop Creation request because " +
                                   ex.getMessage(), "error.shop.creation" );
    }
  }

  /**
   * This business method processes the information given by user during login
   * @param <b>userName</b> - login id specified by the user
   * @param <b>password</b> - password specified by the user
   * @return <b>Profile</b> - the profile information of the user
   * @throws <b>ProfileException</b> if the shop cannot be created
   */
  public Profile validateUser(String userName, String password) throws ProfileException {

    // Check if username and password have been specified. Raise exception otherwise
    if( userName == null || userName.trim().equals("") )
      throw new ProfileException("User name is null", "error.profile.usernull");
    if( password == null || password.trim().equals(""))
      throw new ProfileException("Password is null", "error.profile.passwordnull");

    try {
      // Lookup the customer bean specified by the username
      CustomerLocalHome userHome = getUserLocalHome();
      CustomerLocal user = userHome.findByPrimaryKey(userName);

      // Check if the password in database matches the password specified by user
      if(!password.equals(user.getPassword())) {
        throw new ProfileException( "Invalid Password specified ",
                                    "error.profile.invalidpassword" );
      } else {

        /* If the passwords match, return first name, lastname, email and role
           of user back to the client */
        return( new Profile(userName, user.getFirstName(), user.getLastName(),
                  user.getEmail(),null, null,null,
                  null, null,null,user.getRole(),
                  null, null, null, null));
      }
    } catch( ObjectNotFoundException ex ) {
      throw new ProfileException( "Invalid user name ","error.profile.invaliduser" );
    } catch( Exception ex ) {
      if( ex instanceof ProfileException )
        throw new ProfileException( "Invalid user name ",
                                    "error.profile.invalidpassword" );
      else
        throw new ProfileException( "Unable to validate user because " +
                                   ex.getMessage(), "error.profile.validation" );
    }
  }

  /**
   * This business method changes the password for the user
   * @param <b>userName</b> - username specified by the user during login
   * @param <b>password</b> - Old password value
   * @param <b>newPassword</b> - New password value
   * @param <b>newPasswordConfirm</b> - Confirmation of the new password
   * @throws <b>ProfileException</b> if the password cannot be updated
   * @throws <b>RemoteException</b> container error
   */
  public void changePassword(String userName, String password, 
                             String newPassword, String newPasswordConfirm) 
                             throws ProfileException {

    // Check if the username, old password and new password has been specified                             
    if( userName == null || userName.trim().equals("") )
      throw new ProfileException("User name is null", "error.profile.usernull");
    if( password == null || password.trim().equals("") )
      throw new ProfileException("Old password is null", "error.profile.passwordnull");
    if( newPassword == null || newPassword.trim().equals("") )
      throw new ProfileException("New password is null", "error.profile.newpwdnull");
    if( newPassword == null || newPassword.trim().equals("") )
      throw new ProfileException("Password confirmation is null", 
                                 "error.profile.newpwdconfirmnull");
      // Check if the new password has been confirmed correctly by user
    if(!newPassword.equals(newPasswordConfirm)) {
      throw new ProfileException("Invalid password confirmation", 
                                 "error.profile.invalidpasswordconfirm");
    }
    try {
      // Lookup the customer bean for the specified username
      CustomerLocalHome userHome = getUserLocalHome();      
      String originalUserName = new String(userName);
      CustomerLocal user = userHome.findByPrimaryKey(userName);
      // Check if the old passwords match    
      if(!password.equals(user.getPassword())) {
        throw new ProfileException("Invalid password", "error.profile.invalidpassword");
      }
      user.setPassword(newPassword);
      JAASManager userManager= (JAASManager)ServiceLocator.getLocator().getService("java:/comp/env/UserManager");
      userManager.dropUser(user.getUserName());
      userManager.addUser(user.getUserName(),newPassword,user.getRole());
    } catch( ObjectNotFoundException ex ) {
      // user not found
      throw new ProfileException( "Invalid user name ","error.profile.invaliduser" );
    } catch( ProfileException pex ) {
       // invalid password specified
       throw new ProfileException("Unable to change password", pex.getMessageCode());
    } catch( Exception ex ) {
      throw new ProfileException( "Unable to change password for user because " +
                                   ex.getMessage(),"error.profile.pwdupdation" );
    }
  }

  /**
   * This business method retrieves country information
   * @return <b>TreeMap</b> - Ordered list of countries information
   * @throws <b>ProfileException</b> if the countries could not be retrieved
   * @throws <b>RemoteException</b> container error
   */
  public TreeMap getCountriesList() throws ProfileException {
    TreeMap countriesList = new TreeMap();
    try{
      // Lookup the country bean
      CountryLocalHome countryHome = getCountryLocalHome();
      CountryLocal country = null;

      /* Iterate through the list of countries and add the country information
       * to the TreeMap */
      for ( Iterator countriesIter = countryHome.findAll().iterator();countriesIter.hasNext(); ) {
        country = (CountryLocal)countriesIter.next();
        countriesList.put(country.getId(), country.getCountryName());
      }
      
    } catch( Exception ex ) {
      throw new ProfileException( "Unable to fetch list of countries because " +
                                   ex.getMessage(), "error.countries.ctryretrieval" );
    }
    return countriesList;
  }

  /*
   * Returns the local home for User entity
   */
  private CustomerLocalHome getUserLocalHome() {
    return (CustomerLocalHome)ServiceLocator.getLocator().getService("java:comp/env/ejb/UserLocal");
  }

  /*
   * Returns the local home for Shop entity
   */
  private ShopLocalHome getShopLocalHome() {
    return (ShopLocalHome)ServiceLocator.getLocator().getService("java:comp/env/ejb/ShopLocal");
  }
  
  /*
   * Returns the local home for Shop Detail entity
   */
  private ShopDetailLocalHome getShopDetailLocalHome() {
    return (ShopDetailLocalHome)ServiceLocator.getLocator().getService( "java:comp/env/ejb/ShopDetailLocal" );
  }
  
  /*
   * Returns the local home for Country entity
   */
  private CountryLocalHome getCountryLocalHome() {
    return (CountryLocalHome)ServiceLocator.getLocator().getService( "java:comp/env/ejb/CountryLocal" );
  }


  /*
   * Returns the email id of the VSM administrator or null if it cannot be found
   */
  private String getAdminEmail() {
    try {
      // find user home,
      CustomerLocalHome home = getUserLocalHome();
      // find the ADMIN entity and return the email id
      return home.findByPrimaryKey( "admin" ).getEmail();
    } catch( Exception ex ) {
      return null;
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -