Search in sources :

Example 51 with ServiceException

use of org.b3log.latke.service.ServiceException in project solo by b3log.

the class PreferenceMgmtService method updatePreference.

/**
     * Updates the preference with the specified preference.
     *
     * @param preference the specified preference
     * @throws ServiceException service exception
     */
public void updatePreference(final JSONObject preference) throws ServiceException {
    @SuppressWarnings("unchecked") final Iterator<String> keys = preference.keys();
    while (keys.hasNext()) {
        final String key = keys.next();
        if (preference.isNull(key)) {
            throw new ServiceException("A value is null of preference[key=" + key + "]");
        }
    }
    final Transaction transaction = optionRepository.beginTransaction();
    try {
        final String skinDirName = preference.getString(Skin.SKIN_DIR_NAME);
        final String skinName = Latkes.getSkinName(skinDirName);
        preference.put(Skin.SKIN_NAME, skinName);
        final Set<String> skinDirNames = Skins.getSkinDirNames();
        final JSONArray skinArray = new JSONArray();
        for (final String dirName : skinDirNames) {
            final JSONObject skin = new JSONObject();
            skinArray.put(skin);
            final String name = Latkes.getSkinName(dirName);
            skin.put(Skin.SKIN_NAME, name);
            skin.put(Skin.SKIN_DIR_NAME, dirName);
        }
        preference.put(Skin.SKINS, skinArray.toString());
        final String timeZoneId = preference.getString(Option.ID_C_TIME_ZONE_ID);
        TimeZones.setTimeZone(timeZoneId);
        preference.put(Option.ID_C_SIGNS, preference.get(Option.ID_C_SIGNS).toString());
        final JSONObject oldPreference = preferenceQueryService.getPreference();
        final String adminEmail = oldPreference.getString(Option.ID_C_ADMIN_EMAIL);
        preference.put(Option.ID_C_ADMIN_EMAIL, adminEmail);
        final String version = oldPreference.optString(Option.ID_C_VERSION);
        preference.put(Option.ID_C_VERSION, version);
        final String localeString = preference.getString(Option.ID_C_LOCALE_STRING);
        LOGGER.log(Level.DEBUG, "Current locale[string={0}]", localeString);
        Latkes.setLocale(new Locale(Locales.getLanguage(localeString), Locales.getCountry(localeString)));
        final JSONObject adminEmailOpt = optionRepository.get(Option.ID_C_ADMIN_EMAIL);
        adminEmailOpt.put(Option.OPTION_VALUE, adminEmail);
        optionRepository.update(Option.ID_C_ADMIN_EMAIL, adminEmailOpt);
        final JSONObject allowVisitDraftViaPermalinkOpt = optionRepository.get(Option.ID_C_ALLOW_VISIT_DRAFT_VIA_PERMALINK);
        allowVisitDraftViaPermalinkOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_ALLOW_VISIT_DRAFT_VIA_PERMALINK));
        optionRepository.update(Option.ID_C_ALLOW_VISIT_DRAFT_VIA_PERMALINK, allowVisitDraftViaPermalinkOpt);
        final JSONObject allowRegisterOpt = optionRepository.get(Option.ID_C_ALLOW_REGISTER);
        allowRegisterOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_ALLOW_REGISTER));
        optionRepository.update(Option.ID_C_ALLOW_REGISTER, allowRegisterOpt);
        final JSONObject articleListDisplayCountOpt = optionRepository.get(Option.ID_C_ARTICLE_LIST_DISPLAY_COUNT);
        articleListDisplayCountOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_ARTICLE_LIST_DISPLAY_COUNT));
        optionRepository.update(Option.ID_C_ARTICLE_LIST_DISPLAY_COUNT, articleListDisplayCountOpt);
        final JSONObject articleListPaginationWindowSizeOpt = optionRepository.get(Option.ID_C_ARTICLE_LIST_PAGINATION_WINDOW_SIZE);
        articleListPaginationWindowSizeOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_ARTICLE_LIST_PAGINATION_WINDOW_SIZE));
        optionRepository.update(Option.ID_C_ARTICLE_LIST_PAGINATION_WINDOW_SIZE, articleListPaginationWindowSizeOpt);
        final JSONObject articleListStyleOpt = optionRepository.get(Option.ID_C_ARTICLE_LIST_STYLE);
        articleListStyleOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_ARTICLE_LIST_STYLE));
        optionRepository.update(Option.ID_C_ARTICLE_LIST_STYLE, articleListStyleOpt);
        final JSONObject blogSubtitleOpt = optionRepository.get(Option.ID_C_BLOG_SUBTITLE);
        blogSubtitleOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_BLOG_SUBTITLE));
        optionRepository.update(Option.ID_C_BLOG_SUBTITLE, blogSubtitleOpt);
        final JSONObject blogTitleOpt = optionRepository.get(Option.ID_C_BLOG_TITLE);
        blogTitleOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_BLOG_TITLE));
        optionRepository.update(Option.ID_C_BLOG_TITLE, blogTitleOpt);
        final JSONObject commentableOpt = optionRepository.get(Option.ID_C_COMMENTABLE);
        commentableOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_COMMENTABLE));
        optionRepository.update(Option.ID_C_COMMENTABLE, commentableOpt);
        final JSONObject editorTypeOpt = optionRepository.get(Option.ID_C_EDITOR_TYPE);
        editorTypeOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_EDITOR_TYPE));
        optionRepository.update(Option.ID_C_EDITOR_TYPE, editorTypeOpt);
        final JSONObject enableArticleUpdateHintOpt = optionRepository.get(Option.ID_C_ENABLE_ARTICLE_UPDATE_HINT);
        enableArticleUpdateHintOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_ENABLE_ARTICLE_UPDATE_HINT));
        optionRepository.update(Option.ID_C_ENABLE_ARTICLE_UPDATE_HINT, enableArticleUpdateHintOpt);
        final JSONObject externalRelevantArticlesDisplayCountOpt = optionRepository.get(Option.ID_C_EXTERNAL_RELEVANT_ARTICLES_DISPLAY_CNT);
        externalRelevantArticlesDisplayCountOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_EXTERNAL_RELEVANT_ARTICLES_DISPLAY_CNT));
        optionRepository.update(Option.ID_C_EXTERNAL_RELEVANT_ARTICLES_DISPLAY_CNT, externalRelevantArticlesDisplayCountOpt);
        final JSONObject feedOutputCntOpt = optionRepository.get(Option.ID_C_FEED_OUTPUT_CNT);
        feedOutputCntOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_FEED_OUTPUT_CNT));
        optionRepository.update(Option.ID_C_FEED_OUTPUT_CNT, feedOutputCntOpt);
        final JSONObject feedOutputModeOpt = optionRepository.get(Option.ID_C_FEED_OUTPUT_MODE);
        feedOutputModeOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_FEED_OUTPUT_MODE));
        optionRepository.update(Option.ID_C_FEED_OUTPUT_MODE, feedOutputModeOpt);
        final JSONObject footerContentOpt = optionRepository.get(Option.ID_C_FOOTER_CONTENT);
        footerContentOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_FOOTER_CONTENT));
        optionRepository.update(Option.ID_C_FOOTER_CONTENT, footerContentOpt);
        final JSONObject htmlHeadOpt = optionRepository.get(Option.ID_C_HTML_HEAD);
        htmlHeadOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_HTML_HEAD));
        optionRepository.update(Option.ID_C_HTML_HEAD, htmlHeadOpt);
        final JSONObject keyOfSoloOpt = optionRepository.get(Option.ID_C_KEY_OF_SOLO);
        keyOfSoloOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_KEY_OF_SOLO));
        optionRepository.update(Option.ID_C_KEY_OF_SOLO, keyOfSoloOpt);
        final JSONObject localeStringOpt = optionRepository.get(Option.ID_C_LOCALE_STRING);
        localeStringOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_LOCALE_STRING));
        optionRepository.update(Option.ID_C_LOCALE_STRING, localeStringOpt);
        final JSONObject metaDescriptionOpt = optionRepository.get(Option.ID_C_META_DESCRIPTION);
        metaDescriptionOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_META_DESCRIPTION));
        optionRepository.update(Option.ID_C_META_DESCRIPTION, metaDescriptionOpt);
        final JSONObject metaKeywordsOpt = optionRepository.get(Option.ID_C_META_KEYWORDS);
        metaKeywordsOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_META_KEYWORDS));
        optionRepository.update(Option.ID_C_META_KEYWORDS, metaKeywordsOpt);
        final JSONObject mostCommentArticleDisplayCountOpt = optionRepository.get(Option.ID_C_MOST_COMMENT_ARTICLE_DISPLAY_CNT);
        mostCommentArticleDisplayCountOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_MOST_COMMENT_ARTICLE_DISPLAY_CNT));
        optionRepository.update(Option.ID_C_MOST_COMMENT_ARTICLE_DISPLAY_CNT, mostCommentArticleDisplayCountOpt);
        final JSONObject mostUsedTagDisplayCountOpt = optionRepository.get(Option.ID_C_MOST_USED_TAG_DISPLAY_CNT);
        mostUsedTagDisplayCountOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_MOST_USED_TAG_DISPLAY_CNT));
        optionRepository.update(Option.ID_C_MOST_USED_TAG_DISPLAY_CNT, mostUsedTagDisplayCountOpt);
        final JSONObject mostViewArticleDisplayCountOpt = optionRepository.get(Option.ID_C_MOST_VIEW_ARTICLE_DISPLAY_CNT);
        mostViewArticleDisplayCountOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_MOST_VIEW_ARTICLE_DISPLAY_CNT));
        optionRepository.update(Option.ID_C_MOST_VIEW_ARTICLE_DISPLAY_CNT, mostViewArticleDisplayCountOpt);
        final JSONObject noticeBoardOpt = optionRepository.get(Option.ID_C_NOTICE_BOARD);
        noticeBoardOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_NOTICE_BOARD));
        optionRepository.update(Option.ID_C_NOTICE_BOARD, noticeBoardOpt);
        final JSONObject randomArticlesDisplayCountOpt = optionRepository.get(Option.ID_C_RANDOM_ARTICLES_DISPLAY_CNT);
        randomArticlesDisplayCountOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_RANDOM_ARTICLES_DISPLAY_CNT));
        optionRepository.update(Option.ID_C_RANDOM_ARTICLES_DISPLAY_CNT, randomArticlesDisplayCountOpt);
        final JSONObject recentArticleDisplayCountOpt = optionRepository.get(Option.ID_C_RECENT_ARTICLE_DISPLAY_CNT);
        recentArticleDisplayCountOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_RECENT_ARTICLE_DISPLAY_CNT));
        optionRepository.update(Option.ID_C_RECENT_ARTICLE_DISPLAY_CNT, recentArticleDisplayCountOpt);
        final JSONObject recentCommentDisplayCountOpt = optionRepository.get(Option.ID_C_RECENT_COMMENT_DISPLAY_CNT);
        recentCommentDisplayCountOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_RECENT_COMMENT_DISPLAY_CNT));
        optionRepository.update(Option.ID_C_RECENT_COMMENT_DISPLAY_CNT, recentCommentDisplayCountOpt);
        final JSONObject relevantArticlesDisplayCountOpt = optionRepository.get(Option.ID_C_RELEVANT_ARTICLES_DISPLAY_CNT);
        relevantArticlesDisplayCountOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_RELEVANT_ARTICLES_DISPLAY_CNT));
        optionRepository.update(Option.ID_C_RELEVANT_ARTICLES_DISPLAY_CNT, relevantArticlesDisplayCountOpt);
        final JSONObject signsOpt = optionRepository.get(Option.ID_C_SIGNS);
        signsOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_SIGNS));
        optionRepository.update(Option.ID_C_SIGNS, signsOpt);
        final JSONObject skinDirNameOpt = optionRepository.get(Option.ID_C_SKIN_DIR_NAME);
        skinDirNameOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_SKIN_DIR_NAME));
        optionRepository.update(Option.ID_C_SKIN_DIR_NAME, skinDirNameOpt);
        final JSONObject skinNameOpt = optionRepository.get(Option.ID_C_SKIN_NAME);
        skinNameOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_SKIN_NAME));
        optionRepository.update(Option.ID_C_SKIN_NAME, skinNameOpt);
        final JSONObject skinsOpt = optionRepository.get(Option.ID_C_SKINS);
        skinsOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_SKINS));
        optionRepository.update(Option.ID_C_SKINS, skinsOpt);
        final JSONObject timeZoneIdOpt = optionRepository.get(Option.ID_C_TIME_ZONE_ID);
        timeZoneIdOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_TIME_ZONE_ID));
        optionRepository.update(Option.ID_C_TIME_ZONE_ID, timeZoneIdOpt);
        final JSONObject versionOpt = optionRepository.get(Option.ID_C_VERSION);
        versionOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_VERSION));
        optionRepository.update(Option.ID_C_VERSION, versionOpt);
        transaction.commit();
        final ServletContext servletContext = SoloServletListener.getServletContext();
        Templates.MAIN_CFG.setServletContextForTemplateLoading(servletContext, "/skins/" + skinDirName);
    } catch (final Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, "Updates preference failed", e);
        throw new ServiceException(langPropsService.get("updateFailLabel"));
    }
    LOGGER.log(Level.DEBUG, "Updates preference successfully");
}
Also used : Locale(java.util.Locale) ServiceException(org.b3log.latke.service.ServiceException) Transaction(org.b3log.latke.repository.Transaction) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ServletContext(javax.servlet.ServletContext) ServiceException(org.b3log.latke.service.ServiceException)

