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, null);
87 public void editObject(HttpServletRequest aRequest, HttpServletResponse aResponse, String anId, String aTemplate) 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 {
128 String mediaIdParam = aRequest.getParameter("mid");
129 String commentId = aRequest.getParameter("commentid");
131 if (commentId == null || mediaIdParam==null) {
132 throw new ServletModuleExc("smod comment :: attach :: commentid/mid missing");
136 EntityComment comment = (EntityComment) mainModule.getById(commentId);
137 comment.attach(mediaIdParam);
140 throw new ServletModuleFailure(e);
143 logAdminUsage(aRequest, commentId, "media " + mediaIdParam + " attached");
145 editObject(aRequest, aResponse, commentId, null);
148 public void dettach(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
149 String commentId = aRequest.getParameter("commentid");
150 String midParam = aRequest.getParameter("mid");
151 if (commentId == null) {
152 throw new ServletModuleExc("smod comment :: dettach :: commentid missing");
154 if (midParam == null) {
155 throw new ServletModuleExc("smod comment :: dettach :: mid missing");
159 EntityComment comment = (EntityComment)mainModule.getById(commentId);
160 comment.dettach(commentId, midParam);
163 throw new ServletModuleFailure(e);
166 logAdminUsage(aRequest, commentId, "media " + midParam + " deattached");
168 editObject(aRequest, aResponse, commentId, null);
172 public void list(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
174 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
176 String where = requestParser.getParameter("where");
177 String order = requestParser.getParameterWithDefault("order", "webdb_create desc");
178 int offset = requestParser.getIntegerWithDefault("offset", 0);
180 returnList(aRequest, aResponse, where, order, offset);
183 public void search(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
185 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
186 SQLQueryBuilder queryBuilder = new SQLQueryBuilder();
188 String searchField = requestParser.getParameter("searchfield");
189 String searchText = requestParser.getParameter("searchtext");
190 String searchIsPublished = requestParser.getParameter("searchispublished");
191 String searchStatus = requestParser.getParameter("searchstatus");
192 String searchOrder = requestParser.getParameter("searchorder");
194 if ("0".equals(searchIsPublished)) {
195 queryBuilder.appendAndCondition("is_published='f'");
197 else if ("1".equals(searchIsPublished)) {
198 queryBuilder.appendAndCondition("is_published='t'");
201 if (searchText.length()>0) {
202 queryBuilder.appendAndCondition(
203 "lower("+ searchField + ") like " +
204 "'%" + JDBCStringRoutines.escapeStringLiteral(searchText.toLowerCase()) + "%'");
207 if (searchStatus.length()>0) {
208 queryBuilder.appendAndCondition("to_comment_status="+Integer.parseInt(searchStatus));
211 if (searchOrder.length()>0) {
212 if ("datedesc".equals(searchOrder)) {
213 queryBuilder.appendDescendingOrder("webdb_create");
215 else if ("dateasc".equals(searchOrder)) {
216 queryBuilder.appendAscendingOrder("webdb_create");
218 else if ("articletitle".equals(searchOrder)) {
219 queryBuilder.appendAscendingOrder("(select content.title from content where content.id = comment.to_media)");
221 else if ("creator".equals(searchOrder)) {
222 queryBuilder.appendDescendingOrder("creator");
226 returnList(aRequest, aResponse, queryBuilder.getWhereClause(), queryBuilder.getOrderByClause(), 0);
229 public void articlecomments(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
230 String articleIdString = aRequest.getParameter("articleid");
232 int articleId = Integer.parseInt(articleIdString);
234 returnList( aRequest, aResponse, "to_media="+articleId, "webdb_create desc", 0);
236 catch (Throwable e) {
237 throw new ServletModuleFailure(e);
241 public void returnList(HttpServletRequest aRequest, HttpServletResponse aResponse,
242 String aWhereClause, String anOrderByClause, int anOffset) throws ServletModuleExc {
244 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
245 URLBuilder urlBuilder = new URLBuilder();
248 Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
251 EntityAdapterEngine.retrieveAdapterList(getModel(), definition, aWhereClause,
252 anOrderByClause, nrEntitiesPerListPage, anOffset);
254 responseData.put("nexturl", null);
255 responseData.put("prevurl", null);
257 urlBuilder.setValue("module", "Comment");
258 urlBuilder.setValue("do", "list");
259 urlBuilder.setValue("where", aWhereClause);
260 urlBuilder.setValue("order", anOrderByClause);
262 urlBuilder.setValue("searchfield", requestParser.getParameter("searchfield"));
263 urlBuilder.setValue("searchtext", requestParser.getParameter("searchtext"));
264 urlBuilder.setValue("searchispublished", requestParser.getParameter("searchispublished"));
265 urlBuilder.setValue("searchstatus", requestParser.getParameter("searchstatus"));
266 urlBuilder.setValue("searchorder", requestParser.getParameter("searchorder"));
268 responseData.put("searchfield", requestParser.getParameter("searchfield"));
269 responseData.put("searchtext", requestParser.getParameter("searchtext"));
270 responseData.put("searchispublished", requestParser.getParameter("searchispublished"));
271 responseData.put("searchstatus", requestParser.getParameter("searchstatus"));
272 responseData.put("searchorder", requestParser.getParameter("searchorder"));
274 urlBuilder.setValue("offset", anOffset);
275 responseData.put("offset" , Integer.toString(anOffset));
276 responseData.put("thisurl" , urlBuilder.getQuery());
278 if (commentList.size()>=nrEntitiesPerListPage) {
279 urlBuilder.setValue("offset", anOffset + nrEntitiesPerListPage);
280 responseData.put("nexturl" , urlBuilder.getQuery());
284 urlBuilder.setValue("offset", Math.max(anOffset - nrEntitiesPerListPage, 0));
285 responseData.put("prevurl" , urlBuilder.getQuery());
288 responseData.put("comments", commentList);
289 responseData.put("from" , Integer.toString(anOffset+1));
290 responseData.put("to", Integer.toString(anOffset+commentList.size()-1));
292 ServletHelper.generateResponse(aResponse.getWriter(), responseData, listGenerator);
294 catch (Throwable e) {
295 throw new ServletModuleFailure(e);
299 public void update(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
302 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
304 String returnUrl = requestParser.getParameter("returnurl");
306 String idParam = aRequest.getParameter("id");
307 if (idParam == null) {
308 throw new ServletModuleExc("Wrong call: (id) is missing");
311 Map withValues = getIntersectingValues(aRequest, DatabaseComment.getInstance());
313 if (!withValues.containsKey("is_published")) {
314 withValues.put("is_published","0");
316 if (!withValues.containsKey("is_html")) {
317 withValues.put("is_html","0");
320 String webdbCreate = (String) withValues.get("webdb_create");
321 if (webdbCreate==null || webdbCreate.trim().length()==0) {
322 withValues.remove("webdb_create");
325 String id = mainModule.set(withValues);
327 logAdminUsage(aRequest, id, "object modified");
329 if (returnUrl!=null){
330 ServletHelper.redirect(aResponse, returnUrl);
333 editObject(aRequest, aResponse, idParam, null);
336 catch (Throwable e) {
337 throw new ServletModuleFailure(e);