PageRenderTime 22ms CodeModel.GetById 15ms app.highlight 6ms RepoModel.GetById 0ms app.codeStats 0ms

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

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