Search in sources :

Example 36 with MailingListRecipient

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

the class Upgrade12 method upgradeEventHandlers.

@SuppressWarnings("deprecation")
private int upgradeEventHandlers(OutputStream os) {
    int upgraded = 0;
    List<EventHandlerVO> handlers = this.ejt.query(EVENT_HANDLER_SELECT, new EventHandlerRowMapper());
    // Convert them and update the database with the new handlers
    for (EventHandlerVO vo : handlers) {
        switch(vo.getHandlerType()) {
            case EventHandlerVO.TYPE_EMAIL:
                EmailEventHandlerVO emailHandler = new EmailEventHandlerVO();
                emailHandler.setId(vo.getId());
                emailHandler.setXid(vo.getXid());
                emailHandler.setName(vo.getAlias());
                emailHandler.setDisabled(vo.isDisabled());
                emailHandler.setDefinition(ModuleRegistry.getEventHandlerDefinition(EmailEventHandlerDefinition.TYPE_NAME));
                if (vo.getActiveRecipients() != null) {
                    List<MailingListRecipient> activeRecipients = new ArrayList<>();
                    for (RecipientListEntryBean entry : vo.getActiveRecipients()) {
                        activeRecipients.add(entry.createEmailRecipient());
                    }
                    emailHandler.setActiveRecipients(activeRecipients);
                }
                emailHandler.setSendEscalation(vo.isSendEscalation());
                emailHandler.setEscalationDelayType(vo.getEscalationDelayType());
                emailHandler.setEscalationDelay(vo.getEscalationDelay());
                if (vo.getEscalationRecipients() != null) {
                    List<MailingListRecipient> escalationRecipients = new ArrayList<>();
                    for (RecipientListEntryBean entry : vo.getEscalationRecipients()) {
                        escalationRecipients.add(entry.createEmailRecipient());
                    }
                    emailHandler.setEscalationRecipients(escalationRecipients);
                }
                emailHandler.setSendInactive(vo.isSendInactive());
                emailHandler.setInactiveOverride(vo.isInactiveOverride());
                if (vo.getInactiveRecipients() != null) {
                    List<MailingListRecipient> inactiveRecipients = new ArrayList<>();
                    for (RecipientListEntryBean entry : vo.getInactiveRecipients()) {
                        inactiveRecipients.add(entry.createEmailRecipient());
                    }
                    emailHandler.setInactiveRecipients(inactiveRecipients);
                }
                emailHandler.setIncludeSystemInfo(vo.isIncludeSystemInfo());
                emailHandler.setIncludePointValueCount(vo.getIncludePointValueCount());
                emailHandler.setIncludeLogfile(vo.isIncludeLogfile());
                upgradeEventHandler(emailHandler);
                upgraded++;
                break;
            case EventHandlerVO.TYPE_PROCESS:
                ProcessEventHandlerVO processHandler = new ProcessEventHandlerVO();
                processHandler.setId(vo.getId());
                processHandler.setXid(vo.getXid());
                processHandler.setName(vo.getAlias());
                processHandler.setDisabled(vo.isDisabled());
                processHandler.setDefinition(ModuleRegistry.getEventHandlerDefinition(ProcessEventHandlerDefinition.TYPE_NAME));
                processHandler.setActiveProcessCommand(vo.getActiveProcessCommand());
                processHandler.setActiveProcessTimeout(vo.getActiveProcessTimeout());
                processHandler.setInactiveProcessCommand(vo.getInactiveProcessCommand());
                processHandler.setInactiveProcessTimeout(vo.getInactiveProcessTimeout());
                upgradeEventHandler(processHandler);
                upgraded++;
                break;
            case EventHandlerVO.TYPE_SET_POINT:
                SetPointEventHandlerVO setPointHandler = new SetPointEventHandlerVO();
                setPointHandler.setId(vo.getId());
                setPointHandler.setXid(vo.getXid());
                setPointHandler.setName(vo.getAlias());
                setPointHandler.setDisabled(vo.isDisabled());
                setPointHandler.setDefinition(ModuleRegistry.getEventHandlerDefinition(SetPointEventHandlerDefinition.TYPE_NAME));
                setPointHandler.setTargetPointId(vo.getTargetPointId());
                setPointHandler.setActiveAction(vo.getActiveAction());
                setPointHandler.setActiveValueToSet(vo.getActiveValueToSet());
                setPointHandler.setActivePointId(vo.getActivePointId());
                setPointHandler.setInactiveAction(vo.getInactiveAction());
                setPointHandler.setInactiveValueToSet(vo.getInactiveValueToSet());
                setPointHandler.setInactivePointId(vo.getInactivePointId());
                upgradeEventHandler(setPointHandler);
                upgraded++;
                break;
            default:
                LOG.error("Unknown event detector type: " + vo.getHandlerType());
                try {
                    os.write(new String("Unknown event detector type: " + vo.getHandlerType()).getBytes(StandardCharsets.UTF_8));
                } catch (IOException e2) {
                    LOG.error("Unable to write to upgrade log.", e2);
                }
                break;
        }
    }
    return upgraded;
}
Also used : ProcessEventHandlerVO(com.serotonin.m2m2.vo.event.ProcessEventHandlerVO) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SetPointEventHandlerVO(com.serotonin.m2m2.vo.event.SetPointEventHandlerVO) SetPointEventHandlerVO(com.serotonin.m2m2.vo.event.SetPointEventHandlerVO) ProcessEventHandlerVO(com.serotonin.m2m2.vo.event.ProcessEventHandlerVO) EventHandlerVO(com.serotonin.m2m2.vo.event.EventHandlerVO) AbstractEventHandlerVO(com.serotonin.m2m2.vo.event.AbstractEventHandlerVO) EmailEventHandlerVO(com.serotonin.m2m2.vo.event.EmailEventHandlerVO) EmailEventHandlerVO(com.serotonin.m2m2.vo.event.EmailEventHandlerVO) MailingListRecipient(com.serotonin.m2m2.vo.mailingList.MailingListRecipient) RecipientListEntryBean(com.serotonin.m2m2.web.dwr.beans.RecipientListEntryBean)

