Search in sources :

Example 1 with MailTemplate

use of org.apache.syncope.core.persistence.api.entity.MailTemplate in project syncope by apache.

the class NotificationDataBinderImpl method update.

@Override
public void update(final Notification notification, final NotificationTO notificationTO) {
    BeanUtils.copyProperties(notificationTO, notification, IGNORE_PROPERTIES);
    SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.RequiredValuesMissing);
    MailTemplate template = mailTemplateDAO.find(notificationTO.getTemplate());
    if (template == null) {
        sce.getElements().add("template");
    }
    notification.setTemplate(template);
    if (notification.getEvents().isEmpty()) {
        sce.getElements().add("events");
    }
    if (!notification.getStaticRecipients().isEmpty()) {
        notification.getStaticRecipients().forEach(mail -> {
            Matcher matcher = SyncopeConstants.EMAIL_PATTERN.matcher(mail);
            if (!matcher.matches()) {
                LOG.error("Invalid mail address: {}", mail);
                sce.getElements().add("staticRecipients: " + mail);
            }
        });
    }
    if (!sce.isEmpty()) {
        throw sce;
    }
    // 1. add or update all (valid) abouts from TO
    notificationTO.getAbouts().entrySet().stream().filter(entry -> StringUtils.isNotBlank(entry.getValue())).forEachOrdered((entry) -> {
        AnyType anyType = anyTypeDAO.find(entry.getKey());
        if (anyType == null) {
            LOG.debug("Invalid AnyType {} specified, ignoring...", entry.getKey());
        } else {
            AnyAbout about = notification.getAbout(anyType).orElse(null);
            if (about == null) {
                about = entityFactory.newEntity(AnyAbout.class);
                about.setAnyType(anyType);
                about.setNotification(notification);
                notification.add(about);
            }
            about.set(entry.getValue());
        }
    });
    // 2. remove all abouts not contained in the TO
    notification.getAbouts().removeIf(anyAbout -> !notificationTO.getAbouts().containsKey(anyAbout.getAnyType().getKey()));
    // 3. verify recipientAttrName
    try {
        intAttrNameParser.parse(notification.getRecipientAttrName(), AnyTypeKind.USER);
    } catch (ParseException e) {
        SyncopeClientException invalidRequest = SyncopeClientException.build(ClientExceptionType.InvalidRequest);
        invalidRequest.getElements().add(e.getMessage());
        throw invalidRequest;
    }
    if (notificationTO.getRecipientsProvider() == null) {
        notification.setRecipientsProvider(null);
    } else {
        Implementation recipientsProvider = implementationDAO.find(notificationTO.getRecipientsProvider());
        if (recipientsProvider == null) {
            LOG.debug("Invalid " + Implementation.class.getSimpleName() + " {}, ignoring...", notificationTO.getRecipientsProvider());
        } else {
            notification.setRecipientsProvider(recipientsProvider);
        }
    }
}
Also used : SyncopeConstants(org.apache.syncope.common.lib.SyncopeConstants) Logger(org.slf4j.Logger) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) LoggerFactory(org.slf4j.LoggerFactory) AnyType(org.apache.syncope.core.persistence.api.entity.AnyType) Implementation(org.apache.syncope.core.persistence.api.entity.Implementation) Autowired(org.springframework.beans.factory.annotation.Autowired) BeanUtils(org.apache.syncope.core.spring.BeanUtils) StringUtils(org.apache.commons.lang3.StringUtils) AnyTypeDAO(org.apache.syncope.core.persistence.api.dao.AnyTypeDAO) AnyTypeKind(org.apache.syncope.common.lib.types.AnyTypeKind) Notification(org.apache.syncope.core.persistence.api.entity.Notification) EntityFactory(org.apache.syncope.core.persistence.api.entity.EntityFactory) Component(org.springframework.stereotype.Component) Matcher(java.util.regex.Matcher) NotificationTO(org.apache.syncope.common.lib.to.NotificationTO) AnyAbout(org.apache.syncope.core.persistence.api.entity.AnyAbout) MailTemplate(org.apache.syncope.core.persistence.api.entity.MailTemplate) MailTemplateDAO(org.apache.syncope.core.persistence.api.dao.MailTemplateDAO) ClientExceptionType(org.apache.syncope.common.lib.types.ClientExceptionType) IntAttrNameParser(org.apache.syncope.core.provisioning.java.IntAttrNameParser) ParseException(java.text.ParseException) NotificationDataBinder(org.apache.syncope.core.provisioning.api.data.NotificationDataBinder) ImplementationDAO(org.apache.syncope.core.persistence.api.dao.ImplementationDAO) Matcher(java.util.regex.Matcher) AnyAbout(org.apache.syncope.core.persistence.api.entity.AnyAbout) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) MailTemplate(org.apache.syncope.core.persistence.api.entity.MailTemplate) ParseException(java.text.ParseException) AnyType(org.apache.syncope.core.persistence.api.entity.AnyType) Implementation(org.apache.syncope.core.persistence.api.entity.Implementation)

Example 2 with MailTemplate

use of org.apache.syncope.core.persistence.api.entity.MailTemplate in project syncope by apache.

the class MailTemplateLogic method setFormat.

