Search in sources :

Example 1 with ApplicationMailNotExistsException

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;
}
Also used : Message(javax.mail.Message) JdbcPerunTemplate(org.springframework.jdbc.core.JdbcPerunTemplate) ApplicationMailNotExistsException(cz.metacentrum.perun.registrar.exceptions.ApplicationMailNotExistsException) RegistrarManagerImpl(cz.metacentrum.perun.registrar.impl.RegistrarManagerImpl) LoggerFactory(org.slf4j.LoggerFactory) SecretKeySpec(javax.crypto.spec.SecretKeySpec) MessagingException(javax.mail.MessagingException) Autowired(org.springframework.beans.factory.annotation.Autowired) AuditEvent(cz.metacentrum.perun.audit.events.AuditEvent) cz.metacentrum.perun.core.api(cz.metacentrum.perun.core.api) ApplicationMailExistsException(cz.metacentrum.perun.registrar.exceptions.ApplicationMailExistsException) MailManager(cz.metacentrum.perun.registrar.MailManager) MembersManagerBl(cz.metacentrum.perun.core.bl.MembersManagerBl) ApplicationMailAlreadyRemovedException(cz.metacentrum.perun.registrar.exceptions.ApplicationMailAlreadyRemovedException) Matcher(java.util.regex.Matcher) ApplicationMail(cz.metacentrum.perun.registrar.model.ApplicationMail) cz.metacentrum.perun.core.api.exceptions(cz.metacentrum.perun.core.api.exceptions) UsersManagerBl(cz.metacentrum.perun.core.bl.UsersManagerBl) BigInteger(java.math.BigInteger) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) ApplicationForm(cz.metacentrum.perun.registrar.model.ApplicationForm) RegistrarException(cz.metacentrum.perun.registrar.exceptions.RegistrarException) MailForVoIdUpdated(cz.metacentrum.perun.audit.events.MailManagerEvents.MailForVoIdUpdated) MailSending(cz.metacentrum.perun.audit.events.MailManagerEvents.MailSending) URN_GROUP_FROM_EMAIL(cz.metacentrum.perun.registrar.impl.RegistrarManagerImpl.URN_GROUP_FROM_EMAIL) Mac(javax.crypto.Mac) JavaMailSender(org.springframework.mail.javamail.JavaMailSender) ApplicationFormItemData(cz.metacentrum.perun.registrar.model.ApplicationFormItemData) StandardCharsets(java.nio.charset.StandardCharsets) Application(cz.metacentrum.perun.registrar.model.Application) Pattern(java.util.regex.Pattern) MailException(org.springframework.mail.MailException) PerunBl(cz.metacentrum.perun.core.bl.PerunBl) java.util(java.util) MailForGroupIdRemoved(cz.metacentrum.perun.audit.events.MailManagerEvents.MailForGroupIdRemoved) MailForVoIdAdded(cz.metacentrum.perun.audit.events.MailManagerEvents.MailForVoIdAdded) AttributesManagerBl(cz.metacentrum.perun.core.bl.AttributesManagerBl) MailText(cz.metacentrum.perun.registrar.model.ApplicationMail.MailText) MailType(cz.metacentrum.perun.registrar.model.ApplicationMail.MailType) GroupsManagerBl(cz.metacentrum.perun.core.bl.GroupsManagerBl) InternetAddress(javax.mail.internet.InternetAddress) MailSentForApplication(cz.metacentrum.perun.audit.events.MailManagerEvents.MailSentForApplication) DataSource(javax.sql.DataSource) AuthzResolverBlImpl(cz.metacentrum.perun.core.blImpl.AuthzResolverBlImpl) URN_VO_FROM_EMAIL(cz.metacentrum.perun.registrar.impl.RegistrarManagerImpl.URN_VO_FROM_EMAIL) FormNotExistsException(cz.metacentrum.perun.registrar.exceptions.FormNotExistsException) MailForGroupIdAdded(cz.metacentrum.perun.audit.events.MailManagerEvents.MailForGroupIdAdded) Logger(org.slf4j.Logger) AppType(cz.metacentrum.perun.registrar.model.Application.AppType) ApplicationFormItem(cz.metacentrum.perun.registrar.model.ApplicationFormItem) RegistrarManager(cz.metacentrum.perun.registrar.RegistrarManager) MimeMessage(javax.mail.internet.MimeMessage) URLEncoder(java.net.URLEncoder) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) java.io(java.io) Utils(cz.metacentrum.perun.core.impl.Utils) MailForVoIdRemoved(cz.metacentrum.perun.audit.events.MailManagerEvents.MailForVoIdRemoved) MailForGroupIdUpdated(cz.metacentrum.perun.audit.events.MailManagerEvents.MailForGroupIdUpdated) URN_GROUP_FROM_NAME_EMAIL(cz.metacentrum.perun.registrar.impl.RegistrarManagerImpl.URN_GROUP_FROM_NAME_EMAIL) InvitationSentEvent(cz.metacentrum.perun.audit.events.MailManagerEvents.InvitationSentEvent) Transactional(org.springframework.transaction.annotation.Transactional) ApplicationMailNotExistsException(cz.metacentrum.perun.registrar.exceptions.ApplicationMailNotExistsException) MailText(cz.metacentrum.perun.registrar.model.ApplicationMail.MailText) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) ApplicationMail(cz.metacentrum.perun.registrar.model.ApplicationMail)

