Search in sources :

Example 1 with PerunNotifSender

use of cz.metacentrum.perun.notif.senders.PerunNotifSender in project perun by CESNET.

the class PerunNotifTemplateManagerImpl method processPoolMessages.

@Override
public Set<Integer> processPoolMessages(Integer templateId, List<PoolMessage> notifMessages) {
    if (notifMessages == null || notifMessages.isEmpty() || templateId == null) {
        return null;
    }
    List<PerunNotifMessageDto> messageDtoList = new ArrayList<PerunNotifMessageDto>();
    logger.info("Starting to process messages for template with id:" + templateId);
    PerunNotifTemplate template = allTemplatesById.get(templateId);
    switch(template.getNotifyTrigger()) {
        case ALL_REGEX_IDS:
            for (PoolMessage dto : notifMessages) {
                // Test for all regexIds present
                logger.info("Starting to process dto for templateId: " + templateId + " and keyAttributes: " + dto.getKeyAttributes());
                Set<Integer> foundRegexIds = new HashSet<Integer>();
                for (PerunNotifPoolMessage poolMessage : dto.getList()) {
                    foundRegexIds.add(poolMessage.getRegexId());
                }
                boolean allRegexes = true;
                for (PerunNotifRegex regex : template.getMatchingRegexs()) {
                    if (!foundRegexIds.contains(regex.getId())) {
                        logger.info("Not all regexes found for templateId: " + templateId + ", and keyAttributes: " + dto.getKeyAttributes() + " missing:" + regex.getId());
                        allRegexes = false;
                    }
                }
                if (allRegexes) {
                    logger.info("All regexes found for templateId: " + templateId + " and keyAttribute: " + dto.getKeyAttributes() + " starting to create message.");
                    try {
                        messageDtoList.addAll(createMessageToSend(template, dto));
                    } catch (Exception ex) {
                        logger.error("Error during creating message to send.", ex);
                    }
                }
            }
            break;
        //      - need to ensure delivery of all the msgs required by the template message
        case STREAM:
            DateTime now = new DateTime();
            DateTime oldestTime = new DateTime(now.getMillis() - template.getOldestMessageTime());
            DateTime youngestTime = new DateTime(now.getMillis() - template.getYoungestMessageTime());
            for (PoolMessage parentDto : notifMessages) {
                List<PerunNotifPoolMessage> poolMessages = parentDto.getList();
                if (poolMessages != null) {
                    // Test for oldest message first message in list is oldest,
                    // messages
                    // are sorted from sql query
                    PerunNotifPoolMessage oldestPoolMessage = poolMessages.get(0);
                    if (oldestPoolMessage.getCreated().compareTo(oldestTime) < 0) {
                        // we have and send it
                        try {
                            logger.debug("Oldest message is older than oldest time for template id " + template.getId() + " message will be sent.");
                            messageDtoList.addAll(createMessageToSend(template, parentDto));
                        } catch (Exception ex) {
                            logger.error("Error during creating of messages to send.", ex);
                        }
                    } else {
                        // We test youngest message so we now nothing new will
                        // propably come in close future
                        PerunNotifPoolMessage youngestPoolMessage = poolMessages.get(poolMessages.size() - 1);
                        if (youngestPoolMessage.getCreated().compareTo(youngestTime) < 0) {
                            // Youngest message is older
                            try {
                                logger.debug("Youngest message is older than youngest time for template id " + template.getId() + " message will be sent.");
                                messageDtoList.addAll(createMessageToSend(template, parentDto));
                            } catch (Exception ex) {
                                logger.error("Error during creating of messages to send.", ex);
                            }
                        } else {
                            Period oldestPeriod = new Period(oldestPoolMessage.getCreated().getMillis() - oldestTime.getMillis());
                            Period youngestPeriod = new Period(youngestPoolMessage.getCreated().getMillis() - youngestTime.getMillis());
                            Period period = oldestPeriod.getMillis() < youngestPeriod.getMillis() ? oldestPeriod : youngestPeriod;
                            String remainingTime = "";
                            if (period.getDays() > 0) {
                                remainingTime += period.getDays() + " days ";
                            }
                            if (period.getHours() > 0) {
                                remainingTime += period.getHours() + " hours ";
                            }
                            if (period.getMinutes() > 0) {
                                remainingTime += period.getMinutes() + " minutes ";
                            }
                            if (period.getSeconds() > 0) {
                                remainingTime += period.getSeconds() + " sec.";
                            }
                            logger.debug("The time limits for messages are greater that messages creation time for template id " + template.getId() + ", the message will not be sent yet. " + "Provided no messages is created, the notification will be sent in " + remainingTime);
                        }
                    }
                }
            }
            break;
    }
    Map<PerunNotifTypeOfReceiver, List<PerunNotifMessageDto>> messagesToSend = new HashMap<PerunNotifTypeOfReceiver, List<PerunNotifMessageDto>>();
    Set<Integer> processedIds = new HashSet<Integer>();
    for (PerunNotifMessageDto messageToSend : messageDtoList) {
        List<PerunNotifMessageDto> list = messagesToSend.get(messageToSend.getReceiver().getTypeOfReceiver());
        if (list == null) {
            list = new ArrayList<PerunNotifMessageDto>();
            list.add(messageToSend);
            messagesToSend.put(messageToSend.getReceiver().getTypeOfReceiver(), list);
        } else {
            list.add(messageToSend);
        }
    }
    for (PerunNotifTypeOfReceiver typeOfReceiver : messagesToSend.keySet()) {
        PerunNotifSender handlingSender = null;
        for (PerunNotifSender sender : notifSenders) {
            if (sender.canHandle(typeOfReceiver)) {
                handlingSender = sender;
            }
        }
        if (handlingSender != null) {
            logger.debug("Found handling sender: {}", handlingSender.toString());
            processedIds.addAll(handlingSender.send(messagesToSend.get(typeOfReceiver)));
            logger.debug("Messages send by sender: {}", handlingSender.toString());
        } else {
            logger.error("No handling sender found for: {}", typeOfReceiver);
        }
    }
    return processedIds;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Period(org.joda.time.Period) PerunNotifSender(cz.metacentrum.perun.notif.senders.PerunNotifSender) PerunNotifMessageDto(cz.metacentrum.perun.notif.dto.PerunNotifMessageDto) NotifReceiverAlreadyExistsException(cz.metacentrum.perun.notif.exceptions.NotifReceiverAlreadyExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) TemplateException(freemarker.template.TemplateException) NotifTemplateMessageAlreadyExistsException(cz.metacentrum.perun.notif.exceptions.NotifTemplateMessageAlreadyExistsException) TemplateMessageSyntaxErrorException(cz.metacentrum.perun.notif.exceptions.TemplateMessageSyntaxErrorException) InvalidReferenceException(freemarker.core.InvalidReferenceException) NotExistsException(cz.metacentrum.perun.notif.exceptions.NotExistsException) IOException(java.io.IOException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) FileNotFoundException(java.io.FileNotFoundException) ParseException(freemarker.core.ParseException) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) PerunException(cz.metacentrum.perun.core.api.exceptions.PerunException) DateTime(org.joda.time.DateTime) PoolMessage(cz.metacentrum.perun.notif.dto.PoolMessage) PerunNotifTypeOfReceiver(cz.metacentrum.perun.notif.enums.PerunNotifTypeOfReceiver)

