use of cz.metacentrum.perun.registrar.exceptions.ApplicationMailNotExistsException in project perun by CESNET.
the class MailManagerImpl method getMailById.
@Override
public ApplicationMail getMailById(PerunSession sess, Integer id) throws ApplicationMailNotExistsException {
// TODO authz
ApplicationMail mail;
// get mail def
try {
String query = "select id,app_type,form_id,mail_type,send from application_mails where id=?";
List<ApplicationMail> mails = jdbc.query(query, (resultSet, arg1) -> new ApplicationMail(resultSet.getInt("id"), AppType.valueOf(resultSet.getString("app_type")), resultSet.getInt("form_id"), MailType.valueOf(resultSet.getString("mail_type")), resultSet.getBoolean("send")), id);
// set
if (mails.size() > 1) {
log.error("[MAIL MANAGER] Wrong number of mail definitions returned by unique params, expected 1 but was: {}.", mails.size());
throw new ConsistencyErrorException("Wrong number of mail definitions returned by unique params, expected 1 but was: " + mails.size());
}
mail = mails.get(0);
} catch (EmptyResultDataAccessException ex) {
throw new ApplicationMailNotExistsException("Mail definition with ID=" + id + " doesn't exists.");
}
List<MailText> texts;
try {
texts = jdbc.query(MAIL_TEXTS_SELECT_BY_MAIL_ID, (resultSet, arg1) -> new MailText(new Locale(resultSet.getString("locale")), resultSet.getString("subject"), resultSet.getString("text")), mail.getId());
} catch (EmptyResultDataAccessException ex) {
// if no texts it's error
log.error("[MAIL MANAGER] Mail does not contain any text message.", ex);
return mail;
}
for (MailText text : texts) {
// fill localized messages
mail.getMessage().put(text.getLocale(), text);
}
return mail;
}
use of cz.metacentrum.perun.registrar.exceptions.ApplicationMailNotExistsException in project perun by CESNET.
the class MailManagerImpl method updateMailById.
@Override
@Transactional(rollbackFor = Exception.class)
public void updateMailById(PerunSession sess, ApplicationMail mail) throws FormNotExistsException, ApplicationMailNotExistsException, PrivilegeException {
ApplicationForm form = registrarManager.getFormById(sess, mail.getFormId());
int numberOfExistences = jdbc.queryForInt("select count(1) from application_mails where id=?", mail.getId());
if (numberOfExistences < 1)
throw new ApplicationMailNotExistsException("Application mail does not exist.", mail);
if (numberOfExistences > 1)
throw new ConsistencyErrorException("There is more than one mail with id = " + mail.getId());
// update sending (enabled / disabled)
jdbc.update("update application_mails set send=? where id=?", mail.getSend(), mail.getId());
// update texts (easy way = delete and new insert)
jdbc.update("delete from application_mail_texts where mail_id=?", mail.getId());
for (Locale loc : mail.getMessage().keySet()) {
MailText text = mail.getMessage(loc);
jdbc.update("insert into application_mail_texts(mail_id,locale,subject,text) values (?,?,?,?)", mail.getId(), loc.toString(), text.getSubject(), text.getText());
}
if (form.getGroup() != null) {
perun.getAuditer().log(sess, new MailForGroupIdUpdated(mail, form.getGroup()));
} else {
perun.getAuditer().log(sess, new MailForVoIdUpdated(mail, form.getVo()));
}
}
Aggregations