Search in sources :

Example 41 with MailingListRecipient

use of com.serotonin.m2m2.vo.mailingList.MailingListRecipient in project ma-core-public by MangoAutomation.

the class EmailEventHandlerDefinition method commonValidation.

private void commonValidation(ProcessResult result, EmailEventHandlerVO vo) {
    if (vo.getActiveRecipients() != null) {
        int pos = 0;
        for (MailingListRecipient b : vo.getActiveRecipients()) {
            mailingListService.validateRecipient("activeRecipients[" + pos + "]", b, result, RecipientListEntryType.ADDRESS, RecipientListEntryType.MAILING_LIST, RecipientListEntryType.USER);
            pos++;
        }
    }
    if (vo.isSendEscalation()) {
        if (vo.getEscalationDelay() <= 0)
            result.addContextualMessage("escalationDelay", "eventHandlers.escalDelayError");
        if (!Common.TIME_PERIOD_CODES.isValidId(vo.getEscalationDelayType()))
            result.addContextualMessage("escalationDelayType", "validate.invalidValue");
        if (vo.getEscalationRecipients() != null) {
            int pos = 0;
            for (MailingListRecipient b : vo.getEscalationRecipients()) {
                mailingListService.validateRecipient("escalationRecipients[" + pos + "]", b, result, RecipientListEntryType.ADDRESS, RecipientListEntryType.MAILING_LIST, RecipientListEntryType.USER);
                pos++;
            }
        }
    } else if (vo.isRepeatEscalations()) {
        vo.setRepeatEscalations(false);
    }
    if (StringUtils.isNotEmpty(vo.getCustomTemplate())) {
        try {
            new Template("customTemplate", new StringReader(vo.getCustomTemplate()), Common.freemarkerConfiguration);
        } catch (Exception e) {
            result.addContextualMessage("customTemplate", "common.default", e.getMessage());
        }
    }
    if (vo.getAdditionalContext() != null)
        validateScriptContext(vo.getAdditionalContext(), result);
    else {
        vo.setAdditionalContext(new ArrayList<>());
    }
    if (!StringUtils.isEmpty(vo.getScript())) {
        MangoJavaScriptService service = Common.getBean(MangoJavaScriptService.class);
        try {
            service.compile(vo.getScript(), true);
        } catch (ScriptError e) {
            result.addContextualMessage("script", "eventHandlers.invalidActiveScriptError", e.getTranslatableMessage());
        }
    }
    if (!EmailEventHandlerVO.SUBJECT_INCLUDE_CODES.isValidId(vo.getSubject()))
        result.addContextualMessage("subject", "validate.invalidValue");
}
Also used : ScriptError(com.serotonin.m2m2.rt.script.ScriptError) StringReader(java.io.StringReader) MangoJavaScriptService(com.infiniteautomation.mango.spring.service.MangoJavaScriptService) MailingListRecipient(com.serotonin.m2m2.vo.mailingList.MailingListRecipient) Template(freemarker.template.Template)

Example 42 with MailingListRecipient

use of com.serotonin.m2m2.vo.mailingList.MailingListRecipient in project ma-core-public by MangoAutomation.

the class MailingListService method getActiveRecipients.

/**
 * Get a list of all active recipients for the desired types of entries while also
 *  populating the entries of the list.
 */
public Set<String> getActiveRecipients(List<MailingListRecipient> recipients, long sendTime, RecipientListEntryType... types) {
    PermissionHolder user = Common.getUser();
    this.permissionService.ensureAdminRole(user);
    Set<String> addresses = new HashSet<String>();
    for (MailingListRecipient r : recipients) {
        if (ArrayUtils.contains(types, r.getRecipientType())) {
            switch(r.getRecipientType()) {
                case ADDRESS:
                    addresses.add(r.getReferenceAddress());
                    break;
                case MAILING_LIST:
                    // Reload this whole guy as he may have been serialized or changed
                    MailingList list = dao.get(r.getReferenceId());
                    if (list != null) {
                        if (!list.getInactiveIntervals().contains(MailingListUtility.getIntervalIdAt(sendTime))) {
                            Set<String> activeFromList = getActiveRecipients(list.getEntries(), sendTime, types);
                            addresses.addAll(activeFromList);
                        }
                    }
                    break;
                case PHONE_NUMBER:
                    addresses.add(r.getReferenceAddress());
                    break;
                case USER:
                    User u = userDao.get(r.getReferenceId());
                    if (u == null || u.isDisabled()) {
                        break;
                    } else {
                        addresses.add(u.getEmail());
                    }
                    break;
                case USER_PHONE_NUMBER:
                    User up = userDao.get(r.getReferenceId());
                    if (up == null || up.isDisabled()) {
                        break;
                    } else {
                        addresses.add(up.getPhone());
                    }
                    break;
                default:
                    break;
            }
        }
    }
    return addresses;
}
Also used : User(com.serotonin.m2m2.vo.User) MailingList(com.serotonin.m2m2.vo.mailingList.MailingList) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) MailingListRecipient(com.serotonin.m2m2.vo.mailingList.MailingListRecipient) HashSet(java.util.HashSet)

