Advertisement
Guest User

Untitled

a guest
May 26th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.20 KB | None | 0 0
  1. package by.carrental.services;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.SQLException;
  5.  
  6. import org.apache.log4j.Logger;
  7.  
  8. import by.carrental.beans.User;
  9. import by.carrental.connection.ConnectionPool;
  10. import by.carrental.dao.UserDAO;
  11. import by.carrental.exception.CommandException;
  12.  
  13. /**
  14.  * @author Volha Alimava
  15.  * Created 22.05.2017
  16.  */
  17.  
  18. public class EditProfileService {
  19.     private static Logger log = Logger.getLogger(EditProfileService.class.getName());
  20.    
  21.     private String login;
  22.     private String firstName;
  23.     private String lastName;
  24.     private String country;
  25.     private String city;
  26.     private String street;
  27.     private String phone;
  28.     private String phone2;
  29.     private String passportData;
  30.     private String email;
  31.     private String password;
  32.     private int userId;
  33.     private String currentLogin;
  34. //  private String currentEmail;
  35. //  private String currentPhone;
  36.    
  37.     public EditProfileService(String login, String firstName, String lastName,
  38.             String country, String city, String street,
  39.             String phone, String phone2, String passportData,
  40.             String email, String password, int userId,
  41.             String currentLogin) throws CommandException {
  42.        
  43.         this.login = login;
  44.         this.firstName = firstName;
  45.         this.lastName = lastName;
  46.         this.country = country;
  47.         this.city = city;
  48.         this.street = street;
  49.         this.phone = phone;
  50.         this.phone2 = phone2;
  51.         this.passportData = passportData;
  52.         this.email = email;
  53.         this.password = password;
  54.         this.userId = userId;
  55.         this.currentLogin = currentLogin;
  56. //      this.currentEmail = currentEmail;
  57. //      this.currentPhone = currentPhone;
  58.        
  59.         checkRequiredFields();
  60.     }
  61.    
  62.     public void editProfile() throws CommandException {
  63.         UserDAO userDAO = null;
  64.         try {
  65.             Connection connection = ConnectionPool.getInstance().getConnection();
  66.             userDAO = new UserDAO(connection);
  67.             checkUniqueData(userDAO, currentLogin);
  68.             User user = new User();
  69.             user.setFirstName(firstName);
  70.             user.setLastName(lastName);
  71.             user.setCountry(country);
  72.             user.setCity(city);
  73.             user.setStreet(street);
  74.             user.setPhone(phone);
  75.             user.setPhone2(phone2);
  76.             user.setPassportData(passportData);
  77.             user.setEmail(email);
  78.             user.setLogin(login);
  79.             user.setPassword(password);
  80.             user.setId(userId);
  81.             userDAO.update(user);
  82.         } catch (SQLException ex) {
  83.             log.error("Exception in EditProfileService.editProfile", ex);
  84.             throw new CommandException(ex.getMessage(), ex);
  85.         } finally {
  86.             if (userDAO != null) {
  87.                 Connection connection = userDAO.freeConnection();
  88.                 ConnectionPool.getInstance().releaseConnection(connection);
  89.             }
  90.         }
  91.     }
  92.    
  93.     private void checkRequiredFields() throws CommandException {
  94.  
  95.         if (firstName == null || firstName.isEmpty()) {
  96.             throw new CommandException("First name cannot be empty or null");
  97.         }
  98.         if (lastName == null || lastName.isEmpty()) {
  99.             throw new CommandException("Last name cannot be empty or null");
  100.         }
  101.         if (country == null || country.isEmpty()) {
  102.             throw new CommandException("Country cannot be empty or null");
  103.         }
  104.         if (city == null || city.isEmpty()) {
  105.             throw new CommandException("City cannot be empty or null");
  106.         }
  107.         if (phone == null || phone.isEmpty()) {
  108.             throw new CommandException("Phone cannot be empty or null");
  109.         }
  110.         if (passportData == null || passportData.isEmpty()) {
  111.             throw new CommandException("PassportData cannot be empty or null");
  112.         }
  113.         if (email == null || email.isEmpty()) {
  114.             throw new CommandException("Email cannot be empty or null");
  115.         }
  116.         if (login == null || login.isEmpty()) {
  117.             throw new CommandException("Login cannot be empty or null");
  118.         }
  119.         if (password == null || password.isEmpty()) {
  120.             throw new CommandException("Password cannot be empty or null");
  121.         }
  122.     }
  123.  
  124.     private void checkUniqueData(UserDAO userDAO, String currentLogin) throws CommandException {
  125.        
  126.         try {
  127.             User loginResult = userDAO.getByLogin(login);
  128.            
  129.             if (currentLogin.equals(login)) {
  130.                 log.info("Login haven't been changed");
  131.             }
  132.             if (loginResult != null && login.equals(currentLogin)) {
  133.                 throw new CommandException("Login already exists");
  134.             }
  135. //          User EmailResult = userDAO.getByEmail(email);
  136. //          if (EmailResult != null && email.equals(EmailResult.getEmail())) {
  137. //              throw new CommandException("Email already exists");
  138. //          } || currentEmail.equals(email) || currentPhone.equals(phone
  139. //          User PhoneResult = userDAO.getByPhone(phone);
  140. //          if (PhoneResult != null && phone.equals(PhoneResult.getPhone())) {
  141. //              throw new CommandException("Phone already exists");
  142.            
  143.             User streetResult = userDAO.getByStreet(street);
  144.             User cityResult = userDAO.getByCity(city);
  145.             User countryResult = userDAO.getByCountry(country);
  146.            
  147.             if (streetResult != null && cityResult == null && countryResult == null) {
  148.                 throw new CommandException("City and country haven't been defined");
  149.             }
  150.             if (cityResult != null && countryResult == null ) {
  151.                 throw new CommandException("Country haven't been defined");
  152.             }
  153.             if (countryResult != null && cityResult == null) {
  154.                 throw new CommandException("City haven't been defined");
  155.             }
  156.         } catch (SQLException ex) {
  157.             log.error("Exception in EditProfileService.checkUniqueData", ex);
  158.             throw new CommandException(ex.getMessage(), ex);
  159.         }
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement