multiple file-editable directories
[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(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 //        MirGlobal.localizer().adminInterface().initializeArticle(article);
135       }
136       responseData.put("comment", comment);
137
138       responseData.put("returnurl", requestParser.getParameter("returnurl"));
139       responseData.put("thisurl", urlBuilder.getQuery());
140
141       ServletHelper.generateResponse(aResponse.getWriter(), responseData, templateObjektString);
142     }
143     catch (Throwable e) {
144       throw new ServletModuleFailure(e);
145     }
146   }
147
148   public void attach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
149   {
150     String  mediaIdParam = req.getParameter("mid");
151     String  commentId = req.getParameter("commentid");
152
153     if (commentId == null || mediaIdParam==null) throw new ServletModuleExc("smod comment :: attach :: commentid/mid missing");
154
155     try {
156       EntityComment comment = (EntityComment) mainModule.getById(commentId);
157       comment.attach(mediaIdParam);
158     }
159     catch(Throwable e) {
160       throw new ServletModuleFailure(e);
161     }
162
163     showComment(commentId, req, res);
164   }
165
166   public void dettach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
167   {
168     String  commentId = req.getParameter("commentid");
169     String  midParam = req.getParameter("mid");
170     if (commentId == null)
171       throw new ServletModuleExc("smod comment :: dettach :: commentid missing");
172     if (midParam == null)
173       throw new ServletModuleExc("smod comment :: dettach :: mid missing");
174
175     try {
176       EntityComment comment = (EntityComment)mainModule.getById(commentId);
177       comment.dettach(commentId, midParam);
178     }
179     catch(Throwable e) {
180       throw new ServletModuleFailure(e);
181     }
182
183     showComment(commentId, req, res);
184   }
185
186
187   public void list(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
188   {
189     HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
190
191     String where = requestParser.getParameter("where");
192     String order = requestParser.getParameterWithDefault("order", "webdb_create desc");
193     int offset = requestParser.getIntegerWithDefault("offset", 0);
194
195     returnCommentList(aRequest, aResponse, where, order, offset);
196   }
197
198   public void search(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
199   {
200     HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
201     SQLQueryBuilder queryBuilder = new SQLQueryBuilder();
202
203     String queryField = "";
204     String searchField = requestParser.getParameter("searchfield");
205     String searchText = requestParser.getParameter("searchtext");
206     String searchIsPublished = requestParser.getParameter("searchispublished");
207     String searchStatus = requestParser.getParameter("searchstatus");
208     String searchOrder = requestParser.getParameter("searchorder");
209
210     if (searchIsPublished.equals("0")) {
211       queryBuilder.appendAndCondition("is_published='f'");
212     }
213     else if (searchIsPublished.equals("1")) {
214       queryBuilder.appendAndCondition("is_published='t'");
215     }
216
217     if (searchText.length()>0) {
218         queryBuilder.appendAndCondition(
219           "lower("+ searchField + ") like " +
220           "'%" + JDBCStringRoutines.escapeStringLiteral(searchText.toLowerCase()) + "%'");
221     }
222
223     if (searchStatus.length()>0) {
224       queryBuilder.appendAndCondition("to_comment_status="+Integer.parseInt(searchStatus));
225     }
226
227     if (searchOrder.length()>0) {
228       if (searchOrder.equals("datedesc"))
229         queryBuilder.appendAscendingOrder("webdb_create");
230       else if (searchOrder.equals("dateasc"))
231         queryBuilder.appendDescendingOrder("webdb_create");
232       else if (searchOrder.equals("articletitle"))
233         queryBuilder.appendAscendingOrder("(select content.title from content where content.id = comment.to_media)");
234       else if (searchOrder.equals("creator"))
235         queryBuilder.appendDescendingOrder("creator");
236     }
237
238     returnCommentList(aRequest, aResponse, queryBuilder.getWhereClause(), queryBuilder.getOrderByClause(), 0);
239   }
240
241   public void articlecomments(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
242   {
243     String articleIdString = req.getParameter("articleid");
244     int articleId;
245
246     try {
247       articleId  = Integer.parseInt(articleIdString);
248
249       returnCommentList( req, res, "to_media="+articleId, "webdb_create desc", 0);
250     }
251     catch (Throwable e) {
252       throw new ServletModuleFailure(e);
253     }
254   }
255
256   public void returnCommentList(HttpServletRequest aRequest, HttpServletResponse aResponse,
257      String aWhereClause, String anOrderByClause, int anOffset) throws ServletModuleExc {
258
259     HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
260     URLBuilder urlBuilder = new URLBuilder();
261     EntityAdapterModel model;
262     int nrCommentsPerPage = 20;
263     int count;
264
265     try {
266       Map responseData = ServletHelper.makeGenerationData(new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
267       model = MirGlobal.localizer().dataModel().adapterModel();
268
269       Object commentList =
270           new CachingRewindableIterator(
271             new EntityIteratorAdapter( aWhereClause, anOrderByClause, nrCommentsPerPage,
272               MirGlobal.localizer().dataModel().adapterModel(), "comment", nrCommentsPerPage, anOffset)
273       );
274
275       responseData.put("nexturl", null);
276       responseData.put("prevurl", null);
277
278       count=mainModule.getSize(aWhereClause);
279
280       urlBuilder.setValue("module", "Comment");
281       urlBuilder.setValue("do", "list");
282       urlBuilder.setValue("where", aWhereClause);
283       urlBuilder.setValue("order", anOrderByClause);
284
285       urlBuilder.setValue("searchfield", requestParser.getParameter("searchfield"));
286       urlBuilder.setValue("searchtext", requestParser.getParameter("searchtext"));
287       urlBuilder.setValue("searchispublished", requestParser.getParameter("searchispublished"));
288       urlBuilder.setValue("searchstatus", requestParser.getParameter("searchstatus"));
289       urlBuilder.setValue("searchorder", requestParser.getParameter("searchorder"));
290
291       responseData.put("searchfield", requestParser.getParameter("searchfield"));
292       responseData.put("searchtext", requestParser.getParameter("searchtext"));
293       responseData.put("searchispublished", requestParser.getParameter("searchispublished"));
294       responseData.put("searchstatus", requestParser.getParameter("searchstatus"));
295       responseData.put("searchorder", requestParser.getParameter("searchorder"));
296
297       urlBuilder.setValue("offset", anOffset);
298       responseData.put("offset" , new Integer(anOffset).toString());
299       responseData.put("thisurl" , urlBuilder.getQuery());
300
301       if (count>=anOffset+nrCommentsPerPage) {
302         urlBuilder.setValue("offset", anOffset + nrCommentsPerPage);
303         responseData.put("nexturl" , urlBuilder.getQuery());
304       }
305
306       if (anOffset>0) {
307         urlBuilder.setValue("offset", Math.max(anOffset - nrCommentsPerPage, 0));
308         responseData.put("prevurl" , urlBuilder.getQuery());
309       }
310
311       responseData.put("comments", commentList);
312       responseData.put("from" , Integer.toString(anOffset+1));
313       responseData.put("count", Integer.toString(count));
314       responseData.put("to", Integer.toString(Math.min(anOffset+nrCommentsPerPage, count)));
315
316       ServletHelper.generateResponse(aResponse.getWriter(), responseData, "commentlist.template");
317     }
318     catch (Throwable e) {
319       throw new ServletModuleFailure(e);
320     }
321   }
322 }
323