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);
}
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);
}
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;
}
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;
}
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());
}
Aggregations