add in offset to response data needed to go back to same place in list after a delete...
[mir.git] / source / mircoders / servlet / ServletModuleComment.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
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.
10  *
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.
15  *
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
19  *
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.
30  */
31
32 package mircoders.servlet;
33
34 import java.io.*;
35 import java.sql.*;
36 import java.util.*;
37 import java.net.*;
38
39 import javax.servlet.*;
40 import javax.servlet.http.*;
41
42 import org.apache.struts.util.MessageResources;
43
44 import freemarker.template.*;
45
46 import mir.servlet.*;
47 import mir.module.*;
48 import mir.misc.*;
49 import mir.entity.*;
50 import mir.storage.*;
51 import mir.generator.*;
52 import mir.entity.*;
53 import mir.entity.adapter.*;
54 import mir.util.*;
55 import mir.log.*;
56
57 import mircoders.storage.*;
58 import mircoders.global.*;
59 import mircoders.localizer.*;
60 import mircoders.module.*;
61
62 /*
63  *  ServletModuleComment - controls navigation for Comments
64  *
65  *
66  * @author RK
67  */
68
69 public class ServletModuleComment extends ServletModule
70 {
71
72   private ModuleContent     moduleContent;
73
74   // Singelton / Kontruktor
75   private static ServletModuleComment instance = new ServletModuleComment();
76   public static ServletModule getInstance() { return instance; }
77
78   private ServletModuleComment() {
79     logger = new LoggerWrapper("ServletModule.Comment");
80
81     templateListString = MirConfig.getProp("ServletModule.Comment.ListTemplate");
82     templateObjektString = MirConfig.getProp("ServletModule.Comment.ObjektTemplate");
83     templateConfirmString = MirConfig.getProp("ServletModule.Comment.ConfirmTemplate");
84
85     try {
86       mainModule = new ModuleComment(DatabaseComment.getInstance());
87       moduleContent = new ModuleContent(DatabaseContent.getInstance());
88     }
89     catch (StorageObjectException e) {
90       logger.error("servletmodule comment could not be initialized:" + e.getMessage());
91     }
92   }
93
94   public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
95   {
96     String idParam = req.getParameter("id");
97
98     if (idParam == null)
99       throw new ServletModuleException("Invalid call: id not supplied ");
100
101     showComment(idParam, req, res);
102   }
103
104   public void showComment(String anId, HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException {
105     try {
106       SimpleHash extraInfo = new SimpleHash();
107       TemplateModelRoot data;
108
109       data = (TemplateModelRoot) mainModule.getById(anId);
110
111       extraInfo.put("languages", DatabaseLanguage.getInstance().getPopupData());
112       extraInfo.put("comment_status_values", DatabaseCommentStatus.getInstance().getPopupData());
113
114       deliver(aRequest, aResponse, data, extraInfo, templateObjektString);
115     }
116     catch (Exception e) {
117       throw new ServletModuleException(e.toString());
118     }
119   }
120
121
122   public void list(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException
123   {
124     HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
125
126     String where = requestParser.getParameter("where");
127     String order = requestParser.getParameterWithDefault("order", "webdb_create desc");
128     int offset = requestParser.getIntegerWithDefault("offset", 0);
129
130     returnCommentList(aRequest, aResponse, where, order, offset);
131   }
132
133   public void search(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException
134   {
135     HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
136
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");
142
143     String whereClause = " (1=1) ";
144     String orderClause = "webdb_create desc";
145
146     if (searchIsPublished.equals("0")) {
147       whereClause=whereClause + "is_published='f'";
148     }
149     else if (searchIsPublished.equals("1")) {
150       whereClause=whereClause + "is_published='t'";
151     }
152
153     if (!searchField.equals("") && !searchText.equals("")) {
154       queryField="";
155
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";
162       else
163         queryField = "";
164
165       if (!queryField.equals("")) {
166         if (!whereClause.equals(""))
167           whereClause = whereClause + " and ";
168
169         whereClause = whereClause + "lower(" + queryField + ") like '%" + JDBCStringRoutines.escapeStringLiteral(searchText.toLowerCase()) + "%'";
170       }
171     }
172
173     System.out.println("search order = " + searchOrder);
174
175     if (searchOrder.equals("datedesc")) {
176       orderClause = "webdb_create desc";
177     }
178     else if (searchOrder.equals("dateasc")) {
179       orderClause = "webdb_create asc";
180     }
181     else if (searchOrder.equals("articletitle")) {
182       orderClause = "(select content.title from content where content.id = comment.to_media) asc";
183     }
184
185     returnCommentList(aRequest, aResponse, whereClause, orderClause, 0);
186   }
187
188   public void articlecomments(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
189   {
190     String articleIdString = req.getParameter("articleid");
191     int articleId;
192
193     try {
194       articleId  = Integer.parseInt(articleIdString);
195
196       returnCommentList( req, res, "to_media="+articleId, "webdb_create desc", 0);
197     }
198     catch (ServletModuleException e) {
199       throw e;
200     }
201     catch (Throwable e) {
202       throw new ServletModuleException(e.getMessage());
203     }
204   }
205
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
209     //     old one...
210
211     HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
212     URLBuilder urlBuilder = new URLBuilder();
213     EntityAdapterModel model;
214     int nrCommentsPerPage = 20;
215     int count;
216
217     try {
218       Map responseData = ServletHelper.makeGenerationData(getLocale(aRequest));
219       model = MirGlobal.localizer().dataModel().adapterModel();
220
221       Object commentList =
222           new CachingRewindableIterator(
223             new EntityIteratorAdapter( aWhereClause, anOrderByClause, nrCommentsPerPage,
224               MirGlobal.localizer().dataModel().adapterModel(), "comment", nrCommentsPerPage, anOffset)
225       );
226
227       responseData.put("nexturl", null);
228       responseData.put("prevurl", null);
229
230       count=mainModule.getSize(aWhereClause);
231
232       urlBuilder.setValue("module", "Comment");
233       urlBuilder.setValue("do", "list");
234       urlBuilder.setValue("where", aWhereClause);
235       urlBuilder.setValue("order", anOrderByClause);
236
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"));
241
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"));
246
247       urlBuilder.setValue("offset", anOffset);
248       responseData.put("offset" , new Integer(anOffset).toString());
249       responseData.put("thisurl" , urlBuilder.getQuery());
250
251       if (count>=anOffset+nrCommentsPerPage) {
252         urlBuilder.setValue("offset", anOffset + nrCommentsPerPage);
253         responseData.put("nexturl" , urlBuilder.getQuery());
254       }
255
256       if (anOffset>0) {
257         urlBuilder.setValue("offset", Math.max(anOffset - nrCommentsPerPage, 0));
258         responseData.put("prevurl" , urlBuilder.getQuery());
259       }
260
261       responseData.put("comments", commentList);
262       responseData.put("from" , Integer.toString(anOffset+1));
263       responseData.put("count", Integer.toString(count));
264       responseData.put("to", Integer.toString(Math.min(anOffset+nrCommentsPerPage, count)));
265
266       ServletHelper.generateResponse(aResponse.getWriter(), responseData, "commentlist.template");
267     }
268     catch (Throwable e) {
269       e.printStackTrace(new PrintWriter(new LoggerToWriterAdapter(logger, logger.ERROR_MESSAGE)));
270
271       throw new ServletModuleException(e.getMessage());
272     }
273   }
274 }
275