Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package by.carrental.services;
- import java.sql.Connection;
- import java.sql.SQLException;
- import org.apache.log4j.Logger;
- import by.carrental.beans.User;
- import by.carrental.connection.ConnectionPool;
- import by.carrental.dao.UserDAO;
- import by.carrental.exception.CommandException;
- /**
- * @author Volha Alimava
- * Created 22.05.2017
- */
- public class EditProfileService {
- private static Logger log = Logger.getLogger(EditProfileService.class.getName());
- private String login;
- private String firstName;
- private String lastName;
- private String country;
- private String city;
- private String street;
- private String phone;
- private String phone2;
- private String passportData;
- private String email;
- private String password;
- private int userId;
- private String currentLogin;
- // private String currentEmail;
- // private String currentPhone;
- public EditProfileService(String login, String firstName, String lastName,
- String country, String city, String street,
- String phone, String phone2, String passportData,
- String email, String password, int userId,
- String currentLogin) throws CommandException {
- this.login = login;
- this.firstName = firstName;
- this.lastName = lastName;
- this.country = country;
- this.city = city;
- this.street = street;
- this.phone = phone;
- this.phone2 = phone2;
- this.passportData = passportData;
- this.email = email;
- this.password = password;
- this.userId = userId;
- this.currentLogin = currentLogin;
- // this.currentEmail = currentEmail;
- // this.currentPhone = currentPhone;
- checkRequiredFields();
- }
- public void editProfile() throws CommandException {
- UserDAO userDAO = null;
- try {
- Connection connection = ConnectionPool.getInstance().getConnection();
- userDAO = new UserDAO(connection);
- checkUniqueData(userDAO, currentLogin);
- User user = new User();
- user.setFirstName(firstName);
- user.setLastName(lastName);
- user.setCountry(country);
- user.setCity(city);
- user.setStreet(street);
- user.setPhone(phone);
- user.setPhone2(phone2);
- user.setPassportData(passportData);
- user.setEmail(email);
- user.setLogin(login);
- user.setPassword(password);
- user.setId(userId);
- userDAO.update(user);
- } catch (SQLException ex) {
- log.error("Exception in EditProfileService.editProfile", ex);
- throw new CommandException(ex.getMessage(), ex);
- } finally {
- if (userDAO != null) {
- Connection connection = userDAO.freeConnection();
- ConnectionPool.getInstance().releaseConnection(connection);
- }
- }
- }
- private void checkRequiredFields() throws CommandException {
- if (firstName == null || firstName.isEmpty()) {
- throw new CommandException("First name cannot be empty or null");
- }
- if (lastName == null || lastName.isEmpty()) {
- throw new CommandException("Last name cannot be empty or null");
- }
- if (country == null || country.isEmpty()) {
- throw new CommandException("Country cannot be empty or null");
- }
- if (city == null || city.isEmpty()) {
- throw new CommandException("City cannot be empty or null");
- }
- if (phone == null || phone.isEmpty()) {
- throw new CommandException("Phone cannot be empty or null");
- }
- if (passportData == null || passportData.isEmpty()) {
- throw new CommandException("PassportData cannot be empty or null");
- }
- if (email == null || email.isEmpty()) {
- throw new CommandException("Email cannot be empty or null");
- }
- if (login == null || login.isEmpty()) {
- throw new CommandException("Login cannot be empty or null");
- }
- if (password == null || password.isEmpty()) {
- throw new CommandException("Password cannot be empty or null");
- }
- }
- private void checkUniqueData(UserDAO userDAO, String currentLogin) throws CommandException {
- try {
- User loginResult = userDAO.getByLogin(login);
- if (currentLogin.equals(login)) {
- log.info("Login haven't been changed");
- }
- if (loginResult != null && login.equals(currentLogin)) {
- throw new CommandException("Login already exists");
- }
- // User EmailResult = userDAO.getByEmail(email);
- // if (EmailResult != null && email.equals(EmailResult.getEmail())) {
- // throw new CommandException("Email already exists");
- // } || currentEmail.equals(email) || currentPhone.equals(phone
- // User PhoneResult = userDAO.getByPhone(phone);
- // if (PhoneResult != null && phone.equals(PhoneResult.getPhone())) {
- // throw new CommandException("Phone already exists");
- User streetResult = userDAO.getByStreet(street);
- User cityResult = userDAO.getByCity(city);
- User countryResult = userDAO.getByCountry(country);
- if (streetResult != null && cityResult == null && countryResult == null) {
- throw new CommandException("City and country haven't been defined");
- }
- if (cityResult != null && countryResult == null ) {
- throw new CommandException("Country haven't been defined");
- }
- if (countryResult != null && cityResult == null) {
- throw new CommandException("City haven't been defined");
- }
- } catch (SQLException ex) {
- log.error("Exception in EditProfileService.checkUniqueData", ex);
- throw new CommandException(ex.getMessage(), ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement