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(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 // MirGlobal.localizer().adminInterface().initializeArticle(article);
136 responseData.put("comment", comment);
138 responseData.put("returnurl", requestParser.getParameter("returnurl"));
139 responseData.put("thisurl", urlBuilder.getQuery());
141 ServletHelper.generateResponse(aResponse.getWriter(), responseData, templateObjektString);
143 catch (Throwable e) {
144 throw new ServletModuleFailure(e);
148 public void attach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
150 String mediaIdParam = req.getParameter("mid");
151 String commentId = req.getParameter("commentid");
153 if (commentId == null || mediaIdParam==null) throw new ServletModuleExc("smod comment :: attach :: commentid/mid missing");
156 EntityComment comment = (EntityComment) mainModule.getById(commentId);
157 comment.attach(mediaIdParam);
160 throw new ServletModuleFailure(e);
163 showComment(commentId, req, res);
166 public void dettach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
168 String commentId = req.getParameter("commentid");
169 String midParam = req.getParameter("mid");
170 if (commentId == null)
171 throw new ServletModuleExc("smod comment :: dettach :: commentid missing");
172 if (midParam == null)
173 throw new ServletModuleExc("smod comment :: dettach :: mid missing");
176 EntityComment comment = (EntityComment)mainModule.getById(commentId);
177 comment.dettach(commentId, midParam);
180 throw new ServletModuleFailure(e);
183 showComment(commentId, req, res);
187 public void list(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
189 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
191 String where = requestParser.getParameter("where");
192 String order = requestParser.getParameterWithDefault("order", "webdb_create desc");
193 int offset = requestParser.getIntegerWithDefault("offset", 0);
195 returnCommentList(aRequest, aResponse, where, order, offset);
198 public void search(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
200 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
201 SQLQueryBuilder queryBuilder = new SQLQueryBuilder();
203 String queryField = "";
204 String searchField = requestParser.getParameter("searchfield");
205 String searchText = requestParser.getParameter("searchtext");
206 String searchIsPublished = requestParser.getParameter("searchispublished");
207 String searchStatus = requestParser.getParameter("searchstatus");
208 String searchOrder = requestParser.getParameter("searchorder");
210 if (searchIsPublished.equals("0")) {
211 queryBuilder.appendAndCondition("is_published='f'");
213 else if (searchIsPublished.equals("1")) {
214 queryBuilder.appendAndCondition("is_published='t'");
217 if (searchText.length()>0) {
218 queryBuilder.appendAndCondition(
219 "lower("+ searchField + ") like " +
220 "'%" + JDBCStringRoutines.escapeStringLiteral(searchText.toLowerCase()) + "%'");
223 if (searchStatus.length()>0) {
224 queryBuilder.appendAndCondition("to_comment_status="+Integer.parseInt(searchStatus));
227 if (searchOrder.length()>0) {
228 if (searchOrder.equals("datedesc"))
229 queryBuilder.appendAscendingOrder("webdb_create");
230 else if (searchOrder.equals("dateasc"))
231 queryBuilder.appendDescendingOrder("webdb_create");
232 else if (searchOrder.equals("articletitle"))
233 queryBuilder.appendAscendingOrder("(select content.title from content where content.id = comment.to_media)");
234 else if (searchOrder.equals("creator"))
235 queryBuilder.appendDescendingOrder("creator");
238 returnCommentList(aRequest, aResponse, queryBuilder.getWhereClause(), queryBuilder.getOrderByClause(), 0);
241 public void articlecomments(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
243 String articleIdString = req.getParameter("articleid");
247 articleId = Integer.parseInt(articleIdString);
249 returnCommentList( req, res, "to_media="+articleId, "webdb_create desc", 0);
251 catch (Throwable e) {
252 throw new ServletModuleFailure(e);
256 public void returnCommentList(HttpServletRequest aRequest, HttpServletResponse aResponse,
257 String aWhereClause, String anOrderByClause, int anOffset) throws ServletModuleExc {
259 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
260 URLBuilder urlBuilder = new URLBuilder();
261 EntityAdapterModel model;
262 int nrCommentsPerPage = 20;
266 Map responseData = ServletHelper.makeGenerationData(new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
267 model = MirGlobal.localizer().dataModel().adapterModel();
270 new CachingRewindableIterator(
271 new EntityIteratorAdapter( aWhereClause, anOrderByClause, nrCommentsPerPage,
272 MirGlobal.localizer().dataModel().adapterModel(), "comment", nrCommentsPerPage, anOffset)
275 responseData.put("nexturl", null);
276 responseData.put("prevurl", null);
278 count=mainModule.getSize(aWhereClause);
280 urlBuilder.setValue("module", "Comment");
281 urlBuilder.setValue("do", "list");
282 urlBuilder.setValue("where", aWhereClause);
283 urlBuilder.setValue("order", anOrderByClause);
285 urlBuilder.setValue("searchfield", requestParser.getParameter("searchfield"));
286 urlBuilder.setValue("searchtext", requestParser.getParameter("searchtext"));
287 urlBuilder.setValue("searchispublished", requestParser.getParameter("searchispublished"));
288 urlBuilder.setValue("searchstatus", requestParser.getParameter("searchstatus"));
289 urlBuilder.setValue("searchorder", requestParser.getParameter("searchorder"));
291 responseData.put("searchfield", requestParser.getParameter("searchfield"));
292 responseData.put("searchtext", requestParser.getParameter("searchtext"));
293 responseData.put("searchispublished", requestParser.getParameter("searchispublished"));
294 responseData.put("searchstatus", requestParser.getParameter("searchstatus"));
295 responseData.put("searchorder", requestParser.getParameter("searchorder"));
297 urlBuilder.setValue("offset", anOffset);
298 responseData.put("offset" , new Integer(anOffset).toString());
299 responseData.put("thisurl" , urlBuilder.getQuery());
301 if (count>=anOffset+nrCommentsPerPage) {
302 urlBuilder.setValue("offset", anOffset + nrCommentsPerPage);
303 responseData.put("nexturl" , urlBuilder.getQuery());
307 urlBuilder.setValue("offset", Math.max(anOffset - nrCommentsPerPage, 0));
308 responseData.put("prevurl" , urlBuilder.getQuery());
311 responseData.put("comments", commentList);
312 responseData.put("from" , Integer.toString(anOffset+1));
313 responseData.put("count", Integer.toString(count));
314 responseData.put("to", Integer.toString(Math.min(anOffset+nrCommentsPerPage, count)));
316 ServletHelper.generateResponse(aResponse.getWriter(), responseData, "commentlist.template");
318 catch (Throwable e) {
319 throw new ServletModuleFailure(e);