use of de.metas.email.EMail in project metasfresh-webui-api by metasfresh.
the class MailRestController method changeEmail.
private void changeEmail(final WebuiEmail email, final WebuiEmailBuilder newEmailBuilder, final JSONDocumentChangedEvent event) {
if (!event.isReplace()) {
throw new AdempiereException("Unsupported event").setParameter("event", event);
}
final String fieldName = event.getPath();
if (PATCH_FIELD_To.equals(fieldName)) {
@SuppressWarnings("unchecked") final List<Object> jsonTo = (List<Object>) event.getValue();
@SuppressWarnings("unchecked") final LookupValuesList to = jsonTo.stream().map(mapObj -> (Map<String, Object>) mapObj).map(map -> JSONLookupValue.integerLookupValueFromJsonMap(map)).collect(LookupValuesList.collect());
newEmailBuilder.to(to);
} else if (PATCH_FIELD_Subject.equals(fieldName)) {
final String subject = (String) event.getValue();
newEmailBuilder.subject(subject);
} else if (PATCH_FIELD_Message.equals(fieldName)) {
final String message = (String) event.getValue();
newEmailBuilder.message(message);
} else if (PATCH_FIELD_Attachments.equals(fieldName)) {
@SuppressWarnings("unchecked") final List<Object> jsonAttachments = (List<Object>) event.getValue();
@SuppressWarnings("unchecked") final LookupValuesList attachments = jsonAttachments.stream().map(mapObj -> (Map<String, Object>) mapObj).map(map -> JSONLookupValue.stringLookupValueFromJsonMap(map)).collect(LookupValuesList.collect());
newEmailBuilder.attachments(attachments);
} else if (PATCH_FIELD_TemplateId.equals(fieldName)) {
@SuppressWarnings("unchecked") final LookupValue templateId = JSONLookupValue.integerLookupValueFromJsonMap((Map<String, Object>) event.getValue());
applyTemplate(email, newEmailBuilder, templateId);
} else {
throw new AdempiereException("Unsupported event path").setParameter("event", event).setParameter("fieldName", fieldName).setParameter("availablePaths", PATCH_FIELD_ALL);
}
}
use of de.metas.email.EMail in project metasfresh-webui-api by metasfresh.
the class MailRestController method sendEmail.
private WebuiEmail sendEmail(final WebuiEmail webuiEmail) {
final String emailId = webuiEmail.getEmailId();
//
// Create the email object
final I_AD_Client adClient = Services.get(IClientDAO.class).retriveClient(Env.getCtx(), userSession.getAD_Client_ID());
final String mailCustomType = null;
final I_AD_User from = Services.get(IUserDAO.class).retrieveUser(webuiEmail.getFrom().getIdAsInt());
final List<String> toList = extractEMailAddreses(webuiEmail.getTo()).collect(ImmutableList.toImmutableList());
if (toList.isEmpty()) {
throw new FillMandatoryException("To");
}
final String to = toList.get(0);
final String subject = webuiEmail.getSubject();
final String message = webuiEmail.getMessage();
final boolean html = false;
final EMail email = Services.get(IMailBL.class).createEMail(adClient, mailCustomType, from, to, subject, message, html);
toList.stream().skip(1).forEach(email::addTo);
webuiEmail.getAttachments().stream().map(webuiAttachment -> {
final byte[] content = mailAttachmentsRepo.getAttachmentAsByteArray(emailId, webuiAttachment);
return EMailAttachment.of(webuiAttachment.getDisplayName(), content);
}).forEach(email::addAttachment);
//
// Actually send the email
final EMailSentStatus sentStatus = email.send();
if (!sentStatus.isSentOK()) {
throw new AdempiereException("Failed sending the email: " + sentStatus.getSentMsg());
}
//
// Delete temporary attachments
mailAttachmentsRepo.deleteAttachments(emailId, webuiEmail.getAttachments());
// Mark the webui email as sent
return webuiEmail.toBuilder().sent(true).build();
}
Aggregations