Example 43 with MailingListRecipient

use of com.serotonin.m2m2.vo.mailingList.MailingListRecipient in project ma-core-public by MangoAutomation.

the class MailingListService method validateRecipient.

/**
 * For internal use to validate a mailing list
 */
protected void validateRecipient(MailingList list, String prefix, MailingListRecipient recipient, ProcessResult result, RecipientListEntryType... acceptableTypes) {
    if (!ArrayUtils.contains(acceptableTypes, recipient.getRecipientType())) {
        result.addContextualMessage(prefix + ".recipientType", "mailingLists.validate.invalidEntryType", recipient.getRecipientType(), acceptableTypes);
    } else {
        switch(recipient.getRecipientType()) {
            case ADDRESS:
                AddressEntry ee = (AddressEntry) recipient;
                if (StringUtils.isBlank(ee.getAddress())) {
                    result.addContextualMessage(prefix, "validate.required");
                }
                break;
            case MAILING_LIST:
                // If a mailing list then make sure it exists and there are no circular references
                MailingList sublist = dao.get(recipient.getReferenceId());
                if (sublist == null) {
                    result.addContextualMessage(prefix, "mailingLists.validate.listDoesNotExist");
                } else {
                    Set<Integer> listIds = new HashSet<>();
                    if (list != null) {
                        listIds.add(list.getId());
                    }
                    // Check to see if the top level recipient is the same as me
                    if (!listIds.add(sublist.getId())) {
                        result.addContextualMessage(prefix, "mailingLists.validate.listCannotContainItself");
                        return;
                    }
                    recursivelyCheckMailingListEntries(listIds, sublist, prefix, result);
                }
                break;
            case PHONE_NUMBER:
                PhoneEntry pe = (PhoneEntry) recipient;
                if (StringUtils.isBlank(pe.getPhone())) {
                    result.addContextualMessage(prefix, "validate.required");
                }
                break;
            case USER:
                UserEntry ue = (UserEntry) recipient;
                if (userDao.getXidById(ue.getUserId()) == null) {
                    result.addContextualMessage(prefix, "mailingLists.validate.userDoesNotExist");
                }
                break;
            case USER_PHONE_NUMBER:
                UserPhoneEntry up = (UserPhoneEntry) recipient;
                User userWithPhone = userDao.get(up.getUserId());
                if (userWithPhone == null) {
                    result.addContextualMessage(prefix, "mailingLists.validate.userDoesNotExist");
                } else if (StringUtils.isBlank(userWithPhone.getPhone())) {
                    result.addContextualMessage(prefix, "mailingLists.validate.userDoesNotHavePhoneNumber");
                }
                break;
            default:
                break;
        }
    }
}
Also used : User(com.serotonin.m2m2.vo.User) AddressEntry(com.serotonin.m2m2.vo.mailingList.AddressEntry) MailingList(com.serotonin.m2m2.vo.mailingList.MailingList) UserPhoneEntry(com.serotonin.m2m2.vo.mailingList.UserPhoneEntry) UserEntry(com.serotonin.m2m2.vo.mailingList.UserEntry) UserPhoneEntry(com.serotonin.m2m2.vo.mailingList.UserPhoneEntry) PhoneEntry(com.serotonin.m2m2.vo.mailingList.PhoneEntry) HashSet(java.util.HashSet)

Example 44 with MailingListRecipient

use of com.serotonin.m2m2.vo.mailingList.MailingListRecipient in project ma-core-public by MangoAutomation.

the class Upgrade12 method upgradeEventHandlers.