Example 2 with ApplicationMailNotExistsException

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()));
    }
}
Also used : ApplicationForm(cz.metacentrum.perun.registrar.model.ApplicationForm) ApplicationMailNotExistsException(cz.metacentrum.perun.registrar.exceptions.ApplicationMailNotExistsException) MailForGroupIdUpdated(cz.metacentrum.perun.audit.events.MailManagerEvents.MailForGroupIdUpdated) MailText(cz.metacentrum.perun.registrar.model.ApplicationMail.MailText) MailForVoIdUpdated(cz.metacentrum.perun.audit.events.MailManagerEvents.MailForVoIdUpdated) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

MailForGroupIdUpdated (cz.metacentrum.perun.audit.events.MailManagerEvents.MailForGroupIdUpdated)2 MailForVoIdUpdated (cz.metacentrum.perun.audit.events.MailManagerEvents.MailForVoIdUpdated)2 ApplicationMailNotExistsException (cz.metacentrum.perun.registrar.exceptions.ApplicationMailNotExistsException)2 ApplicationForm (cz.metacentrum.perun.registrar.model.ApplicationForm)2 MailText (cz.metacentrum.perun.registrar.model.ApplicationMail.MailText)2 Transactional (org.springframework.transaction.annotation.Transactional)2 AuditEvent (cz.metacentrum.perun.audit.events.AuditEvent)1 InvitationSentEvent (cz.metacentrum.perun.audit.events.MailManagerEvents.InvitationSentEvent)1 MailForGroupIdAdded (cz.metacentrum.perun.audit.events.MailManagerEvents.MailForGroupIdAdded)1 MailForGroupIdRemoved (cz.metacentrum.perun.audit.events.MailManagerEvents.MailForGroupIdRemoved)1 MailForVoIdAdded (cz.metacentrum.perun.audit.events.MailManagerEvents.MailForVoIdAdded)1 MailForVoIdRemoved (cz.metacentrum.perun.audit.events.MailManagerEvents.MailForVoIdRemoved)1 MailSending (cz.metacentrum.perun.audit.events.MailManagerEvents.MailSending)1 MailSentForApplication (cz.metacentrum.perun.audit.events.MailManagerEvents.MailSentForApplication)1 cz.metacentrum.perun.core.api (cz.metacentrum.perun.core.api)1 cz.metacentrum.perun.core.api.exceptions (cz.metacentrum.perun.core.api.exceptions)1 AttributesManagerBl (cz.metacentrum.perun.core.bl.AttributesManagerBl)1 GroupsManagerBl (cz.metacentrum.perun.core.bl.GroupsManagerBl)1 MembersManagerBl (cz.metacentrum.perun.core.bl.MembersManagerBl)1 PerunBl (cz.metacentrum.perun.core.bl.PerunBl)1