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.log.LoggerWrapper;
36 import mir.servlet.ServletModule;
37 import mir.servlet.ServletModuleExc;
38 import mir.servlet.ServletModuleFailure;
39 import mir.servlet.ServletModuleUserExc;
40 import mir.util.HTTPRequestParser;
41 import mir.util.JDBCStringRoutines;
42 import mir.util.SQLQueryBuilder;
43 import mir.util.URLBuilder;
44 import mircoders.entity.EntityComment;
45 import mircoders.global.MirGlobal;
46 import mircoders.module.ModuleComment;
47 import mircoders.storage.DatabaseComment;
49 import javax.servlet.http.HttpServletRequest;
50 import javax.servlet.http.HttpServletResponse;
54 * ServletModuleComment - controls navigation for Comments
57 * @author the mir-coders
60 public class ServletModuleComment extends ServletModule
62 private static ServletModuleComment instance = new ServletModuleComment();
63 public static ServletModule getInstance() { return instance; }
65 private ServletModuleComment() {
66 logger = new LoggerWrapper("ServletModule.Comment");
68 mainModule = new ModuleComment();
69 definition = "comment";
72 logger.error("servletmodule comment could not be initialized:" + e.getMessage());
76 public void delete(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
78 MirGlobal.accessControl().general().assertMayDeleteComments(ServletHelper.getUser(aRequest));
80 super.delete(aRequest, aResponse);
83 throw new ServletModuleFailure(t);
87 public void edit(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
89 String idParam = aRequest.getParameter("id");
92 throw new ServletModuleExc("Invalid call: id not supplied ");
94 editObject(aRequest, aResponse, idParam);
97 public void editObject(HttpServletRequest aRequest, HttpServletResponse aResponse, String anId) throws ServletModuleExc {
99 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
100 Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] {getLocale(aRequest), getFallbackLocale(aRequest)});
101 EntityAdapterModel model = MirGlobal.localizer().dataModel().adapterModel();
103 URLBuilder urlBuilder = new URLBuilder();
105 urlBuilder.setValue("module", "Comment");
106 urlBuilder.setValue("do", "edit");
107 urlBuilder.setValue("id", anId);
108 urlBuilder.setValue("returnurl", requestParser.getParameter("returnurl"));
111 responseData.put("new", Boolean.FALSE);
112 comment = model.makeEntityAdapter("comment", mainModule.getById(anId));
115 List fields = DatabaseComment.getInstance().getFieldNames();
116 responseData.put("new", Boolean.TRUE);
117 comment = new HashMap();
118 Iterator i = fields.iterator();
119 while (i.hasNext()) {
120 comment.put(i.next(), null);
123 responseData.put("comment", comment);
125 responseData.put("returnurl", requestParser.getParameter("returnurl"));
126 responseData.put("thisurl", urlBuilder.getQuery());
128 ServletHelper.generateResponse(aResponse.getWriter(), responseData, editGenerator);
130 catch (Throwable e) {
131 throw new ServletModuleFailure(e);
135 public void attach(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
137 String mediaIdParam = aRequest.getParameter("mid");
138 String commentId = aRequest.getParameter("commentid");
140 if (commentId == null || mediaIdParam==null) throw new ServletModuleExc("smod comment :: attach :: commentid/mid missing");
143 EntityComment comment = (EntityComment) mainModule.getById(commentId);
144 comment.attach(mediaIdParam);
147 throw new ServletModuleFailure(e);
150 logAdminUsage(aRequest, commentId, "media " + mediaIdParam + " attached");
152 editObject(aRequest, aResponse, commentId);
155 public void dettach(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
157 String commentId = aRequest.getParameter("commentid");
158 String midParam = aRequest.getParameter("mid");
159 if (commentId == null)
160 throw new ServletModuleExc("smod comment :: dettach :: commentid missing");
161 if (midParam == null)
162 throw new ServletModuleExc("smod comment :: dettach :: mid missing");
165 EntityComment comment = (EntityComment)mainModule.getById(commentId);
166 comment.dettach(commentId, midParam);
169 throw new ServletModuleFailure(e);
172 logAdminUsage(aRequest, commentId, "media " + midParam + " deattached");
174 editObject(aRequest, aResponse, commentId);
178 public void list(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
180 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
182 String where = requestParser.getParameter("where");
183 String order = requestParser.getParameterWithDefault("order", "webdb_create desc");
184 int offset = requestParser.getIntegerWithDefault("offset", 0);
186 returnList(aRequest, aResponse, where, order, offset);
189 public void search(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
191 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
192 SQLQueryBuilder queryBuilder = new SQLQueryBuilder();
194 String searchField = requestParser.getParameter("searchfield");
195 String searchText = requestParser.getParameter("searchtext");
196 String searchIsPublished = requestParser.getParameter("searchispublished");
197 String searchStatus = requestParser.getParameter("searchstatus");
198 String searchOrder = requestParser.getParameter("searchorder");
200 if (searchIsPublished.equals("0")) {
201 queryBuilder.appendAndCondition("is_published='f'");
203 else if (searchIsPublished.equals("1")) {
204 queryBuilder.appendAndCondition("is_published='t'");
207 if (searchText.length()>0) {
208 queryBuilder.appendAndCondition(
209 "lower("+ searchField + ") like " +
210 "'%" + JDBCStringRoutines.escapeStringLiteral(searchText.toLowerCase()) + "%'");
213 if (searchStatus.length()>0) {
214 queryBuilder.appendAndCondition("to_comment_status="+Integer.parseInt(searchStatus));
217 if (searchOrder.length()>0) {
218 if (searchOrder.equals("datedesc"))
219 queryBuilder.appendDescendingOrder("webdb_create");
220 else if (searchOrder.equals("dateasc"))
221 queryBuilder.appendAscendingOrder("webdb_create");
222 else if (searchOrder.equals("articletitle"))
223 queryBuilder.appendAscendingOrder("(select content.title from content where content.id = comment.to_media)");
224 else if (searchOrder.equals("creator"))
225 queryBuilder.appendDescendingOrder("creator");
228 returnList(aRequest, aResponse, queryBuilder.getWhereClause(), queryBuilder.getOrderByClause(), 0);
231 public void articlecomments(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
233 String articleIdString = aRequest.getParameter("articleid");
237 articleId = Integer.parseInt(articleIdString);
239 returnList( aRequest, aResponse, "to_media="+articleId, "webdb_create desc", 0);
241 catch (Throwable e) {
242 throw new ServletModuleFailure(e);
246 public void returnList(HttpServletRequest aRequest, HttpServletResponse aResponse,
247 String aWhereClause, String anOrderByClause, int anOffset) throws ServletModuleExc {
249 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
250 URLBuilder urlBuilder = new URLBuilder();
253 Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
256 EntityAdapterEngine.retrieveAdapterList(model, definition, aWhereClause, anOrderByClause, nrEntitiesPerListPage, anOffset);
258 responseData.put("nexturl", null);
259 responseData.put("prevurl", null);
261 urlBuilder.setValue("module", "Comment");
262 urlBuilder.setValue("do", "list");
263 urlBuilder.setValue("where", aWhereClause);
264 urlBuilder.setValue("order", anOrderByClause);
266 urlBuilder.setValue("searchfield", requestParser.getParameter("searchfield"));
267 urlBuilder.setValue("searchtext", requestParser.getParameter("searchtext"));
268 urlBuilder.setValue("searchispublished", requestParser.getParameter("searchispublished"));
269 urlBuilder.setValue("searchstatus", requestParser.getParameter("searchstatus"));
270 urlBuilder.setValue("searchorder", requestParser.getParameter("searchorder"));
272 responseData.put("searchfield", requestParser.getParameter("searchfield"));
273 responseData.put("searchtext", requestParser.getParameter("searchtext"));
274 responseData.put("searchispublished", requestParser.getParameter("searchispublished"));
275 responseData.put("searchstatus", requestParser.getParameter("searchstatus"));
276 responseData.put("searchorder", requestParser.getParameter("searchorder"));
278 urlBuilder.setValue("offset", anOffset);
279 responseData.put("offset" , new Integer(anOffset).toString());
280 responseData.put("thisurl" , urlBuilder.getQuery());
282 if (commentList.size()>=nrEntitiesPerListPage) {
283 urlBuilder.setValue("offset", anOffset + nrEntitiesPerListPage);
284 responseData.put("nexturl" , urlBuilder.getQuery());
288 urlBuilder.setValue("offset", Math.max(anOffset - nrEntitiesPerListPage, 0));
289 responseData.put("prevurl" , urlBuilder.getQuery());
292 responseData.put("comments", commentList);
293 responseData.put("from" , Integer.toString(anOffset+1));
294 responseData.put("to", Integer.toString(anOffset+commentList.size()-1));
296 ServletHelper.generateResponse(aResponse.getWriter(), responseData, listGenerator);
298 catch (Throwable e) {
299 throw new ServletModuleFailure(e);
303 public void update(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
306 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
308 String returnUrl = requestParser.getParameter("returnurl");
310 String idParam = aRequest.getParameter("id");
312 throw new ServletModuleExc("Wrong call: (id) is missing");
314 Map withValues = getIntersectingValues(aRequest, DatabaseComment.getInstance());
316 if (!withValues.containsKey("is_published"))
317 withValues.put("is_published","0");
318 if (!withValues.containsKey("is_html"))
319 withValues.put("is_html","0");
321 String webdbCreate = (String) withValues.get("webdb_create");
322 if (webdbCreate==null || webdbCreate.trim().length()==0)
323 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);
335 catch (Throwable e) {
336 throw new ServletModuleFailure(e);