Example 52 with ServiceException

use of org.b3log.latke.service.ServiceException in project solo by b3log.

the class Skins method fillLangs.

/**
     * Fills the specified data model with the current skink's (WebRoot/skins/${skinName}/lang/lang_xx_XX.properties)
     * and core language (WebRoot/WEB-INF/classes/lang_xx_XX.properties) configurations.
     *
     * @param localeString the specified locale string
     * @param currentSkinDirName the specified current skin directory name
     * @param dataModel the specified data model
     * @throws ServiceException service exception
     */
public static void fillLangs(final String localeString, final String currentSkinDirName, final Map<String, Object> dataModel) throws ServiceException {
    Stopwatchs.start("Fill Skin Langs");
    try {
        final String langName = currentSkinDirName + "." + localeString;
        Map<String, String> langs = LANG_MAP.get(langName);
        if (null == langs) {
            // Collect unused skin languages
            LANG_MAP.clear();
            LOGGER.log(Level.DEBUG, "Loading skin [dirName={0}, locale={1}]", new Object[] { currentSkinDirName, localeString });
            langs = new HashMap<String, String>();
            final String language = Locales.getLanguage(localeString);
            final String country = Locales.getCountry(localeString);
            final ServletContext servletContext = SoloServletListener.getServletContext();
            final InputStream inputStream = servletContext.getResourceAsStream("/skins/" + currentSkinDirName + "/lang/lang_" + language + '_' + country + ".properties");
            final Properties props = new Properties();
            props.load(inputStream);
            final Set<Object> keys = props.keySet();
            for (final Object key : keys) {
                langs.put((String) key, props.getProperty((String) key));
            }
            LANG_MAP.put(langName, langs);
            LOGGER.log(Level.DEBUG, "Loaded skin[dirName={0}, locale={1}, keyCount={2}]", new Object[] { currentSkinDirName, localeString, langs.size() });
        }
        // Fills the current skin's language configurations
        dataModel.putAll(langs);
        // Fills the core language configurations
        final LatkeBeanManager beanManager = Lifecycle.getBeanManager();
        final LangPropsService langPropsService = beanManager.getReference(LangPropsServiceImpl.class);
        dataModel.putAll(langPropsService.getAll(Latkes.getLocale()));
    } catch (final IOException e) {
        LOGGER.log(Level.ERROR, "Fills skin langs failed", e);
        throw new ServiceException(e);
    } finally {
        Stopwatchs.end();
    }
}
Also used : ServiceException(org.b3log.latke.service.ServiceException) InputStream(java.io.InputStream) ServletContext(javax.servlet.ServletContext) IOException(java.io.IOException) Properties(java.util.Properties) LatkeBeanManager(org.b3log.latke.ioc.LatkeBeanManager) LangPropsService(org.b3log.latke.service.LangPropsService)

