Commit 61a45f56 authored by wang lei's avatar wang lei

邮件回复模板功能完善

parent 0b2e58be
...@@ -44,12 +44,14 @@ import org.json.JSONObject; ...@@ -44,12 +44,14 @@ import org.json.JSONObject;
* @version 1.1.1.7, Nov 20, 2015 * @version 1.1.1.7, Nov 20, 2015
* @since 0.3.1 * @since 0.3.1
*/ */
public final class ArticleCommentReplyNotifier extends AbstractEventListener<JSONObject> { public final class ArticleCommentReplyNotifier extends
AbstractEventListener<JSONObject> {
/** /**
* Logger. * Logger.
*/ */
private static final Logger LOGGER = Logger.getLogger(ArticleCommentReplyNotifier.class.getName()); private static final Logger LOGGER = Logger
.getLogger(ArticleCommentReplyNotifier.class.getName());
/** /**
* Mail service. * Mail service.
...@@ -62,73 +64,109 @@ public final class ArticleCommentReplyNotifier extends AbstractEventListener<JSO ...@@ -62,73 +64,109 @@ public final class ArticleCommentReplyNotifier extends AbstractEventListener<JSO
final JSONObject comment = eventData.optJSONObject(Comment.COMMENT); final JSONObject comment = eventData.optJSONObject(Comment.COMMENT);
final JSONObject article = eventData.optJSONObject(Article.ARTICLE); final JSONObject article = eventData.optJSONObject(Article.ARTICLE);
LOGGER.log(Level.DEBUG, "Processing an event[type={0}, data={1}] in listener[className={2}]", LOGGER.log(
new Object[]{event.getType(), eventData, ArticleCommentReplyNotifier.class.getName()}); Level.DEBUG,
final String originalCommentId = comment.optString(Comment.COMMENT_ORIGINAL_COMMENT_ID); "Processing an event[type={0}, data={1}] in listener[className={2}]",
new Object[] { event.getType(), eventData,
ArticleCommentReplyNotifier.class.getName() });
final String originalCommentId = comment
.optString(Comment.COMMENT_ORIGINAL_COMMENT_ID);
if (Strings.isEmptyOrNull(originalCommentId)) { if (Strings.isEmptyOrNull(originalCommentId)) {
LOGGER.log(Level.DEBUG, "This comment[id={0}] is not a reply", comment.optString(Keys.OBJECT_ID)); LOGGER.log(Level.DEBUG, "This comment[id={0}] is not a reply",
comment.optString(Keys.OBJECT_ID));
return; return;
} }
if (Latkes.getServePath().contains("localhost")) { if (Latkes.getServePath().contains("localhost")) {
LOGGER.log(Level.INFO, "Solo runs on local server, so should not send mail"); LOGGER.log(Level.INFO,
"Solo runs on local server, so should not send mail");
return; return;
} }
final LatkeBeanManager beanManager = Lifecycle.getBeanManager(); final LatkeBeanManager beanManager = Lifecycle.getBeanManager();
final PreferenceQueryService preferenceQueryService = beanManager.getReference(PreferenceQueryService.class); final PreferenceQueryService preferenceQueryService = beanManager
.getReference(PreferenceQueryService.class);
final CommentRepository commentRepository = beanManager.getReference(CommentRepositoryImpl.class); final CommentRepository commentRepository = beanManager
.getReference(CommentRepositoryImpl.class);
try { try {
final String commentEmail = comment.getString(Comment.COMMENT_EMAIL); final String commentEmail = comment
final JSONObject originalComment = commentRepository.get(originalCommentId); .getString(Comment.COMMENT_EMAIL);
final String originalCommentEmail = originalComment.getString(Comment.COMMENT_EMAIL); final JSONObject originalComment = commentRepository
.get(originalCommentId);
final String originalCommentEmail = originalComment
.getString(Comment.COMMENT_EMAIL);
if (originalCommentEmail.equalsIgnoreCase(commentEmail)) { if (originalCommentEmail.equalsIgnoreCase(commentEmail)) {
return; return;
} }
final JSONObject preference = preferenceQueryService.getPreference(); final JSONObject preference = preferenceQueryService
.getPreference();
if (null == preference) { if (null == preference) {
throw new EventException("Not found preference"); throw new EventException("Not found preference");
} }
final String blogTitle = preference.getString(Option.ID_C_BLOG_TITLE); final String blogTitle = preference
final String adminEmail = preference.getString(Option.ID_C_ADMIN_EMAIL); .getString(Option.ID_C_BLOG_TITLE);
final String adminEmail = preference
.getString(Option.ID_C_ADMIN_EMAIL);
final String commentContent = comment.getString(Comment.COMMENT_CONTENT); final String commentContent = comment
final String commentSharpURL = comment.getString(Comment.COMMENT_SHARP_URL); .getString(Comment.COMMENT_CONTENT);
final String commentSharpURL = comment
.getString(Comment.COMMENT_SHARP_URL);
final Message message = new Message(); final Message message = new Message();
message.setFrom(adminEmail); message.setFrom(adminEmail);
message.addRecipient(originalCommentEmail); message.addRecipient(originalCommentEmail);
final JSONObject replyNotificationTemplate = preferenceQueryService.getReplyNotificationTemplate(); final JSONObject replyNotificationTemplate = preferenceQueryService
final String mailSubject = replyNotificationTemplate.getString("subject").replace("${blogTitle}", blogTitle); .getReplyNotificationTemplate();
message.setSubject(mailSubject); final String articleTitle = article
final String articleTitle = article.getString(Article.ARTICLE_TITLE); .getString(Article.ARTICLE_TITLE);
final String articleLink = Latkes.getServePath() + article.getString(Article.ARTICLE_PERMALINK); final String articleLink = Latkes.getServePath()
+ article.getString(Article.ARTICLE_PERMALINK);
final String commentName = comment.getString(Comment.COMMENT_NAME); final String commentName = comment.getString(Comment.COMMENT_NAME);
final String commentURL = comment.getString(Comment.COMMENT_URL); final String commentURL = comment.getString(Comment.COMMENT_URL);
String commenter; String commenter;
if (!"http://".equals(commentURL)) { if (!"http://".equals(commentURL)) {
commenter = "<a target=\"_blank\" " + "href=\"" + commentURL + "\">" + commentName + "</a>"; commenter = "<a target=\"_blank\" " + "href=\"" + commentURL
+ "\">" + commentName + "</a>";
} else { } else {
commenter = commentName; commenter = commentName;
} }
final String mailSubject = replyNotificationTemplate.getString(
"subject").replace("${postLink}", articleLink)
.replace("${postTitle}", articleTitle)
.replace("${replier}", commenter)
.replace("${blogTitle}", blogTitle)
.replace("${replyURL}",
Latkes.getServePath() + commentSharpURL)
.replace("${replyContent}", commentContent);
final String mailBody = replyNotificationTemplate.getString("body").replace("${postLink}", articleLink).replace("${postTitle}", articleTitle).replace("${replier}", commenter).replace("${replyURL}", Latkes.getServePath() + commentSharpURL).replace( message.setSubject(mailSubject);
"${replyContent}", commentContent); final String mailBody = replyNotificationTemplate
.getString("body")
.replace("${postLink}", articleLink)
.replace("${postTitle}", articleTitle)
.replace("${replier}", commenter)
.replace("${blogTitle}", blogTitle)
.replace("${replyURL}",
Latkes.getServePath() + commentSharpURL)
.replace("${replyContent}", commentContent);
message.setHtmlBody(mailBody); message.setHtmlBody(mailBody);
LOGGER.log(Level.DEBUG, "Sending a mail[mailSubject={0}, mailBody=[{1}] to [{2}]", LOGGER.log(
new Object[]{mailSubject, mailBody, originalCommentEmail}); Level.DEBUG,
"Sending a mail[mailSubject={0}, mailBody=[{1}] to [{2}]",
new Object[] { mailSubject, mailBody, originalCommentEmail });
mailService.send(message); mailService.send(message);
} catch (final Exception e) { } catch (final Exception e) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment