|
|
|
@ -1,9 +1,16 @@
|
|
|
|
|
package it.tref.liferay.portos.mailmanager.util; |
|
|
|
|
|
|
|
|
|
import it.tref.liferay.portos.mailmanager.model.Email; |
|
|
|
|
import it.tref.liferay.portos.mailmanager.model.EmailTemplate; |
|
|
|
|
import it.tref.liferay.portos.mailmanager.service.EmailLocalServiceUtil; |
|
|
|
|
import it.tref.liferay.portos.mailmanager.service.EmailTemplateLocalServiceUtil; |
|
|
|
|
import it.tref.liferay.portos.mailmanager.shared.util.MailManagerConstants; |
|
|
|
|
import it.tref.liferay.portos.mailmanager.shared.util.MailUtil; |
|
|
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
import com.liferay.mail.model.FileAttachment; |
|
|
|
|
import com.liferay.portal.kernel.json.JSONObject; |
|
|
|
|
import com.liferay.portal.kernel.log.Log; |
|
|
|
|
import com.liferay.portal.kernel.log.LogFactoryUtil; |
|
|
|
|
import com.liferay.portal.kernel.mail.MailMessage; |
|
|
|
|
import com.liferay.portal.kernel.mail.SMTPAccount; |
|
|
|
|
import com.liferay.portal.kernel.util.StringPool; |
|
|
|
@ -13,50 +20,43 @@ import com.liferay.portal.model.Company;
|
|
|
|
|
import com.liferay.portal.service.CompanyLocalServiceUtil; |
|
|
|
|
import com.liferay.portal.service.ServiceContext; |
|
|
|
|
|
|
|
|
|
import it.tref.liferay.portos.mailmanager.model.Email; |
|
|
|
|
import it.tref.liferay.portos.mailmanager.model.EmailTemplate; |
|
|
|
|
import it.tref.liferay.portos.mailmanager.service.EmailLocalServiceUtil; |
|
|
|
|
import it.tref.liferay.portos.mailmanager.service.EmailTemplateLocalServiceUtil; |
|
|
|
|
import it.tref.liferay.portos.mailmanager.shared.util.MailManagerConstants; |
|
|
|
|
import it.tref.liferay.portos.mailmanager.shared.util.MailUtil; |
|
|
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
public class MailManagerUtil { |
|
|
|
|
|
|
|
|
|
public static Email addEmailByTemplate(String className, long classPK, long emailTemplateId, |
|
|
|
|
JSONObject templateParamJSON, List<FileAttachment> fileAttachments, ServiceContext serviceContext) |
|
|
|
|
throws Exception { |
|
|
|
|
return addEmailByTemplate(className, classPK, emailTemplateId, templateParamJSON, fileAttachments, |
|
|
|
|
false, serviceContext); |
|
|
|
|
|
|
|
|
|
return addEmailByTemplate(className, classPK, emailTemplateId, templateParamJSON, fileAttachments, false, |
|
|
|
|
serviceContext); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static Email addEmailByTemplate(String className, long classPK, long emailTemplateId, |
|
|
|
|
JSONObject templateParamJSON, ServiceContext serviceContext) throws Exception { |
|
|
|
|
return addEmailByTemplate(className, classPK, emailTemplateId, templateParamJSON, null, false, |
|
|
|
|
serviceContext); |
|
|
|
|
|
|
|
|
|
return addEmailByTemplate(className, classPK, emailTemplateId, templateParamJSON, null, false, serviceContext); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static Email addEmailByTemplate(String className, long classPK, long emailTemplateId, |
|
|
|
|
JSONObject templateParamJSON, List<FileAttachment> fileAttachments, boolean sendSync, |
|
|
|
|
ServiceContext serviceContext) throws Exception { |
|
|
|
|
|
|
|
|
|
EmailTemplate emailTemplate = EmailTemplateLocalServiceUtil.getEmailTemplate(emailTemplateId); |
|
|
|
|
return addEmailByTemplate(className, classPK, emailTemplate, templateParamJSON, fileAttachments, |
|
|
|
|
sendSync, serviceContext); |
|
|
|
|
return addEmailByTemplate(className, classPK, emailTemplate, templateParamJSON, fileAttachments, sendSync, |
|
|
|
|
serviceContext); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static Email addEmailByTemplate(String className, long classPK, String emailTemplateName, |
|
|
|
|
JSONObject templateParamJSON, List<FileAttachment> fileAttachments, ServiceContext serviceContext) |
|
|
|
|
throws Exception { |
|
|
|
|
|
|
|
|
|
return addEmailByTemplate(className, classPK, emailTemplateName, templateParamJSON, fileAttachments, |
|
|
|
|
false, serviceContext); |
|
|
|
|
return addEmailByTemplate(className, classPK, emailTemplateName, templateParamJSON, fileAttachments, false, |
|
|
|
|
serviceContext); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static Email addEmailByTemplate(String className, long classPK, String emailTemplateName, |
|
|
|
|
JSONObject templateParamJSON, ServiceContext serviceContext) throws Exception { |
|
|
|
|
return addEmailByTemplate(className, classPK, emailTemplateName, templateParamJSON, null, false, |
|
|
|
|
serviceContext); |
|
|
|
|
|
|
|
|
|
return addEmailByTemplate(className, classPK, emailTemplateName, templateParamJSON, null, false, serviceContext); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static Email addEmailByTemplate(String className, long classPK, String emailTemplateName, |
|
|
|
@ -65,15 +65,13 @@ public class MailManagerUtil {
|
|
|
|
|
|
|
|
|
|
EmailTemplate emailTemplate = EmailTemplateLocalServiceUtil.fetchByC_GroupId_Name( |
|
|
|
|
serviceContext.getCompanyId(), serviceContext.getScopeGroupId(), emailTemplateName); |
|
|
|
|
|
|
|
|
|
if (Validator.isNull(emailTemplate)) { |
|
|
|
|
Company company = CompanyLocalServiceUtil.getCompany(serviceContext.getCompanyId()); |
|
|
|
|
emailTemplate = EmailTemplateLocalServiceUtil.findByC_GroupId_Name(serviceContext.getCompanyId(), |
|
|
|
|
company.getGroupId(), emailTemplateName); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return addEmailByTemplate(className, classPK, emailTemplate, templateParamJSON, fileAttachments, |
|
|
|
|
sendSync, serviceContext); |
|
|
|
|
return addEmailByTemplate(className, classPK, emailTemplate, templateParamJSON, fileAttachments, sendSync, |
|
|
|
|
serviceContext); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static Email addEmailByTemplate(String className, long classPK, EmailTemplate emailTemplate, |
|
|
|
@ -84,7 +82,6 @@ public class MailManagerUtil {
|
|
|
|
|
templateParamJSON.getJSONObject(MailManagerConstants.TEMPLATE_PARAMETER_VARIABLES_KEY)); |
|
|
|
|
String body = VelocityUtil.evaluate(emailTemplate.getBody(), |
|
|
|
|
templateParamJSON.getJSONObject(MailManagerConstants.TEMPLATE_PARAMETER_VARIABLES_KEY)); |
|
|
|
|
|
|
|
|
|
String to = templateParamJSON.getString(MailManagerConstants.TEMPLATE_PARAMETER_TO_KEY); |
|
|
|
|
if (Validator.isNull(to)) { |
|
|
|
|
to = emailTemplate.getTo(); |
|
|
|
@ -97,16 +94,15 @@ public class MailManagerUtil {
|
|
|
|
|
if (Validator.isNull(ccn)) { |
|
|
|
|
ccn = emailTemplate.getCcn(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return EmailLocalServiceUtil.addEmail(className, classPK, emailTemplate.getFrom(), |
|
|
|
|
StringUtil.split(to, StringPool.COMMA), StringUtil.split(cc, StringPool.COMMA), |
|
|
|
|
StringUtil.split(ccn, StringPool.COMMA), subject, body, true, fileAttachments, sendSync, |
|
|
|
|
emailTemplate.getTemplateName(), serviceContext); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static Email addEmail(String className, long classPK, MailMessage mailMessage, |
|
|
|
|
ServiceContext serviceContext) throws Exception { |
|
|
|
|
public static Email addEmail(String className, long classPK, MailMessage mailMessage, ServiceContext serviceContext) |
|
|
|
|
throws Exception { |
|
|
|
|
|
|
|
|
|
return addEmail(className, classPK, mailMessage, false, serviceContext); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -118,37 +114,28 @@ public class MailManagerUtil {
|
|
|
|
|
String[] cc = MailUtil.fromInternetAddresses(mailMessage.getCC()); |
|
|
|
|
String[] bcc = MailUtil.fromInternetAddresses(mailMessage.getBCC()); |
|
|
|
|
String[] bulkAddresses = MailUtil.fromInternetAddresses(mailMessage.getBulkAddresses()); |
|
|
|
|
|
|
|
|
|
String subject = mailMessage.getSubject(); |
|
|
|
|
String body = mailMessage.getBody(); |
|
|
|
|
|
|
|
|
|
boolean htmlFormat = mailMessage.getHTMLFormat(); |
|
|
|
|
String[] replyTo = MailUtil.fromInternetAddresses(mailMessage.getReplyTo()); |
|
|
|
|
String messageId = mailMessage.getMessageId(); |
|
|
|
|
String inReplyTo = mailMessage.getInReplyTo(); |
|
|
|
|
|
|
|
|
|
List<FileAttachment> fileAttachments = mailMessage.getFileAttachments(); |
|
|
|
|
|
|
|
|
|
SMTPAccount smtpAccount = mailMessage.getSMTPAccount(); |
|
|
|
|
|
|
|
|
|
String smtpHost = null; |
|
|
|
|
int smtpPort = 0; |
|
|
|
|
boolean smtpSecure = false; |
|
|
|
|
String smtpUsername = null; |
|
|
|
|
String smtpPassword = null; |
|
|
|
|
|
|
|
|
|
if (smtpAccount != null) { |
|
|
|
|
|
|
|
|
|
smtpHost = smtpAccount.getHost(); |
|
|
|
|
smtpPort = smtpAccount.getPort(); |
|
|
|
|
smtpSecure = smtpAccount.isSecure(); |
|
|
|
|
smtpUsername = smtpAccount.getUser(); |
|
|
|
|
smtpPassword = smtpAccount.getPassword(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return EmailLocalServiceUtil.addEmail(className, classPK, from, to, cc, bcc, bulkAddresses, subject, |
|
|
|
|
body, htmlFormat, replyTo, messageId, inReplyTo, smtpHost, smtpPort, smtpSecure, |
|
|
|
|
smtpUsername, smtpPassword, fileAttachments, sendSync, null, serviceContext); |
|
|
|
|
return EmailLocalServiceUtil.addEmail(className, classPK, from, to, cc, bcc, bulkAddresses, subject, body, |
|
|
|
|
htmlFormat, replyTo, messageId, inReplyTo, smtpHost, smtpPort, smtpSecure, smtpUsername, smtpPassword, |
|
|
|
|
fileAttachments, sendSync, null, serviceContext); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|