Example 53 with ServiceException

use of org.b3log.latke.service.ServiceException in project solo by b3log.

the class LinkMgmtService method updateLink.

/**
     * Updates a link by the specified request json object.
     *
     * @param requestJSONObject the specified request json object, for example,
     * <pre>
     * {
     *     "link": {
     *         "oId": "",
     *         "linkTitle": "",
     *         "linkAddress": ""
     *     }
     * }, see {@link Link} for more details
     * </pre>
     * @throws ServiceException service exception
     */
public void updateLink(final JSONObject requestJSONObject) throws ServiceException {
    final Transaction transaction = linkRepository.beginTransaction();
    try {
        final JSONObject link = requestJSONObject.getJSONObject(Link.LINK);
        final String linkId = link.getString(Keys.OBJECT_ID);
        final JSONObject oldLink = linkRepository.get(linkId);
        link.put(Link.LINK_ORDER, oldLink.getInt(Link.LINK_ORDER));
        linkRepository.update(linkId, link);
        transaction.commit();
    } catch (final Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        throw new ServiceException(e);
    }
}
Also used : Transaction(org.b3log.latke.repository.Transaction) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) ServiceException(org.b3log.latke.service.ServiceException)

Example 54 with ServiceException

use of org.b3log.latke.service.ServiceException in project solo by b3log.

the class LinkMgmtService method addLink.

/**
     * Adds a link with the specified request json object.
     * 
     * @param requestJSONObject the specified request json object, for example,
     * <pre>
     * {
     *     "link": {
     *         "linkTitle": "",
     *         "linkAddress": "",
     *         "linkDescription": "" // optional
     *     }
     * }, see {@link Link} for more details
     * </pre>
     * @return generated link id
     * @throws ServiceException service exception
     */
public String addLink(final JSONObject requestJSONObject) throws ServiceException {
    final Transaction transaction = linkRepository.beginTransaction();
    try {
        final JSONObject link = requestJSONObject.getJSONObject(Link.LINK);
        final int maxOrder = linkRepository.getMaxOrder();
        link.put(Link.LINK_ORDER, maxOrder + 1);
        final String ret = linkRepository.add(link);
        transaction.commit();
        return ret;
    } catch (final Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, "Adds a link failed", e);
        throw new ServiceException(e);
    }
}
Also used : Transaction(org.b3log.latke.repository.Transaction) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) ServiceException(org.b3log.latke.service.ServiceException)

Example 55 with ServiceException

use of org.b3log.latke.service.ServiceException in project solo by b3log.

the class LinkQueryService method getLink.

/**
     * Gets a link by the specified link id.
     *
     * @param linkId the specified link id
     * @return for example,
     * <pre>
     * {
     *     "link": {
     *         "oId": "",
     *         "linkTitle": "",
     *         "linkAddress": "",
     *         "linkDescription": ""
     *     }
     * }
     * </pre>, returns {@code null} if not found
     * @throws ServiceException service exception
     */
