Search in sources :

Example 1 with MailText

use of cz.metacentrum.perun.registrar.model.ApplicationMail.MailText in project perun by CESNET.

the class MailManagerImpl method getApplicationMails.

@Override
public List<ApplicationMail> getApplicationMails(PerunSession sess, ApplicationForm form) throws PerunException {
    List<ApplicationMail> mails = new ArrayList<ApplicationMail>();
    mails = jdbc.query(MAILS_SELECT_BY_FORM_ID, new RowMapper<ApplicationMail>() {

        @Override
        public ApplicationMail mapRow(ResultSet rs, int arg1) throws SQLException {
            return new ApplicationMail(rs.getInt("id"), AppType.valueOf(rs.getString("app_type")), rs.getInt("form_id"), MailType.valueOf(rs.getString("mail_type")), rs.getBoolean("send"));
        }
    }, form.getId());
    for (ApplicationMail mail : mails) {
        List<MailText> texts = new ArrayList<MailText>();
        texts = jdbc.query(MAIL_TEXTS_SELECT_BY_MAIL_ID, new RowMapper<MailText>() {

            @Override
            public MailText mapRow(ResultSet rs, int arg1) throws SQLException {
                return new MailText(new Locale(rs.getString("locale")), rs.getString("subject"), rs.getString("text"));
            }
        }, mail.getId());
        for (MailText text : texts) {
            // fil localized messages
            mail.getMessage().put(text.getLocale(), text);
        }
    }
    return mails;
}
Also used : ResultSet(java.sql.ResultSet) MailText(cz.metacentrum.perun.registrar.model.ApplicationMail.MailText) ApplicationMail(cz.metacentrum.perun.registrar.model.ApplicationMail) RowMapper(org.springframework.jdbc.core.RowMapper)

Example 2 with MailText

use of cz.metacentrum.perun.registrar.model.ApplicationMail.MailText in project perun by CESNET.

the class MailManagerImpl method getMailTextInvitation.

private String getMailTextInvitation(ApplicationMail mail, Locale lang, Vo vo, Group group, User user, String name) {
    MailText mt = mail.getMessage(lang);
    String mailText = EMPTY_STRING;
    if (mt.getText() != null && !mt.getText().isEmpty()) {
        mailText = mt.getText();
        mailText = substituteCommonStringsForInvite(vo, group, user, name, mailText);
    }
    return mailText;
}
Also used : MailText(cz.metacentrum.perun.registrar.model.ApplicationMail.MailText)

Example 3 with MailText

use of cz.metacentrum.perun.registrar.model.ApplicationMail.MailText 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 4 with MailText

use of cz.metacentrum.perun.registrar.model.ApplicationMail.MailText 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)

Example 5 with MailText

use of cz.metacentrum.perun.registrar.model.ApplicationMail.MailText in project perun by CESNET.

the class MailManagerImpl method getMailSubject.

private String getMailSubject(ApplicationMail mail, Locale lang, Application app, List<ApplicationFormItemData> data, String reason, List<Exception> exceptions) {
    MailText mt = mail.getMessage(lang);
    String mailSubject = EMPTY_STRING;
    if (mt.getText() != null && !mt.getText().isEmpty()) {
        mailSubject = mt.getSubject();
        mailSubject = substituteCommonStrings(app, data, mailSubject, reason, exceptions);
    }
    // substitute common strings
    return mailSubject;
}
Also used : MailText(cz.metacentrum.perun.registrar.model.ApplicationMail.MailText)

Aggregations

MailText (cz.metacentrum.perun.registrar.model.ApplicationMail.MailText)9 ApplicationForm (cz.metacentrum.perun.registrar.model.ApplicationForm)4 ApplicationMail (cz.metacentrum.perun.registrar.model.ApplicationMail)4 MailForGroupIdUpdated (cz.metacentrum.perun.audit.events.MailManagerEvents.MailForGroupIdUpdated)3 MailForVoIdUpdated (cz.metacentrum.perun.audit.events.MailManagerEvents.MailForVoIdUpdated)3 ApplicationMailNotExistsException (cz.metacentrum.perun.registrar.exceptions.ApplicationMailNotExistsException)3 AuditEvent (cz.metacentrum.perun.audit.events.AuditEvent)2 InvitationSentEvent (cz.metacentrum.perun.audit.events.MailManagerEvents.InvitationSentEvent)2 MailForGroupIdAdded (cz.metacentrum.perun.audit.events.MailManagerEvents.MailForGroupIdAdded)2 MailForGroupIdRemoved (cz.metacentrum.perun.audit.events.MailManagerEvents.MailForGroupIdRemoved)2 MailForVoIdAdded (cz.metacentrum.perun.audit.events.MailManagerEvents.MailForVoIdAdded)2 MailForVoIdRemoved (cz.metacentrum.perun.audit.events.MailManagerEvents.MailForVoIdRemoved)2 MailSending (cz.metacentrum.perun.audit.events.MailManagerEvents.MailSending)2 MailSentForApplication (cz.metacentrum.perun.audit.events.MailManagerEvents.MailSentForApplication)2 cz.metacentrum.perun.core.api (cz.metacentrum.perun.core.api)2 cz.metacentrum.perun.core.api.exceptions (cz.metacentrum.perun.core.api.exceptions)2 AttributesManagerBl (cz.metacentrum.perun.core.bl.AttributesManagerBl)2 GroupsManagerBl (cz.metacentrum.perun.core.bl.GroupsManagerBl)2 MembersManagerBl (cz.metacentrum.perun.core.bl.MembersManagerBl)2 PerunBl (cz.metacentrum.perun.core.bl.PerunBl)2