Search in sources :

Example 16 with MailingListRecipient

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

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 17 with MailingListRecipient

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

the class MailingListService method commonValidation.

/**
 * Common validation logic for insert/update of Mailing lists
 */
protected ProcessResult commonValidation(MailingList vo) {
    ProcessResult result = super.validate(vo);
    if (vo.getReceiveAlarmEmails() == null) {
        result.addContextualMessage("receiveAlarmEmails", "validate.invalidValue");
    }
    if (vo.getEntries() == null || vo.getEntries().size() == 0) {
        result.addContextualMessage("recipients", "mailingLists.validate.entries");
    } else {
        int index = 0;
        for (MailingListRecipient recipient : vo.getEntries()) {
            validateRecipient(vo, "recipients[" + index + "]", recipient, result, RecipientListEntryType.values());
            index++;
        }
    }
    if (vo.getInactiveIntervals() != null) {
        if (vo.getInactiveIntervals().size() > 672)
            result.addContextualMessage("inactiveSchedule", "validate.invalidValue");
    }
    return result;
}
Also used : ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) MailingListRecipient(com.serotonin.m2m2.vo.mailingList.MailingListRecipient)

Example 18 with MailingListRecipient

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

the class MailingListService method recursivelyCheckMailingListEntries.

/**
 */
private void recursivelyCheckMailingListEntries(Set<Integer> listIds, MailingList list, String prefix, ProcessResult result) {
    for (MailingListRecipient recipient : list.getEntries()) {
        switch(recipient.getRecipientType()) {
            case MAILING_LIST:
                if (!listIds.add(recipient.getReferenceId())) {
                    // Failed, we already have this list as reference
                    result.addContextualMessage(prefix, "mailingLists.validate.listCannotContainItself");
                    return;
                } else {
                    MailingList sublist = dao.get(recipient.getReferenceId());
                    recursivelyCheckMailingListEntries(listIds, sublist, prefix, result);
                }
                break;
            default:
                break;
        }
    }
}
Also used : MailingList(com.serotonin.m2m2.vo.mailingList.MailingList) MailingListRecipient(com.serotonin.m2m2.vo.mailingList.MailingListRecipient)

Example 19 with MailingListRecipient

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

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 20 with MailingListRecipient

use of com.serotonin.m2m2.vo.mailingList.MailingListRecipient in project ma-modules-public by infiniteautomation.

the class MailingListModelMapping method map.

@Override
public MailingListModel map(Object from, PermissionHolder user, RestModelMapper mapper) {
    MailingList vo = (MailingList) from;
    MailingListModel model;
    if (mailingListService.hasRecipientViewPermission(user, vo)) {
        model = new MailingListWithRecipientsModel(vo);
        if (vo.getEntries() != null && vo.getEntries().size() > 0) {
            List<EmailRecipientModel> recipients = new ArrayList<>();
            ((MailingListWithRecipientsModel) model).setRecipients(recipients);
            for (MailingListRecipient entry : vo.getEntries()) {
                recipients.add(mapper.map(entry, EmailRecipientModel.class, user));
            }
        }
    } else {
        model = new MailingListModel(vo);
    }
    model.setReadPermissions(new MangoPermissionModel(vo.getReadPermission()));
    model.setEditPermissions(new MangoPermissionModel(vo.getEditPermission()));
    return model;
}
Also used : MangoPermissionModel(com.infiniteautomation.mango.rest.latest.model.permissions.MangoPermissionModel) MailingList(com.serotonin.m2m2.vo.mailingList.MailingList) ArrayList(java.util.ArrayList) MailingListRecipient(com.serotonin.m2m2.vo.mailingList.MailingListRecipient)

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