Search in sources :

Example 1 with RecipientListEntryType

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

the class RecipientListEntryBean method jsonRead.

@Override
public void jsonRead(JsonReader reader, JsonObject jsonObject) throws JsonException {
    String text = jsonObject.getString("recipientType");
    RecipientListEntryType type = RecipientListEntryType.fromName(text);
    if (type == null) {
        throw new TranslatableJsonException("emport.error.recipient.invalid", "recipientType", text, RecipientListEntryType.values());
    }
    switch(type) {
        case ADDRESS:
            referenceAddress = jsonObject.getString("address");
            if (referenceAddress == null)
                throw new TranslatableJsonException("emport.error.recipient.missing.reference", "address");
            break;
        case MAILING_LIST:
            text = jsonObject.getString("mailingList");
            if (text == null)
                throw new TranslatableJsonException("emport.error.recipient.missing.reference", "mailingList");
            MailingList ml = MailingListDao.getInstance().getByXid(text);
            if (ml == null)
                throw new TranslatableJsonException("emport.error.recipient.invalid.reference", "mailingList", text);
            referenceId = ml.getId();
            break;
        case PHONE_NUMBER:
            referenceAddress = jsonObject.getString("phone");
            if (referenceAddress == null)
                throw new TranslatableJsonException("emport.error.recipient.missing.reference", "phone");
            break;
        case USER:
            text = jsonObject.getString("username");
            if (text == null)
                throw new TranslatableJsonException("emport.error.recipient.missing.reference", "username");
            User user = UserDao.getInstance().getByXid(text);
            if (user == null)
                throw new TranslatableJsonException("emport.error.recipient.invalid.reference", "user", text);
            referenceId = user.getId();
            referenceAddress = user.getEmail();
            break;
        case USER_PHONE_NUMBER:
            text = jsonObject.getString("username");
            if (text == null)
                throw new TranslatableJsonException("emport.error.recipient.missing.reference", "username");
            User userPhone = UserDao.getInstance().getByXid(text);
            if (userPhone == null)
                throw new TranslatableJsonException("emport.error.recipient.invalid.reference", "user", text);
            referenceId = userPhone.getId();
            referenceAddress = userPhone.getPhone();
            break;
        default:
            throw new ShouldNeverHappenException("Unknown recipient type: " + type);
    }
}
Also used : User(com.serotonin.m2m2.vo.User) MailingList(com.serotonin.m2m2.vo.mailingList.MailingList) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) RecipientListEntryType(com.serotonin.m2m2.vo.mailingList.RecipientListEntryType)

Example 2 with RecipientListEntryType

use of com.serotonin.m2m2.vo.mailingList.RecipientListEntryType 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 3 with RecipientListEntryType

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

the class MailingListService method getAlarmAddresses.

/**
 *  Get any addresses for mailing lists that are mailed on alarm level up to and including 'alarmLevel'
 *
 * @param time of gathering addresses used to determine if a list is inactive
 * @param types for types of entries to return
 */
public Set<String> getAlarmAddresses(AlarmLevels alarmLevel, long time, RecipientListEntryType... types) {
    PermissionHolder user = Common.getUser();
    this.permissionService.ensureAdminRole(user);
    List<MailingList> result = new ArrayList<>();
    // TODO Mango 4.0 this is only weakly consistent
    cache.asMap().forEach((id, ml) -> {
        if (ml.getReceiveAlarmEmails().value() >= 0 && ml.getReceiveAlarmEmails().value() <= alarmLevel.value()) {
            result.add(ml);
        }
    });
    Set<String> addresses = new HashSet<>();
    for (MailingList list : result) {
        addresses.addAll(getActiveRecipients(list.getEntries(), time, types));
    }
    return addresses;
}
Also used : MailingList(com.serotonin.m2m2.vo.mailingList.MailingList) ArrayList(java.util.ArrayList) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) HashSet(java.util.HashSet)

Example 4 with RecipientListEntryType

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

the class MailingListRecipientResolver method resolve.

@Override
public Type resolve(JsonValue jsonValue) throws JsonException {
    if (jsonValue == null)
        return null;
    JsonObject json = jsonValue.toJsonObject();
    String text = json.getString("recipientType");
    if (text == null)
        throw new TranslatableJsonException("emport.error.recipient.missing", "recipientType", RecipientListEntryType.values());
    RecipientListEntryType type = RecipientListEntryType.fromName(text);
    if (type == null) {
        throw new TranslatableJsonException("emport.error.recipient.invalid", "recipientType", text, RecipientListEntryType.values());
    }
    switch(type) {
        case ADDRESS:
            return AddressEntry.class;
        case MAILING_LIST:
            return MailingListEntry.class;
        case PHONE_NUMBER:
            return PhoneEntry.class;
        case USER:
            return UserEntry.class;
        case USER_PHONE_NUMBER:
            return UserPhoneEntry.class;
        default:
            throw new ShouldNeverHappenException("Unknown recipient type: " + type);
    }
}
Also used : ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) JsonObject(com.serotonin.json.type.JsonObject) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException)

Example 5 with RecipientListEntryType

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

the class MailingListDao method getRecipientType.

private MailingListRecipient getRecipientType(Record record) {
    int intType = record.get(mailingListMembersTable.typeId);
    RecipientListEntryType type = RecipientListEntryType.fromValue(intType);
    Integer userId = record.get(mailingListMembersTable.userId);
    switch(type) {
        case ADDRESS:
            AddressEntry ae = new AddressEntry();
            ae.setAddress(record.get(mailingListMembersTable.address));
            return ae;
        case MAILING_LIST:
            MailingListEntry ml = new MailingListEntry();
            ml.setMailingListId(userId != null ? userId : 0);
            return ml;
        case PHONE_NUMBER:
            PhoneEntry pe = new PhoneEntry();
            pe.setPhone(record.get(mailingListMembersTable.address));
            return pe;
        case USER:
        case USER_PHONE_NUMBER:
            UserEntry ue = new UserEntry();
            ue.setUserId(userId != null ? userId : 0);
            return ue;
        default:
            throw new ShouldNeverHappenException("Unknown mailing list entry type: " + intType);
    }
}
Also used : MailingListEntry(com.serotonin.m2m2.vo.mailingList.MailingListEntry) AddressEntry(com.serotonin.m2m2.vo.mailingList.AddressEntry) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) RecipientListEntryType(com.serotonin.m2m2.vo.mailingList.RecipientListEntryType) UserEntry(com.serotonin.m2m2.vo.mailingList.UserEntry) PhoneEntry(com.serotonin.m2m2.vo.mailingList.PhoneEntry)

Aggregations

ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)4 MailingList (com.serotonin.m2m2.vo.mailingList.MailingList)4 RecipientListEntryType (com.serotonin.m2m2.vo.mailingList.RecipientListEntryType)4 User (com.serotonin.m2m2.vo.User)3 AddressEntry (com.serotonin.m2m2.vo.mailingList.AddressEntry)3 PhoneEntry (com.serotonin.m2m2.vo.mailingList.PhoneEntry)3 UserEntry (com.serotonin.m2m2.vo.mailingList.UserEntry)3 HashSet (java.util.HashSet)3 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)2 MailingListEntry (com.serotonin.m2m2.vo.mailingList.MailingListEntry)2 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)2 JsonObject (com.serotonin.json.type.JsonObject)1 MailingListRecipient (com.serotonin.m2m2.vo.mailingList.MailingListRecipient)1 UserPhoneEntry (com.serotonin.m2m2.vo.mailingList.UserPhoneEntry)1 ArrayList (java.util.ArrayList)1