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 the com.oreilly.servlet library, any library
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24 * the above that use the same license as the above), and distribute linked
25 * combinations including the two. You must obey the GNU General Public
26 * License in all respects for all of the code used other than the above
27 * mentioned libraries. If you modify this file, you may extend this exception
28 * to your version of the file, but you are not obligated to do so. If you do
29 * not wish to do so, delete this exception statement from your version.
32 package mircoders.servlet;
39 import javax.servlet.*;
40 import javax.servlet.http.*;
42 import org.apache.struts.util.MessageResources;
44 import freemarker.template.*;
51 import mir.generator.*;
53 import mir.entity.adapter.*;
57 import mircoders.storage.*;
58 import mircoders.global.*;
59 import mircoders.localizer.*;
60 import mircoders.module.*;
63 * ServletModuleComment - controls navigation for Comments
69 public class ServletModuleComment extends ServletModule
72 private ModuleContent moduleContent;
74 // Singelton / Kontruktor
75 private static ServletModuleComment instance = new ServletModuleComment();
76 public static ServletModule getInstance() { return instance; }
78 private ServletModuleComment() {
79 logger = new LoggerWrapper("ServletModule.Comment");
81 templateListString = MirConfig.getProp("ServletModule.Comment.ListTemplate");
82 templateObjektString = MirConfig.getProp("ServletModule.Comment.ObjektTemplate");
83 templateConfirmString = MirConfig.getProp("ServletModule.Comment.ConfirmTemplate");
86 mainModule = new ModuleComment(DatabaseComment.getInstance());
87 moduleContent = new ModuleContent(DatabaseContent.getInstance());
89 catch (StorageObjectException e) {
90 logger.error("servletmodule comment could not be initialized:" + e.getMessage());
94 public void list(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException
96 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
98 String where = requestParser.getParameter("where");
99 String order = requestParser.getParameter("order");
100 int offset = requestParser.getIntegerWithDefault("offset", 0);
102 returnCommentList(aRequest, aResponse, where, order, offset);
105 public void search(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException
107 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
109 String queryField = "";
110 String searchField = requestParser.getParameter("searchfield");
111 String searchText = requestParser.getParameter("searchtext");
112 String searchIsPublished = requestParser.getParameter("searchispublished");
113 String searchOrder = requestParser.getParameter("searchorder");
115 String whereClause = " (1=1) ";
116 String orderClause = "webdb_create desc";
118 if (searchIsPublished.equals("0")) {
119 whereClause=whereClause + "is_published='f'";
121 else if (searchIsPublished.equals("1")) {
122 whereClause=whereClause + "is_published='t'";
125 if (!searchField.equals("") && !searchText.equals("")) {
128 if (searchField.equals("title"))
129 queryField = "title";
130 else if (searchField.equals("creator"))
131 queryField = "creator";
132 else if (searchField.equals("description"))
133 queryField = "description";
137 if (!queryField.equals("")) {
138 if (!whereClause.equals(""))
139 whereClause = whereClause + " and ";
141 // ML: searchText must be properly escaped!
142 whereClause = whereClause + "lower(" + queryField + ") like '%" + searchText.toLowerCase() + "%'";
146 System.out.println("search order = " + searchOrder);
148 if (searchOrder.equals("datedesc")) {
149 orderClause = "webdb_create desc";
151 else if (searchOrder.equals("dateasc")) {
152 orderClause = "webdb_create asc";
154 else if (searchOrder.equals("articletitle")) {
155 orderClause = "(select content.title from content where content.id = comment.to_media) asc";
158 returnCommentList(aRequest, aResponse, whereClause, orderClause, 0);
161 public void articlecomments(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
163 String articleIdString = req.getParameter("articleid");
167 articleId = Integer.parseInt(articleIdString);
169 returnCommentList( req, res, "to_media="+articleId, "webdb_create desc", 0);
171 catch (ServletModuleException e) {
174 catch (Throwable e) {
175 throw new ServletModuleException(e.getMessage());
179 public void returnCommentList(HttpServletRequest aRequest, HttpServletResponse aResponse,
180 String aWhereClause, String anOrderByClause, int anOffset) throws ServletModuleException {
181 // ML: experiment in using the producer's generation system instead of the
184 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
185 URLBuilder urlBuilder = new URLBuilder();
186 EntityAdapterModel model;
187 int nrCommentsPerPage = 20;
191 Map responseData = ServletHelper.makeGenerationData(getLocale(aRequest));
192 model = MirGlobal.localizer().dataModel().adapterModel();
195 new CachingRewindableIterator(
196 new EntityIteratorAdapter( aWhereClause, anOrderByClause, nrCommentsPerPage,
197 MirGlobal.localizer().dataModel().adapterModel(), "comment", nrCommentsPerPage, anOffset)
200 responseData.put("nexturl", null);
201 responseData.put("prevurl", null);
203 count=mainModule.getSize(aWhereClause);
205 urlBuilder.setValue("module", "Comment");
206 urlBuilder.setValue("do", "list");
207 urlBuilder.setValue("where", aWhereClause);
208 urlBuilder.setValue("order", anOrderByClause);
210 urlBuilder.setValue("searchfield", requestParser.getParameter("searchfield"));
211 urlBuilder.setValue("searchtext", requestParser.getParameter("searchtext"));
212 urlBuilder.setValue("searchispublished", requestParser.getParameter("searchispublished"));
213 urlBuilder.setValue("searchorder", requestParser.getParameter("searchorder"));
215 responseData.put("searchfield", requestParser.getParameter("searchfield"));
216 responseData.put("searchtext", requestParser.getParameter("searchtext"));
217 responseData.put("searchispublished", requestParser.getParameter("searchispublished"));
218 responseData.put("searchorder", requestParser.getParameter("searchorder"));
220 urlBuilder.setValue("offset", anOffset);
221 responseData.put("thisurl" , urlBuilder.getQuery());
223 if (count>=anOffset+nrCommentsPerPage) {
224 urlBuilder.setValue("offset", anOffset + nrCommentsPerPage);
225 responseData.put("nexturl" , urlBuilder.getQuery());
229 urlBuilder.setValue("offset", Math.max(anOffset - nrCommentsPerPage, 0));
230 responseData.put("prevurl" , urlBuilder.getQuery());
233 responseData.put("comments", commentList);
234 responseData.put("from" , Integer.toString(anOffset+1));
235 responseData.put("count", Integer.toString(count));
236 responseData.put("to", Integer.toString(Math.min(anOffset+nrCommentsPerPage, count)));
238 ServletHelper.generateResponse(aResponse.getWriter(), responseData, "commentlist.template");
240 catch (Throwable e) {
241 e.printStackTrace(new PrintWriter(new LoggerToWriterAdapter(logger, logger.ERROR_MESSAGE)));
243 throw new ServletModuleException(e.getMessage());