public JSONObject getLink(final String linkId) throws ServiceException {
    final JSONObject ret = new JSONObject();
    try {
        final JSONObject link = linkRepository.get(linkId);
        if (null == link) {
            return null;
        }
        ret.put(Link.LINK, link);
        return ret;
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Gets a link failed", e);
        throw new ServiceException(e);
    }
}
Also used : JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) ServiceException(org.b3log.latke.service.ServiceException)

Aggregations

ServiceException (org.b3log.latke.service.ServiceException)102 JSONObject (org.json.JSONObject)91 JSONException (org.json.JSONException)45 RepositoryException (org.b3log.latke.repository.RepositoryException)34 Transaction (org.b3log.latke.repository.Transaction)31 RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)23 JSONArray (org.json.JSONArray)17 JSONRenderer (org.b3log.latke.servlet.renderer.JSONRenderer)16 EventException (org.b3log.latke.event.EventException)14 IOException (java.io.IOException)11 ParseException (java.text.ParseException)10 ArrayList (java.util.ArrayList)8 Query (org.b3log.latke.repository.Query)8 Date (java.util.Date)7 AbstractFreeMarkerRenderer (org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer)6 Template (freemarker.template.Template)3 FreeMarkerRenderer (org.b3log.latke.servlet.renderer.freemarker.FreeMarkerRenderer)3 ArchiveDate (org.b3log.solo.model.ArchiveDate)3 URL (java.net.URL)2 HashMap (java.util.HashMap)2