@Override
  public void creteUserAccountForParent(
      final String username,
      final String password,
      final String firstName,
      final String lastName,
      final Date dateOfBirth,
      final String admissionNumber) {

    Student student = this.studentDao.findActiveStudentByAdmissionNumber(admissionNumber);

    if (student == null) {
      throw new BusinessException(
          "Student not found with admission number or not active. " + admissionNumber + ".");
    }

    Collection<Relation> relations = this.relationDao.findRelationsByStudentId(student.getId());

    if (relations == null || relations.isEmpty()) {
      throw new BusinessException(
          "No relations found for student with admission number " + admissionNumber + ".");
    }

    Relation relationMatch = null;

    for (Relation relation : relations) {

      if (firstName.equals(relation.getFirstName())
          && lastName.equals(relation.getLastName())
          && dateOfBirth.equals(relation.getDateOfBirth())) {
        relationMatch = relation;
        break;
      }
    }

    if (relationMatch == null) {
      throw new BusinessException("Relation details does not match our records.");
    }

    UserAccount userAccount = new UserAccount();

    userAccount.setUsername(username);
    userAccount.setPassword(password);

    userAccount.setUserAccountType(UserAccountTypeConstant.PARENT);
    userAccount.setActive(true);

    userAccount = this.createNewUserAccount(userAccount);

    UserRole role = new UserRole();
    role.setUserRole(AuthorityConstant.ROLE_PARENT);
    role.setUserAccount(userAccount);
    this.userRoleDao.persist(role);

    relationMatch.setUserAccount(userAccount);

    this.relationDao.persist(relationMatch);
  }
  @Override
  public void updatePasswordForUserAccount(final String username, final String password) {

    UserAccount userAccount = this.findUserAccountByUsername(username);
    PasswordEncoder encoder = new PasswordEncoder();
    String encodedPassword = encoder.encodePassword(password, null);
    userAccount.setPassword(encodedPassword);
    this.updateUserAccount(userAccount);
  }
  @Override
  public UserAccount createNewUserAccount(final UserAccount userAccount) {
    UserAccount deplicate =
        this.userAccountDao.findUserAccountByUsername(userAccount.getUsername());

    if (deplicate != null) {
      throw new BusinessException("Username unavailable.");
    }

    PasswordEncoder encoder = new PasswordEncoder();
    String encodedPassword = encoder.encodePassword(userAccount.getPassword(), null);
    userAccount.setPassword(encodedPassword);
    return this.userAccountDao.persist(userAccount);
  }
  @Override
  public void creteUserAccountForStudent(
      final String username,
      final String password,
      final String firstName,
      final String lastName,
      final Date dateOfBirth,
      final String admissionNumber) {

    Student student = this.studentDao.findActiveStudentByAdmissionNumber(admissionNumber);

    if (student == null) {
      throw new BusinessException(
          "Student not found with admission number '" + admissionNumber + "' or not active.");
    }

    if (!firstName.equals(student.getFirstName())
        || !lastName.equals(student.getLastName())
        || !dateOfBirth.equals(student.getDateOfBirth())) {
      throw new BusinessException("Student details does not match our records.");
    }

    UserAccount userAccount = new UserAccount();

    userAccount.setUsername(username);
    userAccount.setPassword(password);
    userAccount.setUserAccountType(UserAccountTypeConstant.STUDENT);
    userAccount.setActive(true);

    userAccount = this.createNewUserAccount(userAccount);

    UserRole role = new UserRole();
    role.setUserRole(AuthorityConstant.ROLE_STUDENT);
    role.setUserAccount(userAccount);

    this.userRoleDao.persist(role);

    student.setUserAccount(userAccount);

    this.studentService.saveStudent(student);
  }
  @Override
  public void creteUserAccountForEmployee(
      final String username,
      final String password,
      final String firstName,
      final String lastName,
      final Date dateOfBirth,
      final String employeeNumber) {

    Employee employee = this.employeeDao.findAllEmployeesByEmployeeNumber(employeeNumber);

    if (employee == null) {
      throw new BusinessException(
          "Employee not found with empoyee number : " + employeeNumber + ".");
    }

    if (!firstName.equals(employee.getFirstName())
        || !lastName.equals(employee.getLastName())
        || !dateOfBirth.equals(employee.getDateOfBirth())) {
      throw new BusinessException("Employee details does not match our records.");
    }

    UserAccount userAccount = new UserAccount();

    userAccount.setUsername(username);
    userAccount.setPassword(password);
    userAccount.setUserAccountType(UserAccountTypeConstant.EMPLOYEE);
    userAccount.setActive(true);

    userAccount = this.createNewUserAccount(userAccount);

    UserRole role = new UserRole();
    role.setUserRole(AuthorityConstant.ROLE_EMPLOYEE);
    role.setUserAccount(userAccount);
    this.userRoleDao.persist(role);

    employee.setUserAccount(userAccount);

    this.employeeService.saveEmployee(employee);
  }