use of cz.metacentrum.perun.notif.dto.PerunNotifEmailMessageToSendDto in project perun by CESNET.
the class PerunNotifEmailGroupSender method send.
@Override
public Set<Integer> send(List<PerunNotifMessageDto> dtosToSend) {
Set<Integer> usedPoolIds = new HashSet<Integer>();
List<PerunNotifEmailMessageToSendDto> messagesToSend = new ArrayList<PerunNotifEmailMessageToSendDto>();
for (PerunNotifMessageDto messageDto : dtosToSend) {
PoolMessage dto = messageDto.getPoolMessage();
PerunNotifTemplate template = messageDto.getTemplate();
PerunNotifReceiver receiver = messageDto.getReceiver();
try {
String groupSender = dto.getKeyAttributes().get(template.getSender());
if (groupSender == null || groupSender.isEmpty()) {
groupSender = template.getSender();
}
logger.debug("Calculated sender : {}", groupSender);
Integer groupId = Integer.valueOf(receiver.getTarget());
Group group = perun.getGroupsManagerBl().getGroupById(session, groupId);
List<Member> groupMembers = perun.getGroupsManagerBl().getGroupMembers(session, group);
if (groupMembers != null) {
for (Member member : groupMembers) {
try {
PerunNotifEmailMessageToSendDto memberEmailDto = new PerunNotifEmailMessageToSendDto();
memberEmailDto.setMessage(messageDto.getMessageToSend());
memberEmailDto.setSubject(messageDto.getSubject());
memberEmailDto.setReceiver((String) perun.getAttributesManagerBl().getAttribute(session, perun.getUsersManager().getUserByMember(session, member), "urn:perun:user:attribute-def:def:preferredMail").getValue());
memberEmailDto.setSender(groupSender);
messagesToSend.add(memberEmailDto);
} catch (Exception ex) {
logger.error("PreferredEmail cannot be retrieved, userId: {}", member.getUserId(), ex);
}
}
}
usedPoolIds.addAll(messageDto.getUsedPoolIds());
} catch (NumberFormatException ex) {
logger.error("GroupId cannot be parsed: {}", receiver.getTarget());
} catch (GroupNotExistsException ex) {
logger.error("Group with id: {} does not exists.", receiver.getTarget());
} catch (InternalErrorException ex) {
logger.error("Error during processing messageDto.", ex);
}
}
perunNotifEmailManager.sendMessages(messagesToSend);
return usedPoolIds;
}
use of cz.metacentrum.perun.notif.dto.PerunNotifEmailMessageToSendDto in project perun by CESNET.
the class PerunNotifEmailManagerImpl method sendMessages.
@Override
public void sendMessages(List<PerunNotifEmailMessageToSendDto> list) {
List<PerunNotifPlainMessage> emailList = new ArrayList<PerunNotifPlainMessage>();
for (PerunNotifEmailMessageToSendDto emailMessage : list) {
PerunNotifPlainMessage message = new PerunNotifPlainMessage(emailMessage.getSender(), null, emailMessage.getSubject(), emailMessage.getMessage());
message.addRecipientTo(emailMessage.getReceiver());
emailList.add(message);
}
sendEmailsInBatch(emailList);
logger.info("Message successfully sended.");
}
use of cz.metacentrum.perun.notif.dto.PerunNotifEmailMessageToSendDto in project perun by CESNET.
the class PerunNotifEmailUserSender method send.
@Override
public Set<Integer> send(List<PerunNotifMessageDto> dtosToSend) {
Set<Integer> usedPools = new HashSet<Integer>();
List<PerunNotifEmailMessageToSendDto> messagesToSend = new ArrayList<PerunNotifEmailMessageToSendDto>();
for (PerunNotifMessageDto messageDto : dtosToSend) {
PerunNotifReceiver receiver = messageDto.getReceiver();
PoolMessage dto = messageDto.getPoolMessage();
logger.debug("Creating email for user, receiver: {}", receiver.getId());
PerunNotifEmailMessageToSendDto emailDto = new PerunNotifEmailMessageToSendDto();
emailDto.setMessage(messageDto.getMessageToSend());
emailDto.setSubject(messageDto.getSubject());
usedPools.addAll(messageDto.getUsedPoolIds());
String sender = messageDto.getSender();
emailDto.setSender(sender);
logger.debug("Calculated sender for receiver: {}, sender: {}", Arrays.asList(receiver.getId(), sender));
String myReceiverId = dto.getKeyAttributes().get(receiver.getTarget());
if (myReceiverId == null || myReceiverId.isEmpty()) {
//Can be set one static account
emailDto.setReceiver(receiver.getTarget());
} else {
//We try to resolve id
Integer id = null;
try {
id = Integer.valueOf(myReceiverId);
} catch (NumberFormatException ex) {
logger.error("Cannot resolve id: {}, error: {}", Arrays.asList(id, ex.getMessage()));
logger.debug("ST:", ex);
}
if (id != null) {
try {
User user = perun.getUsersManagerBl().getUserById(session, id);
Attribute emailAttribute = perun.getAttributesManagerBl().getAttribute(session, user, "urn:perun:user:attribute-def:def:preferredMail");
if (emailAttribute != null && StringUtils.hasText(emailAttribute.toString())) {
emailDto.setReceiver((String) emailAttribute.getValue());
}
} catch (UserNotExistsException ex) {
logger.error("Cannot found user with id: {}, ex: {}", Arrays.asList(id, ex.getMessage()));
logger.debug("ST:", ex);
} catch (AttributeNotExistsException ex) {
logger.warn("Cannot found email for user with id: {}, ex: {}", Arrays.asList(id, ex.getMessage()));
logger.debug("ST:", ex);
} catch (Exception ex) {
logger.error("Error during user email recognition, ex: {}", ex.getMessage());
logger.debug("ST:", ex);
}
}
}
messagesToSend.add(emailDto);
}
perunNotifEmailManager.sendMessages(messagesToSend);
return usedPools;
}
Aggregations