@SuppressWarnings("deprecation")
private int upgradeEventHandlers(OutputStream os) {
    int upgraded = 0;
    List<EventHandlerVO> handlers = this.ejt.query(EVENT_HANDLER_SELECT, new EventHandlerRowMapper());
    // Convert them and update the database with the new handlers
    for (EventHandlerVO vo : handlers) {
        switch(vo.getHandlerType()) {
            case EventHandlerVO.TYPE_EMAIL:
                EmailEventHandlerVO emailHandler = new EmailEventHandlerVO();
                emailHandler.setId(vo.getId());
                emailHandler.setXid(vo.getXid());
                emailHandler.setName(vo.getAlias());
                emailHandler.setDisabled(vo.isDisabled());
                emailHandler.setDefinition(ModuleRegistry.getEventHandlerDefinition(EmailEventHandlerDefinition.TYPE_NAME));
                if (vo.getActiveRecipients() != null) {
                    List<MailingListRecipient> activeRecipients = new ArrayList<>();
                    for (RecipientListEntryBean entry : vo.getActiveRecipients()) {
                        activeRecipients.add(entry.createEmailRecipient());
                    }
                    emailHandler.setActiveRecipients(activeRecipients);
                }
                emailHandler.setSendEscalation(vo.isSendEscalation());
                emailHandler.setEscalationDelayType(vo.getEscalationDelayType());
                emailHandler.setEscalationDelay(vo.getEscalationDelay());
                if (vo.getEscalationRecipients() != null) {
                    List<MailingListRecipient> escalationRecipients = new ArrayList<>();
                    for (RecipientListEntryBean entry : vo.getEscalationRecipients()) {
                        escalationRecipients.add(entry.createEmailRecipient());
                    }
                    emailHandler.setEscalationRecipients(escalationRecipients);
                }
                emailHandler.setSendInactive(vo.isSendInactive());
                emailHandler.setInactiveOverride(vo.isInactiveOverride());
                if (vo.getInactiveRecipients() != null) {
                    List<MailingListRecipient> inactiveRecipients = new ArrayList<>();
                    for (RecipientListEntryBean entry : vo.getInactiveRecipients()) {
                        inactiveRecipients.add(entry.createEmailRecipient());
                    }
                    emailHandler.setInactiveRecipients(inactiveRecipients);
                }
                emailHandler.setIncludeSystemInfo(vo.isIncludeSystemInfo());
                emailHandler.setIncludePointValueCount(vo.getIncludePointValueCount());
                emailHandler.setIncludeLogfile(vo.isIncludeLogfile());
                upgradeEventHandler(emailHandler);
                upgraded++;
                break;
            case EventHandlerVO.TYPE_PROCESS:
                ProcessEventHandlerVO processHandler = new ProcessEventHandlerVO();
                processHandler.setId(vo.getId());
                processHandler.setXid(vo.getXid());
                processHandler.setName(vo.getAlias());
                processHandler.setDisabled(vo.isDisabled());
                processHandler.setDefinition(ModuleRegistry.getEventHandlerDefinition(ProcessEventHandlerDefinition.TYPE_NAME));
                processHandler.setActiveProcessCommand(vo.getActiveProcessCommand());
                processHandler.setActiveProcessTimeout(vo.getActiveProcessTimeout());
                processHandler.setInactiveProcessCommand(vo.getInactiveProcessCommand());
                processHandler.setInactiveProcessTimeout(vo.getInactiveProcessTimeout());
                upgradeEventHandler(processHandler);
                upgraded++;
                break;
            case EventHandlerVO.TYPE_SET_POINT:
                SetPointEventHandlerVO setPointHandler = new SetPointEventHandlerVO();
                setPointHandler.setId(vo.getId());
                setPointHandler.setXid(vo.getXid());
                setPointHandler.setName(vo.getAlias());
                setPointHandler.setDisabled(vo.isDisabled());
                setPointHandler.setDefinition(ModuleRegistry.getEventHandlerDefinition(SetPointEventHandlerDefinition.TYPE_NAME));
                setPointHandler.setTargetPointId(vo.getTargetPointId());
                setPointHandler.setActiveAction(vo.getActiveAction());
                setPointHandler.setActiveValueToSet(vo.getActiveValueToSet());
                setPointHandler.setActivePointId(vo.getActivePointId());
                setPointHandler.setInactiveAction(vo.getInactiveAction());
                setPointHandler.setInactiveValueToSet(vo.getInactiveValueToSet());
                setPointHandler.setInactivePointId(vo.getInactivePointId());
                upgradeEventHandler(setPointHandler);
                upgraded++;
                break;
            default:
                LOG.error("Unknown event detector type: " + vo.getHandlerType());
                try {
                    os.write(new String("Unknown event detector type: " + vo.getHandlerType()).getBytes(StandardCharsets.UTF_8));
                } catch (IOException e2) {
                    LOG.error("Unable to write to upgrade log.", e2);
                }
                break;
        }
    }
    return upgraded;
}
Also used : ProcessEventHandlerVO(com.serotonin.m2m2.vo.event.ProcessEventHandlerVO) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SetPointEventHandlerVO(com.serotonin.m2m2.vo.event.SetPointEventHandlerVO) SetPointEventHandlerVO(com.serotonin.m2m2.vo.event.SetPointEventHandlerVO) ProcessEventHandlerVO(com.serotonin.m2m2.vo.event.ProcessEventHandlerVO) EventHandlerVO(com.serotonin.m2m2.vo.event.EventHandlerVO) AbstractEventHandlerVO(com.serotonin.m2m2.vo.event.AbstractEventHandlerVO) EmailEventHandlerVO(com.serotonin.m2m2.vo.event.EmailEventHandlerVO) EmailEventHandlerVO(com.serotonin.m2m2.vo.event.EmailEventHandlerVO) MailingListRecipient(com.serotonin.m2m2.vo.mailingList.MailingListRecipient) RecipientListEntryBean(com.serotonin.m2m2.web.dwr.beans.RecipientListEntryBean)

