use of org.b3log.latke.service.ServiceException in project solo by b3log.
the class ArticleMgmtService method getPermalinkForAddArticle.
/**
* Gets article permalink for adding article with the specified
* article.
*
* @param article the specified article
* @return permalink
* @throws ServiceException if invalid permalink occurs
*/
private String getPermalinkForAddArticle(final JSONObject article) throws ServiceException {
final Date date = (Date) article.opt(Article.ARTICLE_CREATE_DATE);
String ret = article.optString(Article.ARTICLE_PERMALINK);
if (Strings.isEmptyOrNull(ret)) {
ret = "/articles/" + DateFormatUtils.format(date, "yyyy/MM/dd") + "/" + article.optString(Keys.OBJECT_ID) + ".html";
}
if (!ret.startsWith("/")) {
ret = "/" + ret;
}
if (PermalinkQueryService.invalidArticlePermalinkFormat(ret)) {
throw new ServiceException(langPropsService.get("invalidPermalinkFormatLabel"));
}
if (permalinkQueryService.exist(ret)) {
throw new ServiceException(langPropsService.get("duplicatedPermalinkLabel"));
}
return ret.replaceAll(" ", "-");
}
use of org.b3log.latke.service.ServiceException in project solo by b3log.
the class PageQueryService method getPage.
/**
* Gets a page by the specified page id.
*
* @param pageId the specified page id
* @return for example,
* <pre>
* {
* "page": {
* "oId": "",
* "pageTitle": "",
* "pageContent": ""
* "pageOrder": int,
* "pagePermalink": "",
* "pageCommentCount": int,
* "pageCommentable": boolean,
* "pageType": "",
* "pageOpenTarget": ""
* }
* }
* </pre>, returns {@code null} if not found
* @throws ServiceException service exception
*/
public JSONObject getPage(final String pageId) throws ServiceException {
final JSONObject ret = new JSONObject();
try {
final JSONObject page = pageRepository.get(pageId);
if (null == page) {
return null;
}
ret.put(Page.PAGE, page);
return ret;
} catch (final Exception e) {
LOGGER.log(Level.ERROR, e.getMessage(), e);
throw new ServiceException(e);
}
}
use of org.b3log.latke.service.ServiceException in project solo by b3log.
the class PreferenceMgmtService method updateReplyNotificationTemplate.
/**
* Updates the reply notification template with the specified reply notification template.
*
* @param replyNotificationTemplate the specified reply notification template
* @throws ServiceException service exception
*/
public void updateReplyNotificationTemplate(final JSONObject replyNotificationTemplate) throws ServiceException {
final Transaction transaction = optionRepository.beginTransaction();
try {
final JSONObject bodyOpt = optionRepository.get(Option.ID_C_REPLY_NOTI_TPL_BODY);
bodyOpt.put(Option.OPTION_VALUE, replyNotificationTemplate.optString("body"));
optionRepository.update(Option.ID_C_REPLY_NOTI_TPL_BODY, bodyOpt);
final JSONObject subjectOpt = optionRepository.get(Option.ID_C_REPLY_NOTI_TPL_SUBJECT);
subjectOpt.put(Option.OPTION_VALUE, replyNotificationTemplate.optString("subject"));
optionRepository.update(Option.ID_C_REPLY_NOTI_TPL_SUBJECT, subjectOpt);
transaction.commit();
} catch (final Exception e) {
if (transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.ERROR, "Updates reply notification failed", e);
throw new ServiceException(e);
}
}
use of org.b3log.latke.service.ServiceException in project solo by b3log.
the class PreferenceQueryService method getReplyNotificationTemplate.
/**
* Gets the reply notification template.
*
* @return reply notification template, returns {@code null} if not found
* @throws ServiceException service exception
*/
public JSONObject getReplyNotificationTemplate() throws ServiceException {
try {
final JSONObject ret = new JSONObject();
final JSONObject preference = getPreference();
ret.put("subject", preference.optString(Option.ID_C_REPLY_NOTI_TPL_SUBJECT));
ret.put("body", preference.optString(Option.ID_C_REPLY_NOTI_TPL_BODY));
return ret;
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Updates reply notification template failed", e);
throw new ServiceException(e);
}
}
use of org.b3log.latke.service.ServiceException in project solo by b3log.
the class UserMgmtService method addUser.
/**
* Adds a user with the specified request json object.
*
* @param requestJSONObject the specified request json object, for example, <pre>
* {
* "userName": "",
* "userEmail": "",
* "userPassword": "", // Unhashed
* "userURL": "", // optional, uses 'servePath' instead if not specified
* "userRole": "", // optional, uses {@value Role#DEFAULT_ROLE} instead if not specified
* "userAvatar": "" // optional, users generated gravatar url instead if not specified
* }
* </pre>,see {@link User} for more details
*
* @return generated user id
* @throws ServiceException service exception
*/
public String addUser(final JSONObject requestJSONObject) throws ServiceException {
final Transaction transaction = userRepository.beginTransaction();
try {
final JSONObject user = new JSONObject();
final String userEmail = requestJSONObject.optString(User.USER_EMAIL).trim().toLowerCase();
final JSONObject duplicatedUser = userRepository.getByEmail(userEmail);
if (null != duplicatedUser) {
if (transaction.isActive()) {
transaction.rollback();
}
throw new ServiceException(langPropsService.get("duplicatedEmailLabel"));
}
user.put(User.USER_EMAIL, userEmail);
final String userName = requestJSONObject.optString(User.USER_NAME);
user.put(User.USER_NAME, userName);
final String userPassword = requestJSONObject.optString(User.USER_PASSWORD);
user.put(User.USER_PASSWORD, MD5.hash(userPassword));
String userURL = requestJSONObject.optString(User.USER_URL);
if (Strings.isEmptyOrNull(userURL)) {
userURL = Latkes.getServePath();
}
if (!Strings.isURL(userURL)) {
throw new ServiceException(langPropsService.get("urlInvalidLabel"));
}
user.put(User.USER_URL, userURL);
final String roleName = requestJSONObject.optString(User.USER_ROLE, Role.DEFAULT_ROLE);
user.put(User.USER_ROLE, roleName);
user.put(UserExt.USER_ARTICLE_COUNT, 0);
user.put(UserExt.USER_PUBLISHED_ARTICLE_COUNT, 0);
String userAvatar = requestJSONObject.optString(UserExt.USER_AVATAR);
if (Strings.isEmptyOrNull(userAvatar)) {
userAvatar = Thumbnails.getGravatarURL(userEmail, "128");
}
user.put(UserExt.USER_AVATAR, userAvatar);
userRepository.add(user);
transaction.commit();
return user.optString(Keys.OBJECT_ID);
} catch (final RepositoryException e) {
if (transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.ERROR, "Adds a user failed", e);
throw new ServiceException(e);
}
}
Aggregations