use of org.b3log.latke.service.ServiceException in project solo by b3log.
the class Filler method fillUserTemplate.
/**
* Fills the specified template.
*
* @param request the specified HTTP servlet request
* @param template the specified template
* @param dataModel data model
* @param preference the specified preference
* @throws ServiceException service exception
*/
public void fillUserTemplate(final HttpServletRequest request, final Template template, final Map<String, Object> dataModel, final JSONObject preference) throws ServiceException {
Stopwatchs.start("Fill User Template[name=" + template.getName() + "]");
try {
LOGGER.log(Level.DEBUG, "Filling user template[name{0}]", template.getName());
if (Templates.hasExpression(template, "<#list links as link>")) {
fillLinks(dataModel);
}
if (Templates.hasExpression(template, "<#list tags as tag>")) {
fillTags(dataModel);
}
if (Templates.hasExpression(template, "<#list recentComments as comment>")) {
fillRecentComments(dataModel, preference);
}
if (Templates.hasExpression(template, "<#list mostUsedTags as tag>")) {
fillMostUsedTags(dataModel, preference);
}
if (Templates.hasExpression(template, "<#list mostCommentArticles as article>")) {
fillMostCommentArticles(dataModel, preference);
}
if (Templates.hasExpression(template, "<#list mostViewCountArticles as article>")) {
fillMostViewCountArticles(dataModel, preference);
}
if (Templates.hasExpression(template, "<#list archiveDates as archiveDate>")) {
fillArchiveDates(dataModel, preference);
}
if (Templates.hasExpression(template, "<#include \"side.ftl\"/>")) {
fillSide(request, dataModel, preference);
}
final String noticeBoard = preference.getString(Option.ID_C_NOTICE_BOARD);
dataModel.put(Option.ID_C_NOTICE_BOARD, noticeBoard);
} catch (final JSONException e) {
LOGGER.log(Level.ERROR, "Fills user template failed", e);
throw new ServiceException(e);
} finally {
Stopwatchs.end();
}
}
use of org.b3log.latke.service.ServiceException in project solo by b3log.
the class Filler method fillBlogHeader.
/**
* Fills header.ftl.
*
* @param request the specified HTTP servlet request
* @param response the specified HTTP servlet response
* @param dataModel data model
* @param preference the specified preference
* @throws ServiceException service exception
*/
public void fillBlogHeader(final HttpServletRequest request, final HttpServletResponse response, final Map<String, Object> dataModel, final JSONObject preference) throws ServiceException {
Stopwatchs.start("Fill Header");
try {
LOGGER.debug("Filling header....");
final String topBarHTML = topBars.getTopBarHTML(request, response);
dataModel.put(Common.LOGIN_URL, userService.createLoginURL(Common.ADMIN_INDEX_URI));
dataModel.put(Common.LOGOUT_URL, userService.createLogoutURL("/"));
dataModel.put(Common.ONLINE_VISITOR_CNT, StatisticQueryService.getOnlineVisitorCount());
dataModel.put(Common.TOP_BAR, topBarHTML);
dataModel.put(Option.ID_C_ARTICLE_LIST_DISPLAY_COUNT, preference.getInt(Option.ID_C_ARTICLE_LIST_DISPLAY_COUNT));
dataModel.put(Option.ID_C_ARTICLE_LIST_PAGINATION_WINDOW_SIZE, preference.getInt(Option.ID_C_ARTICLE_LIST_PAGINATION_WINDOW_SIZE));
dataModel.put(Option.ID_C_LOCALE_STRING, preference.getString(Option.ID_C_LOCALE_STRING));
dataModel.put(Option.ID_C_BLOG_TITLE, preference.getString(Option.ID_C_BLOG_TITLE));
dataModel.put(Option.ID_C_BLOG_SUBTITLE, preference.getString(Option.ID_C_BLOG_SUBTITLE));
dataModel.put(Option.ID_C_HTML_HEAD, preference.getString(Option.ID_C_HTML_HEAD));
String metaKeywords = preference.getString(Option.ID_C_META_KEYWORDS);
if (Strings.isEmptyOrNull(metaKeywords)) {
metaKeywords = "";
}
dataModel.put(Option.ID_C_META_KEYWORDS, metaKeywords);
String metaDescription = preference.getString(Option.ID_C_META_DESCRIPTION);
if (Strings.isEmptyOrNull(metaDescription)) {
metaDescription = "";
}
dataModel.put(Option.ID_C_META_DESCRIPTION, metaDescription);
dataModel.put(Common.YEAR, String.valueOf(Calendar.getInstance().get(Calendar.YEAR)));
dataModel.put(Common.IS_LOGGED_IN, null != userQueryService.getCurrentUser(request));
dataModel.put(Common.FAVICON_API, SoloServletListener.FAVICON_API);
final String noticeBoard = preference.getString(Option.ID_C_NOTICE_BOARD);
dataModel.put(Option.ID_C_NOTICE_BOARD, noticeBoard);
final Query query = new Query().setPageCount(1);
final JSONObject result = userRepository.get(query);
final JSONArray users = result.getJSONArray(Keys.RESULTS);
final List<JSONObject> userList = CollectionUtils.jsonArrayToList(users);
dataModel.put(User.USERS, userList);
final JSONObject admin = userRepository.getAdmin();
dataModel.put(Common.ADMIN_USER, admin);
final String skinDirName = (String) request.getAttribute(Keys.TEMAPLTE_DIR_NAME);
dataModel.put(Skin.SKIN_DIR_NAME, skinDirName);
Keys.fillRuntime(dataModel);
fillMinified(dataModel);
fillPageNavigations(dataModel);
fillStatistic(dataModel);
} catch (final JSONException e) {
LOGGER.log(Level.ERROR, "Fills blog header failed", e);
throw new ServiceException(e);
} catch (final RepositoryException e) {
LOGGER.log(Level.ERROR, "Fills blog header failed", e);
throw new ServiceException(e);
} finally {
Stopwatchs.end();
}
}
use of org.b3log.latke.service.ServiceException in project solo by b3log.
the class Filler method fillRecentComments.
/**
* Fills post comments recently.
*
* @param dataModel data model
* @param preference the specified preference
* @throws ServiceException service exception
*/
public void fillRecentComments(final Map<String, Object> dataModel, final JSONObject preference) throws ServiceException {
Stopwatchs.start("Fill Recent Comments");
try {
LOGGER.debug("Filling recent comments....");
final int recentCommentDisplayCnt = preference.getInt(Option.ID_C_RECENT_COMMENT_DISPLAY_CNT);
final List<JSONObject> recentComments = commentRepository.getRecentComments(recentCommentDisplayCnt);
for (final JSONObject comment : recentComments) {
final String content = comment.getString(Comment.COMMENT_CONTENT);
comment.put(Comment.COMMENT_CONTENT, content);
comment.put(Comment.COMMENT_NAME, comment.getString(Comment.COMMENT_NAME));
comment.put(Comment.COMMENT_URL, comment.getString(Comment.COMMENT_URL));
// Erases email for security reason
comment.remove(Comment.COMMENT_EMAIL);
}
dataModel.put(Common.RECENT_COMMENTS, recentComments);
} catch (final JSONException e) {
LOGGER.log(Level.ERROR, "Fills recent comments failed", e);
throw new ServiceException(e);
} catch (final RepositoryException e) {
LOGGER.log(Level.ERROR, "Fills recent comments failed", e);
throw new ServiceException(e);
} finally {
Stopwatchs.end();
}
}
use of org.b3log.latke.service.ServiceException in project solo by b3log.
the class ArticleConsole method updateArticle.
/**
* Updates an article by the specified request json object.
*
* <p>
* Renders the response with a json object, for example,
* <pre>
* {
* "sc": boolean,
* "msg": ""
* }
* </pre>
* </p>
*
* @param context the specified http request context
* @param request the specified http servlet request, for example, <pre>
* {
* "article": {
* "oId": "",
* "articleTitle": "",
* "articleAbstract": "",
* "articleContent": "",
* "articleTags": "tag1,tag2,tag3",
* "articlePermalink": "", // optional
* "articleIsPublished": boolean,
* "articleSignId": "" // optional
* "articleCommentable": boolean,
* "articleViewPwd": "",
* "postToCommunity": boolean
* }
* }
* </pre>
*
* @param response the specified http servlet response
* @throws Exception exception
*/
@RequestProcessing(value = "/console/article/", method = HTTPRequestMethod.PUT)
public void updateArticle(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
if (!userQueryService.isLoggedIn(request, response)) {
response.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
final JSONRenderer renderer = new JSONRenderer();
context.setRenderer(renderer);
final JSONObject ret = new JSONObject();
try {
final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, response);
final JSONObject article = requestJSONObject.getJSONObject(Article.ARTICLE);
final String articleId = article.getString(Keys.OBJECT_ID);
renderer.setJSONObject(ret);
if (!articleQueryService.canAccessArticle(articleId, request)) {
ret.put(Keys.MSG, langPropsService.get("forbiddenLabel"));
ret.put(Keys.STATUS_CODE, false);
return;
}
articleMgmtService.updateArticle(requestJSONObject);
ret.put(Keys.MSG, langPropsService.get("updateSuccLabel"));
ret.put(Keys.STATUS_CODE, true);
} catch (final ServiceException e) {
LOGGER.log(Level.ERROR, e.getMessage(), e);
final JSONObject jsonObject = QueryResults.defaultResult();
renderer.setJSONObject(jsonObject);
jsonObject.put(Keys.MSG, e.getMessage());
}
}
use of org.b3log.latke.service.ServiceException in project solo by b3log.
the class ArticleMgmtService method topArticle.
/**
* Puts an article specified by the given article id to top or cancel top.
*
* @param articleId the given article id
* @param top the specified flag, {@code true} to top, {@code false} to
* cancel top
* @throws ServiceException service exception
*/
public void topArticle(final String articleId, final boolean top) throws ServiceException {
final Transaction transaction = articleRepository.beginTransaction();
try {
final JSONObject topArticle = articleRepository.get(articleId);
topArticle.put(ARTICLE_PUT_TOP, top);
articleRepository.update(articleId, topArticle);
transaction.commit();
} catch (final Exception e) {
if (transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.ERROR, "Can't put the article[oId{0}] to top", articleId);
throw new ServiceException(e);
}
}
Aggregations