Example 45 with MailingListRecipient

use of com.serotonin.m2m2.vo.mailingList.MailingListRecipient in project ma-core-public by MangoAutomation.

the class EmailHandlerRTTest method testSendInactive.

@Test
public void testSendInactive() {
    EmailEventHandlerVO vo = createVO();
    List<MailingListRecipient> activeRecipients = createRecipients();
    vo.setActiveRecipients(activeRecipients);
    vo.setSendInactive(true);
    List<MailingListRecipient> inactiveRecipients = createRecipients();
    vo.setInactiveRecipients(inactiveRecipients);
    EmailHandlerRT rt = new EmailHandlerRT(vo);
    EventInstance evt = createDataPointEventInstance();
    rt.eventRaised(evt);
    // Ensure there is one scheduled
    assertEquals(1, scheduledItems.size());
    scheduledItems.clear();
    // Make Inactive
    evt.returnToNormal(this.timer.currentTimeMillis(), ReturnCause.RETURN_TO_NORMAL);
    rt.eventInactive(evt);
    assertEquals(1, scheduledItems.size());
}
Also used : EventInstance(com.serotonin.m2m2.rt.event.EventInstance) EmailEventHandlerVO(com.serotonin.m2m2.vo.event.EmailEventHandlerVO) MailingListRecipient(com.serotonin.m2m2.vo.mailingList.MailingListRecipient) Test(org.junit.Test)

Aggregations

MailingListRecipient (com.serotonin.m2m2.vo.mailingList.MailingListRecipient)31 ArrayList (java.util.ArrayList)17 AddressEntry (com.serotonin.m2m2.vo.mailingList.AddressEntry)13 MailingList (com.serotonin.m2m2.vo.mailingList.MailingList)13 Test (org.junit.Test)10 MailingListEntry (com.serotonin.m2m2.vo.mailingList.MailingListEntry)9 EmailEventHandlerVO (com.serotonin.m2m2.vo.event.EmailEventHandlerVO)8 UserEntry (com.serotonin.m2m2.vo.mailingList.UserEntry)8 PhoneEntry (com.serotonin.m2m2.vo.mailingList.PhoneEntry)7 EventInstance (com.serotonin.m2m2.rt.event.EventInstance)6 IntStringPair (com.serotonin.db.pair.IntStringPair)5 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)4 User (com.serotonin.m2m2.vo.User)4 RecipientListEntryType (com.serotonin.m2m2.vo.mailingList.RecipientListEntryType)4 RecipientListEntryBean (com.serotonin.m2m2.web.dwr.beans.RecipientListEntryBean)4 HashSet (java.util.HashSet)4 List (java.util.List)4 ScriptPermissions (com.infiniteautomation.mango.util.script.ScriptPermissions)3 EmailRecipientModel (com.infiniteautomation.mango.rest.latest.model.mailingList.EmailRecipientModel)2 ExpectValidationException (com.infiniteautomation.mango.rules.ExpectValidationException)2