@PreAuthorize("hasRole('" + StandardEntitlement.MAIL_TEMPLATE_UPDATE + "')")
public void setFormat(final String key, final MailTemplateFormat format, final String template) {
    MailTemplate mailTemplate = mailTemplateDAO.find(key);
    if (mailTemplate == null) {
        LOG.error("Could not find mail template '" + key + "'");
        throw new NotFoundException(key);
    }
    if (format == MailTemplateFormat.HTML) {
        mailTemplate.setHTMLTemplate(template);
    } else {
        mailTemplate.setTextTemplate(template);
    }
    mailTemplateDAO.save(mailTemplate);
}
Also used : MailTemplate(org.apache.syncope.core.persistence.api.entity.MailTemplate) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 3 with MailTemplate

use of org.apache.syncope.core.persistence.api.entity.MailTemplate in project syncope by apache.

the class MailTemplateLogic method create.

@PreAuthorize("hasRole('" + StandardEntitlement.MAIL_TEMPLATE_CREATE + "')")
public MailTemplateTO create(final String key) {
    if (mailTemplateDAO.find(key) != null) {
        throw new DuplicateException(key);
    }
    MailTemplate mailTemplate = entityFactory.newEntity(MailTemplate.class);
    mailTemplate.setKey(key);
    mailTemplateDAO.save(mailTemplate);
    return getMailTemplateTO(key);
}
Also used : DuplicateException(org.apache.syncope.core.persistence.api.dao.DuplicateException) MailTemplate(org.apache.syncope.core.persistence.api.entity.MailTemplate) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 4 with MailTemplate

use of org.apache.syncope.core.persistence.api.entity.MailTemplate in project syncope by apache.

the class MailTemplateLogic method getFormat.

@PreAuthorize("hasRole('" + StandardEntitlement.MAIL_TEMPLATE_READ + "')")
public String getFormat(final String key, final MailTemplateFormat format) {
    MailTemplate mailTemplate = mailTemplateDAO.find(key);
    if (mailTemplate == null) {
        LOG.error("Could not find mail template '" + key + "'");
        throw new NotFoundException(key);
    }
    String template = format == MailTemplateFormat.HTML ? mailTemplate.getHTMLTemplate() : mailTemplate.getTextTemplate();
    if (StringUtils.isBlank(template)) {
        LOG.error("Could not find mail template '" + key + "' in " + format + " format");
        throw new NotFoundException(key + " in " + format);
    }
    return template;
}
Also used : MailTemplate(org.apache.syncope.core.persistence.api.entity.MailTemplate) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 5 with MailTemplate

use of org.apache.syncope.core.persistence.api.entity.MailTemplate in project syncope by apache.

the class MailTemplateLogic method delete.

@PreAuthorize("hasRole('" + StandardEntitlement.MAIL_TEMPLATE_DELETE + "')")
public MailTemplateTO delete(final String key) {
    MailTemplate mailTemplate = mailTemplateDAO.find(key);
    if (mailTemplate == null) {
        LOG.error("Could not find mail template '" + key + "'");
        throw new NotFoundException(key);
    }
    List<Notification> notifications = notificationDAO.findByTemplate(mailTemplate);
    if (!notifications.isEmpty()) {
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InUse);
        sce.getElements().addAll(notifications.stream().map(Entity::getKey).collect(Collectors.toList()));
        throw sce;
    }
    MailTemplateTO deleted = getMailTemplateTO(key);
    mailTemplateDAO.delete(key);
    return deleted;
}
Also used : Entity(org.apache.syncope.core.persistence.api.entity.Entity) MailTemplateTO(org.apache.syncope.common.lib.to.MailTemplateTO) MailTemplate(org.apache.syncope.core.persistence.api.entity.MailTemplate) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) Notification(org.apache.syncope.core.persistence.api.entity.Notification) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

MailTemplate (org.apache.syncope.core.persistence.api.entity.MailTemplate)8 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 NotFoundException (org.apache.syncope.core.persistence.api.dao.NotFoundException)3 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)2 Notification (org.apache.syncope.core.persistence.api.entity.Notification)2 AbstractTest (org.apache.syncope.core.persistence.jpa.AbstractTest)2 Test (org.junit.jupiter.api.Test)2 ParseException (java.text.ParseException)1 Matcher (java.util.regex.Matcher)1 StringUtils (org.apache.commons.lang3.StringUtils)1 SyncopeConstants (org.apache.syncope.common.lib.SyncopeConstants)1 MailTemplateTO (org.apache.syncope.common.lib.to.MailTemplateTO)1 NotificationTO (org.apache.syncope.common.lib.to.NotificationTO)1 AnyTypeKind (org.apache.syncope.common.lib.types.AnyTypeKind)1 ClientExceptionType (org.apache.syncope.common.lib.types.ClientExceptionType)1 AnyTypeDAO (org.apache.syncope.core.persistence.api.dao.AnyTypeDAO)1 DuplicateException (org.apache.syncope.core.persistence.api.dao.DuplicateException)1 ImplementationDAO (org.apache.syncope.core.persistence.api.dao.ImplementationDAO)1 MailTemplateDAO (org.apache.syncope.core.persistence.api.dao.MailTemplateDAO)1 AnyAbout (org.apache.syncope.core.persistence.api.entity.AnyAbout)1