Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/com/ifmo/epampractice/dao/PassportDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ public interface PassportDao extends BasicDaoInterface<PassportEntity> {
@Override
void save(PassportEntity PassportEntity);

PassportEntity getBySerialNumber(String serialNumber);
}
18 changes: 18 additions & 0 deletions src/main/java/com/ifmo/epampractice/daoimpl/PassportDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class PassportDaoImpl implements PassportDao {
private static final String DELETE_QUERY = "DELETE FROM passport WHERE id = ?";
private static final String SAVE_QUERY = "INSERT INTO passport (issue_country, issuer, issue_date," +
" expiration_date, serial_number) VALUES (?, ?, ?, ?, ?)";
private static final String GET_BE_SERIAL_NUMBER_QUERY = "SELECT * FROM passport WHERE serial_number = ?";

private static final Logger logger = LogManager.getLogger(CarDaoImpl.class);

Expand Down Expand Up @@ -97,6 +98,23 @@ public void save(PassportEntity passportEntity) {

}

@Override
public PassportEntity getBySerialNumber(String serialNumber) {
try (Connection connection = dbConnector.getConnection();
PreparedStatement statement = connection.prepareStatement(GET_BE_SERIAL_NUMBER_QUERY)
) {
statement.setString(1, serialNumber);
try (ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) {
return entityFromResultSet(resultSet);
}
}
} catch (SQLException e) {
logger.error(e);
}
return null;
}

