/b3log-solo/core/src/main/java/org/b3log/solo/web/processor/console/TagConsole.java
Java | 239 lines | 117 code | 26 blank | 96 comment | 6 complexity | 4779739ff5f36c3525acb08e3086a3fd MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1
- /*
- * Copyright (c) 2009, 2010, 2011, B3log Team
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package org.b3log.solo.web.processor.console;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.b3log.latke.Keys;
- import org.b3log.latke.annotation.RequestProcessing;
- import org.b3log.latke.annotation.RequestProcessor;
- import org.b3log.latke.service.LangPropsService;
- import org.b3log.latke.servlet.HTTPRequestContext;
- import org.b3log.latke.servlet.HTTPRequestMethod;
- import org.b3log.latke.servlet.renderer.JSONRenderer;
- import org.b3log.solo.model.Common;
- import org.b3log.solo.model.Tag;
- import org.b3log.solo.service.TagMgmtService;
- import org.b3log.solo.service.TagQueryService;
- import org.b3log.solo.util.Users;
- import org.json.JSONException;
- import org.json.JSONObject;
- /**
- * Tag console request processing.
- *
- * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
- * @version 1.0.0.0, Oct 24, 2011
- * @since 0.4.0
- */
- @RequestProcessor
- public final class TagConsole {
- /**
- * Logger.
- */
- private static final Logger LOGGER =
- Logger.getLogger(TagConsole.class.getName());
- /**
- * Tag query service.
- */
- private TagQueryService tagQueryService = TagQueryService.getInstance();
- /**
- * Tag management service.
- */
- private TagMgmtService tagMgmtService = TagMgmtService.getInstance();
- /**
- * User utilities.
- */
- private Users userUtils = Users.getInstance();
- /**
- * Language service.
- */
- private LangPropsService langPropsService = LangPropsService.getInstance();
- /**
- * Gets all tags.
- *
- * <p>
- * Renders the response with a json object, for example,
- * <pre>
- * {
- * "sc": boolean,
- * "tags": [
- * {"tagTitle": "", tagReferenceCount": int, ....},
- * ....
- * ]
- * }
- * </pre>
- * </p>
- *
- * @param request the specified http servlet request
- * @param response the specified http servlet response
- * @param context the specified http request context
- * @throws IOException io exception
- */
- @RequestProcessing(value = "/console/tags", method = HTTPRequestMethod.GET)
- public void getTags(final HttpServletRequest request,
- final HttpServletResponse response,
- final HTTPRequestContext context)
- throws IOException {
- if (!userUtils.isLoggedIn(request)) {
- response.sendError(HttpServletResponse.SC_FORBIDDEN);
- return;
- }
- final JSONRenderer renderer = new JSONRenderer();
- context.setRenderer(renderer);
- final JSONObject jsonObject = new JSONObject();
- renderer.setJSONObject(jsonObject);
- try {
- jsonObject.put(Tag.TAGS, tagQueryService.getTags());
- jsonObject.put(Keys.STATUS_CODE, true);
- } catch (final Exception e) {
- LOGGER.log(Level.SEVERE, "Gets tags failed", e);
- try {
- jsonObject.put(Keys.STATUS_CODE, false);
- } catch (final JSONException ex) {
- throw new IOException(ex);
- }
- }
- }
- /**
- * Gets all unused tags.
- *
- * <p>
- * Renders the response with a json object, for example,
- * <pre>
- * {
- * "sc": boolean,
- * "unusedTags": [
- * {"tagTitle": "", tagReferenceCount": int, ....},
- * ....
- * ]
- * }
- * </pre>
- * </p>
- *
- * @param request the specified http servlet request
- * @param response the specified http servlet response
- * @param context the specified http request context
- * @throws IOException io exception
- */
- @RequestProcessing(value = "/console/tag/unused",
- method = HTTPRequestMethod.GET)
- public void getUnusedTags(final HttpServletRequest request,
- final HttpServletResponse response,
- final HTTPRequestContext context)
- throws IOException {
- if (!userUtils.isLoggedIn(request)) {
- response.sendError(HttpServletResponse.SC_FORBIDDEN);
- return;
- }
- final JSONRenderer renderer = new JSONRenderer();
- context.setRenderer(renderer);
- final JSONObject jsonObject = new JSONObject();
- renderer.setJSONObject(jsonObject);
- final List<JSONObject> unusedTags = new ArrayList<JSONObject>();
- try {
- jsonObject.put(Common.UNUSED_TAGS, unusedTags);
- final List<JSONObject> tags = tagQueryService.getTags();
- for (int i = 0; i < tags.size(); i++) {
- final JSONObject tag = tags.get(i);
- final int tagRefCnt = tag.getInt(Tag.TAG_REFERENCE_COUNT);
- if (0 == tagRefCnt) {
- unusedTags.add(tag);
- }
- }
- jsonObject.put(Keys.STATUS_CODE, true);
- } catch (final Exception e) {
- LOGGER.log(Level.SEVERE, "Gets unused tags failed", e);
- try {
- jsonObject.put(Keys.STATUS_CODE, false);
- } catch (final JSONException ex) {
- throw new IOException(ex);
- }
- }
- }
- /**
- * Removes all unused tags.
- *
- * <p>
- * Renders the response with a json object, for example,
- * <pre>
- * {
- * "msg": ""
- * }
- * </pre>
- * </p>
- *
- * @param request the specified http servlet request
- * @param response the specified http servlet response
- * @param context the specified http request context
- * @throws IOException io exception
- */
- @RequestProcessing(value = "/console/tag/unused",
- method = HTTPRequestMethod.DELETE)
- public void removeUnusedTags(final HttpServletRequest request,
- final HttpServletResponse response,
- final HTTPRequestContext context)
- throws IOException {
- if (!userUtils.isAdminLoggedIn(request)) {
- response.sendError(HttpServletResponse.SC_FORBIDDEN);
- return;
- }
- final JSONRenderer renderer = new JSONRenderer();
- context.setRenderer(renderer);
- final JSONObject jsonObject = new JSONObject();
- renderer.setJSONObject(jsonObject);
- try {
- tagMgmtService.removeUnusedTags();
- jsonObject.put(Keys.MSG, langPropsService.get("removeSuccLabel"));
- } catch (final Exception e) {
- LOGGER.log(Level.SEVERE, "Removes unused tags failed", e);
- try {
- jsonObject.put(Keys.MSG, langPropsService.get("removeFailLabel"));
- } catch (final JSONException ex) {
- throw new IOException(ex);
- }
- }
- }
- }