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();
71 logger.error("servletmodule comment could not be initialized:" + e.getMessage());
75 public void delete(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
77 MirGlobal.accessControl().general().assertMayDeleteComments(ServletHelper.getUser(aRequest));
79 super.delete(aRequest, aResponse);
82 throw new ServletModuleFailure(t);
86 public void edit(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
88 String idParam = aRequest.getParameter("id");
91 throw new ServletModuleExc("Invalid call: id not supplied ");
93 editObject(aRequest, aResponse, idParam);
96 public void editObject(HttpServletRequest aRequest, HttpServletResponse aResponse, String anId) throws ServletModuleExc {
98 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
99 Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] {getLocale(aRequest), getFallbackLocale(aRequest)});
100 EntityAdapterModel model = MirGlobal.localizer().dataModel().adapterModel();
102 URLBuilder urlBuilder = new URLBuilder();
104 urlBuilder.setValue("module", "Comment");
105 urlBuilder.setValue("do", "edit");
106 urlBuilder.setValue("id", anId);
107 urlBuilder.setValue("returnurl", requestParser.getParameter("returnurl"));
110 responseData.put("new", Boolean.FALSE);
111 comment = model.makeEntityAdapter("comment", mainModule.getById(anId));
114 List fields = DatabaseComment.getInstance().getFieldNames();
115 responseData.put("new", Boolean.TRUE);
116 comment = new HashMap();
117 Iterator i = fields.iterator();
118 while (i.hasNext()) {
119 comment.put(i.next(), null);
122 responseData.put("comment", comment);
124 responseData.put("returnurl", requestParser.getParameter("returnurl"));
125 responseData.put("thisurl", urlBuilder.getQuery());
127 ServletHelper.generateResponse(aResponse.getWriter(), responseData, editGenerator);
129 catch (Throwable e) {
130 throw new ServletModuleFailure(e);
134 public void attach(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
136 String mediaIdParam = aRequest.getParameter("mid");
137 String commentId = aRequest.getParameter("commentid");
139 if (commentId == null || mediaIdParam==null) throw new ServletModuleExc("smod comment :: attach :: commentid/mid missing");
142 EntityComment comment = (EntityComment) mainModule.getById(commentId);
143 comment.attach(mediaIdParam);
146 throw new ServletModuleFailure(e);
149 logAdminUsage(aRequest, commentId, "media " + mediaIdParam + " attached");
151 editObject(aRequest, aResponse, commentId);
154 public void dettach(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
156 String commentId = aRequest.getParameter("commentid");
157 String midParam = aRequest.getParameter("mid");
158 if (commentId == null)
159 throw new ServletModuleExc("smod comment :: dettach :: commentid missing");
160 if (midParam == null)
161 throw new ServletModuleExc("smod comment :: dettach :: mid missing");
164 EntityComment comment = (EntityComment)mainModule.getById(commentId);
165 comment.dettach(commentId, midParam);
168 throw new ServletModuleFailure(e);
171 logAdminUsage(aRequest, commentId, "media " + midParam + " deattached");
173 editObject(aRequest, aResponse, commentId);
177 public void list(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
179 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
181 String where = requestParser.getParameter("where");
182 String order = requestParser.getParameterWithDefault("order", "webdb_create desc");
183 int offset = requestParser.getIntegerWithDefault("offset", 0);
185 returnList(aRequest, aResponse, where, order, offset);
188 public void search(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
190 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
191 SQLQueryBuilder queryBuilder = new SQLQueryBuilder();
193 String searchField = requestParser.getParameter("searchfield");
194 String searchText = requestParser.getParameter("searchtext");
195 String searchIsPublished = requestParser.getParameter("searchispublished");
196 String searchStatus = requestParser.getParameter("searchstatus");
197 String searchOrder = requestParser.getParameter("searchorder");
199 if (searchIsPublished.equals("0")) {
200 queryBuilder.appendAndCondition("is_published='f'");
202 else if (searchIsPublished.equals("1")) {
203 queryBuilder.appendAndCondition("is_published='t'");
206 if (searchText.length()>0) {
207 queryBuilder.appendAndCondition(
208 "lower("+ searchField + ") like " +
209 "'%" + JDBCStringRoutines.escapeStringLiteral(searchText.toLowerCase()) + "%'");
212 if (searchStatus.length()>0) {
213 queryBuilder.appendAndCondition("to_comment_status="+Integer.parseInt(searchStatus));
216 if (searchOrder.length()>0) {
217 if (searchOrder.equals("datedesc"))
218 queryBuilder.appendDescendingOrder("webdb_create");
219 else if (searchOrder.equals("dateasc"))
220 queryBuilder.appendAscendingOrder("webdb_create");
221 else if (searchOrder.equals("articletitle"))
222 queryBuilder.appendAscendingOrder("(select content.title from content where content.id = comment.to_media)");
223 else if (searchOrder.equals("creator"))
224 queryBuilder.appendDescendingOrder("creator");
227 returnList(aRequest, aResponse, queryBuilder.getWhereClause(), queryBuilder.getOrderByClause(), 0);
230 public void articlecomments(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
232 String articleIdString = aRequest.getParameter("articleid");
236 articleId = Integer.parseInt(articleIdString);
238 returnList( aRequest, aResponse, "to_media="+articleId, "webdb_create desc", 0);
240 catch (Throwable e) {
241 throw new ServletModuleFailure(e);
245 public void returnList(HttpServletRequest aRequest, HttpServletResponse aResponse,
246 String aWhereClause, String anOrderByClause, int anOffset) throws ServletModuleExc {
248 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
249 URLBuilder urlBuilder = new URLBuilder();
252 Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
255 EntityAdapterEngine.retrieveAdapterList(model, definition, aWhereClause, anOrderByClause, nrEntitiesPerListPage, anOffset);
257 responseData.put("nexturl", null);
258 responseData.put("prevurl", null);
260 urlBuilder.setValue("module", "Comment");
261 urlBuilder.setValue("do", "list");
262 urlBuilder.setValue("where", aWhereClause);
263 urlBuilder.setValue("order", anOrderByClause);
265 urlBuilder.setValue("searchfield", requestParser.getParameter("searchfield"));
266 urlBuilder.setValue("searchtext", requestParser.getParameter("searchtext"));
267 urlBuilder.setValue("searchispublished", requestParser.getParameter("searchispublished"));
268 urlBuilder.setValue("searchstatus", requestParser.getParameter("searchstatus"));
269 urlBuilder.setValue("searchorder", requestParser.getParameter("searchorder"));
271 responseData.put("searchfield", requestParser.getParameter("searchfield"));
272 responseData.put("searchtext", requestParser.getParameter("searchtext"));
273 responseData.put("searchispublished", requestParser.getParameter("searchispublished"));
274 responseData.put("searchstatus", requestParser.getParameter("searchstatus"));
275 responseData.put("searchorder", requestParser.getParameter("searchorder"));
277 urlBuilder.setValue("offset", anOffset);
278 responseData.put("offset" , new Integer(anOffset).toString());
279 responseData.put("thisurl" , urlBuilder.getQuery());
281 if (commentList.size()>=nrEntitiesPerListPage) {
282 urlBuilder.setValue("offset", anOffset + nrEntitiesPerListPage);
283 responseData.put("nexturl" , urlBuilder.getQuery());
287 urlBuilder.setValue("offset", Math.max(anOffset - nrEntitiesPerListPage, 0));
288 responseData.put("prevurl" , urlBuilder.getQuery());
291 responseData.put("comments", commentList);
292 responseData.put("from" , Integer.toString(anOffset+1));
293 responseData.put("to", Integer.toString(anOffset+commentList.size()-1));
295 ServletHelper.generateResponse(aResponse.getWriter(), responseData, listGenerator);
297 catch (Throwable e) {
298 throw new ServletModuleFailure(e);
302 public void update(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
305 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
307 String returnUrl = requestParser.getParameter("returnurl");
309 String idParam = aRequest.getParameter("id");
311 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");
317 if (!withValues.containsKey("is_html"))
318 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");
324 String id = mainModule.set(withValues);
326 logAdminUsage(aRequest, id, "object modified");
328 if (returnUrl!=null){
329 ServletHelper.redirect(aResponse, returnUrl);
332 editObject(aRequest, aResponse, idParam);
334 catch (Throwable e) {
335 throw new ServletModuleFailure(e);