use of cz.metacentrum.perun.notif.entities.PerunNotifTemplate in project perun by CESNET.
the class PerunNotifPoolMessageManagerImpl method createPerunNotifPoolMessagesForTemplates.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public List<PerunNotifPoolMessage> createPerunNotifPoolMessagesForTemplates(Map<Integer, List<PerunNotifTemplate>> templatesWithRegexIds, PerunNotifAuditMessage perunAuditMessage) {
List<PerunNotifPoolMessage> result = new ArrayList<PerunNotifPoolMessage>();
// We parse recieved message from auditer to get all objects
// List<PerunBean> retrievedObjects = ParseUtils.parseMessage(perunMessage.getMessage());
List<PerunBean> retrievedObjects = AuditParser.parseLog(perunAuditMessage.getMessage());
// Objects which can be later used when proccessing managerCalls
Map<String, Object> usableObjects = parseRetrievedObjects(retrievedObjects);
usableObjects.put(parseClassName(PerunSession.class.toString()), session);
Map<String, String> retrievedProperties = new HashMap<String, String>();
for (Integer regexId : templatesWithRegexIds.keySet()) {
// We list through every regexId recognized in message
List<PerunNotifTemplate> templates = templatesWithRegexIds.get(regexId);
if ((templates != null) && (!templates.isEmpty())) {
for (PerunNotifTemplate template : templates) {
// We list through every template which uses regexId
Map<String, String> retrievedPrimaryProperties = new HashMap<String, String>();
Set<String> classNames = new HashSet<String>();
classNames.addAll(template.getPrimaryProperties().keySet());
for (String className : classNames) {
if (className != null && !className.equals(METHOD_CLASSNAME)) {
// Listing through all classNames
try {
logger.debug("Resolving class with name: " + className);
Class resolvedClass = Class.forName(className);
Object matchingObject = null;
for (Object myObject : retrievedObjects) {
if (resolvedClass.isAssignableFrom(myObject.getClass())) {
matchingObject = myObject;
logger.debug("Parsed object: " + matchingObject.toString() + " from message recognized for class: " + className);
}
}
if (matchingObject != null) {
if (template.getPrimaryProperties().get(className) != null) {
List<String> methods = template.getPrimaryProperties().get(className);
retrieveProperties(methods, className, retrievedPrimaryProperties, retrievedProperties, matchingObject);
}
} else {
logger.error("No object recognized in objects from message for class: " + className);
}
} catch (ClassNotFoundException ex) {
logger.error("Class from template cannot be resolved: " + className, ex);
}
}
}
if (template.getPrimaryProperties().get(METHOD_CLASSNAME) != null) {
for (String methodName : template.getPrimaryProperties().get(METHOD_CLASSNAME)) {
String value = retrieveMethodProperty(retrievedProperties, methodName, usableObjects);
retrievedPrimaryProperties.put(methodName, value);
}
}
if (retrievedPrimaryProperties != null && !retrievedPrimaryProperties.isEmpty()) {
PerunNotifPoolMessage poolMessage = new PerunNotifPoolMessage();
poolMessage.setCreated(Instant.now());
poolMessage.setKeyAttributes(retrievedPrimaryProperties);
poolMessage.setRegexId(regexId);
poolMessage.setTemplateId(template.getId());
poolMessage.setNotifMessage(perunAuditMessage.getMessage());
result.add(poolMessage);
}
}
} else {
logger.info("No template for regex id: " + regexId + " found.");
}
}
return result;
}
use of cz.metacentrum.perun.notif.entities.PerunNotifTemplate 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.entities.PerunNotifTemplate in project perun by CESNET.
the class PerunNotifTemplateDaoImpl method getAllPerunNotifTemplates.
public List<PerunNotifTemplate> getAllPerunNotifTemplates() {
List<PerunNotifTemplate> result = this.getJdbcTemplate().query("SELECT * from pn_template", PerunNotifTemplate.PERUN_NOTIF_TEMPLATE);
for (PerunNotifTemplate template : result) {
// Gets all template ids which are connected to given regexIds
Set<PerunNotifRegex> perunNotifRegexs = perunNotifRegexDao.getPerunNotifRegexForTemplateId(template.getId());
template.setMatchingRegexs(perunNotifRegexs);
List<PerunNotifReceiver> perunNotifReceiver = this.getJdbcTemplate().query("SELECT * from pn_receiver where template_id = ?", new Object[] { template.getId() }, PerunNotifReceiver.PERUN_NOTIF_RECEIVER);
template.setReceivers(perunNotifReceiver);
List<PerunNotifTemplateMessage> perunNotifTemplateMessages = this.getJdbcTemplate().query("SELECT * from pn_template_message where template_id = ?", new Object[] { template.getId() }, PerunNotifTemplateMessage.PERUN_NOTIF_TEMPLATE_MESSAGE_ROW_MAPPER);
template.setPerunNotifTemplateMessages(perunNotifTemplateMessages);
}
return result;
}
use of cz.metacentrum.perun.notif.entities.PerunNotifTemplate in project perun by CESNET.
the class PerunNotifTemplateDaoImpl method getPerunNotifTemplateById.
@Override
public PerunNotifTemplate getPerunNotifTemplateById(int id) {
PerunNotifTemplate template = null;
try {
template = this.getJdbcTemplate().queryForObject("SELECT * from pn_template where id = ?", new Object[] { id }, PerunNotifTemplate.PERUN_NOTIF_TEMPLATE);
} catch (EmptyResultDataAccessException ex) {
// This exception is thrown when object is not found
return null;
}
Set<PerunNotifRegex> regexes = perunNotifRegexDao.getPerunNotifRegexForTemplateId(template.getId());
template.setMatchingRegexs(regexes);
List<PerunNotifReceiver> perunNotifReceiver = this.getJdbcTemplate().query("SELECT * from pn_receiver where template_id = ?", new Object[] { template.getId() }, PerunNotifReceiver.PERUN_NOTIF_RECEIVER);
template.setReceivers(perunNotifReceiver);
List<PerunNotifTemplateMessage> perunNotifTemplateMessages = this.getJdbcTemplate().query("SELECT * from pn_template_message where template_id = ?", new Object[] { template.getId() }, PerunNotifTemplateMessage.PERUN_NOTIF_TEMPLATE_MESSAGE_ROW_MAPPER);
template.setPerunNotifTemplateMessages(perunNotifTemplateMessages);
return template;
}
use of cz.metacentrum.perun.notif.entities.PerunNotifTemplate in project perun by CESNET.
the class PerunNotifTemplateDaoImpl method saveTemplateRegexRelation.
@Override
public void saveTemplateRegexRelation(int templateId, Integer regexId) {
if (perunNotifRegexDao.isRegexRelation(templateId, regexId)) {
// Relation exists
return;
} else {
perunNotifRegexDao.saveTemplateRegexRelation(templateId, regexId);
PerunNotifTemplate template = getPerunNotifTemplateById(templateId);
template.addPerunNotifRegex(perunNotifRegexDao.getPerunNotifRegexById(regexId));
}
}
Aggregations