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 java.util.HashMap;
34 import java.util.Iterator;
35 import java.util.List;
36 import java.util.Locale;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse;
41 import mir.config.MirPropertiesConfiguration;
42 import mir.entity.adapter.EntityAdapterModel;
43 import mir.entity.adapter.EntityIteratorAdapter;
44 import mir.log.LoggerWrapper;
45 import mir.servlet.ServletModule;
46 import mir.servlet.ServletModuleExc;
47 import mir.servlet.ServletModuleFailure;
48 import mir.servlet.ServletModuleUserExc;
49 import mir.util.CachingRewindableIterator;
50 import mir.util.HTTPRequestParser;
51 import mir.util.JDBCStringRoutines;
52 import mir.util.SQLQueryBuilder;
53 import mir.util.URLBuilder;
54 import mircoders.entity.EntityComment;
55 import mircoders.global.MirGlobal;
56 import mircoders.module.ModuleComment;
57 import mircoders.module.ModuleContent;
58 import mircoders.storage.DatabaseComment;
59 import mircoders.storage.DatabaseContent;
62 * ServletModuleComment - controls navigation for Comments
68 public class ServletModuleComment extends ServletModule
70 private ModuleContent moduleContent;
72 private static ServletModuleComment instance = new ServletModuleComment();
73 public static ServletModule getInstance() { return instance; }
75 private ServletModuleComment() {
76 logger = new LoggerWrapper("ServletModule.Comment");
78 configuration = MirPropertiesConfiguration.instance();
79 templateListString = configuration.getString("ServletModule.Comment.ListTemplate");
80 templateObjektString = configuration.getString("ServletModule.Comment.ObjektTemplate");
81 templateConfirmString = configuration.getString("ServletModule.Comment.ConfirmTemplate");
83 mainModule = new ModuleComment(DatabaseComment.getInstance());
84 moduleContent = new ModuleContent(DatabaseContent.getInstance());
87 logger.error("servletmodule comment could not be initialized:" + e.getMessage());
91 public void delete(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
92 if (!configuration.getString("Mir.Localizer.Admin.AllowDeleteArticle", "0").equals("1"))
93 throw new ServletModuleExc("Operation not permitted");
95 super.delete(req, res);
98 public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
100 String idParam = req.getParameter("id");
103 throw new ServletModuleExc("Invalid call: id not supplied ");
105 showComment(idParam, req, res);
108 public void showComment(String anId, HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
110 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
111 Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] {getLocale(aRequest), getFallbackLocale(aRequest)});
112 EntityAdapterModel model = MirGlobal.localizer().dataModel().adapterModel();
114 URLBuilder urlBuilder = new URLBuilder();
116 urlBuilder.setValue("module", "Comment");
117 urlBuilder.setValue("do", "edit");
118 urlBuilder.setValue("id", anId);
119 urlBuilder.setValue("returnurl", requestParser.getParameter("returnurl"));
122 responseData.put("new", Boolean.FALSE);
123 comment = model.makeEntityAdapter("comment", mainModule.getById(anId));
126 List fields = DatabaseComment.getInstance().getFields();
127 responseData.put("new", Boolean.TRUE);
128 comment = new HashMap();
129 Iterator i = fields.iterator();
130 while (i.hasNext()) {
131 comment.put(i.next(), null);
134 responseData.put("comment", comment);
136 responseData.put("returnurl", requestParser.getParameter("returnurl"));
137 responseData.put("thisurl", urlBuilder.getQuery());
139 ServletHelper.generateResponse(aResponse.getWriter(), responseData, templateObjektString);
141 catch (Throwable e) {
142 throw new ServletModuleFailure(e);
146 public void attach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
148 String mediaIdParam = req.getParameter("mid");
149 String commentId = req.getParameter("commentid");
151 if (commentId == null || mediaIdParam==null) throw new ServletModuleExc("smod comment :: attach :: commentid/mid missing");
154 EntityComment comment = (EntityComment) mainModule.getById(commentId);
155 comment.attach(mediaIdParam);
158 throw new ServletModuleFailure(e);
161 showComment(commentId, req, res);
164 public void dettach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
166 String commentId = req.getParameter("commentid");
167 String midParam = req.getParameter("mid");
168 if (commentId == null)
169 throw new ServletModuleExc("smod comment :: dettach :: commentid missing");
170 if (midParam == null)
171 throw new ServletModuleExc("smod comment :: dettach :: mid missing");
174 EntityComment comment = (EntityComment)mainModule.getById(commentId);
175 comment.dettach(commentId, midParam);
178 throw new ServletModuleFailure(e);
181 showComment(commentId, req, res);
185 public void list(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
187 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
189 String where = requestParser.getParameter("where");
190 String order = requestParser.getParameterWithDefault("order", "webdb_create desc");
191 int offset = requestParser.getIntegerWithDefault("offset", 0);
193 returnCommentList(aRequest, aResponse, where, order, offset);
196 public void search(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
198 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
199 SQLQueryBuilder queryBuilder = new SQLQueryBuilder();
201 String queryField = "";
202 String searchField = requestParser.getParameter("searchfield");
203 String searchText = requestParser.getParameter("searchtext");
204 String searchIsPublished = requestParser.getParameter("searchispublished");
205 String searchStatus = requestParser.getParameter("searchstatus");
206 String searchOrder = requestParser.getParameter("searchorder");
208 if (searchIsPublished.equals("0")) {
209 queryBuilder.appendAndCondition("is_published='f'");
211 else if (searchIsPublished.equals("1")) {
212 queryBuilder.appendAndCondition("is_published='t'");
215 if (searchText.length()>0) {
216 queryBuilder.appendAndCondition(
217 "lower("+ searchField + ") like " +
218 "'%" + JDBCStringRoutines.escapeStringLiteral(searchText.toLowerCase()) + "%'");
221 if (searchStatus.length()>0) {
222 queryBuilder.appendAndCondition("to_comment_status="+Integer.parseInt(searchStatus));
225 if (searchOrder.length()>0) {
226 if (searchOrder.equals("datedesc"))
227 queryBuilder.appendAscendingOrder("webdb_create");
228 else if (searchOrder.equals("dateasc"))
229 queryBuilder.appendDescendingOrder("webdb_create");
230 else if (searchOrder.equals("articletitle"))
231 queryBuilder.appendAscendingOrder("(select content.title from content where content.id = comment.to_media)");
232 else if (searchOrder.equals("creator"))
233 queryBuilder.appendDescendingOrder("creator");
236 returnCommentList(aRequest, aResponse, queryBuilder.getWhereClause(), queryBuilder.getOrderByClause(), 0);
239 public void articlecomments(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
241 String articleIdString = req.getParameter("articleid");
245 articleId = Integer.parseInt(articleIdString);
247 returnCommentList( req, res, "to_media="+articleId, "webdb_create desc", 0);
249 catch (Throwable e) {
250 throw new ServletModuleFailure(e);
254 public void returnCommentList(HttpServletRequest aRequest, HttpServletResponse aResponse,
255 String aWhereClause, String anOrderByClause, int anOffset) throws ServletModuleExc {
257 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
258 URLBuilder urlBuilder = new URLBuilder();
259 EntityAdapterModel model;
260 int nrCommentsPerPage = 20;
264 Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
265 model = MirGlobal.localizer().dataModel().adapterModel();
268 new CachingRewindableIterator(
269 new EntityIteratorAdapter( aWhereClause, anOrderByClause, nrCommentsPerPage,
270 MirGlobal.localizer().dataModel().adapterModel(), "comment", nrCommentsPerPage, anOffset)
273 responseData.put("nexturl", null);
274 responseData.put("prevurl", null);
276 count=mainModule.getSize(aWhereClause);
278 urlBuilder.setValue("module", "Comment");
279 urlBuilder.setValue("do", "list");
280 urlBuilder.setValue("where", aWhereClause);
281 urlBuilder.setValue("order", anOrderByClause);
283 urlBuilder.setValue("searchfield", requestParser.getParameter("searchfield"));
284 urlBuilder.setValue("searchtext", requestParser.getParameter("searchtext"));
285 urlBuilder.setValue("searchispublished", requestParser.getParameter("searchispublished"));
286 urlBuilder.setValue("searchstatus", requestParser.getParameter("searchstatus"));
287 urlBuilder.setValue("searchorder", requestParser.getParameter("searchorder"));
289 responseData.put("searchfield", requestParser.getParameter("searchfield"));
290 responseData.put("searchtext", requestParser.getParameter("searchtext"));
291 responseData.put("searchispublished", requestParser.getParameter("searchispublished"));
292 responseData.put("searchstatus", requestParser.getParameter("searchstatus"));
293 responseData.put("searchorder", requestParser.getParameter("searchorder"));
295 urlBuilder.setValue("offset", anOffset);
296 responseData.put("offset" , new Integer(anOffset).toString());
297 responseData.put("thisurl" , urlBuilder.getQuery());
299 if (count>=anOffset+nrCommentsPerPage) {
300 urlBuilder.setValue("offset", anOffset + nrCommentsPerPage);
301 responseData.put("nexturl" , urlBuilder.getQuery());
305 urlBuilder.setValue("offset", Math.max(anOffset - nrCommentsPerPage, 0));
306 responseData.put("prevurl" , urlBuilder.getQuery());
309 responseData.put("comments", commentList);
310 responseData.put("from" , Integer.toString(anOffset+1));
311 responseData.put("count", Integer.toString(count));
312 responseData.put("to", Integer.toString(Math.min(anOffset+nrCommentsPerPage, count)));
314 ServletHelper.generateResponse(aResponse.getWriter(), responseData, "commentlist.template");
316 catch (Throwable e) {
317 throw new ServletModuleFailure(e);