Search in sources :

Example 31 with MailingListRecipient

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

the class EmailHandlerRTTest method testSendActive.

@Test
public void testSendActive() {
    EmailEventHandlerVO vo = createVO();
    List<MailingListRecipient> activeRecipients = createRecipients();
    vo.setActiveRecipients(activeRecipients);
    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(0, 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)

Example 32 with MailingListRecipient

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

the class EmailEventHandlerVO method readObject.

@SuppressWarnings({ "unchecked", "deprecation" })
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    // NOTE: after deserialization the recipient lists need to be cleaned of deleted references,
    // See MailingListDao.cleanRecipientsList.  This happens in the DAO
    int ver = in.readInt();
    subject = SUBJECT_INCLUDE_EVENT_MESSAGE;
    if (ver == 1) {
        List<RecipientListEntryBean> legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            activeRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                activeRecipients.add(b.createEmailRecipient());
            }
        }
        sendEscalation = in.readBoolean();
        repeatEscalations = false;
        escalationDelayType = in.readInt();
        escalationDelay = in.readInt();
        legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            escalationRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                escalationRecipients.add(b.createEmailRecipient());
            }
        }
        sendInactive = in.readBoolean();
        inactiveOverride = in.readBoolean();
        legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            inactiveRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                inactiveRecipients.add(b.createEmailRecipient());
            }
        }
        includeSystemInfo = in.readBoolean();
        includePointValueCount = in.readInt();
        includeLogfile = in.readBoolean();
        customTemplate = null;
        additionalContext = new ArrayList<IntStringPair>();
        scriptRoles = new ScriptPermissions();
        script = null;
    } else if (ver == 2) {
        List<RecipientListEntryBean> legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            activeRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                activeRecipients.add(b.createEmailRecipient());
            }
        }
        sendEscalation = in.readBoolean();
        repeatEscalations = false;
        escalationDelayType = in.readInt();
        escalationDelay = in.readInt();
        legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            escalationRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                escalationRecipients.add(b.createEmailRecipient());
            }
        }
        sendInactive = in.readBoolean();
        inactiveOverride = in.readBoolean();
        legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            inactiveRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                inactiveRecipients.add(b.createEmailRecipient());
            }
        }
        includeSystemInfo = in.readBoolean();
        includePointValueCount = in.readInt();
        includeLogfile = in.readBoolean();
        customTemplate = SerializationHelper.readSafeUTF(in);
        additionalContext = new ArrayList<IntStringPair>();
        scriptRoles = new ScriptPermissions();
        script = null;
    } else if (ver == 3) {
        List<RecipientListEntryBean> legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            activeRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                activeRecipients.add(b.createEmailRecipient());
            }
        }
        sendEscalation = in.readBoolean();
        repeatEscalations = false;
        escalationDelayType = in.readInt();
        escalationDelay = in.readInt();
        legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            escalationRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                escalationRecipients.add(b.createEmailRecipient());
            }
        }
        sendInactive = in.readBoolean();
        inactiveOverride = in.readBoolean();
        legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            inactiveRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                inactiveRecipients.add(b.createEmailRecipient());
            }
        }
        includeSystemInfo = in.readBoolean();
        includePointValueCount = in.readInt();
        includeLogfile = in.readBoolean();
        customTemplate = SerializationHelper.readSafeUTF(in);
        additionalContext = (List<IntStringPair>) in.readObject();
        scriptRoles = new ScriptPermissions();
        script = null;
    } else if (ver == 4) {
        List<RecipientListEntryBean> legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            activeRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                activeRecipients.add(b.createEmailRecipient());
            }
        }
        sendEscalation = in.readBoolean();
        repeatEscalations = false;
        escalationDelayType = in.readInt();
        escalationDelay = in.readInt();
        legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            escalationRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                escalationRecipients.add(b.createEmailRecipient());
            }
        }
        sendInactive = in.readBoolean();
        legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            inactiveRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                inactiveRecipients.add(b.createEmailRecipient());
            }
        }
        includeSystemInfo = in.readBoolean();
        includePointValueCount = in.readInt();
        includeLogfile = in.readBoolean();
        customTemplate = SerializationHelper.readSafeUTF(in);
        additionalContext = (List<IntStringPair>) in.readObject();
        com.serotonin.m2m2.rt.script.ScriptPermissions oldPermissions = (com.serotonin.m2m2.rt.script.ScriptPermissions) in.readObject();
        if (oldPermissions != null) {
            // We will be using this in the upgrade so this is temporary and will be used to set
            // the scriptRoles
            legacyScriptRoles = oldPermissions.getAllLegacyPermissions();
            legacyPermissionHolderName = oldPermissions.getPermissionHolderName();
        } else {
            scriptRoles = new ScriptPermissions();
        }
        script = SerializationHelper.readSafeUTF(in);
    } else if (ver == 5) {
        List<RecipientListEntryBean> legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            activeRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                activeRecipients.add(b.createEmailRecipient());
            }
        }
        sendEscalation = in.readBoolean();
        repeatEscalations = in.readBoolean();
        escalationDelayType = in.readInt();
        escalationDelay = in.readInt();
        legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            escalationRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                escalationRecipients.add(b.createEmailRecipient());
            }
        }
        sendInactive = in.readBoolean();
        inactiveOverride = in.readBoolean();
        legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            inactiveRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                inactiveRecipients.add(b.createEmailRecipient());
            }
        }
        includeSystemInfo = in.readBoolean();
        includePointValueCount = in.readInt();
        includeLogfile = in.readBoolean();
        customTemplate = SerializationHelper.readSafeUTF(in);
        additionalContext = (List<IntStringPair>) in.readObject();
        com.serotonin.m2m2.rt.script.ScriptPermissions oldPermissions = (com.serotonin.m2m2.rt.script.ScriptPermissions) in.readObject();
        if (oldPermissions != null) {
            // We will be using this in the upgrade so this is temporary and will be used to set
            // the scriptRoles
            legacyScriptRoles = oldPermissions.getAllLegacyPermissions();
            legacyPermissionHolderName = oldPermissions.getPermissionHolderName();
        } else {
            scriptRoles = new ScriptPermissions();
        }
        script = SerializationHelper.readSafeUTF(in);
    } else if (ver == 6) {
        List<RecipientListEntryBean> legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            activeRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                activeRecipients.add(b.createEmailRecipient());
            }
        }
        sendEscalation = in.readBoolean();
        repeatEscalations = in.readBoolean();
        escalationDelayType = in.readInt();
        escalationDelay = in.readInt();
        legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            escalationRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                escalationRecipients.add(b.createEmailRecipient());
            }
        }
        sendInactive = in.readBoolean();
        inactiveOverride = in.readBoolean();
        legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            inactiveRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                inactiveRecipients.add(b.createEmailRecipient());
            }
        }
        includeSystemInfo = in.readBoolean();
        includePointValueCount = in.readInt();
        includeLogfile = in.readBoolean();
        customTemplate = SerializationHelper.readSafeUTF(in);
        additionalContext = (List<IntStringPair>) in.readObject();
        scriptRoles = (ScriptPermissions) in.readObject();
        script = SerializationHelper.readSafeUTF(in);
    } else if (ver == 7) {
        List<RecipientListEntryBean> legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            activeRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                activeRecipients.add(b.createEmailRecipient());
            }
        }
        sendEscalation = in.readBoolean();
        repeatEscalations = in.readBoolean();
        escalationDelayType = in.readInt();
        escalationDelay = in.readInt();
        legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            escalationRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                escalationRecipients.add(b.createEmailRecipient());
            }
        }
        sendInactive = in.readBoolean();
        inactiveOverride = in.readBoolean();
        legacy = (List<RecipientListEntryBean>) in.readObject();
        if (legacy != null) {
            inactiveRecipients = new ArrayList<>();
            for (RecipientListEntryBean b : legacy) {
                inactiveRecipients.add(b.createEmailRecipient());
            }
        }
        includeSystemInfo = in.readBoolean();
        includePointValueCount = in.readInt();
        includeLogfile = in.readBoolean();
        customTemplate = SerializationHelper.readSafeUTF(in);
        additionalContext = (List<IntStringPair>) in.readObject();
        scriptRoles = (ScriptPermissions) in.readObject();
        script = SerializationHelper.readSafeUTF(in);
        subject = in.readInt();
    } else if (ver == 8) {
        activeRecipients = (List<MailingListRecipient>) in.readObject();
        sendEscalation = in.readBoolean();
        repeatEscalations = in.readBoolean();
        escalationDelayType = in.readInt();
        escalationDelay = in.readInt();
        escalationRecipients = (List<MailingListRecipient>) in.readObject();
        sendInactive = in.readBoolean();
        inactiveOverride = in.readBoolean();
        inactiveRecipients = (List<MailingListRecipient>) in.readObject();
        includeSystemInfo = in.readBoolean();
        includePointValueCount = in.readInt();
        includeLogfile = in.readBoolean();
        customTemplate = SerializationHelper.readSafeUTF(in);
        additionalContext = (List<IntStringPair>) in.readObject();
        scriptRoles = (ScriptPermissions) in.readObject();
        script = SerializationHelper.readSafeUTF(in);
        subject = in.readInt();
    }
}
Also used : IntStringPair(com.serotonin.db.pair.IntStringPair) ArrayList(java.util.ArrayList) ScriptPermissions(com.infiniteautomation.mango.util.script.ScriptPermissions) ArrayList(java.util.ArrayList) List(java.util.List) RecipientListEntryBean(com.serotonin.m2m2.web.dwr.beans.RecipientListEntryBean)

