small changes here and there
[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  any library licensed under the Apache Software License,
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23  * (or with modified versions of the above that use the same license as the above),
24  * and distribute linked combinations including the two.  You must obey the
25  * GNU General Public License in all respects for all of the code used other than
26  * the above mentioned libraries.  If you modify this file, you may extend this
27  * exception to your version of the file, but you are not obligated to do so.
28  * If you do not wish to do so, delete this exception statement from your version.
29  */
30
31 package mircoders.servlet;
32
33 import java.util.HashMap;
34 import java.util.Iterator;
35 import java.util.List;
36 import java.util.Locale;
37 import java.util.Map;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse;
40
41 import mir.config.MirPropertiesConfiguration;
42 import mir.entity.adapter.EntityAdapterModel;
43 import mir.entity.adapter.EntityIteratorAdapter;
44 import mir.log.LoggerWrapper;
45 import mir.servlet.ServletModule;
46 import mir.servlet.ServletModuleExc;
47 import mir.servlet.ServletModuleFailure;
48 import mir.servlet.ServletModuleUserExc;
49 import mir.util.CachingRewindableIterator;
50 import mir.util.HTTPRequestParser;
51 import mir.util.JDBCStringRoutines;
52 import mir.util.SQLQueryBuilder;
53 import mir.util.URLBuilder;
54 import mircoders.entity.EntityComment;
55 import mircoders.global.MirGlobal;
56 import mircoders.module.ModuleComment;
57 import mircoders.module.ModuleContent;
58 import mircoders.storage.DatabaseComment;
59 import mircoders.storage.DatabaseContent;
60
61 /*
62  *  ServletModuleComment - controls navigation for Comments
63  *
64  *
65  *  @author RK
66  */
67
68 public class ServletModuleComment extends ServletModule
69 {
70   private ModuleContent     moduleContent;
71
72   private static ServletModuleComment instance = new ServletModuleComment();
73   public static ServletModule getInstance() { return instance; }
74
75   private ServletModuleComment() {
76     logger = new LoggerWrapper("ServletModule.Comment");
77     try {
78       configuration = MirPropertiesConfiguration.instance();
79       templateListString = configuration.getString("ServletModule.Comment.ListTemplate");
80       templateObjektString = configuration.getString("ServletModule.Comment.ObjektTemplate");
81       templateConfirmString = configuration.getString("ServletModule.Comment.ConfirmTemplate");
82
83       mainModule = new ModuleComment(DatabaseComment.getInstance());
84       moduleContent = new ModuleContent(DatabaseContent.getInstance());
85     }
86     catch (Exception e) {
87       logger.error("servletmodule comment could not be initialized:" + e.getMessage());
88     }
89   }
90
91   public void delete(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
92     if (!configuration.getString("Mir.Localizer.Admin.AllowDeleteArticle", "0").equals("1"))
93       throw new ServletModuleExc("Operation not permitted");
94
95     super.delete(req, res);
96   }
97
98   public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
99   {
100     String idParam = req.getParameter("id");
101
102     if (idParam == null)
103       throw new ServletModuleExc("Invalid call: id not supplied ");
104
105     showComment(idParam, req, res);
106   }
107
108   public void showComment(String anId, HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
109     try {
110       HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
111       Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] {getLocale(aRequest), getFallbackLocale(aRequest)});
112       EntityAdapterModel model = MirGlobal.localizer().dataModel().adapterModel();
113       Map comment;
114       URLBuilder urlBuilder = new URLBuilder();
115
116       urlBuilder.setValue("module", "Comment");
117       urlBuilder.setValue("do", "edit");
118       urlBuilder.setValue("id", anId);
119       urlBuilder.setValue("returnurl", requestParser.getParameter("returnurl"));
120
121       if (anId != null) {
122         responseData.put("new", Boolean.FALSE);
123         comment = model.makeEntityAdapter("comment", mainModule.getById(anId));
124       }
125       else {
126         List fields = DatabaseComment.getInstance().getFields();
127         responseData.put("new", Boolean.TRUE);
128         comment = new HashMap();
129         Iterator i = fields.iterator();
130         while (i.hasNext()) {
131           comment.put(i.next(), null);
132         }
133       }
134       responseData.put("comment", comment);
135
136       responseData.put("returnurl", requestParser.getParameter("returnurl"));
137       responseData.put("thisurl", urlBuilder.getQuery());
138
139       ServletHelper.generateResponse(aResponse.getWriter(), responseData, templateObjektString);
140     }
141     catch (Throwable e) {
142       throw new ServletModuleFailure(e);
143     }
144   }
145
146   public void attach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
147   {
148     String  mediaIdParam = req.getParameter("mid");
149     String  commentId = req.getParameter("commentid");
150
151     if (commentId == null || mediaIdParam==null) throw new ServletModuleExc("smod comment :: attach :: commentid/mid missing");
152
153     try {
154       EntityComment comment = (EntityComment) mainModule.getById(commentId);
155       comment.attach(mediaIdParam);
156     }
157     catch(Throwable e) {
158       throw new ServletModuleFailure(e);
159     }
160
161     showComment(commentId, req, res);
162   }
163
164   public void dettach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
165   {
166     String  commentId = req.getParameter("commentid");
167     String  midParam = req.getParameter("mid");
168     if (commentId == null)
169       throw new ServletModuleExc("smod comment :: dettach :: commentid missing");
170     if (midParam == null)
171       throw new ServletModuleExc("smod comment :: dettach :: mid missing");
172
173     try {
174       EntityComment comment = (EntityComment)mainModule.getById(commentId);
175       comment.dettach(commentId, midParam);
176     }
177     catch(Throwable e) {
178       throw new ServletModuleFailure(e);
179     }
180
181     showComment(commentId, req, res);
182   }
183
184
185   public void list(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
186   {
187     HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
188
189     String where = requestParser.getParameter("where");
190     String order = requestParser.getParameterWithDefault("order", "webdb_create desc");
191     int offset = requestParser.getIntegerWithDefault("offset", 0);
192
193     returnCommentList(aRequest, aResponse, where, order, offset);
194   }
195
196   public void search(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
197   {
198     HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
199     SQLQueryBuilder queryBuilder = new SQLQueryBuilder();
200
201     String queryField = "";
202     String searchField = requestParser.getParameter("searchfield");
203     String searchText = requestParser.getParameter("searchtext");
204     String searchIsPublished = requestParser.getParameter("searchispublished");
205     String searchStatus = requestParser.getParameter("searchstatus");
206     String searchOrder = requestParser.getParameter("searchorder");
207
208     if (searchIsPublished.equals("0")) {
209       queryBuilder.appendAndCondition("is_published='f'");
210     }
211     else if (searchIsPublished.equals("1")) {
212       queryBuilder.appendAndCondition("is_published='t'");
213     }
214
215     if (searchText.length()>0) {
216         queryBuilder.appendAndCondition(
217           "lower("+ searchField + ") like " +
218           "'%" + JDBCStringRoutines.escapeStringLiteral(searchText.toLowerCase()) + "%'");
219     }
220
221     if (searchStatus.length()>0) {
222       queryBuilder.appendAndCondition("to_comment_status="+Integer.parseInt(searchStatus));
223     }
224
225     if (searchOrder.length()>0) {
226       if (searchOrder.equals("datedesc"))
227         queryBuilder.appendAscendingOrder("webdb_create");
228       else if (searchOrder.equals("dateasc"))
229         queryBuilder.appendDescendingOrder("webdb_create");
230       else if (searchOrder.equals("articletitle"))
231         queryBuilder.appendAscendingOrder("(select content.title from content where content.id = comment.to_media)");
232       else if (searchOrder.equals("creator"))
233         queryBuilder.appendDescendingOrder("creator");
234     }
235
236     returnCommentList(aRequest, aResponse, queryBuilder.getWhereClause(), queryBuilder.getOrderByClause(), 0);
237   }
238
239   public void articlecomments(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
240   {
241     String articleIdString = req.getParameter("articleid");
242     int articleId;
243
244     try {
245       articleId  = Integer.parseInt(articleIdString);
246
247       returnCommentList( req, res, "to_media="+articleId, "webdb_create desc", 0);
248     }
249     catch (Throwable e) {
250       throw new ServletModuleFailure(e);
251     }
252   }
253
254   public void returnCommentList(HttpServletRequest aRequest, HttpServletResponse aResponse,
255      String aWhereClause, String anOrderByClause, int anOffset) throws ServletModuleExc {
256
257     HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
258     URLBuilder urlBuilder = new URLBuilder();
259     EntityAdapterModel model;
260     int nrCommentsPerPage = 20;
261     int count;
262
263     try {
264       Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
265       model = MirGlobal.localizer().dataModel().adapterModel();
266
267       Object commentList =
268           new CachingRewindableIterator(
269             new EntityIteratorAdapter( aWhereClause, anOrderByClause, nrCommentsPerPage,
270               MirGlobal.localizer().dataModel().adapterModel(), "comment", nrCommentsPerPage, anOffset)
271       );
272
273       responseData.put("nexturl", null);
274       responseData.put("prevurl", null);
275
276       count=mainModule.getSize(aWhereClause);
277
278       urlBuilder.setValue("module", "Comment");
279       urlBuilder.setValue("do", "list");
280       urlBuilder.setValue("where", aWhereClause);
281       urlBuilder.setValue("order", anOrderByClause);
282
283       urlBuilder.setValue("searchfield", requestParser.getParameter("searchfield"));
284       urlBuilder.setValue("searchtext", requestParser.getParameter("searchtext"));
285       urlBuilder.setValue("searchispublished", requestParser.getParameter("searchispublished"));
286       urlBuilder.setValue("searchstatus", requestParser.getParameter("searchstatus"));
287       urlBuilder.setValue("searchorder", requestParser.getParameter("searchorder"));
288
289       responseData.put("searchfield", requestParser.getParameter("searchfield"));
290       responseData.put("searchtext", requestParser.getParameter("searchtext"));
291       responseData.put("searchispublished", requestParser.getParameter("searchispublished"));
292       responseData.put("searchstatus", requestParser.getParameter("searchstatus"));
293       responseData.put("searchorder", requestParser.getParameter("searchorder"));
294
295       urlBuilder.setValue("offset", anOffset);
296       responseData.put("offset" , new Integer(anOffset).toString());
297       responseData.put("thisurl" , urlBuilder.getQuery());
298
299       if (count>=anOffset+nrCommentsPerPage) {
300         urlBuilder.setValue("offset", anOffset + nrCommentsPerPage);
301         responseData.put("nexturl" , urlBuilder.getQuery());
302       }
303
304       if (anOffset>0) {
305         urlBuilder.setValue("offset", Math.max(anOffset - nrCommentsPerPage, 0));
306         responseData.put("prevurl" , urlBuilder.getQuery());
307       }
308
309       responseData.put("comments", commentList);
310       responseData.put("from" , Integer.toString(anOffset+1));
311       responseData.put("count", Integer.toString(count));
312       responseData.put("to", Integer.toString(Math.min(anOffset+nrCommentsPerPage, count)));
313
314       ServletHelper.generateResponse(aResponse.getWriter(), responseData, "commentlist.template");
315     }
316     catch (Throwable e) {
317       throw new ServletModuleFailure(e);
318     }
319   }
320 }
321