2 * Copyright (C) 2001, 2002 The Mir-coders group
\r
4 * This file is part of Mir.
\r
6 * Mir is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 2 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Mir is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with Mir; if not, write to the Free Software
\r
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 * In addition, as a special exception, The Mir-coders gives permission to link
\r
21 * the code of this program with the com.oreilly.servlet library, any library
\r
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
\r
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
\r
24 * the above that use the same license as the above), and distribute linked
\r
25 * combinations including the two. You must obey the GNU General Public
\r
26 * License in all respects for all of the code used other than the above
\r
27 * mentioned libraries. If you modify this file, you may extend this exception
\r
28 * to your version of the file, but you are not obligated to do so. If you do
\r
29 * not wish to do so, delete this exception statement from your version.
\r
32 package mircoders.servlet;
\r
34 import java.util.HashMap;
\r
35 import java.util.Iterator;
\r
36 import java.util.List;
\r
37 import java.util.Locale;
\r
38 import java.util.Map;
\r
39 import javax.servlet.http.HttpServletRequest;
\r
40 import javax.servlet.http.HttpServletResponse;
\r
42 import mir.config.MirPropertiesConfiguration;
\r
43 import mir.entity.adapter.EntityAdapterModel;
\r
44 import mir.entity.adapter.EntityIteratorAdapter;
\r
45 import mir.log.LoggerWrapper;
\r
46 import mir.servlet.ServletModule;
\r
47 import mir.servlet.ServletModuleExc;
\r
48 import mir.servlet.ServletModuleFailure;
\r
49 import mir.util.CachingRewindableIterator;
\r
50 import mir.util.HTTPRequestParser;
\r
51 import mir.util.JDBCStringRoutines;
\r
52 import mir.util.SQLQueryBuilder;
\r
53 import mir.util.URLBuilder;
\r
54 import mircoders.entity.EntityComment;
\r
55 import mircoders.global.MirGlobal;
\r
56 import mircoders.module.ModuleComment;
\r
57 import mircoders.module.ModuleContent;
\r
58 import mircoders.storage.DatabaseComment;
\r
59 import mircoders.storage.DatabaseContent;
\r
62 * ServletModuleComment - controls navigation for Comments
\r
68 public class ServletModuleComment extends ServletModule
\r
70 private ModuleContent moduleContent;
\r
72 private static ServletModuleComment instance = new ServletModuleComment();
\r
73 public static ServletModule getInstance() { return instance; }
\r
75 private ServletModuleComment() {
\r
76 logger = new LoggerWrapper("ServletModule.Comment");
\r
78 configuration = MirPropertiesConfiguration.instance();
\r
79 templateListString = configuration.getString("ServletModule.Comment.ListTemplate");
\r
80 templateObjektString = configuration.getString("ServletModule.Comment.ObjektTemplate");
\r
81 templateConfirmString = configuration.getString("ServletModule.Comment.ConfirmTemplate");
\r
83 mainModule = new ModuleComment(DatabaseComment.getInstance());
\r
84 moduleContent = new ModuleContent(DatabaseContent.getInstance());
\r
86 catch (Exception e) {
\r
87 logger.error("servletmodule comment could not be initialized:" + e.getMessage());
\r
91 public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
\r
93 String idParam = req.getParameter("id");
\r
95 if (idParam == null)
\r
96 throw new ServletModuleExc("Invalid call: id not supplied ");
\r
98 showComment(idParam, req, res);
\r
101 public void showComment(String anId, HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
\r
103 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
104 Map responseData = ServletHelper.makeGenerationData(new Locale[] {getLocale(aRequest), getFallbackLocale(aRequest)});
\r
105 EntityAdapterModel model = MirGlobal.localizer().dataModel().adapterModel();
\r
107 URLBuilder urlBuilder = new URLBuilder();
\r
109 urlBuilder.setValue("module", "Comment");
\r
110 urlBuilder.setValue("do", "edit");
\r
111 urlBuilder.setValue("id", anId);
\r
112 urlBuilder.setValue("returnurl", requestParser.getParameter("returnurl"));
\r
114 if (anId != null) {
\r
115 responseData.put("new", Boolean.FALSE);
\r
116 comment = model.makeEntityAdapter("comment", mainModule.getById(anId));
\r
119 List fields = DatabaseComment.getInstance().getFields();
\r
120 responseData.put("new", Boolean.TRUE);
\r
121 comment = new HashMap();
\r
122 Iterator i = fields.iterator();
\r
123 while (i.hasNext()) {
\r
124 comment.put(i.next(), null);
\r
127 // MirGlobal.localizer().adminInterface().initializeArticle(article);
\r
129 responseData.put("comment", comment);
\r
131 responseData.put("returnurl", requestParser.getParameter("returnurl"));
\r
132 responseData.put("thisurl", urlBuilder.getQuery());
\r
134 ServletHelper.generateResponse(aResponse.getWriter(), responseData, templateObjektString);
\r
136 catch (Throwable e) {
\r
137 throw new ServletModuleFailure(e);
\r
141 public void attach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
\r
143 String mediaIdParam = req.getParameter("mid");
\r
144 String commentId = req.getParameter("commentid");
\r
146 if (commentId == null || mediaIdParam==null) throw new ServletModuleExc("smod comment :: attach :: commentid/mid missing");
\r
149 EntityComment comment = (EntityComment) mainModule.getById(commentId);
\r
150 comment.attach(mediaIdParam);
\r
152 catch(Throwable e) {
\r
153 throw new ServletModuleFailure(e);
\r
156 showComment(commentId, req, res);
\r
159 public void dettach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
\r
161 String commentId = req.getParameter("commentid");
\r
162 String midParam = req.getParameter("mid");
\r
163 if (commentId == null)
\r
164 throw new ServletModuleExc("smod comment :: dettach :: commentid missing");
\r
165 if (midParam == null)
\r
166 throw new ServletModuleExc("smod comment :: dettach :: mid missing");
\r
169 EntityComment comment = (EntityComment)mainModule.getById(commentId);
\r
170 comment.dettach(commentId, midParam);
\r
172 catch(Throwable e) {
\r
173 throw new ServletModuleFailure(e);
\r
176 showComment(commentId, req, res);
\r
180 public void list(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
\r
182 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
184 String where = requestParser.getParameter("where");
\r
185 String order = requestParser.getParameterWithDefault("order", "webdb_create desc");
\r
186 int offset = requestParser.getIntegerWithDefault("offset", 0);
\r
188 returnCommentList(aRequest, aResponse, where, order, offset);
\r
191 public void search(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
\r
193 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
194 SQLQueryBuilder queryBuilder = new SQLQueryBuilder();
\r
196 String queryField = "";
\r
197 String searchField = requestParser.getParameter("searchfield");
\r
198 String searchText = requestParser.getParameter("searchtext");
\r
199 String searchIsPublished = requestParser.getParameter("searchispublished");
\r
200 String searchStatus = requestParser.getParameter("searchstatus");
\r
201 String searchOrder = requestParser.getParameter("searchorder");
\r
203 if (searchIsPublished.equals("0")) {
\r
204 queryBuilder.appendAndCondition("is_published='f'");
\r
206 else if (searchIsPublished.equals("1")) {
\r
207 queryBuilder.appendAndCondition("is_published='t'");
\r
210 if (searchText.length()>0) {
\r
211 queryBuilder.appendAndCondition(
\r
212 "lower("+ searchField + ") like " +
\r
213 "'%" + JDBCStringRoutines.escapeStringLiteral(searchText.toLowerCase()) + "%'");
\r
216 if (searchStatus.length()>0) {
\r
217 queryBuilder.appendAndCondition("to_comment_status="+Integer.parseInt(searchStatus));
\r
220 if (searchOrder.length()>0) {
\r
221 if (searchOrder.equals("datedesc"))
\r
222 queryBuilder.appendAscendingOrder("webdb_create");
\r
223 else if (searchOrder.equals("dateasc"))
\r
224 queryBuilder.appendDescendingOrder("webdb_create");
\r
225 else if (searchOrder.equals("articletitle"))
\r
226 queryBuilder.appendAscendingOrder("(select content.title from content where content.id = comment.to_media)");
\r
227 else if (searchOrder.equals("creator"))
\r
228 queryBuilder.appendDescendingOrder("creator");
\r
231 returnCommentList(aRequest, aResponse, queryBuilder.getWhereClause(), queryBuilder.getOrderByClause(), 0);
\r
234 public void articlecomments(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
\r
236 String articleIdString = req.getParameter("articleid");
\r
240 articleId = Integer.parseInt(articleIdString);
\r
242 returnCommentList( req, res, "to_media="+articleId, "webdb_create desc", 0);
\r
244 catch (Throwable e) {
\r
245 throw new ServletModuleFailure(e);
\r
249 public void returnCommentList(HttpServletRequest aRequest, HttpServletResponse aResponse,
\r
250 String aWhereClause, String anOrderByClause, int anOffset) throws ServletModuleExc {
\r
252 HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
\r
253 URLBuilder urlBuilder = new URLBuilder();
\r
254 EntityAdapterModel model;
\r
255 int nrCommentsPerPage = 20;
\r
259 Map responseData = ServletHelper.makeGenerationData(new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
\r
260 model = MirGlobal.localizer().dataModel().adapterModel();
\r
262 Object commentList =
\r
263 new CachingRewindableIterator(
\r
264 new EntityIteratorAdapter( aWhereClause, anOrderByClause, nrCommentsPerPage,
\r
265 MirGlobal.localizer().dataModel().adapterModel(), "comment", nrCommentsPerPage, anOffset)
\r
268 responseData.put("nexturl", null);
\r
269 responseData.put("prevurl", null);
\r
271 count=mainModule.getSize(aWhereClause);
\r
273 urlBuilder.setValue("module", "Comment");
\r
274 urlBuilder.setValue("do", "list");
\r
275 urlBuilder.setValue("where", aWhereClause);
\r
276 urlBuilder.setValue("order", anOrderByClause);
\r
278 urlBuilder.setValue("searchfield", requestParser.getParameter("searchfield"));
\r
279 urlBuilder.setValue("searchtext", requestParser.getParameter("searchtext"));
\r
280 urlBuilder.setValue("searchispublished", requestParser.getParameter("searchispublished"));
\r
281 urlBuilder.setValue("searchstatus", requestParser.getParameter("searchstatus"));
\r
282 urlBuilder.setValue("searchorder", requestParser.getParameter("searchorder"));
\r
284 responseData.put("searchfield", requestParser.getParameter("searchfield"));
\r
285 responseData.put("searchtext", requestParser.getParameter("searchtext"));
\r
286 responseData.put("searchispublished", requestParser.getParameter("searchispublished"));
\r
287 responseData.put("searchstatus", requestParser.getParameter("searchstatus"));
\r
288 responseData.put("searchorder", requestParser.getParameter("searchorder"));
\r
290 urlBuilder.setValue("offset", anOffset);
\r
291 responseData.put("offset" , new Integer(anOffset).toString());
\r
292 responseData.put("thisurl" , urlBuilder.getQuery());
\r
294 if (count>=anOffset+nrCommentsPerPage) {
\r
295 urlBuilder.setValue("offset", anOffset + nrCommentsPerPage);
\r
296 responseData.put("nexturl" , urlBuilder.getQuery());
\r
300 urlBuilder.setValue("offset", Math.max(anOffset - nrCommentsPerPage, 0));
\r
301 responseData.put("prevurl" , urlBuilder.getQuery());
\r
304 responseData.put("comments", commentList);
\r
305 responseData.put("from" , Integer.toString(anOffset+1));
\r
306 responseData.put("count", Integer.toString(count));
\r
307 responseData.put("to", Integer.toString(Math.min(anOffset+nrCommentsPerPage, count)));
\r
309 ServletHelper.generateResponse(aResponse.getWriter(), responseData, "commentlist.template");
\r
311 catch (Throwable e) {
\r
312 throw new ServletModuleFailure(e);
\r