Aggregations

AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)1 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)1 PerunException (cz.metacentrum.perun.core.api.exceptions.PerunException)1 UserNotExistsException (cz.metacentrum.perun.core.api.exceptions.UserNotExistsException)1 PerunNotifMessageDto (cz.metacentrum.perun.notif.dto.PerunNotifMessageDto)1 PoolMessage (cz.metacentrum.perun.notif.dto.PoolMessage)1 PerunNotifTypeOfReceiver (cz.metacentrum.perun.notif.enums.PerunNotifTypeOfReceiver)1 NotExistsException (cz.metacentrum.perun.notif.exceptions.NotExistsException)1 NotifReceiverAlreadyExistsException (cz.metacentrum.perun.notif.exceptions.NotifReceiverAlreadyExistsException)1 NotifTemplateMessageAlreadyExistsException (cz.metacentrum.perun.notif.exceptions.NotifTemplateMessageAlreadyExistsException)1 TemplateMessageSyntaxErrorException (cz.metacentrum.perun.notif.exceptions.TemplateMessageSyntaxErrorException)1 PerunNotifSender (cz.metacentrum.perun.notif.senders.PerunNotifSender)1 InvalidReferenceException (freemarker.core.InvalidReferenceException)1 ParseException (freemarker.core.ParseException)1 TemplateException (freemarker.template.TemplateException)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 DateTime (org.joda.time.DateTime)1 Period (org.joda.time.Period)1