if (login.contains("'" + "")) {
login = login.replace("'", "");
}
String mail = user.getMail();
try {
User user2 = jdbcTemplate
.queryForObject(
"select * from user where login like "
+ "'"
+ login
+ "%'"
+ " and nom = ? and prenom = ? order by id desc limit 0,1",
new Object[] { nom, prenom }, new UserMapper());
if (user2 != null) {
int max = 2;
String log = user2.getLogin();
String sNb = log.charAt(log.length() - 1) + "";
if (isInteger(sNb)) {
int nb = Integer.parseInt(sNb);
max = nb + 1;
}
String sMax = String.valueOf(max);
login += sMax;
}
} catch (EmptyResultDataAccessException e) {
}
String mdp = generate(5);
String hash = getEncodedPassword(mdp);
String role = user.getRole();
int classe = 0;
if (role.equals("eleve"))
classe = user.getClasse().getId();
User user3;
if (role.equals("prof-principal")) {
jdbcTemplate
.update("insert into user(nom,prenom,login,mdp,hash, role,idClasse, mail) values(?,?,?,?,?,?,?,?)",
new Object[] { nom, prenom, login, mdp, hash, role,
null, mail });
user3 = jdbcTemplate
.queryForObject(
"select * from user where login = ? and mdp = ? order by id desc limit 0,1",
new Object[] { login, mdp }, new UserMapper());
Long idUser = user3.getId();
for (int i = 0; i < user.getLesClasses().length; i++) {
jdbcTemplate
.update("insert into prof_principal(idUser,idClasse) values(?,?)",
new Object[] { idUser, user.getLesClasses()[i] });
}
} else if (!role.equals("eleve")) {
jdbcTemplate
.update("insert into user(nom,prenom,login,mdp, hash, role,idClasse, mail) values(?,?,?,?,?,?,?,?)",
new Object[] { nom, prenom, login, mdp, hash, role,
null, mail });
} else {
jdbcTemplate
.update("insert into user(nom,prenom,login,mdp, hash, role,idClasse, mail) values(?,?,?,?,?,?,?,?)",
new Object[] { nom, prenom, login, mdp, hash, role,
classe, mail });
}
if (role.startsWith("prof")) {
user3 = jdbcTemplate
.queryForObject(
"select * from user where login = ? and mdp = ? order by id desc limit 0,1",
new Object[] { login, mdp }, new UserMapper());
jdbcTemplate
.update("update user set idDiscipline = ? where id = ?",
new Object[] { user.getDiscipline().getId(),
user3.getId() });
}
User userInfo = new User();
userInfo.setLogin(login);
userInfo.setPass(mdp);
return userInfo;
}