Search in sources :

Example 11 with MailingListRecipient

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

the class EmailEventHandlerVO method jsonRead.

@SuppressWarnings("unchecked")
@Override
public void jsonRead(JsonReader reader, JsonObject jsonObject) throws JsonException {
    super.jsonRead(reader, jsonObject);
    String text = null;
    TypeDefinition recipType = new TypeDefinition(List.class, MailingListRecipient.class);
    JsonArray jsonActiveRecipients = jsonObject.getJsonArray("activeRecipients");
    if (jsonActiveRecipients != null)
        activeRecipients = (List<MailingListRecipient>) reader.read(recipType, jsonActiveRecipients);
    JsonBoolean b = jsonObject.getJsonBoolean("sendEscalation");
    if (b != null)
        sendEscalation = b.booleanValue();
    if (sendEscalation) {
        text = jsonObject.getString("escalationDelayType");
        if (text != null) {
            escalationDelayType = Common.TIME_PERIOD_CODES.getId(text);
            if (escalationDelayType == -1)
                throw new TranslatableJsonException("emport.error.invalid", "escalationDelayType", text, Common.TIME_PERIOD_CODES.getCodeList());
        }
        Integer i = jsonObject.getInt("escalationDelay", 1);
        if (i != null)
            escalationDelay = i;
        JsonArray jsonEscalationRecipients = jsonObject.getJsonArray("escalationRecipients");
        if (jsonEscalationRecipients != null)
            escalationRecipients = (List<MailingListRecipient>) reader.read(recipType, jsonEscalationRecipients);
        b = jsonObject.getJsonBoolean("keepSendingEscalations");
        if (b != null)
            repeatEscalations = b.booleanValue();
    }
    b = jsonObject.getJsonBoolean("sendInactive");
    if (b != null)
        sendInactive = b.booleanValue();
    if (sendInactive) {
        b = jsonObject.getJsonBoolean("inactiveOverride");
        if (b != null)
            inactiveOverride = b.booleanValue();
        if (inactiveOverride) {
            JsonArray jsonInactiveRecipients = jsonObject.getJsonArray("inactiveRecipients");
            if (jsonInactiveRecipients != null)
                inactiveRecipients = (List<MailingListRecipient>) reader.read(recipType, jsonInactiveRecipients);
        }
    }
    b = jsonObject.getJsonBoolean("includeSystemInformation");
    if (b != null)
        includeSystemInfo = b.booleanValue();
    includePointValueCount = jsonObject.getInt("includePointValueCount", 0);
    b = jsonObject.getJsonBoolean("includeLogfile");
    if (b != null)
        includeSystemInfo = b.booleanValue();
    customTemplate = jsonObject.getString("customTemplate");
    JsonArray context = jsonObject.getJsonArray("additionalContext");
    if (context != null) {
        List<IntStringPair> additionalContext = new ArrayList<>();
        for (JsonValue jv : context) {
            JsonObject jo = jv.toJsonObject();
            String dataPointXid = jo.getString("dataPointXid");
            if (dataPointXid == null)
                throw new TranslatableJsonException("emport.error.context.missing", "dataPointXid");
            Integer dpId = DataPointDao.getInstance().getIdByXid(dataPointXid);
            if (dpId == null)
                throw new TranslatableJsonException("emport.error.missingPoint", dataPointXid);
            String contextKey = jo.getString("contextKey");
            if (contextKey == null)
                throw new TranslatableJsonException("emport.error.context.missing", "contextKey");
            additionalContext.add(new IntStringPair(dpId, contextKey));
        }
        this.additionalContext = additionalContext;
    } else
        this.additionalContext = new ArrayList<>();
    script = jsonObject.getString("script");
    text = jsonObject.getString("subject");
    if (text != null) {
        subject = SUBJECT_INCLUDE_CODES.getId(text);
        if (subject == -1)
            throw new TranslatableJsonException("emport.error.invalid", "subject", text, SUBJECT_INCLUDE_CODES.getCodeList());
    } else {
        // For legacy compatibility
        subject = StringUtils.isEmpty(name) ? SUBJECT_INCLUDE_EVENT_MESSAGE : SUBJECT_INCLUDE_NAME;
    }
}
Also used : JsonArray(com.serotonin.json.type.JsonArray) IntStringPair(com.serotonin.db.pair.IntStringPair) ArrayList(java.util.ArrayList) JsonValue(com.serotonin.json.type.JsonValue) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonObject(com.serotonin.json.type.JsonObject) ArrayList(java.util.ArrayList) List(java.util.List) JsonBoolean(com.serotonin.json.type.JsonBoolean) TypeDefinition(com.serotonin.json.util.TypeDefinition)

Example 12 with MailingListRecipient

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

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)

Example 13 with MailingListRecipient

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

the class EmailHandlerRTTest method testSendActiveInactive.

@Test
public void testSendActiveInactive() {
    EmailEventHandlerVO vo = createVO();
    vo.setSendInactive(true);
    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(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)

Example 14 with MailingListRecipient

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

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

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