Search in sources :

Example 1 with PerunNotifEmailMessageToSendDto

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;
}
Also used : PerunNotifTemplate(cz.metacentrum.perun.notif.entities.PerunNotifTemplate) GroupNotExistsException(cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException) ArrayList(java.util.ArrayList) PerunNotifReceiver(cz.metacentrum.perun.notif.entities.PerunNotifReceiver) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) PerunNotifMessageDto(cz.metacentrum.perun.notif.dto.PerunNotifMessageDto) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) GroupNotExistsException(cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException) PoolMessage(cz.metacentrum.perun.notif.dto.PoolMessage) PerunNotifEmailMessageToSendDto(cz.metacentrum.perun.notif.dto.PerunNotifEmailMessageToSendDto) HashSet(java.util.HashSet)

Example 2 with PerunNotifEmailMessageToSendDto

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.");
}
Also used : PerunNotifPlainMessage(cz.metacentrum.perun.notif.mail.PerunNotifPlainMessage) PerunNotifEmailMessageToSendDto(cz.metacentrum.perun.notif.dto.PerunNotifEmailMessageToSendDto)

Example 3 with PerunNotifEmailMessageToSendDto

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;
}
Also used : UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) PerunNotifReceiver(cz.metacentrum.perun.notif.entities.PerunNotifReceiver) PerunNotifMessageDto(cz.metacentrum.perun.notif.dto.PerunNotifMessageDto) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) PoolMessage(cz.metacentrum.perun.notif.dto.PoolMessage) PerunNotifEmailMessageToSendDto(cz.metacentrum.perun.notif.dto.PerunNotifEmailMessageToSendDto)

Aggregations

PerunNotifEmailMessageToSendDto (cz.metacentrum.perun.notif.dto.PerunNotifEmailMessageToSendDto)3 PerunNotifMessageDto (cz.metacentrum.perun.notif.dto.PerunNotifMessageDto)2 PoolMessage (cz.metacentrum.perun.notif.dto.PoolMessage)2 PerunNotifReceiver (cz.metacentrum.perun.notif.entities.PerunNotifReceiver)2 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)1 GroupNotExistsException (cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException)1 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)1 UserNotExistsException (cz.metacentrum.perun.core.api.exceptions.UserNotExistsException)1 PerunNotifTemplate (cz.metacentrum.perun.notif.entities.PerunNotifTemplate)1 PerunNotifPlainMessage (cz.metacentrum.perun.notif.mail.PerunNotifPlainMessage)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1