code cleaning, new config
[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.PrintWriter;
35 import java.util.Map;
36
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletResponse;
39
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;
60
61 /*
62  *  ServletModuleComment - controls navigation for Comments
63  *
64  *
65  * @author RK
66  */
67
68 public class ServletModuleComment extends ServletModule
69 {
70
71   private ModuleContent     moduleContent;
72
73   // Singelton / Kontruktor
74   private static ServletModuleComment instance = new ServletModuleComment();
75   public static ServletModule getInstance() { return instance; }
76
77   private ServletModuleComment() {
78     logger = new LoggerWrapper("ServletModule.Comment");
79
80
81     try {
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");
86
87       mainModule = new ModuleComment(DatabaseComment.getInstance());
88       moduleContent = new ModuleContent(DatabaseContent.getInstance());
89     }
90     catch (Exception e) {
91       logger.error("servletmodule comment could not be initialized:" + e.getMessage());
92     }
93   }
94
95   public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
96   {
97     String idParam = req.getParameter("id");
98
99     if (idParam == null)
100       throw new ServletModuleException("Invalid call: id not supplied ");
101
102     showComment(idParam, req, res);
103   }
104
105   public void showComment(String anId, HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException {
106     try {
107       SimpleHash extraInfo = new SimpleHash();
108       TemplateModelRoot data;
109
110       data = (TemplateModelRoot) mainModule.getById(anId);
111
112       extraInfo.put("languages", DatabaseLanguage.getInstance().getPopupData());
113       extraInfo.put("comment_status_values", DatabaseCommentStatus.getInstance().getPopupData());
114
115       deliver(aRequest, aResponse, data, extraInfo, templateObjektString);
116     }
117     catch (Exception e) {
118       throw new ServletModuleException(e.toString());
119     }
120   }
121
122
123   public void list(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException
124   {
125     HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
126
127     String where = requestParser.getParameter("where");
128     String order = requestParser.getParameterWithDefault("order", "webdb_create desc");
129     int offset = requestParser.getIntegerWithDefault("offset", 0);
130
131     returnCommentList(aRequest, aResponse, where, order, offset);
132   }
133
134   public void search(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException
135   {
136     HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
137
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");
143
144     String whereClause = " (1=1) ";
145     String orderClause = "webdb_create desc";
146
147     if (searchIsPublished.equals("0")) {
148       whereClause=whereClause + "is_published='f'";
149     }
150     else if (searchIsPublished.equals("1")) {
151       whereClause=whereClause + "is_published='t'";
152     }
153
154     if (!searchField.equals("") && !searchText.equals("")) {
155       queryField="";
156
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";
163       else
164         queryField = "";
165
166       if (!queryField.equals("")) {
167         if (!whereClause.equals(""))
168           whereClause = whereClause + " and ";
169
170         whereClause = whereClause + "lower(" + queryField + ") like '%" + JDBCStringRoutines.escapeStringLiteral(searchText.toLowerCase()) + "%'";
171       }
172     }
173
174     System.out.println("search order = " + searchOrder);
175
176     if (searchOrder.equals("datedesc")) {
177       orderClause = "webdb_create desc";
178     }
179     else if (searchOrder.equals("dateasc")) {
180       orderClause = "webdb_create asc";
181     }
182     else if (searchOrder.equals("articletitle")) {
183       orderClause = "(select content.title from content where content.id = comment.to_media) asc";
184     }
185
186     returnCommentList(aRequest, aResponse, whereClause, orderClause, 0);
187   }
188
189   public void articlecomments(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
190   {
191     String articleIdString = req.getParameter("articleid");
192     int articleId;
193
194     try {
195       articleId  = Integer.parseInt(articleIdString);
196
197       returnCommentList( req, res, "to_media="+articleId, "webdb_create desc", 0);
198     }
199     catch (ServletModuleException e) {
200       throw e;
201     }
202     catch (Throwable e) {
203       throw new ServletModuleException(e.getMessage());
204     }
205   }
206
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
210     //     old one...
211
212     HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
213     URLBuilder urlBuilder = new URLBuilder();
214     EntityAdapterModel model;
215     int nrCommentsPerPage = 20;
216     int count;
217
218     try {
219       Map responseData = ServletHelper.makeGenerationData(getLocale(aRequest));
220       model = MirGlobal.localizer().dataModel().adapterModel();
221
222       Object commentList =
223           new CachingRewindableIterator(
224             new EntityIteratorAdapter( aWhereClause, anOrderByClause, nrCommentsPerPage,
225               MirGlobal.localizer().dataModel().adapterModel(), "comment", nrCommentsPerPage, anOffset)
226       );
227
228       responseData.put("nexturl", null);
229       responseData.put("prevurl", null);
230
231       count=mainModule.getSize(aWhereClause);
232
233       urlBuilder.setValue("module", "Comment");
234       urlBuilder.setValue("do", "list");
235       urlBuilder.setValue("where", aWhereClause);
236       urlBuilder.setValue("order", anOrderByClause);
237
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"));
242
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"));
247
248       urlBuilder.setValue("offset", anOffset);
249       responseData.put("offset" , new Integer(anOffset).toString());
250       responseData.put("thisurl" , urlBuilder.getQuery());
251
252       if (count>=anOffset+nrCommentsPerPage) {
253         urlBuilder.setValue("offset", anOffset + nrCommentsPerPage);
254         responseData.put("nexturl" , urlBuilder.getQuery());
255       }
256
257       if (anOffset>0) {
258         urlBuilder.setValue("offset", Math.max(anOffset - nrCommentsPerPage, 0));
259         responseData.put("prevurl" , urlBuilder.getQuery());
260       }
261
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)));
266
267       ServletHelper.generateResponse(aResponse.getWriter(), responseData, "commentlist.template");
268     }
269     catch (Throwable e) {
270       e.printStackTrace(new PrintWriter(new LoggerToWriterAdapter(logger, LoggerWrapper.ERROR_MESSAGE)));
271
272       throw new ServletModuleException(e.getMessage());
273     }
274   }
275 }
276