private PassportEntity entityFromResultSet(ResultSet resultSet) throws SQLException {
return new PassportEntity(
resultSet.getInt("id"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package com.ifmo.epampractice.serviceimpl;

import com.ifmo.epampractice.dao.DrivingLicenseDao;
import com.ifmo.epampractice.dao.PassportDao;
import com.ifmo.epampractice.dao.UsersDao;
import com.ifmo.epampractice.entity.DrivingLicenseEntity;
import com.ifmo.epampractice.entity.PassportEntity;
import com.ifmo.epampractice.entity.UsersEntity;
import com.ifmo.epampractice.services.PasswordHashService;
import com.ifmo.epampractice.services.PersonalDataService;
import com.ifmo.epampractice.security.SecureString;

import java.time.LocalDate;

public class PersonalDataServiceImpl implements PersonalDataService {

private UsersDao usersDao;
private PassportDao passportDao;
private DrivingLicenseDao drivingLicenseDao;
private PasswordHashService passwordHashService;

public PersonalDataServiceImpl(UsersDao usersDao, PassportDao passportDao, PasswordHashService passwordHashService){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

long line

this.usersDao = usersDao;
this.passportDao = passportDao;
this.drivingLicenseDao = drivingLicenseDao;
this.passwordHashService = passwordHashService;
}

@Override
public PassportEntity changePassport(int userId, String issueCountry, String issuer, LocalDate issueDate,
LocalDate expirationDate, String serialNumber) {
UsersEntity usersEntity = usersDao.get(userId);
PassportEntity passportEntity = passportDao.get(usersEntity.getPassId());
if (usersEntity == null){
throw new RuntimeException();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to write some kind of message

}
if (passportEntity == null){
return createPassport(usersEntity, issueCountry, issuer, issueDate, expirationDate, serialNumber);
}
else {
return updatePassport(passportEntity, issueCountry, issuer, issueDate, expirationDate, serialNumber);
}


}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

long lines and too many empty lines


@Override
public DrivingLicenseEntity changeDrivingLicence(int userId, LocalDate issueDate, LocalDate expirationDate,
String serialNumber) {

UsersEntity usersEntity = usersDao.get(userId);
DrivingLicenseEntity drivingLicenseEntity = drivingLicenseDao.get(usersEntity.getDrivingLicenseId());
if (usersEntity == null){
throw new RuntimeException();
}
if (drivingLicenseEntity == null){
return createDrivingLicense(usersEntity, issueDate, expirationDate, serialNumber);
}
else {
return updateDrivingLicense(drivingLicenseEntity, issueDate, expirationDate, serialNumber);
}
//need method to validate data
//return drivingLicenceEntity
}

@Override
public UsersEntity changeUserData(int id, String email, SecureString password, String name,
String contactPhone, String address) {
UsersEntity usersEntity = usersDao.get(id);
validateUserData(email, password, name, contactPhone, address);
usersEntity.setEmail(email);
String passwordHash = passwordHashService.getHash(password);
usersEntity.setPasswordHash(passwordHash);
usersEntity.setName(name);
usersEntity.setContactPhone(contactPhone);
usersEntity.setAddress(address);
usersDao.update(usersEntity);
return usersEntity;
}

private PassportEntity createPassport(UsersEntity usersEntity, String issueCountry, String issuer, LocalDate issueDate,
LocalDate expirationDate, String serialNumber){
validatePassport(issueCountry, issuer, issueDate, expirationDate, serialNumber);

PassportEntity passportEntity = new PassportEntity(0, issueCountry, issuer, issueDate, expirationDate,
serialNumber);
passportDao.save(passportEntity);
passportEntity = passportDao.getBySerialNumber(passportEntity.getSerialNumber());
usersEntity.setPassId(passportEntity.getId());
usersDao.update(usersEntity);
return passportEntity;
}
private PassportEntity updatePassport(PassportEntity passportEntity, String issueCountry, String issuer,
LocalDate issueDate, LocalDate expirationDate, String serialNumber){
validatePassport(issueCountry, issuer, issueDate, expirationDate, serialNumber);
passportEntity.setIssueCountry(issueCountry);
passportEntity.setIssuer(issuer);
passportEntity.setIssueDate(issueDate);
passportEntity.setExpirationDate(expirationDate);
passportEntity.setSerialNumber(serialNumber);
passportDao.update(passportEntity);
return passportEntity;
}

private DrivingLicenseEntity createDrivingLicense(UsersEntity usersEntity, LocalDate issueDate,
LocalDate expirationDate, String serialNumber){
validateDrivingLicense(issueDate, expirationDate, serialNumber);
DrivingLicenseEntity drivingLicenseEntity = new DrivingLicenseEntity(0, issueDate, expirationDate,
serialNumber);
drivingLicenseDao.save(drivingLicenseEntity);
drivingLicenseEntity = drivingLicenseDao.getBySerialNumber(drivingLicenseEntity.getSerialNumber());
usersEntity.setDrivingLicenseId(drivingLicenseEntity.getId());
usersDao.update(usersEntity);
return drivingLicenseEntity;
}

private DrivingLicenseEntity updateDrivingLicense (DrivingLicenseEntity drivingLicenseEntity, LocalDate issueDate,
LocalDate expirationDate, String serialNumber){
validateDrivingLicense(issueDate, expirationDate, serialNumber);
drivingLicenseEntity.setIssueDate(issueDate);
drivingLicenseEntity.setExpirationDate(expirationDate);
drivingLicenseEntity.setSerialNumber(serialNumber);
drivingLicenseDao.update(drivingLicenseEntity);
return drivingLicenseEntity;
}


private static final String PHONE_REGEX = "[\\d\\s\\(\\)\\+]+";

private void validateUserData(String email, SecureString password, String name,
String phone, String address) throws RuntimeException {
if (email == null || email.length() == 0) {
throw new RuntimeException("Email not specified.");
}
if (password == null) {
throw new RuntimeException("Password is not specified.");
}
if (name == null || name.length() == 0) {
throw new RuntimeException("Name is not specified.");
}
if (phone == null || phone.length() == 0) {
throw new RuntimeException("Phone number is not specified.");
}
if (!phone.matches(PHONE_REGEX)) {
throw new RuntimeException("Phone number may only contain digits, spaces, parentheses and '+' sign.");
}
if (address == null || address.length() == 0) {
throw new RuntimeException("Address is not specified.");
}
}
private void validatePassport (String issueCountry, String issuer, LocalDate issueDate,
LocalDate expirationDate, String serialNumber) throws RuntimeException {
if (issueCountry == null || issueCountry.length() == 0) {
throw new RuntimeException("IssueCountry not specified.");
}
if (issuer == null || issuer.length() == 0) {
throw new RuntimeException("Issuer is not specified.");
}
if (issueDate == null) {
throw new RuntimeException("IssueDate date is not specified.");
}
if (expirationDate == null) {
throw new RuntimeException("ExpirationDate date is not specified.");
}
if (serialNumber == null || serialNumber.length() == 0) {
throw new RuntimeException("SerialNumber number is not specified.");
}
}

private void validateDrivingLicense(LocalDate issueDate, LocalDate expirationDate, String serialNumber){
if (issueDate == null) {
throw new RuntimeException("IssueDate date is not specified.");
}
if (expirationDate == null) {
throw new RuntimeException("ExpirationDate date is not specified.");
}
if (serialNumber == null || serialNumber.length() == 0) {
throw new RuntimeException("SerialNumber number is not specified.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.ifmo.epampractice.services;

import com.ifmo.epampractice.entity.DrivingLicenseEntity;
import com.ifmo.epampractice.entity.PassportEntity;
import com.ifmo.epampractice.entity.UsersEntity;
import com.ifmo.epampractice.enums.UserRole;
import com.ifmo.epampractice.security.SecureString;

import java.time.LocalDate;

public interface PersonalDataService {
public PassportEntity changePassport (int userId, String issueCountry, String issuer, LocalDate issueDate,
LocalDate expirationDate, String serialNumber);
public DrivingLicenseEntity changeDrivingLicence (int userId, LocalDate issueDate, LocalDate expirationDate,
String serialNumber);
public UsersEntity changeUserData(int id, String email, SecureString password, String name,
String contactPhone, String address);
}