misc. fixes
[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
39 import javax.servlet.http.HttpServletRequest;
40 import javax.servlet.http.HttpServletResponse;
41
42 import mir.config.MirPropertiesConfiguration;
43 import mir.entity.adapter.EntityAdapterModel;
44 import mir.entity.adapter.EntityIteratorAdapter;
45 import mir.log.LoggerWrapper;
46 import mir.servlet.ServletModule;
47 import mir.servlet.ServletModuleExc;
48 import mir.servlet.ServletModuleFailure;
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 edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
92   {
93     String idParam = req.getParameter("id");
94
95     if (idParam == null)
96       throw new ServletModuleExc("Invalid call: id not supplied ");
97
98     showComment(idParam, req, res);
99   }
100
101   public void showComment(String anId, HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc {
102     try {
103       HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
104       Map responseData = ServletHelper.makeGenerationData(new Locale[] {getLocale(aRequest), getFallbackLocale(aRequest)});
105       EntityAdapterModel model = MirGlobal.localizer().dataModel().adapterModel();
106       Map comment;
107       URLBuilder urlBuilder = new URLBuilder();
108
109       urlBuilder.setValue("module", "Comment");
110       urlBuilder.setValue("do", "edit");
111       urlBuilder.setValue("id", anId);
112       urlBuilder.setValue("returnurl", requestParser.getParameter("returnurl"));
113
114       if (anId != null) {
115         responseData.put("new", Boolean.FALSE);
116         comment = model.makeEntityAdapter("comment", mainModule.getById(anId));
117       }
118       else {
119         List fields = DatabaseComment.getInstance().getFields();
120         responseData.put("new", Boolean.TRUE);
121         comment = new HashMap();
122         Iterator i = fields.iterator();
123         while (i.hasNext()) {
124           comment.put(i.next(), null);
125         }
126
127 //        MirGlobal.localizer().adminInterface().initializeArticle(article);
128       }
129       responseData.put("comment", comment);
130
131       responseData.put("returnurl", requestParser.getParameter("returnurl"));
132       responseData.put("thisurl", urlBuilder.getQuery());
133
134       ServletHelper.generateResponse(aResponse.getWriter(), responseData, templateObjektString);
135     }
136     catch (Throwable e) {
137       throw new ServletModuleFailure(e);
138     }
139   }
140
141   public void attach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
142   {
143     String  mediaIdParam = req.getParameter("mid");
144     String  commentId = req.getParameter("commentid");
145
146     if (commentId == null || mediaIdParam==null) throw new ServletModuleExc("smod comment :: attach :: commentid/mid missing");
147
148     try {
149       EntityComment comment = (EntityComment) mainModule.getById(commentId);
150       comment.attach(mediaIdParam);
151     }
152     catch(Throwable e) {
153       throw new ServletModuleFailure(e);
154     }
155
156     showComment(commentId, req, res);
157   }
158
159   public void dettach(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
160   {
161     String  commentId = req.getParameter("commentid");
162     String  midParam = req.getParameter("mid");
163     if (commentId == null)
164       throw new ServletModuleExc("smod comment :: dettach :: commentid missing");
165     if (midParam == null)
166       throw new ServletModuleExc("smod comment :: dettach :: mid missing");
167
168     try {
169       EntityComment comment = (EntityComment)mainModule.getById(commentId);
170       comment.dettach(commentId, midParam);
171     }
172     catch(Throwable e) {
173       throw new ServletModuleFailure(e);
174     }
175
176     showComment(commentId, req, res);
177   }
178
179
180   public void list(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
181   {
182     HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
183
184     String where = requestParser.getParameter("where");
185     String order = requestParser.getParameterWithDefault("order", "webdb_create desc");
186     int offset = requestParser.getIntegerWithDefault("offset", 0);
187
188     returnCommentList(aRequest, aResponse, where, order, offset);
189   }
190
191   public void search(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
192   {
193     HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
194     SQLQueryBuilder queryBuilder = new SQLQueryBuilder();
195
196     String queryField = "";
197     String searchField = requestParser.getParameter("searchfield");
198     String searchText = requestParser.getParameter("searchtext");
199     String searchIsPublished = requestParser.getParameter("searchispublished");
200     String searchStatus = requestParser.getParameter("searchstatus");
201     String searchOrder = requestParser.getParameter("searchorder");
202
203     if (searchIsPublished.equals("0")) {
204       queryBuilder.appendAndCondition("is_published='f'");
205     }
206     else if (searchIsPublished.equals("1")) {
207       queryBuilder.appendAndCondition("is_published='t'");
208     }
209
210     if (searchText.length()>0) {
211         queryBuilder.appendAndCondition(
212           "lower("+ searchField + ") like " +
213           "'%" + JDBCStringRoutines.escapeStringLiteral(searchText.toLowerCase()) + "%'");
214     }
215
216     if (searchStatus.length()>0) {
217       queryBuilder.appendAndCondition("to_comment_status="+Integer.parseInt(searchStatus));
218     }
219
220     if (searchOrder.length()>0) {
221       if (searchOrder.equals("datedesc"))
222         queryBuilder.appendAscendingOrder("webdb_create");
223       else if (searchOrder.equals("dateasc"))
224         queryBuilder.appendDescendingOrder("webdb_create");
225       else if (searchOrder.equals("articletitle"))
226         queryBuilder.appendAscendingOrder("(select content.title from content where content.id = comment.to_media)");
227       else if (searchOrder.equals("creator"))
228         queryBuilder.appendDescendingOrder("creator");
229     }
230
231     returnCommentList(aRequest, aResponse, queryBuilder.getWhereClause(), queryBuilder.getOrderByClause(), 0);
232   }
233
234   public void articlecomments(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
235   {
236     String articleIdString = req.getParameter("articleid");
237     int articleId;
238
239     try {
240       articleId  = Integer.parseInt(articleIdString);
241
242       returnCommentList( req, res, "to_media="+articleId, "webdb_create desc", 0);
243     }
244     catch (Throwable e) {
245       throw new ServletModuleFailure(e);
246     }
247   }
248
249   public void returnCommentList(HttpServletRequest aRequest, HttpServletResponse aResponse,
250      String aWhereClause, String anOrderByClause, int anOffset) throws ServletModuleExc {
251
252     HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
253     URLBuilder urlBuilder = new URLBuilder();
254     EntityAdapterModel model;
255     int nrCommentsPerPage = 20;
256     int count;
257
258     try {
259       Map responseData = ServletHelper.makeGenerationData(new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
260       model = MirGlobal.localizer().dataModel().adapterModel();
261
262       Object commentList =
263           new CachingRewindableIterator(
264             new EntityIteratorAdapter( aWhereClause, anOrderByClause, nrCommentsPerPage,
265               MirGlobal.localizer().dataModel().adapterModel(), "comment", nrCommentsPerPage, anOffset)
266       );
267
268       responseData.put("nexturl", null);
269       responseData.put("prevurl", null);
270
271       count=mainModule.getSize(aWhereClause);
272
273       urlBuilder.setValue("module", "Comment");
274       urlBuilder.setValue("do", "list");
275       urlBuilder.setValue("where", aWhereClause);
276       urlBuilder.setValue("order", anOrderByClause);
277
278       urlBuilder.setValue("searchfield", requestParser.getParameter("searchfield"));
279       urlBuilder.setValue("searchtext", requestParser.getParameter("searchtext"));
280       urlBuilder.setValue("searchispublished", requestParser.getParameter("searchispublished"));
281       urlBuilder.setValue("searchstatus", requestParser.getParameter("searchstatus"));
282       urlBuilder.setValue("searchorder", requestParser.getParameter("searchorder"));
283
284       responseData.put("searchfield", requestParser.getParameter("searchfield"));
285       responseData.put("searchtext", requestParser.getParameter("searchtext"));
286       responseData.put("searchispublished", requestParser.getParameter("searchispublished"));
287       responseData.put("searchstatus", requestParser.getParameter("searchstatus"));
288       responseData.put("searchorder", requestParser.getParameter("searchorder"));
289
290       urlBuilder.setValue("offset", anOffset);
291       responseData.put("offset" , new Integer(anOffset).toString());
292       responseData.put("thisurl" , urlBuilder.getQuery());
293
294       if (count>=anOffset+nrCommentsPerPage) {
295         urlBuilder.setValue("offset", anOffset + nrCommentsPerPage);
296         responseData.put("nexturl" , urlBuilder.getQuery());
297       }
298
299       if (anOffset>0) {
300         urlBuilder.setValue("offset", Math.max(anOffset - nrCommentsPerPage, 0));
301         responseData.put("prevurl" , urlBuilder.getQuery());
302       }
303
304       responseData.put("comments", commentList);
305       responseData.put("from" , Integer.toString(anOffset+1));
306       responseData.put("count", Integer.toString(count));
307       responseData.put("to", Integer.toString(Math.min(anOffset+nrCommentsPerPage, count)));
308
309       ServletHelper.generateResponse(aResponse.getWriter(), responseData, "commentlist.template");
310     }
311     catch (Throwable e) {
312       throw new ServletModuleFailure(e);
313     }
314   }
315 }
316