PageRenderTime 45ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/b3log-solo/core/src/main/java/org/b3log/solo/web/processor/console/TagConsole.java

http://b3log-solo.googlecode.com/
Java | 239 lines | 117 code | 26 blank | 96 comment | 6 complexity | 4779739ff5f36c3525acb08e3086a3fd MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1
  1. /*
  2. * Copyright (c) 2009, 2010, 2011, B3log Team
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.b3log.solo.web.processor.console;
  17. import java.io.IOException;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import javax.servlet.http.HttpServletRequest;
  23. import javax.servlet.http.HttpServletResponse;
  24. import org.b3log.latke.Keys;
  25. import org.b3log.latke.annotation.RequestProcessing;
  26. import org.b3log.latke.annotation.RequestProcessor;
  27. import org.b3log.latke.service.LangPropsService;
  28. import org.b3log.latke.servlet.HTTPRequestContext;
  29. import org.b3log.latke.servlet.HTTPRequestMethod;
  30. import org.b3log.latke.servlet.renderer.JSONRenderer;
  31. import org.b3log.solo.model.Common;
  32. import org.b3log.solo.model.Tag;
  33. import org.b3log.solo.service.TagMgmtService;
  34. import org.b3log.solo.service.TagQueryService;
  35. import org.b3log.solo.util.Users;
  36. import org.json.JSONException;
  37. import org.json.JSONObject;
  38. /**
  39. * Tag console request processing.
  40. *
  41. * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
  42. * @version 1.0.0.0, Oct 24, 2011
  43. * @since 0.4.0
  44. */
  45. @RequestProcessor
  46. public final class TagConsole {
  47. /**
  48. * Logger.
  49. */
  50. private static final Logger LOGGER =
  51. Logger.getLogger(TagConsole.class.getName());
  52. /**
  53. * Tag query service.
  54. */
  55. private TagQueryService tagQueryService = TagQueryService.getInstance();
  56. /**
  57. * Tag management service.
  58. */
  59. private TagMgmtService tagMgmtService = TagMgmtService.getInstance();
  60. /**
  61. * User utilities.
  62. */
  63. private Users userUtils = Users.getInstance();
  64. /**
  65. * Language service.
  66. */
  67. private LangPropsService langPropsService = LangPropsService.getInstance();
  68. /**
  69. * Gets all tags.
  70. *
  71. * <p>
  72. * Renders the response with a json object, for example,
  73. * <pre>
  74. * {
  75. * "sc": boolean,
  76. * "tags": [
  77. * {"tagTitle": "", tagReferenceCount": int, ....},
  78. * ....
  79. * ]
  80. * }
  81. * </pre>
  82. * </p>
  83. *
  84. * @param request the specified http servlet request
  85. * @param response the specified http servlet response
  86. * @param context the specified http request context
  87. * @throws IOException io exception
  88. */
  89. @RequestProcessing(value = "/console/tags", method = HTTPRequestMethod.GET)
  90. public void getTags(final HttpServletRequest request,
  91. final HttpServletResponse response,
  92. final HTTPRequestContext context)
  93. throws IOException {
  94. if (!userUtils.isLoggedIn(request)) {
  95. response.sendError(HttpServletResponse.SC_FORBIDDEN);
  96. return;
  97. }
  98. final JSONRenderer renderer = new JSONRenderer();
  99. context.setRenderer(renderer);
  100. final JSONObject jsonObject = new JSONObject();
  101. renderer.setJSONObject(jsonObject);
  102. try {
  103. jsonObject.put(Tag.TAGS, tagQueryService.getTags());
  104. jsonObject.put(Keys.STATUS_CODE, true);
  105. } catch (final Exception e) {
  106. LOGGER.log(Level.SEVERE, "Gets tags failed", e);
  107. try {
  108. jsonObject.put(Keys.STATUS_CODE, false);
  109. } catch (final JSONException ex) {
  110. throw new IOException(ex);
  111. }
  112. }
  113. }
  114. /**
  115. * Gets all unused tags.
  116. *
  117. * <p>
  118. * Renders the response with a json object, for example,
  119. * <pre>
  120. * {
  121. * "sc": boolean,
  122. * "unusedTags": [
  123. * {"tagTitle": "", tagReferenceCount": int, ....},
  124. * ....
  125. * ]
  126. * }
  127. * </pre>
  128. * </p>
  129. *
  130. * @param request the specified http servlet request
  131. * @param response the specified http servlet response
  132. * @param context the specified http request context
  133. * @throws IOException io exception
  134. */
  135. @RequestProcessing(value = "/console/tag/unused",
  136. method = HTTPRequestMethod.GET)
  137. public void getUnusedTags(final HttpServletRequest request,
  138. final HttpServletResponse response,
  139. final HTTPRequestContext context)
  140. throws IOException {
  141. if (!userUtils.isLoggedIn(request)) {
  142. response.sendError(HttpServletResponse.SC_FORBIDDEN);
  143. return;
  144. }
  145. final JSONRenderer renderer = new JSONRenderer();
  146. context.setRenderer(renderer);
  147. final JSONObject jsonObject = new JSONObject();
  148. renderer.setJSONObject(jsonObject);
  149. final List<JSONObject> unusedTags = new ArrayList<JSONObject>();
  150. try {
  151. jsonObject.put(Common.UNUSED_TAGS, unusedTags);
  152. final List<JSONObject> tags = tagQueryService.getTags();
  153. for (int i = 0; i < tags.size(); i++) {
  154. final JSONObject tag = tags.get(i);
  155. final int tagRefCnt = tag.getInt(Tag.TAG_REFERENCE_COUNT);
  156. if (0 == tagRefCnt) {
  157. unusedTags.add(tag);
  158. }
  159. }
  160. jsonObject.put(Keys.STATUS_CODE, true);
  161. } catch (final Exception e) {
  162. LOGGER.log(Level.SEVERE, "Gets unused tags failed", e);
  163. try {
  164. jsonObject.put(Keys.STATUS_CODE, false);
  165. } catch (final JSONException ex) {
  166. throw new IOException(ex);
  167. }
  168. }
  169. }
  170. /**
  171. * Removes all unused tags.
  172. *
  173. * <p>
  174. * Renders the response with a json object, for example,
  175. * <pre>
  176. * {
  177. * "msg": ""
  178. * }
  179. * </pre>
  180. * </p>
  181. *
  182. * @param request the specified http servlet request
  183. * @param response the specified http servlet response
  184. * @param context the specified http request context
  185. * @throws IOException io exception
  186. */
  187. @RequestProcessing(value = "/console/tag/unused",
  188. method = HTTPRequestMethod.DELETE)
  189. public void removeUnusedTags(final HttpServletRequest request,
  190. final HttpServletResponse response,
  191. final HTTPRequestContext context)
  192. throws IOException {
  193. if (!userUtils.isAdminLoggedIn(request)) {
  194. response.sendError(HttpServletResponse.SC_FORBIDDEN);
  195. return;
  196. }
  197. final JSONRenderer renderer = new JSONRenderer();
  198. context.setRenderer(renderer);
  199. final JSONObject jsonObject = new JSONObject();
  200. renderer.setJSONObject(jsonObject);
  201. try {
  202. tagMgmtService.removeUnusedTags();
  203. jsonObject.put(Keys.MSG, langPropsService.get("removeSuccLabel"));
  204. } catch (final Exception e) {
  205. LOGGER.log(Level.SEVERE, "Removes unused tags failed", e);
  206. try {
  207. jsonObject.put(Keys.MSG, langPropsService.get("removeFailLabel"));
  208. } catch (final JSONException ex) {
  209. throw new IOException(ex);
  210. }
  211. }
  212. }
  213. }