Search in sources :

Example 1 with MailingListEntry

use of com.serotonin.m2m2.vo.mailingList.MailingListEntry 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)

Example 2 with MailingListEntry

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

the class MailingListServiceTest method testActiveRecipients.

/**
 * ensure Active interval access works
 */
@Test
public void testActiveRecipients() {
    // First create a list with 2 entries
    // Create an inactive list
    MailingList vo = new MailingList();
    vo.setXid(MailingListDao.getInstance().generateUniqueXid());
    vo.setName("MailingList");
    vo.setReceiveAlarmEmails(AlarmLevels.NONE);
    List<MailingListRecipient> entries = new ArrayList<>();
    AddressEntry entry1 = new AddressEntry();
    entry1.setAddress("entry1List1@example.com");
    entries.add(entry1);
    AddressEntry entry2 = new AddressEntry();
    entry2.setAddress("entry2List1@example.com");
    entries.add(entry2);
    vo.setEntries(entries);
    service.insert(vo);
    MailingListEntry mlEntry = new MailingListEntry();
    mlEntry.setMailingListId(vo.getId());
    List<MailingListRecipient> mlRecipients = Collections.singletonList(mlEntry);
    // Set our time to the start of this week, the test a full week's intervals
    ZonedDateTime now = ZonedDateTime.now();
    ZonedDateTime startOfWeek = now.with(ChronoField.DAY_OF_WEEK, 1).toLocalDate().atStartOfDay(ZoneId.systemDefault());
    ZonedDateTime endOfNextWeek = startOfWeek.plus(2, ChronoUnit.WEEKS);
    while (startOfWeek.isBefore(endOfNextWeek)) {
        // Setup our interval
        vo.setInactiveIntervals(Collections.singleton(MailingListService.MailingListUtility.getIntervalIdAt(startOfWeek.toInstant().toEpochMilli())));
        vo = service.update(vo.getId(), vo);
        Set<String> recipients = service.getActiveRecipients(mlRecipients, startOfWeek.toInstant().toEpochMilli(), RecipientListEntryType.MAILING_LIST, RecipientListEntryType.ADDRESS, RecipientListEntryType.USER);
        assertEquals(0, recipients.size());
        // Update the list to be active and re-test
        ZonedDateTime previousIntervalStartTime = startOfWeek.minus(15, ChronoUnit.MINUTES);
        vo.setInactiveIntervals(Collections.singleton(MailingListService.MailingListUtility.getIntervalIdAt(previousIntervalStartTime.toInstant().toEpochMilli())));
        vo = service.update(vo.getId(), vo);
        recipients = service.getActiveRecipients(mlRecipients, startOfWeek.toInstant().toEpochMilli(), RecipientListEntryType.MAILING_LIST, RecipientListEntryType.ADDRESS, RecipientListEntryType.USER);
        assertEquals(2, recipients.size());
        for (MailingListRecipient e : entries) {
            assertTrue(recipients.contains(e.toString()));
        }
        // Advance 1 minute
        startOfWeek = startOfWeek.plus(1, ChronoUnit.MINUTES);
    }
}
Also used : MailingListEntry(com.serotonin.m2m2.vo.mailingList.MailingListEntry) AddressEntry(com.serotonin.m2m2.vo.mailingList.AddressEntry) ZonedDateTime(java.time.ZonedDateTime) MailingList(com.serotonin.m2m2.vo.mailingList.MailingList) ArrayList(java.util.ArrayList) MailingListRecipient(com.serotonin.m2m2.vo.mailingList.MailingListRecipient) Test(org.junit.Test)

Example 3 with MailingListEntry

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

the class MailingListServiceTest method testRecursiveMailingListsAreInvalid.

@Test
@ExpectValidationException("recipients[0]")
public void testRecursiveMailingListsAreInvalid() {
    MailingList recursiveMailingList = service.insert(newVO(readUser));
    List<MailingListRecipient> entries = new ArrayList<>();
    recursiveMailingList.setEntries(entries);
    MailingListEntry ml = new MailingListEntry();
    ml.setMailingListId(recursiveMailingList.getId());
    entries.add(ml);
    service.update(recursiveMailingList.getId(), recursiveMailingList);
}
Also used : MailingListEntry(com.serotonin.m2m2.vo.mailingList.MailingListEntry) MailingList(com.serotonin.m2m2.vo.mailingList.MailingList) ArrayList(java.util.ArrayList) MailingListRecipient(com.serotonin.m2m2.vo.mailingList.MailingListRecipient) Test(org.junit.Test) ExpectValidationException(com.infiniteautomation.mango.rules.ExpectValidationException)

Example 4 with MailingListEntry

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

the class MailingListEntryModel method fromModel.

@Override
public MailingListRecipient fromModel() {
    MailingListEntry entry = new MailingListEntry();
    Integer id = MailingListDao.getInstance().getIdByXid(xid);
    if (id != null) {
        entry.setMailingListId(id);
    }
    return entry;
}
Also used : MailingListEntry(com.serotonin.m2m2.vo.mailingList.MailingListEntry)

Example 5 with MailingListEntry

use of com.serotonin.m2m2.vo.mailingList.MailingListEntry 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)

Aggregations

MailingListEntry (com.serotonin.m2m2.vo.mailingList.MailingListEntry)5 AddressEntry (com.serotonin.m2m2.vo.mailingList.AddressEntry)3 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)2 MailingList (com.serotonin.m2m2.vo.mailingList.MailingList)2 MailingListRecipient (com.serotonin.m2m2.vo.mailingList.MailingListRecipient)2 PhoneEntry (com.serotonin.m2m2.vo.mailingList.PhoneEntry)2 RecipientListEntryType (com.serotonin.m2m2.vo.mailingList.RecipientListEntryType)2 UserEntry (com.serotonin.m2m2.vo.mailingList.UserEntry)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 ExpectValidationException (com.infiniteautomation.mango.rules.ExpectValidationException)1 ZonedDateTime (java.time.ZonedDateTime)1