maintenance + localized operations introduced for comments
[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 list(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException
95   {
96     HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
97
98     String where = requestParser.getParameter("where");
99     String order = requestParser.getParameter("order");
100     int offset = requestParser.getIntegerWithDefault("offset", 0);
101
102     returnCommentList(aRequest, aResponse, where, order, offset);
103   }
104
105   public void search(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException
106   {
107     HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
108
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");
114
115     String whereClause = " (1=1) ";
116     String orderClause = "webdb_create desc";
117
118     if (searchIsPublished.equals("0")) {
119       whereClause=whereClause + "is_published='f'";
120     }
121     else if (searchIsPublished.equals("1")) {
122       whereClause=whereClause + "is_published='t'";
123     }
124
125     if (!searchField.equals("") && !searchText.equals("")) {
126       queryField="";
127
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";
134       else
135         queryField = "";
136
137       if (!queryField.equals("")) {
138         if (!whereClause.equals(""))
139           whereClause = whereClause + " and ";
140
141         // ML: searchText must be properly escaped!
142         whereClause = whereClause + "lower(" + queryField + ") like '%" + searchText.toLowerCase() + "%'";
143       }
144     }
145
146     System.out.println("search order = " + searchOrder);
147
148     if (searchOrder.equals("datedesc")) {
149       orderClause = "webdb_create desc";
150     }
151     else if (searchOrder.equals("dateasc")) {
152       orderClause = "webdb_create asc";
153     }
154     else if (searchOrder.equals("articletitle")) {
155       orderClause = "(select content.title from content where content.id = comment.to_media) asc";
156     }
157
158     returnCommentList(aRequest, aResponse, whereClause, orderClause, 0);
159   }
160
161   public void articlecomments(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
162   {
163     String articleIdString = req.getParameter("articleid");
164     int articleId;
165
166     try {
167       articleId  = Integer.parseInt(articleIdString);
168
169       returnCommentList( req, res, "to_media="+articleId, "webdb_create desc", 0);
170     }
171     catch (ServletModuleException e) {
172       throw e;
173     }
174     catch (Throwable e) {
175       throw new ServletModuleException(e.getMessage());
176     }
177   }
178
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
182     //     old one...
183
184     HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
185     URLBuilder urlBuilder = new URLBuilder();
186     EntityAdapterModel model;
187     int nrCommentsPerPage = 20;
188     int count;
189
190     try {
191       Map responseData = ServletHelper.makeGenerationData(getLocale(aRequest));
192       model = MirGlobal.localizer().dataModel().adapterModel();
193
194       Object commentList =
195           new CachingRewindableIterator(
196             new EntityIteratorAdapter( aWhereClause, anOrderByClause, nrCommentsPerPage,
197               MirGlobal.localizer().dataModel().adapterModel(), "comment", nrCommentsPerPage, anOffset)
198       );
199
200       responseData.put("nexturl", null);
201       responseData.put("prevurl", null);
202
203       count=mainModule.getSize(aWhereClause);
204
205       urlBuilder.setValue("module", "Comment");
206       urlBuilder.setValue("do", "list");
207       urlBuilder.setValue("where", aWhereClause);
208       urlBuilder.setValue("order", anOrderByClause);
209
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"));
214
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"));
219
220       urlBuilder.setValue("offset", anOffset);
221       responseData.put("thisurl" , urlBuilder.getQuery());
222
223       if (count>=anOffset+nrCommentsPerPage) {
224         urlBuilder.setValue("offset", anOffset + nrCommentsPerPage);
225         responseData.put("nexturl" , urlBuilder.getQuery());
226       }
227
228       if (anOffset>0) {
229         urlBuilder.setValue("offset", Math.max(anOffset - nrCommentsPerPage, 0));
230         responseData.put("prevurl" , urlBuilder.getQuery());
231       }
232
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)));
237
238       ServletHelper.generateResponse(aResponse.getWriter(), responseData, "commentlist.template");
239     }
240     catch (Throwable e) {
241       e.printStackTrace(new PrintWriter(new LoggerToWriterAdapter(logger, logger.ERROR_MESSAGE)));
242
243       throw new ServletModuleException(e.getMessage());
244     }
245   }
246 }
247