2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with any library licensed under the Apache Software License,
22 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23 * (or with modified versions of the above that use the same license as the above),
24 * and distribute linked combinations including the two. You must obey the
25 * GNU General Public License in all respects for all of the code used other than
26 * the above mentioned libraries. If you modify this file, you may extend this
27 * exception to your version of the file, but you are not obligated to do so.
28 * If you do not wish to do so, delete this exception statement from your version.
31 package mircoders.servlet;
33 import mir.entity.adapter.EntityAdapterEngine;
34 import mir.entity.adapter.EntityAdapterModel;
35 import mir.servlet.AdminServletModule;
36 import mir.servlet.ServletModuleExc;
37 import mir.servlet.ServletModuleFailure;
38 import mir.servlet.ServletModuleUserExc;
39 import mir.util.HTTPRequestParser;
40 import mir.util.JDBCStringRoutines;
41 import mir.util.SQLQueryBuilder;
42 import mir.util.URLBuilder;
43 import mircoders.entity.EntityComment;
44 import mircoders.global.MirGlobal;
45 import mircoders.module.ModuleComment;
46 import mircoders.storage.DatabaseComment;
48 import javax.servlet.http.HttpServletRequest;
49 import javax.servlet.http.HttpServletResponse;
50 import java.util.HashMap;
51 import java.util.Iterator;
52 import java.util.List;
53 import java.util.Locale;
57 * ServletModule for managing Comments
60 public class ServletModuleComment extends AdminServletModule {
61 public ServletModuleComment() {
62 mainModule = new ModuleComment();
63 definition = "comment";
66 public void delete(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
68 MirGlobal.accessControl().general().assertMayDeleteComments(ServletHelper.getUser(aRequest));
70 super.delete(aRequest, aResponse);
73 throw new ServletModuleFailure(t);
77 public void edit(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
78 String idParam = aRequest.getParameter("id");
80 if (idParam == null) {
81 throw new ServletModuleExc("Invalid call: id not supplied ");
84 editObject(aRequest, aResponse, idParam);
87 public void editObject(HttpServletRequest aRequest, HttpServletResponse aResponse, String anId) throws ServletModuleExc {
89 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
90 Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] {getLocale(aRequest), getFallbackLocale(aRequest)});
91 EntityAdapterModel model = MirGlobal.localizer().dataModel().adapterModel();
93 URLBuilder urlBuilder = new URLBuilder();
95 urlBuilder.setValue("module", "Comment");
96 urlBuilder.setValue("do", "edit");
97 urlBuilder.setValue("id", anId);
98 urlBuilder.setValue("returnurl", requestParser.getParameter("returnurl"));
101 responseData.put("new", Boolean.FALSE);
102 comment = model.makeEntityAdapter("comment", mainModule.getById(anId));
105 List fields = DatabaseComment.getInstance().getFieldNames();
106 responseData.put("new", Boolean.TRUE);
107 Map emptyComment = new HashMap();
108 Iterator i = fields.iterator();
109 while (i.hasNext()) {
110 emptyComment.put(i.next(), null);
113 comment = emptyComment;
115 responseData.put("comment", comment);
117 responseData.put("returnurl", requestParser.getParameter("returnurl"));
118 responseData.put("thisurl", urlBuilder.getQuery());
120 ServletHelper.generateResponse(aResponse.getWriter(), responseData, editGenerator);
122 catch (Throwable e) {
123 throw new ServletModuleFailure(e);
127 public void attach(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
129 String mediaIdParam = aRequest.getParameter("mid");
130 String commentId = aRequest.getParameter("commentid");
132 if (commentId == null || mediaIdParam==null) {
133 throw new ServletModuleExc("smod comment :: attach :: commentid/mid missing");
137 EntityComment comment = (EntityComment) mainModule.getById(commentId);
138 comment.attach(mediaIdParam);
141 throw new ServletModuleFailure(e);
144 logAdminUsage(aRequest, commentId, "media " + mediaIdParam + " attached");
146 editObject(aRequest, aResponse, commentId);
149 public void dettach(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
151 String commentId = aRequest.getParameter("commentid");
152 String midParam = aRequest.getParameter("mid");
153 if (commentId == null) {
154 throw new ServletModuleExc("smod comment :: dettach :: commentid missing");
156 if (midParam == null) {
157 throw new ServletModuleExc("smod comment :: dettach :: mid missing");
161 EntityComment comment = (EntityComment)mainModule.getById(commentId);
162 comment.dettach(commentId, midParam);
165 throw new ServletModuleFailure(e);
168 logAdminUsage(aRequest, commentId, "media " + midParam + " deattached");
170 editObject(aRequest, aResponse, commentId);
174 public void list(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
176 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
178 String where = requestParser.getParameter("where");
179 String order = requestParser.getParameterWithDefault("order", "webdb_create desc");
180 int offset = requestParser.getIntegerWithDefault("offset", 0);
182 returnList(aRequest, aResponse, where, order, offset);
185 public void search(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
187 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
188 SQLQueryBuilder queryBuilder = new SQLQueryBuilder();
190 String searchField = requestParser.getParameter("searchfield");
191 String searchText = requestParser.getParameter("searchtext");
192 String searchIsPublished = requestParser.getParameter("searchispublished");
193 String searchStatus = requestParser.getParameter("searchstatus");
194 String searchOrder = requestParser.getParameter("searchorder");
196 if ("0".equals(searchIsPublished)) {
197 queryBuilder.appendAndCondition("is_published='f'");
199 else if ("1".equals(searchIsPublished)) {
200 queryBuilder.appendAndCondition("is_published='t'");
203 if (searchText.length()>0) {
204 queryBuilder.appendAndCondition(
205 "lower("+ searchField + ") like " +
206 "'%" + JDBCStringRoutines.escapeStringLiteral(searchText.toLowerCase()) + "%'");
209 if (searchStatus.length()>0) {
210 queryBuilder.appendAndCondition("to_comment_status="+Integer.parseInt(searchStatus));
213 if (searchOrder.length()>0) {
214 if ("datedesc".equals(searchOrder)) {
215 queryBuilder.appendDescendingOrder("webdb_create");
217 else if ("dateasc".equals(searchOrder)) {
218 queryBuilder.appendAscendingOrder("webdb_create");
220 else if ("articletitle".equals(searchOrder)) {
221 queryBuilder.appendAscendingOrder("(select content.title from content where content.id = comment.to_media)");
223 else if ("creator".equals(searchOrder)) {
224 queryBuilder.appendDescendingOrder("creator");
228 returnList(aRequest, aResponse, queryBuilder.getWhereClause(), queryBuilder.getOrderByClause(), 0);
231 public void articlecomments(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
232 String articleIdString = aRequest.getParameter("articleid");
234 int articleId = Integer.parseInt(articleIdString);
236 returnList( aRequest, aResponse, "to_media="+articleId, "webdb_create desc", 0);
238 catch (Throwable e) {
239 throw new ServletModuleFailure(e);
243 public void returnList(HttpServletRequest aRequest, HttpServletResponse aResponse,
244 String aWhereClause, String anOrderByClause, int anOffset) throws ServletModuleExc {
246 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
247 URLBuilder urlBuilder = new URLBuilder();
250 Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
253 EntityAdapterEngine.retrieveAdapterList(getModel(), definition, aWhereClause,
254 anOrderByClause, nrEntitiesPerListPage, anOffset);
256 responseData.put("nexturl", null);
257 responseData.put("prevurl", null);
259 urlBuilder.setValue("module", "Comment");
260 urlBuilder.setValue("do", "list");
261 urlBuilder.setValue("where", aWhereClause);
262 urlBuilder.setValue("order", anOrderByClause);
264 urlBuilder.setValue("searchfield", requestParser.getParameter("searchfield"));
265 urlBuilder.setValue("searchtext", requestParser.getParameter("searchtext"));
266 urlBuilder.setValue("searchispublished", requestParser.getParameter("searchispublished"));
267 urlBuilder.setValue("searchstatus", requestParser.getParameter("searchstatus"));
268 urlBuilder.setValue("searchorder", requestParser.getParameter("searchorder"));
270 responseData.put("searchfield", requestParser.getParameter("searchfield"));
271 responseData.put("searchtext", requestParser.getParameter("searchtext"));
272 responseData.put("searchispublished", requestParser.getParameter("searchispublished"));
273 responseData.put("searchstatus", requestParser.getParameter("searchstatus"));
274 responseData.put("searchorder", requestParser.getParameter("searchorder"));
276 urlBuilder.setValue("offset", anOffset);
277 responseData.put("offset" , Integer.toString(anOffset));
278 responseData.put("thisurl" , urlBuilder.getQuery());
280 if (commentList.size()>=nrEntitiesPerListPage) {
281 urlBuilder.setValue("offset", anOffset + nrEntitiesPerListPage);
282 responseData.put("nexturl" , urlBuilder.getQuery());
286 urlBuilder.setValue("offset", Math.max(anOffset - nrEntitiesPerListPage, 0));
287 responseData.put("prevurl" , urlBuilder.getQuery());
290 responseData.put("comments", commentList);
291 responseData.put("from" , Integer.toString(anOffset+1));
292 responseData.put("to", Integer.toString(anOffset+commentList.size()-1));
294 ServletHelper.generateResponse(aResponse.getWriter(), responseData, listGenerator);
296 catch (Throwable e) {
297 throw new ServletModuleFailure(e);
301 public void update(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
304 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
306 String returnUrl = requestParser.getParameter("returnurl");
308 String idParam = aRequest.getParameter("id");
309 if (idParam == null) {
310 throw new ServletModuleExc("Wrong call: (id) is missing");
313 Map withValues = getIntersectingValues(aRequest, DatabaseComment.getInstance());
315 if (!withValues.containsKey("is_published")) {
316 withValues.put("is_published","0");
318 if (!withValues.containsKey("is_html")) {
319 withValues.put("is_html","0");
322 String webdbCreate = (String) withValues.get("webdb_create");
323 if (webdbCreate==null || webdbCreate.trim().length()==0) {
324 withValues.remove("webdb_create");
327 String id = mainModule.set(withValues);
329 logAdminUsage(aRequest, id, "object modified");
331 if (returnUrl!=null){
332 ServletHelper.redirect(aResponse, returnUrl);
335 editObject(aRequest, aResponse, idParam);
338 catch (Throwable e) {
339 throw new ServletModuleFailure(e);