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 edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
96 String idParam = req.getParameter("id");
99 throw new ServletModuleException("Invalid call: id not supplied ");
101 showComment(idParam, req, res);
104 public void showComment(String anId, HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException {
106 SimpleHash extraInfo = new SimpleHash();
107 TemplateModelRoot data;
109 data = (TemplateModelRoot) mainModule.getById(anId);
111 extraInfo.put("languages", DatabaseLanguage.getInstance().getPopupData());
112 extraInfo.put("comment_status_values", DatabaseCommentStatus.getInstance().getPopupData());
114 deliver(aRequest, aResponse, data, extraInfo, templateObjektString);
116 catch (Exception e) {
117 throw new ServletModuleException(e.toString());
122 public void list(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException
124 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
126 String where = requestParser.getParameter("where");
127 String order = requestParser.getParameterWithDefault("order", "webdb_create desc");
128 int offset = requestParser.getIntegerWithDefault("offset", 0);
130 returnCommentList(aRequest, aResponse, where, order, offset);
133 public void search(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException
135 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
137 String queryField = "";
138 String searchField = requestParser.getParameter("searchfield");
139 String searchText = requestParser.getParameter("searchtext");
140 String searchIsPublished = requestParser.getParameter("searchispublished");
141 String searchOrder = requestParser.getParameter("searchorder");
143 String whereClause = " (1=1) ";
144 String orderClause = "webdb_create desc";
146 if (searchIsPublished.equals("0")) {
147 whereClause=whereClause + "is_published='f'";
149 else if (searchIsPublished.equals("1")) {
150 whereClause=whereClause + "is_published='t'";
153 if (!searchField.equals("") && !searchText.equals("")) {
156 if (searchField.equals("title"))
157 queryField = "title";
158 else if (searchField.equals("creator"))
159 queryField = "creator";
160 else if (searchField.equals("description"))
161 queryField = "description";
165 if (!queryField.equals("")) {
166 if (!whereClause.equals(""))
167 whereClause = whereClause + " and ";
169 whereClause = whereClause + "lower(" + queryField + ") like '%" + JDBCStringRoutines.escapeStringLiteral(searchText.toLowerCase()) + "%'";
173 System.out.println("search order = " + searchOrder);
175 if (searchOrder.equals("datedesc")) {
176 orderClause = "webdb_create desc";
178 else if (searchOrder.equals("dateasc")) {
179 orderClause = "webdb_create asc";
181 else if (searchOrder.equals("articletitle")) {
182 orderClause = "(select content.title from content where content.id = comment.to_media) asc";
185 returnCommentList(aRequest, aResponse, whereClause, orderClause, 0);
188 public void articlecomments(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
190 String articleIdString = req.getParameter("articleid");
194 articleId = Integer.parseInt(articleIdString);
196 returnCommentList( req, res, "to_media="+articleId, "webdb_create desc", 0);
198 catch (ServletModuleException e) {
201 catch (Throwable e) {
202 throw new ServletModuleException(e.getMessage());
206 public void returnCommentList(HttpServletRequest aRequest, HttpServletResponse aResponse,
207 String aWhereClause, String anOrderByClause, int anOffset) throws ServletModuleException {
208 // ML: experiment in using the producer's generation system instead of the
211 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
212 URLBuilder urlBuilder = new URLBuilder();
213 EntityAdapterModel model;
214 int nrCommentsPerPage = 20;
218 Map responseData = ServletHelper.makeGenerationData(getLocale(aRequest));
219 model = MirGlobal.localizer().dataModel().adapterModel();
222 new CachingRewindableIterator(
223 new EntityIteratorAdapter( aWhereClause, anOrderByClause, nrCommentsPerPage,
224 MirGlobal.localizer().dataModel().adapterModel(), "comment", nrCommentsPerPage, anOffset)
227 responseData.put("nexturl", null);
228 responseData.put("prevurl", null);
230 count=mainModule.getSize(aWhereClause);
232 urlBuilder.setValue("module", "Comment");
233 urlBuilder.setValue("do", "list");
234 urlBuilder.setValue("where", aWhereClause);
235 urlBuilder.setValue("order", anOrderByClause);
237 urlBuilder.setValue("searchfield", requestParser.getParameter("searchfield"));
238 urlBuilder.setValue("searchtext", requestParser.getParameter("searchtext"));
239 urlBuilder.setValue("searchispublished", requestParser.getParameter("searchispublished"));
240 urlBuilder.setValue("searchorder", requestParser.getParameter("searchorder"));
242 responseData.put("searchfield", requestParser.getParameter("searchfield"));
243 responseData.put("searchtext", requestParser.getParameter("searchtext"));
244 responseData.put("searchispublished", requestParser.getParameter("searchispublished"));
245 responseData.put("searchorder", requestParser.getParameter("searchorder"));
247 urlBuilder.setValue("offset", anOffset);
248 responseData.put("thisurl" , urlBuilder.getQuery());
250 if (count>=anOffset+nrCommentsPerPage) {
251 urlBuilder.setValue("offset", anOffset + nrCommentsPerPage);
252 responseData.put("nexturl" , urlBuilder.getQuery());
256 urlBuilder.setValue("offset", Math.max(anOffset - nrCommentsPerPage, 0));
257 responseData.put("prevurl" , urlBuilder.getQuery());
260 responseData.put("comments", commentList);
261 responseData.put("from" , Integer.toString(anOffset+1));
262 responseData.put("count", Integer.toString(count));
263 responseData.put("to", Integer.toString(Math.min(anOffset+nrCommentsPerPage, count)));
265 ServletHelper.generateResponse(aResponse.getWriter(), responseData, "commentlist.template");
267 catch (Throwable e) {
268 e.printStackTrace(new PrintWriter(new LoggerToWriterAdapter(logger, logger.ERROR_MESSAGE)));
270 throw new ServletModuleException(e.getMessage());