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;
39 import javax.servlet.http.HttpServletRequest;
40 import javax.servlet.http.HttpServletResponse;
42 import mir.config.MirPropertiesConfiguration;
43 import mir.entity.adapter.EntityAdapterModel;
44 import mir.entity.adapter.EntityIteratorAdapter;
45 import mir.log.LoggerWrapper;
46 import mir.servlet.ServletModule;
47 import mir.servlet.ServletModuleExc;
48 import mir.servlet.ServletModuleFailure;
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 edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
93 String idParam = req.getParameter("id");
96 throw new ServletModuleExc("Invalid call: id not supplied ");
98 showComment(idParam, req, res);
101 public void showComment(String anId, HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
103 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
104 Map responseData = ServletHelper.makeGenerationData(new Locale[] {getLocale(aRequest), getFallbackLocale(aRequest)});
105 EntityAdapterModel model = MirGlobal.localizer().dataModel().adapterModel();
107 URLBuilder urlBuilder = new URLBuilder();
109 urlBuilder.setValue("module", "Comment");
110 urlBuilder.setValue("do", "edit");
111 urlBuilder.setValue("id", anId);
112 urlBuilder.setValue("returnurl", requestParser.getParameter("returnurl"));
115 responseData.put("new", Boolean.FALSE);
116 comment = model.makeEntityAdapter("comment", mainModule.getById(anId));
119 List fields = DatabaseComment.getInstance().getFields();
120 responseData.put("new", Boolean.TRUE);
121 comment = new HashMap();
122 Iterator i = fields.iterator();
123 while (i.hasNext()) {
124 comment.put(i.next(), null);
127 // MirGlobal.localizer().adminInterface().initializeArticle(article);
129 responseData.put("comment", comment);
131 responseData.put("returnurl", requestParser.getParameter("returnurl"));
132 responseData.put("thisurl", urlBuilder.getQuery());
134 ServletHelper.generateResponse(aResponse.getWriter(), responseData, templateObjektString);
136 catch (Throwable e) {
137 throw new ServletModuleFailure(e);
141 public void attach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
143 String mediaIdParam = req.getParameter("mid");
144 String commentId = req.getParameter("commentid");
146 if (commentId == null || mediaIdParam==null) throw new ServletModuleExc("smod comment :: attach :: commentid/mid missing");
149 EntityComment comment = (EntityComment) mainModule.getById(commentId);
150 comment.attach(mediaIdParam);
153 throw new ServletModuleFailure(e);
156 showComment(commentId, req, res);
159 public void dettach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
161 String commentId = req.getParameter("commentid");
162 String midParam = req.getParameter("mid");
163 if (commentId == null)
164 throw new ServletModuleExc("smod comment :: dettach :: commentid missing");
165 if (midParam == null)
166 throw new ServletModuleExc("smod comment :: dettach :: mid missing");
169 EntityComment comment = (EntityComment)mainModule.getById(commentId);
170 comment.dettach(commentId, midParam);
173 throw new ServletModuleFailure(e);
176 showComment(commentId, req, res);
180 public void list(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
182 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
184 String where = requestParser.getParameter("where");
185 String order = requestParser.getParameterWithDefault("order", "webdb_create desc");
186 int offset = requestParser.getIntegerWithDefault("offset", 0);
188 returnCommentList(aRequest, aResponse, where, order, offset);
191 public void search(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
193 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
194 SQLQueryBuilder queryBuilder = new SQLQueryBuilder();
196 String queryField = "";
197 String searchField = requestParser.getParameter("searchfield");
198 String searchText = requestParser.getParameter("searchtext");
199 String searchIsPublished = requestParser.getParameter("searchispublished");
200 String searchStatus = requestParser.getParameter("searchstatus");
201 String searchOrder = requestParser.getParameter("searchorder");
203 if (searchIsPublished.equals("0")) {
204 queryBuilder.appendAndCondition("is_published='f'");
206 else if (searchIsPublished.equals("1")) {
207 queryBuilder.appendAndCondition("is_published='t'");
210 if (searchText.length()>0) {
211 queryBuilder.appendAndCondition(
212 "lower("+ searchField + ") like " +
213 "'%" + JDBCStringRoutines.escapeStringLiteral(searchText.toLowerCase()) + "%'");
216 if (searchStatus.length()>0) {
217 queryBuilder.appendAndCondition("to_comment_status="+Integer.parseInt(searchStatus));
220 if (searchOrder.length()>0) {
221 if (searchOrder.equals("datedesc"))
222 queryBuilder.appendAscendingOrder("webdb_create");
223 else if (searchOrder.equals("dateasc"))
224 queryBuilder.appendDescendingOrder("webdb_create");
225 else if (searchOrder.equals("articletitle"))
226 queryBuilder.appendAscendingOrder("(select content.title from content where content.id = comment.to_media)");
227 else if (searchOrder.equals("creator"))
228 queryBuilder.appendDescendingOrder("creator");
231 returnCommentList(aRequest, aResponse, queryBuilder.getWhereClause(), queryBuilder.getOrderByClause(), 0);
234 public void articlecomments(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
236 String articleIdString = req.getParameter("articleid");
240 articleId = Integer.parseInt(articleIdString);
242 returnCommentList( req, res, "to_media="+articleId, "webdb_create desc", 0);
244 catch (Throwable e) {
245 throw new ServletModuleFailure(e);
249 public void returnCommentList(HttpServletRequest aRequest, HttpServletResponse aResponse,
250 String aWhereClause, String anOrderByClause, int anOffset) throws ServletModuleExc {
252 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
253 URLBuilder urlBuilder = new URLBuilder();
254 EntityAdapterModel model;
255 int nrCommentsPerPage = 20;
259 Map responseData = ServletHelper.makeGenerationData(new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
260 model = MirGlobal.localizer().dataModel().adapterModel();
263 new CachingRewindableIterator(
264 new EntityIteratorAdapter( aWhereClause, anOrderByClause, nrCommentsPerPage,
265 MirGlobal.localizer().dataModel().adapterModel(), "comment", nrCommentsPerPage, anOffset)
268 responseData.put("nexturl", null);
269 responseData.put("prevurl", null);
271 count=mainModule.getSize(aWhereClause);
273 urlBuilder.setValue("module", "Comment");
274 urlBuilder.setValue("do", "list");
275 urlBuilder.setValue("where", aWhereClause);
276 urlBuilder.setValue("order", anOrderByClause);
278 urlBuilder.setValue("searchfield", requestParser.getParameter("searchfield"));
279 urlBuilder.setValue("searchtext", requestParser.getParameter("searchtext"));
280 urlBuilder.setValue("searchispublished", requestParser.getParameter("searchispublished"));
281 urlBuilder.setValue("searchstatus", requestParser.getParameter("searchstatus"));
282 urlBuilder.setValue("searchorder", requestParser.getParameter("searchorder"));
284 responseData.put("searchfield", requestParser.getParameter("searchfield"));
285 responseData.put("searchtext", requestParser.getParameter("searchtext"));
286 responseData.put("searchispublished", requestParser.getParameter("searchispublished"));
287 responseData.put("searchstatus", requestParser.getParameter("searchstatus"));
288 responseData.put("searchorder", requestParser.getParameter("searchorder"));
290 urlBuilder.setValue("offset", anOffset);
291 responseData.put("offset" , new Integer(anOffset).toString());
292 responseData.put("thisurl" , urlBuilder.getQuery());
294 if (count>=anOffset+nrCommentsPerPage) {
295 urlBuilder.setValue("offset", anOffset + nrCommentsPerPage);
296 responseData.put("nexturl" , urlBuilder.getQuery());
300 urlBuilder.setValue("offset", Math.max(anOffset - nrCommentsPerPage, 0));
301 responseData.put("prevurl" , urlBuilder.getQuery());
304 responseData.put("comments", commentList);
305 responseData.put("from" , Integer.toString(anOffset+1));
306 responseData.put("count", Integer.toString(count));
307 responseData.put("to", Integer.toString(Math.min(anOffset+nrCommentsPerPage, count)));
309 ServletHelper.generateResponse(aResponse.getWriter(), responseData, "commentlist.template");
311 catch (Throwable e) {
312 throw new ServletModuleFailure(e);