Commit 2c45639a authored by D's avatar D Committed by GitHub

Merge pull request #12177 from feroozkhanchintu/master

Refactored Long Method
parents 4860c78c 9ddeedc3
...@@ -159,55 +159,50 @@ public class FeedProcessor { ...@@ -159,55 +159,50 @@ public class FeedProcessor {
final boolean isFullContent = "fullContent".equals(preference.getString(Option.ID_C_FEED_OUTPUT_MODE)); final boolean isFullContent = "fullContent".equals(preference.getString(Option.ID_C_FEED_OUTPUT_MODE));
for (int i = 0; i < articles.length(); i++) { for (int i = 0; i < articles.length(); i++) {
Entry entry = getEntry(hasMultipleUsers, authorName, articles, isFullContent, i);
feed.addEntry(entry);
}
renderer.setContent(feed.toString());
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Get blog article feed error", e);
try {
context.getResponse().sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
} catch (final IOException ex) {
throw new RuntimeException(ex);
}
}
}
private Entry getEntry(final boolean hasMultipleUsers, String authorName, final JSONArray articles,
final boolean isFullContent, int i)
throws org.json.JSONException, org.b3log.latke.service.ServiceException {
final JSONObject article = articles.getJSONObject(i); final JSONObject article = articles.getJSONObject(i);
final Entry entry = new Entry(); final Entry entry = new Entry();
feed.addEntry(entry);
final String title = StringEscapeUtils.escapeXml(article.getString(Article.ARTICLE_TITLE)); final String title = StringEscapeUtils.escapeXml(article.getString(Article.ARTICLE_TITLE));
entry.setTitle(title); entry.setTitle(title);
final String summary = isFullContent final String summary = isFullContent ? StringEscapeUtils.escapeXml(article.getString(Article.ARTICLE_CONTENT))
? StringEscapeUtils.escapeXml(article.getString(Article.ARTICLE_CONTENT))
: StringEscapeUtils.escapeXml(article.optString(Article.ARTICLE_ABSTRACT)); : StringEscapeUtils.escapeXml(article.optString(Article.ARTICLE_ABSTRACT));
entry.setSummary(summary); entry.setSummary(summary);
final Date updated = (Date) article.get(Article.ARTICLE_UPDATE_DATE); final Date updated = (Date) article.get(Article.ARTICLE_UPDATE_DATE);
entry.setUpdated(updated); entry.setUpdated(updated);
final String link = Latkes.getServePath() + article.getString(Article.ARTICLE_PERMALINK); final String link = Latkes.getServePath() + article.getString(Article.ARTICLE_PERMALINK);
entry.setLink(link); entry.setLink(link);
entry.setId(link); entry.setId(link);
if (hasMultipleUsers) { if (hasMultipleUsers) {
authorName = StringEscapeUtils.escapeXml(articleQueryService.getAuthor(article).getString(User.USER_NAME)); authorName = StringEscapeUtils.escapeXml(articleQueryService.getAuthor(article).getString(User.USER_NAME));
} }
entry.setAuthor(authorName); entry.setAuthor(authorName);
final String tagsString = article.getString(Article.ARTICLE_TAGS_REF); final String tagsString = article.getString(Article.ARTICLE_TAGS_REF);
final String[] tagStrings = tagsString.split(","); final String[] tagStrings = tagsString.split(",");
for (int j = 0; j < tagStrings.length; j++) { for (int j = 0; j < tagStrings.length; j++) {
final Category catetory = new Category(); final Category catetory = new Category();
entry.addCatetory(catetory); entry.addCatetory(catetory);
final String tag = tagStrings[j]; final String tag = tagStrings[j];
catetory.setTerm(tag); catetory.setTerm(tag);
} }
} return entry;
renderer.setContent(feed.toString());
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Get blog article feed error", e);
try {
context.getResponse().sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
} catch (final IOException ex) {
throw new RuntimeException(ex);
}
}
} }
/** /**
...@@ -296,53 +291,49 @@ public class FeedProcessor { ...@@ -296,53 +291,49 @@ public class FeedProcessor {
final boolean isFullContent = "fullContent".equals(preference.getString(Option.ID_C_FEED_OUTPUT_MODE)); final boolean isFullContent = "fullContent".equals(preference.getString(Option.ID_C_FEED_OUTPUT_MODE));
for (int i = 0; i < articles.size(); i++) { for (int i = 0; i < articles.size(); i++) {
Entry entry = getEntryForArticle(articles, hasMultipleUsers, authorName, isFullContent, i);
feed.addEntry(entry);
}
renderer.setContent(feed.toString());
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Get tag article feed error", e);
try {
context.getResponse().sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
} catch (final IOException ex) {
throw new RuntimeException(ex);
}
}
}
private Entry getEntryForArticle(final List<JSONObject> articles, final boolean hasMultipleUsers, String authorName,
final boolean isFullContent, int i)
throws org.json.JSONException, org.b3log.latke.service.ServiceException {
final JSONObject article = articles.get(i); final JSONObject article = articles.get(i);
final Entry entry = new Entry(); final Entry entry = new Entry();
feed.addEntry(entry);
final String title = StringEscapeUtils.escapeXml(article.getString(Article.ARTICLE_TITLE)); final String title = StringEscapeUtils.escapeXml(article.getString(Article.ARTICLE_TITLE));
entry.setTitle(title); entry.setTitle(title);
final String summary = isFullContent final String summary = isFullContent ? StringEscapeUtils.escapeXml(article.getString(Article.ARTICLE_CONTENT))
? StringEscapeUtils.escapeXml(article.getString(Article.ARTICLE_CONTENT))
: StringEscapeUtils.escapeXml(article.optString(Article.ARTICLE_ABSTRACT)); : StringEscapeUtils.escapeXml(article.optString(Article.ARTICLE_ABSTRACT));
entry.setSummary(summary); entry.setSummary(summary);
final Date updated = (Date) article.get(Article.ARTICLE_UPDATE_DATE); final Date updated = (Date) article.get(Article.ARTICLE_UPDATE_DATE);
entry.setUpdated(updated); entry.setUpdated(updated);
final String link = Latkes.getServePath() + article.getString(Article.ARTICLE_PERMALINK); final String link = Latkes.getServePath() + article.getString(Article.ARTICLE_PERMALINK);
entry.setLink(link); entry.setLink(link);
entry.setId(link); entry.setId(link);
if (hasMultipleUsers) { if (hasMultipleUsers) {
authorName = StringEscapeUtils.escapeXml(articleQueryService.getAuthor(article).getString(User.USER_NAME)); authorName = StringEscapeUtils.escapeXml(articleQueryService.getAuthor(article).getString(User.USER_NAME));
} }
entry.setAuthor(authorName); entry.setAuthor(authorName);
final String tagsString = article.getString(Article.ARTICLE_TAGS_REF); final String tagsString = article.getString(Article.ARTICLE_TAGS_REF);
final String[] tagStrings = tagsString.split(","); final String[] tagStrings = tagsString.split(",");
for (int j = 0; j < tagStrings.length; j++) { for (int j = 0; j < tagStrings.length; j++) {
final Category catetory = new Category(); final Category catetory = new Category();
entry.addCatetory(catetory); entry.addCatetory(catetory);
catetory.setTerm(tagStrings[j]); catetory.setTerm(tagStrings[j]);
} }
} return entry;
renderer.setContent(feed.toString());
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Get tag article feed error", e);
try {
context.getResponse().sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
} catch (final IOException ex) {
throw new RuntimeException(ex);
}
}
} }
/** /**
...@@ -403,57 +394,52 @@ public class FeedProcessor { ...@@ -403,57 +394,52 @@ public class FeedProcessor {
final boolean isFullContent = "fullContent".equals(preference.getString(Option.ID_C_FEED_OUTPUT_MODE)); final boolean isFullContent = "fullContent".equals(preference.getString(Option.ID_C_FEED_OUTPUT_MODE));
for (int i = 0; i < articles.length(); i++) { for (int i = 0; i < articles.length(); i++) {
Item item = getItem(articles, hasMultipleUsers, authorName, isFullContent, i);
channel.addItem(item);
}
renderer.setContent(channel.toString());
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Get blog article rss error", e);
try {
context.getResponse().sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
} catch (final IOException ex) {
throw new RuntimeException(ex);
}
}
}
private Item getItem(final JSONArray articles, final boolean hasMultipleUsers, String authorName,
final boolean isFullContent, int i)
throws org.json.JSONException, org.b3log.latke.service.ServiceException {
final JSONObject article = articles.getJSONObject(i); final JSONObject article = articles.getJSONObject(i);
final Item item = new Item(); final Item item = new Item();
channel.addItem(item);
final String title = StringEscapeUtils.escapeXml(article.getString(Article.ARTICLE_TITLE)); final String title = StringEscapeUtils.escapeXml(article.getString(Article.ARTICLE_TITLE));
item.setTitle(title); item.setTitle(title);
final String description = isFullContent final String description = isFullContent
? StringEscapeUtils.escapeXml(article.getString(Article.ARTICLE_CONTENT)) ? StringEscapeUtils.escapeXml(article.getString(Article.ARTICLE_CONTENT))
: StringEscapeUtils.escapeXml(article.optString(Article.ARTICLE_ABSTRACT)); : StringEscapeUtils.escapeXml(article.optString(Article.ARTICLE_ABSTRACT));
item.setDescription(description); item.setDescription(description);
final Date pubDate = (Date) article.get(Article.ARTICLE_UPDATE_DATE); final Date pubDate = (Date) article.get(Article.ARTICLE_UPDATE_DATE);
item.setPubDate(pubDate); item.setPubDate(pubDate);
final String link = Latkes.getServePath() + article.getString(Article.ARTICLE_PERMALINK); final String link = Latkes.getServePath() + article.getString(Article.ARTICLE_PERMALINK);
item.setLink(link); item.setLink(link);
item.setGUID(link); item.setGUID(link);
final String authorEmail = article.getString(Article.ARTICLE_AUTHOR_EMAIL); final String authorEmail = article.getString(Article.ARTICLE_AUTHOR_EMAIL);
if (hasMultipleUsers) { if (hasMultipleUsers) {
authorName = StringEscapeUtils.escapeXml(articleQueryService.getAuthor(article).getString(User.USER_NAME)); authorName = StringEscapeUtils.escapeXml(articleQueryService.getAuthor(article).getString(User.USER_NAME));
} }
item.setAuthor(authorEmail + "(" + authorName + ")"); item.setAuthor(authorEmail + "(" + authorName + ")");
final String tagsString = article.getString(Article.ARTICLE_TAGS_REF); final String tagsString = article.getString(Article.ARTICLE_TAGS_REF);
final String[] tagStrings = tagsString.split(","); final String[] tagStrings = tagsString.split(",");
for (int j = 0; j < tagStrings.length; j++) { for (int j = 0; j < tagStrings.length; j++) {
final org.b3log.solo.model.feed.rss.Category catetory = new org.b3log.solo.model.feed.rss.Category(); final org.b3log.solo.model.feed.rss.Category catetory = new org.b3log.solo.model.feed.rss.Category();
item.addCatetory(catetory); item.addCatetory(catetory);
final String tag = tagStrings[j]; final String tag = tagStrings[j];
catetory.setTerm(tag); catetory.setTerm(tag);
} }
} return item;
renderer.setContent(channel.toString());
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Get blog article rss error", e);
try {
context.getResponse().sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
} catch (final IOException ex) {
throw new RuntimeException(ex);
}
}
} }
/** /**
...@@ -547,54 +533,50 @@ public class FeedProcessor { ...@@ -547,54 +533,50 @@ public class FeedProcessor {
final boolean isFullContent = "fullContent".equals(preference.getString(Option.ID_C_FEED_OUTPUT_MODE)); final boolean isFullContent = "fullContent".equals(preference.getString(Option.ID_C_FEED_OUTPUT_MODE));
for (int i = 0; i < articles.size(); i++) { for (int i = 0; i < articles.size(); i++) {
Item item = getItemForArticles(articles, hasMultipleUsers, authorName, isFullContent, i);
channel.addItem(item);
}
renderer.setContent(channel.toString());
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Get tag article rss error", e);
try {
context.getResponse().sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
} catch (final IOException ex) {
throw new RuntimeException(ex);
}
}
}
private Item getItemForArticles(final List<JSONObject> articles, final boolean hasMultipleUsers, String authorName,
final boolean isFullContent, int i)
throws org.json.JSONException, org.b3log.latke.service.ServiceException {
final JSONObject article = articles.get(i); final JSONObject article = articles.get(i);
final Item item = new Item(); final Item item = new Item();
channel.addItem(item);
final String title = StringEscapeUtils.escapeXml(article.getString(Article.ARTICLE_TITLE)); final String title = StringEscapeUtils.escapeXml(article.getString(Article.ARTICLE_TITLE));
item.setTitle(title); item.setTitle(title);
final String description = isFullContent final String description = isFullContent
? StringEscapeUtils.escapeXml(article.getString(Article.ARTICLE_CONTENT)) ? StringEscapeUtils.escapeXml(article.getString(Article.ARTICLE_CONTENT))
: StringEscapeUtils.escapeXml(article.optString(Article.ARTICLE_ABSTRACT)); : StringEscapeUtils.escapeXml(article.optString(Article.ARTICLE_ABSTRACT));
item.setDescription(description); item.setDescription(description);
final Date pubDate = (Date) article.get(Article.ARTICLE_UPDATE_DATE); final Date pubDate = (Date) article.get(Article.ARTICLE_UPDATE_DATE);
item.setPubDate(pubDate); item.setPubDate(pubDate);
final String link = Latkes.getServePath() + article.getString(Article.ARTICLE_PERMALINK); final String link = Latkes.getServePath() + article.getString(Article.ARTICLE_PERMALINK);
item.setLink(link); item.setLink(link);
item.setGUID(link); item.setGUID(link);
final String authorEmail = article.getString(Article.ARTICLE_AUTHOR_EMAIL); final String authorEmail = article.getString(Article.ARTICLE_AUTHOR_EMAIL);
if (hasMultipleUsers) { if (hasMultipleUsers) {
authorName = StringEscapeUtils.escapeXml(articleQueryService.getAuthor(article).getString(User.USER_NAME)); authorName = StringEscapeUtils.escapeXml(articleQueryService.getAuthor(article).getString(User.USER_NAME));
} }
item.setAuthor(authorEmail + "(" + authorName + ")"); item.setAuthor(authorEmail + "(" + authorName + ")");
final String tagsString = article.getString(Article.ARTICLE_TAGS_REF); final String tagsString = article.getString(Article.ARTICLE_TAGS_REF);
final String[] tagStrings = tagsString.split(","); final String[] tagStrings = tagsString.split(",");
for (int j = 0; j < tagStrings.length; j++) { for (int j = 0; j < tagStrings.length; j++) {
final org.b3log.solo.model.feed.rss.Category catetory = new org.b3log.solo.model.feed.rss.Category(); final org.b3log.solo.model.feed.rss.Category catetory = new org.b3log.solo.model.feed.rss.Category();
item.addCatetory(catetory); item.addCatetory(catetory);
catetory.setTerm(tagStrings[j]); catetory.setTerm(tagStrings[j]);
} }
} return item;
renderer.setContent(channel.toString());
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Get tag article rss error", e);
try {
context.getResponse().sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
} catch (final IOException ex) {
throw new RuntimeException(ex);
}
}
} }
} }
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