Example 37 with MailingListRecipient

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

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

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

the class MailingListServiceTest method newVO.

@Override
MailingList newVO(User owner) {
    MailingList vo = new MailingList();
    vo.setXid(MailingListDao.getInstance().generateUniqueXid());
    vo.setName("MailingList");
    vo.setReceiveAlarmEmails(AlarmLevels.NONE);
    List<MailingListRecipient> entries = new ArrayList<>();
    AddressEntry entry = new AddressEntry();
    entry.setAddress("entry1@example.com");
    entries.add(entry);
    vo.setEntries(entries);
    return vo;
}
Also used : AddressEntry(com.serotonin.m2m2.vo.mailingList.AddressEntry) MailingList(com.serotonin.m2m2.vo.mailingList.MailingList) ArrayList(java.util.ArrayList) MailingListRecipient(com.serotonin.m2m2.vo.mailingList.MailingListRecipient)

Example 39 with MailingListRecipient

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

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

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

the class MailingListServiceTest method assertVoEqual.

@Override
void assertVoEqual(MailingList expected, MailingList actual) {
    assertEquals(expected.getId(), actual.getId());
    assertEquals(expected.getReceiveAlarmEmails(), actual.getReceiveAlarmEmails());
    assertEquals(expected.getEntries().size(), actual.getEntries().size());
    for (int i = 0; i < expected.getEntries().size(); i++) {
        MailingListRecipient expectedRecipient = expected.getEntries().get(i);
        MailingListRecipient actualRecipient = actual.getEntries().get(i);
        switch(expectedRecipient.getRecipientType()) {
            case ADDRESS:
            case PHONE_NUMBER:
                assertEquals(expectedRecipient.getReferenceAddress(), actualRecipient.getReferenceAddress());
                break;
            case USER:
            case USER_PHONE_NUMBER:
            case MAILING_LIST:
                assertEquals(expectedRecipient.getReferenceId(), actualRecipient.getReferenceId());
                break;
            default:
                fail("unknown recipient type");
        }
    }
    assertPermission(expected.getReadPermission(), actual.getReadPermission());
    assertPermission(expected.getEditPermission(), actual.getEditPermission());
}
Also used : 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