74089d33f12559a627d0fb6a9db0e5f6fe2a8c87
[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 mir.entity.adapter.EntityAdapterEngine;
34 import mir.entity.adapter.EntityAdapterModel;
35 import mir.log.LoggerWrapper;
36 import mir.servlet.ServletModule;
37 import mir.servlet.ServletModuleExc;
38 import mir.servlet.ServletModuleFailure;
39 import mir.servlet.ServletModuleUserExc;
40 import mir.util.HTTPRequestParser;
41 import mir.util.JDBCStringRoutines;
42 import mir.util.SQLQueryBuilder;
43 import mir.util.URLBuilder;
44 import mircoders.entity.EntityComment;
45 import mircoders.global.MirGlobal;
46 import mircoders.module.ModuleComment;
47 import mircoders.storage.DatabaseComment;
48
49 import javax.servlet.http.HttpServletRequest;
50 import javax.servlet.http.HttpServletResponse;
51 import java.util.*;
52
53 /*
54  *  ServletModuleComment - controls navigation for Comments
55  *
56  *
57  *  @author the mir-coders
58  */
59
60 public class ServletModuleComment extends ServletModule
61 {
62   private static ServletModuleComment instance = new ServletModuleComment();
63   public static ServletModule getInstance() { return instance; }
64
65   private ServletModuleComment() {
66     logger = new LoggerWrapper("ServletModule.Comment");
67     try {
68       mainModule = new ModuleComment();
69       definition = "comment";
70     }
71     catch (Exception e) {
72       logger.error("servletmodule comment could not be initialized:" + e.getMessage());
73     }
74   }
75
76   public void delete(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
77     try {
78       MirGlobal.accessControl().general().assertMayDeleteComments(ServletHelper.getUser(aRequest));
79
80       super.delete(aRequest, aResponse);
81     }
82     catch (Throwable t) {
83       throw new ServletModuleFailure(t);
84     }
85   }
86
87   public void edit(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
88   {
89     String idParam = aRequest.getParameter("id");
90
91     if (idParam == null)
92       throw new ServletModuleExc("Invalid call: id not supplied ");
93
94     editObject(aRequest, aResponse, idParam);
95   }
96
97   public void editObject(HttpServletRequest aRequest, HttpServletResponse aResponse, String anId) throws ServletModuleExc {
98     try {
99       HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
100       Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] {getLocale(aRequest), getFallbackLocale(aRequest)});
101       EntityAdapterModel model = MirGlobal.localizer().dataModel().adapterModel();
102       Map comment;
103       URLBuilder urlBuilder = new URLBuilder();
104
105       urlBuilder.setValue("module", "Comment");
106       urlBuilder.setValue("do", "edit");
107       urlBuilder.setValue("id", anId);
108       urlBuilder.setValue("returnurl", requestParser.getParameter("returnurl"));
109
110       if (anId != null) {
111         responseData.put("new", Boolean.FALSE);
112         comment = model.makeEntityAdapter("comment", mainModule.getById(anId));
113       }
114       else {
115         List fields = DatabaseComment.getInstance().getFieldNames();
116         responseData.put("new", Boolean.TRUE);
117         comment = new HashMap();
118         Iterator i = fields.iterator();
119         while (i.hasNext()) {
120           comment.put(i.next(), null);
121         }
122       }
123       responseData.put("comment", comment);
124
125       responseData.put("returnurl", requestParser.getParameter("returnurl"));
126       responseData.put("thisurl", urlBuilder.getQuery());
127
128       ServletHelper.generateResponse(aResponse.getWriter(), responseData, editGenerator);
129     }
130     catch (Throwable e) {
131       throw new ServletModuleFailure(e);
132     }
133   }
134
135   public void attach(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
136   {
137     String  mediaIdParam = aRequest.getParameter("mid");
138     String  commentId = aRequest.getParameter("commentid");
139
140     if (commentId == null || mediaIdParam==null) throw new ServletModuleExc("smod comment :: attach :: commentid/mid missing");
141
142     try {
143       EntityComment comment = (EntityComment) mainModule.getById(commentId);
144       comment.attach(mediaIdParam);
145     }
146     catch(Throwable e) {
147       throw new ServletModuleFailure(e);
148     }
149
150     logAdminUsage(aRequest, commentId, "media " + mediaIdParam + " attached");
151
152     editObject(aRequest, aResponse, commentId);
153   }
154
155   public void dettach(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
156   {
157     String  commentId = aRequest.getParameter("commentid");
158     String  midParam = aRequest.getParameter("mid");
159     if (commentId == null)
160       throw new ServletModuleExc("smod comment :: dettach :: commentid missing");
161     if (midParam == null)
162       throw new ServletModuleExc("smod comment :: dettach :: mid missing");
163
164     try {
165       EntityComment comment = (EntityComment)mainModule.getById(commentId);
166       comment.dettach(commentId, midParam);
167     }
168     catch(Throwable e) {
169       throw new ServletModuleFailure(e);
170     }
171
172     logAdminUsage(aRequest, commentId, "media " + midParam + " deattached");
173
174     editObject(aRequest, aResponse, commentId);
175   }
176
177
178   public void list(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
179   {
180     HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
181
182     String where = requestParser.getParameter("where");
183     String order = requestParser.getParameterWithDefault("order", "webdb_create desc");
184     int offset = requestParser.getIntegerWithDefault("offset", 0);
185
186     returnList(aRequest, aResponse, where, order, offset);
187   }
188
189   public void search(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
190   {
191     HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
192     SQLQueryBuilder queryBuilder = new SQLQueryBuilder();
193
194     String searchField = requestParser.getParameter("searchfield");
195     String searchText = requestParser.getParameter("searchtext");
196     String searchIsPublished = requestParser.getParameter("searchispublished");
197     String searchStatus = requestParser.getParameter("searchstatus");
198     String searchOrder = requestParser.getParameter("searchorder");
199
200     if (searchIsPublished.equals("0")) {
201       queryBuilder.appendAndCondition("is_published='f'");
202     }
203     else if (searchIsPublished.equals("1")) {
204       queryBuilder.appendAndCondition("is_published='t'");
205     }
206
207     if (searchText.length()>0) {
208         queryBuilder.appendAndCondition(
209           "lower("+ searchField + ") like " +
210           "'%" + JDBCStringRoutines.escapeStringLiteral(searchText.toLowerCase()) + "%'");
211     }
212
213     if (searchStatus.length()>0) {
214       queryBuilder.appendAndCondition("to_comment_status="+Integer.parseInt(searchStatus));
215     }
216
217     if (searchOrder.length()>0) {
218       if (searchOrder.equals("datedesc"))
219         queryBuilder.appendDescendingOrder("webdb_create");
220       else if (searchOrder.equals("dateasc"))
221         queryBuilder.appendAscendingOrder("webdb_create");
222       else if (searchOrder.equals("articletitle"))
223         queryBuilder.appendAscendingOrder("(select content.title from content where content.id = comment.to_media)");
224       else if (searchOrder.equals("creator"))
225         queryBuilder.appendDescendingOrder("creator");
226     }
227
228     returnList(aRequest, aResponse, queryBuilder.getWhereClause(), queryBuilder.getOrderByClause(), 0);
229   }
230
231   public void articlecomments(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
232   {
233     String articleIdString = aRequest.getParameter("articleid");
234     int articleId;
235
236     try {
237       articleId  = Integer.parseInt(articleIdString);
238
239       returnList( aRequest, aResponse, "to_media="+articleId, "webdb_create desc", 0);
240     }
241     catch (Throwable e) {
242       throw new ServletModuleFailure(e);
243     }
244   }
245
246   public void returnList(HttpServletRequest aRequest, HttpServletResponse aResponse,
247      String aWhereClause, String anOrderByClause, int anOffset) throws ServletModuleExc {
248
249     HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
250     URLBuilder urlBuilder = new URLBuilder();
251
252     try {
253       Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
254
255       List commentList =
256          EntityAdapterEngine.retrieveAdapterList(model, definition, aWhereClause, anOrderByClause, nrEntitiesPerListPage, anOffset);
257
258       responseData.put("nexturl", null);
259       responseData.put("prevurl", null);
260
261       urlBuilder.setValue("module", "Comment");
262       urlBuilder.setValue("do", "list");
263       urlBuilder.setValue("where", aWhereClause);
264       urlBuilder.setValue("order", anOrderByClause);
265
266       urlBuilder.setValue("searchfield", requestParser.getParameter("searchfield"));
267       urlBuilder.setValue("searchtext", requestParser.getParameter("searchtext"));
268       urlBuilder.setValue("searchispublished", requestParser.getParameter("searchispublished"));
269       urlBuilder.setValue("searchstatus", requestParser.getParameter("searchstatus"));
270       urlBuilder.setValue("searchorder", requestParser.getParameter("searchorder"));
271
272       responseData.put("searchfield", requestParser.getParameter("searchfield"));
273       responseData.put("searchtext", requestParser.getParameter("searchtext"));
274       responseData.put("searchispublished", requestParser.getParameter("searchispublished"));
275       responseData.put("searchstatus", requestParser.getParameter("searchstatus"));
276       responseData.put("searchorder", requestParser.getParameter("searchorder"));
277
278       urlBuilder.setValue("offset", anOffset);
279       responseData.put("offset" , new Integer(anOffset).toString());
280       responseData.put("thisurl" , urlBuilder.getQuery());
281
282       if (commentList.size()>=nrEntitiesPerListPage) {
283         urlBuilder.setValue("offset", anOffset + nrEntitiesPerListPage);
284         responseData.put("nexturl" , urlBuilder.getQuery());
285       }
286
287       if (anOffset>0) {
288         urlBuilder.setValue("offset", Math.max(anOffset - nrEntitiesPerListPage, 0));
289         responseData.put("prevurl" , urlBuilder.getQuery());
290       }
291
292       responseData.put("comments", commentList);
293       responseData.put("from" , Integer.toString(anOffset+1));
294       responseData.put("to", Integer.toString(anOffset+commentList.size()-1));
295
296       ServletHelper.generateResponse(aResponse.getWriter(), responseData, listGenerator);
297     }
298     catch (Throwable e) {
299       throw new ServletModuleFailure(e);
300     }
301   }
302
303   public void update(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
304   {
305     try {
306       HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
307
308       String returnUrl = requestParser.getParameter("returnurl");
309
310       String idParam = aRequest.getParameter("id");
311       if (idParam == null)
312         throw new ServletModuleExc("Wrong call: (id) is missing");
313
314       Map withValues = getIntersectingValues(aRequest, DatabaseComment.getInstance());
315
316       if (!withValues.containsKey("is_published"))
317         withValues.put("is_published","0");
318       if (!withValues.containsKey("is_html"))
319         withValues.put("is_html","0");
320
321       String webdbCreate = (String) withValues.get("webdb_create");
322       if (webdbCreate==null || webdbCreate.trim().length()==0)
323         withValues.remove("webdb_create");
324
325       String id = mainModule.set(withValues);
326
327       logAdminUsage(aRequest, id, "object modified");
328
329       if (returnUrl!=null){
330         ServletHelper.redirect(aResponse, returnUrl);
331       }
332       else
333         editObject(aRequest, aResponse, idParam);
334     }
335     catch (Throwable e) {
336       throw new ServletModuleFailure(e);
337     }
338   }
339 }