Search in sources :

Example 1 with IdmMessageDto

use of eu.bcvsolutions.idm.core.notification.api.dto.IdmMessageDto in project CzechIdMng by bcvsolutions.

the class IdentityMonitoredFieldsProcessor method process.

@Override
public EventResult<IdmIdentityDto> process(EntityEvent<IdmIdentityDto> event) {
    List<String> fields = getCommaSeparatedValues((String) this.getConfigurationMap().get(PROPERTY_MONITORED_FIELDS));
    String recipientsRole = (String) this.getConfigurationMap().get(PROPERTY_RECIPIENTS_ROLE);
    if (CollectionUtils.isEmpty(fields)) {
        LOG.debug("None monitored fields found in configuration.");
        return new DefaultEventResult<>(event, this);
    }
    List<IdmIdentityDto> recipients = service.findAllByRoleName(recipientsRole);
    if (CollectionUtils.isEmpty(recipients)) {
        LOG.debug("None recievers found in configuration.");
        return new DefaultEventResult<>(event, this);
    }
    IdmIdentityDto newIdentity = event.getContent();
    IdmIdentityDto identity = event.getOriginalSource();
    List<ChangedField> changedFields = new ArrayList<>();
    // Check monitored fields on some changes
    fields.forEach(field -> {
        try {
            Object value = EntityUtils.getEntityValue(identity, field);
            Object newValue = EntityUtils.getEntityValue(newIdentity, field);
            if (value == null && newValue == null) {
                return;
            }
            if (value != null && !value.equals(newValue)) {
                changedFields.add(new ChangedField(field, value.toString(), newValue == null ? null : newValue.toString()));
                return;
            }
            if (newValue != null && !newValue.equals(value)) {
                changedFields.add(new ChangedField(field, value == null ? null : value.toString(), newValue.toString()));
                return;
            }
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | IntrospectionException e) {
            throw new ResultCodeException(CoreResultCode.BAD_REQUEST, e);
        }
    });
    if (!changedFields.isEmpty()) {
        IdmMessageDto message = new IdmMessageDto.Builder(NotificationLevel.WARNING).addParameter("fullName", service.getNiceLabel(identity)).addParameter("identity", identity).addParameter("changedFields", changedFields).addParameter("url", configurationService.getFrontendUrl(String.format("identity/%s/profile", identity.getId()))).build();
        notificationManager.send(String.format("core:%s", TOPIC), message, recipients);
    }
    return new DefaultEventResult<>(event, this);
}
Also used : IdmMessageDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmMessageDto) ArrayList(java.util.ArrayList) IntrospectionException(java.beans.IntrospectionException) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DefaultEventResult(eu.bcvsolutions.idm.core.api.event.DefaultEventResult) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)

Example 2 with IdmMessageDto

use of eu.bcvsolutions.idm.core.notification.api.dto.IdmMessageDto in project CzechIdMng by bcvsolutions.

the class AbstractNotificationSender method cloneMessage.

/**
 * Clone notification message. Method just clone {@link IdmMessageDto}, or if object {@link IdmMessageDto}
 * contain {@link IdmNotificationTemplateDto} it will be generate new message from templates and parameters if any.
 * Clone message from template will not contain plain text of object {@link GuardedString}, just asterisk.
 * For show {@link GuardedString} in plain text use method for generate from template.
 *
 * @param notification
 * @return
 */
protected IdmMessageDto cloneMessage(IdmNotificationDto notification) {
    IdmMessageDto message = notification.getMessage();
    if (message.getTemplate() != null) {
        return this.getMessage(notification, false);
    }
    // 
    message = new IdmMessageDto(message);
    // build message, message may contain some parameters
    return this.notificationTemplateService.buildMessage(message, false);
}
Also used : IdmMessageDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmMessageDto)

Example 3 with IdmMessageDto

use of eu.bcvsolutions.idm.core.notification.api.dto.IdmMessageDto in project CzechIdMng by bcvsolutions.

the class AbstractNotificationSender method send.

@Override
@Transactional
public List<N> send(String topic, IdmMessageDto message, IdmIdentityDto identitySender, List<IdmIdentityDto> recipients) {
    Assert.notNull(message, "Message is required");
    List<N> sendMessages = new ArrayList<>();
    // 
    List<IdmNotificationRecipientDto> notificationRecipients = new ArrayList<>();
    recipients.forEach(recipient -> {
        notificationRecipients.add(new IdmNotificationRecipientDto(recipient.getId()));
    });
    // 
    List<IdmNotificationLogDto> notifications = notificationTemplateService.prepareNotifications(topic, message);
    // 
    if (notifications.isEmpty()) {
        LOG.info("Notification for [topic:{}] not found. Any message will not be sent.", topic);
        // no notifications found
        return sendMessages;
    }
    // iterate over all prepared notifications, set recipients and send them
    for (IdmNotificationLogDto notification : notifications) {
        final IdmMessageDto notificationMessage = notification.getMessage();
        if (notificationMessage.getHtmlMessage() == null && notificationMessage.getSubject() == null && notificationMessage.getTextMessage() == null && notificationMessage.getModel() == null) {
            LOG.error("Notification has empty template and message. Message will not be sent! [topic:{}]", topic);
            continue;
        }
        notification.setRecipients(notificationRecipients);
        notification.setIdentitySender(identitySender == null ? null : identitySender.getId());
        // 
        sendMessages.add(send(notification));
    }
    return sendMessages;
}
Also used : IdmNotificationLogDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationLogDto) IdmMessageDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmMessageDto) ArrayList(java.util.ArrayList) IdmNotificationRecipientDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationRecipientDto) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with IdmMessageDto

use of eu.bcvsolutions.idm.core.notification.api.dto.IdmMessageDto in project CzechIdMng by bcvsolutions.

the class DefaultIdmNotificationTemplateService method buildMessage.

@Override
public IdmMessageDto buildMessage(IdmMessageDto message, boolean showGuardedString) {
    StringWriter bodyHtml = new StringWriter();
    StringWriter bodyText = new StringWriter();
    StringWriter subject = new StringWriter();
    IdmNotificationTemplateDto template = message.getTemplate() == null ? null : get(message.getTemplate().getId());
    // 
    if (template == null) {
        return message;
    }
    // get parameters from messages
    Map<String, Object> model = message.getParameters();
    // 
    // create copy of parameters
    Map<String, Object> parameters = new HashMap<>();
    // templates, but no parameters
    if (model != null) {
        for (Entry<String, Object> entry : model.entrySet()) {
            if (entry.getValue() instanceof GuardedString && showGuardedString) {
                parameters.put(entry.getKey(), ((GuardedString) entry.getValue()).asString());
            } else if (entry.getValue() instanceof GuardedString) {
                parameters.put(entry.getKey(), ((GuardedString) entry.getValue()).toString());
            } else {
                parameters.put(entry.getKey(), entry.getValue());
            }
        }
    }
    // prepare html, text, subject
    String html = template.getBodyHtml();
    String text = template.getBodyText();
    String subjectString = template.getSubject();
    // Same parameters for all (html, txt, subject)
    VelocityContext velocityContext = getContext(parameters);
    // include some tools from Apache velocity -
    // http://velocity.apache.org/tools/devel/generic.html#tools
    velocityContext.put("display", new DisplayTool());
    velocityContext.put("date", new DateTool());
    // 
    velocityEngine.evaluate(velocityContext, bodyHtml, template.getCode(), html);
    velocityEngine.evaluate(velocityContext, bodyText, template.getCode(), text);
    velocityEngine.evaluate(velocityContext, subject, template.getCode(), subjectString);
    // 
    IdmMessageDto newMessage;
    // if is set model from message build with them
    if (message.getModel() != null) {
        newMessage = new IdmMessageDto.Builder().setHtmlMessage(bodyHtml.toString()).setTextMessage(bodyText.toString()).setSubject(StringUtils.isNotEmpty(subject.toString()) ? subject.toString() : message.getModel().getStatusEnum()).setLevel(// level get from old message
        message.getLevel()).setTemplate(template).setParameters(model).setModel(message.getModel()).build();
    } else {
        // Build IdmMessage
        newMessage = new IdmMessageDto.Builder().setHtmlMessage(bodyHtml.toString()).setTextMessage(bodyText.toString()).setSubject(subject.toString()).setLevel(// level
        message.getLevel()).setTemplate(template).setParameters(model).build();
    }
    // 
    return newMessage;
}
Also used : DisplayTool(org.apache.velocity.tools.generic.DisplayTool) HashMap(java.util.HashMap) VelocityContext(org.apache.velocity.VelocityContext) IdmMessageDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmMessageDto) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) IdmNotificationTemplateDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationTemplateDto) DateTool(org.apache.velocity.tools.generic.DateTool) StringWriter(java.io.StringWriter)

Example 5 with IdmMessageDto

use of eu.bcvsolutions.idm.core.notification.api.dto.IdmMessageDto in project CzechIdMng by bcvsolutions.

the class IdmMessageDtoUnitTest method testModelOveloadMessageAndSubject.

@Test
public void testModelOveloadMessageAndSubject() {
    ResultModel model = new DefaultResultModel(CoreResultCode.INTERNAL_SERVER_ERROR);
    IdmMessageDto message = new IdmMessageDto.Builder().setModel(model).setMessage(PARAMETER_TEXT).setSubject(PARAMETER_SUBJECT).build();
    Assert.assertEquals(PARAMETER_SUBJECT, message.getSubject());
    Assert.assertEquals(PARAMETER_TEXT, message.getTextMessage());
    Assert.assertEquals(PARAMETER_TEXT, message.getHtmlMessage());
    Assert.assertEquals(NotificationLevel.ERROR, message.getLevel());
}
Also used : DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) IdmMessageDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmMessageDto) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) ResultModel(eu.bcvsolutions.idm.core.api.dto.ResultModel) AbstractUnitTest(eu.bcvsolutions.idm.test.api.AbstractUnitTest) Test(org.junit.Test)

Aggregations

IdmMessageDto (eu.bcvsolutions.idm.core.notification.api.dto.IdmMessageDto)22 Test (org.junit.Test)14 AbstractUnitTest (eu.bcvsolutions.idm.test.api.AbstractUnitTest)10 DefaultResultModel (eu.bcvsolutions.idm.core.api.dto.DefaultResultModel)6 ResultModel (eu.bcvsolutions.idm.core.api.dto.ResultModel)6 IdmNotificationTemplateDto (eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationTemplateDto)6 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)5 IdmNotificationLogDto (eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationLogDto)5 IdmNotificationDto (eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationDto)3 IdmNotificationFilter (eu.bcvsolutions.idm.core.notification.api.dto.filter.IdmNotificationFilter)3 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)3 ArrayList (java.util.ArrayList)3 IdmNotificationRecipientDto (eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationRecipientDto)2 StringWriter (java.io.StringWriter)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 Lists (com.google.common.collect.Lists)1 InitTestData (eu.bcvsolutions.idm.InitTestData)1 DefaultEventResult (eu.bcvsolutions.idm.core.api.event.DefaultEventResult)1 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)1