Search in sources :

Example 1 with RepositoryException

use of org.b3log.latke.repository.RepositoryException in project solo by b3log.

the class ArchiveDateQueryService method getByArchiveDateString.

/**
     * Gets an archive date by the specified archive date string.
     * 
     * @param archiveDateString the specified archive date string (yyyy/MM)
     * @return for example,
     * <pre>
     * {
     *     "archiveDate": {
     *         "oId": "",
     *         "archiveTime": "",
     *         "archiveDatePublishedArticleCount": int,
     *         "archiveDateArticleCount": int
     *     }
     * }
     * </pre>, returns {@code null} if not found
     * @throws ServiceException service exception
     */
public JSONObject getByArchiveDateString(final String archiveDateString) throws ServiceException {
    final JSONObject ret = new JSONObject();
    try {
        final JSONObject archiveDate = archiveDateRepository.getByArchiveDate(archiveDateString);
        if (null == archiveDate) {
            return null;
        }
        ret.put(ArchiveDate.ARCHIVE_DATE, archiveDate);
        return ret;
    } catch (final RepositoryException e) {
        LOGGER.log(Level.ERROR, "Gets archive date[string=" + archiveDateString + "] failed", e);
        throw new ServiceException("Gets archive date[string=" + archiveDateString + "] failed");
    }
}
Also used : JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) RepositoryException(org.b3log.latke.repository.RepositoryException)

Example 2 with RepositoryException

use of org.b3log.latke.repository.RepositoryException 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);
    }
}
Also used : Transaction(org.b3log.latke.repository.Transaction) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) RepositoryException(org.b3log.latke.repository.RepositoryException)

Example 3 with RepositoryException

use of org.b3log.latke.repository.RepositoryException in project solo by b3log.

the class InitService method archiveDate.

/**
     * Archive the create date with the specified article.
     *
     * @param article the specified article, for example,      <pre>
     * {
     *     ....,
     *     "oId": "",
     *     "articleCreateDate": java.util.Date,
     *     ....
     * }
     * </pre>
     *
     * @throws RepositoryException repository exception
     */
public void archiveDate(final JSONObject article) throws RepositoryException {
    final Date createDate = (Date) article.opt(Article.ARTICLE_CREATE_DATE);
    final String createDateString = DateFormatUtils.format(createDate, "yyyy/MM");
    final JSONObject archiveDate = new JSONObject();
    try {
        archiveDate.put(ArchiveDate.ARCHIVE_TIME, DateUtils.parseDate(createDateString, new String[] { "yyyy/MM" }).getTime());
        archiveDate.put(ArchiveDate.ARCHIVE_DATE_ARTICLE_COUNT, 1);
        archiveDate.put(ArchiveDate.ARCHIVE_DATE_PUBLISHED_ARTICLE_COUNT, 1);
        archiveDateRepository.add(archiveDate);
    } catch (final ParseException e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        throw new RepositoryException(e);
    }
    final JSONObject archiveDateArticleRelation = new JSONObject();
    archiveDateArticleRelation.put(ArchiveDate.ARCHIVE_DATE + "_" + Keys.OBJECT_ID, archiveDate.optString(Keys.OBJECT_ID));
    archiveDateArticleRelation.put(Article.ARTICLE + "_" + Keys.OBJECT_ID, article.optString(Keys.OBJECT_ID));
    archiveDateArticleRepository.add(archiveDateArticleRelation);
}
Also used : JSONObject(org.json.JSONObject) RepositoryException(org.b3log.latke.repository.RepositoryException) ParseException(java.text.ParseException) Date(java.util.Date)

Example 4 with RepositoryException

use of org.b3log.latke.repository.RepositoryException in project solo by b3log.

the class InitService method addHelloWorldArticle.

/**
     * Adds the specified "Hello World" article.
     *
     * @param article the specified "Hello World" article
     * @return generated article id
     * @throws RepositoryException repository exception
     */
private String addHelloWorldArticle(final JSONObject article) throws RepositoryException {
    final String ret = Ids.genTimeMillisId();
    try {
        article.put(Keys.OBJECT_ID, ret);
        // Step 1: Add tags
        final String tagsString = article.optString(Article.ARTICLE_TAGS_REF);
        final String[] tagTitles = tagsString.split(",");
        final JSONArray tags = tag(tagTitles, article);
        // Step 2: Add tag-article relations
        addTagArticleRelation(tags, article);
        // Step 3: Inc blog article and comment count statictis
        final JSONObject statistic = statisticRepository.get(Statistic.STATISTIC);
        statistic.put(Statistic.STATISTIC_BLOG_ARTICLE_COUNT, 1);
        statistic.put(Statistic.STATISTIC_PUBLISHED_ARTICLE_COUNT, 1);
        statistic.put(Statistic.STATISTIC_PUBLISHED_BLOG_COMMENT_COUNT, 1);
        statistic.put(Statistic.STATISTIC_BLOG_COMMENT_COUNT, 1);
        statisticRepository.update(Statistic.STATISTIC, statistic);
        // Step 4: Add archive date-article relations
        archiveDate(article);
        // Step 5: Add article
        articleRepository.add(article);
        // Step 6: Update admin user for article statistic
        final JSONObject admin = userRepository.getAdmin();
        admin.put(UserExt.USER_ARTICLE_COUNT, 1);
        admin.put(UserExt.USER_PUBLISHED_ARTICLE_COUNT, 1);
        userRepository.update(admin.optString(Keys.OBJECT_ID), admin);
    } catch (final RepositoryException e) {
        LOGGER.log(Level.ERROR, "Adds an article failed", e);
        throw new RepositoryException(e);
    }
    return ret;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) RepositoryException(org.b3log.latke.repository.RepositoryException)

Example 5 with RepositoryException

use of org.b3log.latke.repository.RepositoryException in project solo by b3log.

the class StatisticMgmtService method updateStatistic.

/**
     * Updates the statistic with the specified statistic.
     *
     * @param statistic the specified statistic
     * @throws ServiceException service exception
     */
public void updateStatistic(final JSONObject statistic) throws ServiceException {
    final Transaction transaction = statisticRepository.beginTransaction();
    try {
        statisticRepository.update(Statistic.STATISTIC, statistic);
        transaction.commit();
    } catch (final RepositoryException e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, "Updates statistic failed", e);
    }
    LOGGER.log(Level.DEBUG, "Updates statistic successfully");
}
Also used : Transaction(org.b3log.latke.repository.Transaction) RepositoryException(org.b3log.latke.repository.RepositoryException)

Aggregations

RepositoryException (org.b3log.latke.repository.RepositoryException)35 JSONObject (org.json.JSONObject)32 ServiceException (org.b3log.latke.service.ServiceException)14 JSONArray (org.json.JSONArray)10 Query (org.b3log.latke.repository.Query)8 Transaction (org.b3log.latke.repository.Transaction)8 Date (java.util.Date)5 ParseException (java.text.ParseException)4 PropertyFilter (org.b3log.latke.repository.PropertyFilter)4 JSONException (org.json.JSONException)4 EventException (org.b3log.latke.event.EventException)2 ArchiveDate (org.b3log.solo.model.ArchiveDate)2 IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 LatkeBeanManager (org.b3log.latke.ioc.LatkeBeanManager)1 GeneralUser (org.b3log.latke.user.GeneralUser)1 ArticleRepository (org.b3log.solo.repository.ArticleRepository)1 PageRepository (org.b3log.solo.repository.PageRepository)1