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;
34 import java.io.PrintWriter;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletResponse;
40 import mir.config.MirPropertiesConfiguration;
41 import mir.entity.adapter.EntityAdapterModel;
42 import mir.entity.adapter.EntityIteratorAdapter;
43 import mir.log.LoggerToWriterAdapter;
44 import mir.log.LoggerWrapper;
45 import mir.servlet.ServletModule;
46 import mir.servlet.ServletModuleException;
47 import mir.util.CachingRewindableIterator;
48 import mir.util.HTTPRequestParser;
49 import mir.util.JDBCStringRoutines;
50 import mir.util.URLBuilder;
51 import mircoders.global.MirGlobal;
52 import mircoders.module.ModuleComment;
53 import mircoders.module.ModuleContent;
54 import mircoders.storage.DatabaseComment;
55 import mircoders.storage.DatabaseCommentStatus;
56 import mircoders.storage.DatabaseContent;
57 import mircoders.storage.DatabaseLanguage;
58 import freemarker.template.SimpleHash;
59 import freemarker.template.TemplateModelRoot;
62 * ServletModuleComment - controls navigation for Comments
68 public class ServletModuleComment extends ServletModule
71 private ModuleContent moduleContent;
73 // Singelton / Kontruktor
74 private static ServletModuleComment instance = new ServletModuleComment();
75 public static ServletModule getInstance() { return instance; }
77 private ServletModuleComment() {
78 logger = new LoggerWrapper("ServletModule.Comment");
82 configuration = MirPropertiesConfiguration.instance();
83 templateListString = configuration.getString("ServletModule.Comment.ListTemplate");
84 templateObjektString = configuration.getString("ServletModule.Comment.ObjektTemplate");
85 templateConfirmString = configuration.getString("ServletModule.Comment.ConfirmTemplate");
87 mainModule = new ModuleComment(DatabaseComment.getInstance());
88 moduleContent = new ModuleContent(DatabaseContent.getInstance());
91 logger.error("servletmodule comment could not be initialized:" + e.getMessage());
95 public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
97 String idParam = req.getParameter("id");
100 throw new ServletModuleException("Invalid call: id not supplied ");
102 showComment(idParam, req, res);
105 public void showComment(String anId, HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException {
107 SimpleHash extraInfo = new SimpleHash();
108 TemplateModelRoot data;
110 data = (TemplateModelRoot) mainModule.getById(anId);
112 extraInfo.put("languages", DatabaseLanguage.getInstance().getPopupData());
113 extraInfo.put("comment_status_values", DatabaseCommentStatus.getInstance().getPopupData());
115 deliver(aRequest, aResponse, data, extraInfo, templateObjektString);
117 catch (Exception e) {
118 throw new ServletModuleException(e.toString());
123 public void list(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException
125 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
127 String where = requestParser.getParameter("where");
128 String order = requestParser.getParameterWithDefault("order", "webdb_create desc");
129 int offset = requestParser.getIntegerWithDefault("offset", 0);
131 returnCommentList(aRequest, aResponse, where, order, offset);
134 public void search(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException
136 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
138 String queryField = "";
139 String searchField = requestParser.getParameter("searchfield");
140 String searchText = requestParser.getParameter("searchtext");
141 String searchIsPublished = requestParser.getParameter("searchispublished");
142 String searchOrder = requestParser.getParameter("searchorder");
144 String whereClause = " (1=1) ";
145 String orderClause = "webdb_create desc";
147 if (searchIsPublished.equals("0")) {
148 whereClause=whereClause + "is_published='f'";
150 else if (searchIsPublished.equals("1")) {
151 whereClause=whereClause + "is_published='t'";
154 if (!searchField.equals("") && !searchText.equals("")) {
157 if (searchField.equals("title"))
158 queryField = "title";
159 else if (searchField.equals("creator"))
160 queryField = "creator";
161 else if (searchField.equals("description"))
162 queryField = "description";
166 if (!queryField.equals("")) {
167 if (!whereClause.equals(""))
168 whereClause = whereClause + " and ";
170 whereClause = whereClause + "lower(" + queryField + ") like '%" + JDBCStringRoutines.escapeStringLiteral(searchText.toLowerCase()) + "%'";
174 System.out.println("search order = " + searchOrder);
176 if (searchOrder.equals("datedesc")) {
177 orderClause = "webdb_create desc";
179 else if (searchOrder.equals("dateasc")) {
180 orderClause = "webdb_create asc";
182 else if (searchOrder.equals("articletitle")) {
183 orderClause = "(select content.title from content where content.id = comment.to_media) asc";
186 returnCommentList(aRequest, aResponse, whereClause, orderClause, 0);
189 public void articlecomments(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
191 String articleIdString = req.getParameter("articleid");
195 articleId = Integer.parseInt(articleIdString);
197 returnCommentList( req, res, "to_media="+articleId, "webdb_create desc", 0);
199 catch (ServletModuleException e) {
202 catch (Throwable e) {
203 throw new ServletModuleException(e.getMessage());
207 public void returnCommentList(HttpServletRequest aRequest, HttpServletResponse aResponse,
208 String aWhereClause, String anOrderByClause, int anOffset) throws ServletModuleException {
209 // ML: experiment in using the producer's generation system instead of the
212 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
213 URLBuilder urlBuilder = new URLBuilder();
214 EntityAdapterModel model;
215 int nrCommentsPerPage = 20;
219 Map responseData = ServletHelper.makeGenerationData(getLocale(aRequest));
220 model = MirGlobal.localizer().dataModel().adapterModel();
223 new CachingRewindableIterator(
224 new EntityIteratorAdapter( aWhereClause, anOrderByClause, nrCommentsPerPage,
225 MirGlobal.localizer().dataModel().adapterModel(), "comment", nrCommentsPerPage, anOffset)
228 responseData.put("nexturl", null);
229 responseData.put("prevurl", null);
231 count=mainModule.getSize(aWhereClause);
233 urlBuilder.setValue("module", "Comment");
234 urlBuilder.setValue("do", "list");
235 urlBuilder.setValue("where", aWhereClause);
236 urlBuilder.setValue("order", anOrderByClause);
238 urlBuilder.setValue("searchfield", requestParser.getParameter("searchfield"));
239 urlBuilder.setValue("searchtext", requestParser.getParameter("searchtext"));
240 urlBuilder.setValue("searchispublished", requestParser.getParameter("searchispublished"));
241 urlBuilder.setValue("searchorder", requestParser.getParameter("searchorder"));
243 responseData.put("searchfield", requestParser.getParameter("searchfield"));
244 responseData.put("searchtext", requestParser.getParameter("searchtext"));
245 responseData.put("searchispublished", requestParser.getParameter("searchispublished"));
246 responseData.put("searchorder", requestParser.getParameter("searchorder"));
248 urlBuilder.setValue("offset", anOffset);
249 responseData.put("offset" , new Integer(anOffset).toString());
250 responseData.put("thisurl" , urlBuilder.getQuery());
252 if (count>=anOffset+nrCommentsPerPage) {
253 urlBuilder.setValue("offset", anOffset + nrCommentsPerPage);
254 responseData.put("nexturl" , urlBuilder.getQuery());
258 urlBuilder.setValue("offset", Math.max(anOffset - nrCommentsPerPage, 0));
259 responseData.put("prevurl" , urlBuilder.getQuery());
262 responseData.put("comments", commentList);
263 responseData.put("from" , Integer.toString(anOffset+1));
264 responseData.put("count", Integer.toString(count));
265 responseData.put("to", Integer.toString(Math.min(anOffset+nrCommentsPerPage, count)));
267 ServletHelper.generateResponse(aResponse.getWriter(), responseData, "commentlist.template");
269 catch (Throwable e) {
270 e.printStackTrace(new PrintWriter(new LoggerToWriterAdapter(logger, LoggerWrapper.ERROR_MESSAGE)));
272 throw new ServletModuleException(e.getMessage());