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);
}
}
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;
}
}
}
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;
}
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);
}
}
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);
}
}
Aggregations