}
@Override
@Transactional(readOnly = false)
public Long saveUzer(UzerDTO dto, String oldPassword) throws Exception {
Uzer uzer = uzerDAO.find(dto.getId());
boolean passwordHasChanged = false;
if (uzer == null) {
uzer = new Uzer();
// version 0 or 1 ? do use constant ?
uzer.setVersion(0);
} else {
if (!uzer.getPassword().equals(dto.getPassword())) {
passwordHasChanged = true;
String oldPasswordEncrypted = EncryptionUtils.encrypt(EncryptionUtils.encrypt(oldPassword, MessageDigestAlgorithm.MD5),
MessageDigestAlgorithm.SHA_512);
if (!uzer.getPassword().equals(oldPasswordEncrypted)) {
throw new CustomException(MessageFormat.format(
LocaleManager.getInstance(getSession()).getMessageResource(getClass()).getString("EXCEPTION_BAD_OLD_PASSWORD_FOR_USER"),
dto.getName()));
}
}
}
// before here, do save program link deletion...
applyDiffUzer(uzer, dto);
// Encrypts the user password before creating or changing it
if (dto.getId() == null || passwordHasChanged) {
uzer.setPassword(EncryptionUtils.encrypt(EncryptionUtils.encrypt(uzer.getPassword(), MessageDigestAlgorithm.MD5),
MessageDigestAlgorithm.SHA_512));
}
uzer = uzerDAO.save(uzer);
// force program.programmanager, program.processmanager,
// program.ecmmanager save
List<Program> programsLinked = new ArrayList<Program>();
if (uzer.getProgramsManaged() != null) {
for (Program p : uzer.getProgramsManaged()) {
programsLinked.add(p);
}
}
if (uzer.getProcessesManaged() != null) {
for (Program p : uzer.getProcessesManaged()) {
programsLinked.add(p);
}
}
if (uzer.getEcmManaged() != null) {
for (Program p : uzer.getEcmManaged()) {
programsLinked.add(p);
}
}
List<Long> ids = new ArrayList<Long>();
for (Program p : programsLinked) {
if (!ids.contains(p.getId())) {
programDAO.save(p);
ids.add(p.getId());
}
}
return uzer.getId();
}