Example 33 with MailingListRecipient

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

the class RecipientListEntryBean method createEmailRecipient.

/**
 * Convert to mailing list recipient
 */
public MailingListRecipient createEmailRecipient() {
    RecipientListEntryType type = RecipientListEntryType.fromValue(recipientType);
    switch(type) {
        case ADDRESS:
            AddressEntry a = new AddressEntry();
            a.setAddress(referenceAddress);
            return a;
        case MAILING_LIST:
            MailingListEntry ml = new MailingListEntry();
            ml.setMailingListId(referenceId);
            return ml;
        case PHONE_NUMBER:
            PhoneEntry pe = new PhoneEntry();
            pe.setPhone(referenceAddress);
            return pe;
        case USER:
            UserEntry u = new UserEntry();
            u.setUserId(referenceId);
            return u;
        case USER_PHONE_NUMBER:
            PhoneEntry p = new PhoneEntry();
            p.setPhone(referenceAddress);
            return p;
        default:
            throw new ShouldNeverHappenException("Unknown recipient type: " + recipientType);
    }
}
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)

Example 34 with MailingListRecipient

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

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

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

the class MailingListDao method saveRelationalData.

@Override
public void saveRelationalData(MailingList existing, MailingList ml) {
    // Delete existing entries
    if (existing != null) {
        create.deleteFrom(mailingListInactiveTable).where(mailingListInactiveTable.mailingListId.eq(ml.getId())).execute();
        create.deleteFrom(mailingListMembersTable).where(mailingListMembersTable.mailingListId.eq(ml.getId())).execute();
    }
    // Save relational data
    Set<Integer> inactiveIntervals = ml.getInactiveIntervals();
    if (inactiveIntervals.size() > 0) {
        BatchBindStep batchInactive = create.batch(create.insertInto(mailingListInactiveTable).columns(mailingListInactiveTable.mailingListId, mailingListInactiveTable.inactiveInterval).values((Integer) null, null));
        for (Integer interval : inactiveIntervals) batchInactive.bind(ml.getId(), interval);
        batchInactive.execute();
    }
    List<MailingListRecipient> entries = ml.getEntries();
    if (entries.size() > 0) {
        BatchBindStep batchMembers = create.batch(create.insertInto(mailingListMembersTable).columns(mailingListMembersTable.mailingListId, mailingListMembersTable.typeId, mailingListMembersTable.userId, mailingListMembersTable.address).values((Integer) null, null, null, null));
        for (MailingListRecipient r : entries) batchMembers.bind(ml.getId(), r.getRecipientType().value(), r.getReferenceId(), r.getReferenceAddress());
        batchMembers.execute();
    }
    if (existing != null) {
        if (!existing.getReadPermission().equals(ml.getReadPermission())) {
            permissionService.deletePermissions(existing.getReadPermission());
        }
        if (!existing.getEditPermission().equals(ml.getEditPermission())) {
            permissionService.deletePermissions(existing.getEditPermission());
        }
    }
}
Also used : BatchBindStep(org.jooq.BatchBindStep) 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