use of cz.metacentrum.perun.notif.enums.PerunNotifTypeOfReceiver 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;
}
Aggregations