cleanup / abuse system fix / prepping for a release
[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       Object 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         Map emptyComment = new HashMap();
118         Iterator i = fields.iterator();
119         while (i.hasNext()) {
120           emptyComment.put(i.next(), null);
121         }
122
123         comment = emptyComment;
124       }
125       responseData.put("comment", comment);
126
127       responseData.put("returnurl", requestParser.getParameter("returnurl"));
128       responseData.put("thisurl", urlBuilder.getQuery());
129
130       ServletHelper.generateResponse(aResponse.getWriter(), responseData, editGenerator);
131     }
132     catch (Throwable e) {
133       throw new ServletModuleFailure(e);
134     }
135   }
136
137   public void attach(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
138   {
139     String  mediaIdParam = aRequest.getParameter("mid");
140     String  commentId = aRequest.getParameter("commentid");
141
142     if (commentId == null || mediaIdParam==null) throw new ServletModuleExc("smod comment :: attach :: commentid/mid missing");
143
144     try {
145       EntityComment comment = (EntityComment) mainModule.getById(commentId);
146       comment.attach(mediaIdParam);
147     }
148     catch(Throwable e) {
149       throw new ServletModuleFailure(e);
150     }
151
152     logAdminUsage(aRequest, commentId, "media " + mediaIdParam + " attached");
153
154     editObject(aRequest, aResponse, commentId);
155   }
156
157   public void dettach(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
158   {
159     String  commentId = aRequest.getParameter("commentid");
160     String  midParam = aRequest.getParameter("mid");
161     if (commentId == null)
162       throw new ServletModuleExc("smod comment :: dettach :: commentid missing");
163     if (midParam == null)
164       throw new ServletModuleExc("smod comment :: dettach :: mid missing");
165
166     try {
167       EntityComment comment = (EntityComment)mainModule.getById(commentId);
168       comment.dettach(commentId, midParam);
169     }
170     catch(Throwable e) {
171       throw new ServletModuleFailure(e);
172     }
173
174     logAdminUsage(aRequest, commentId, "media " + midParam + " deattached");
175
176     editObject(aRequest, aResponse, commentId);
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     returnList(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 searchField = requestParser.getParameter("searchfield");
197     String searchText = requestParser.getParameter("searchtext");
198     String searchIsPublished = requestParser.getParameter("searchispublished");
199     String searchStatus = requestParser.getParameter("searchstatus");
200     String searchOrder = requestParser.getParameter("searchorder");
201
202     if (searchIsPublished.equals("0")) {
203       queryBuilder.appendAndCondition("is_published='f'");
204     }
205     else if (searchIsPublished.equals("1")) {
206       queryBuilder.appendAndCondition("is_published='t'");
207     }
208
209     if (searchText.length()>0) {
210         queryBuilder.appendAndCondition(
211           "lower("+ searchField + ") like " +
212           "'%" + JDBCStringRoutines.escapeStringLiteral(searchText.toLowerCase()) + "%'");
213     }
214
215     if (searchStatus.length()>0) {
216       queryBuilder.appendAndCondition("to_comment_status="+Integer.parseInt(searchStatus));
217     }
218
219     if (searchOrder.length()>0) {
220       if (searchOrder.equals("datedesc"))
221         queryBuilder.appendDescendingOrder("webdb_create");
222       else if (searchOrder.equals("dateasc"))
223         queryBuilder.appendAscendingOrder("webdb_create");
224       else if (searchOrder.equals("articletitle"))
225         queryBuilder.appendAscendingOrder("(select content.title from content where content.id = comment.to_media)");
226       else if (searchOrder.equals("creator"))
227         queryBuilder.appendDescendingOrder("creator");
228     }
229
230     returnList(aRequest, aResponse, queryBuilder.getWhereClause(), queryBuilder.getOrderByClause(), 0);
231   }
232
233   public void articlecomments(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
234   {
235     String articleIdString = aRequest.getParameter("articleid");
236     int articleId;
237
238     try {
239       articleId  = Integer.parseInt(articleIdString);
240
241       returnList( aRequest, aResponse, "to_media="+articleId, "webdb_create desc", 0);
242     }
243     catch (Throwable e) {
244       throw new ServletModuleFailure(e);
245     }
246   }
247
248   public void returnList(HttpServletRequest aRequest, HttpServletResponse aResponse,
249      String aWhereClause, String anOrderByClause, int anOffset) throws ServletModuleExc {
250
251     HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
252     URLBuilder urlBuilder = new URLBuilder();
253
254     try {
255       Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
256
257       List commentList =
258          EntityAdapterEngine.retrieveAdapterList(model, definition, aWhereClause, anOrderByClause, nrEntitiesPerListPage, anOffset);
259
260       responseData.put("nexturl", null);
261       responseData.put("prevurl", null);
262
263       urlBuilder.setValue("module", "Comment");
264       urlBuilder.setValue("do", "list");
265       urlBuilder.setValue("where", aWhereClause);
266       urlBuilder.setValue("order", anOrderByClause);
267
268       urlBuilder.setValue("searchfield", requestParser.getParameter("searchfield"));
269       urlBuilder.setValue("searchtext", requestParser.getParameter("searchtext"));
270       urlBuilder.setValue("searchispublished", requestParser.getParameter("searchispublished"));
271       urlBuilder.setValue("searchstatus", requestParser.getParameter("searchstatus"));
272       urlBuilder.setValue("searchorder", requestParser.getParameter("searchorder"));
273
274       responseData.put("searchfield", requestParser.getParameter("searchfield"));
275       responseData.put("searchtext", requestParser.getParameter("searchtext"));
276       responseData.put("searchispublished", requestParser.getParameter("searchispublished"));
277       responseData.put("searchstatus", requestParser.getParameter("searchstatus"));
278       responseData.put("searchorder", requestParser.getParameter("searchorder"));
279
280       urlBuilder.setValue("offset", anOffset);
281       responseData.put("offset" , new Integer(anOffset).toString());
282       responseData.put("thisurl" , urlBuilder.getQuery());
283
284       if (commentList.size()>=nrEntitiesPerListPage) {
285         urlBuilder.setValue("offset", anOffset + nrEntitiesPerListPage);
286         responseData.put("nexturl" , urlBuilder.getQuery());
287       }
288
289       if (anOffset>0) {
290         urlBuilder.setValue("offset", Math.max(anOffset - nrEntitiesPerListPage, 0));
291         responseData.put("prevurl" , urlBuilder.getQuery());
292       }
293
294       responseData.put("comments", commentList);
295       responseData.put("from" , Integer.toString(anOffset+1));
296       responseData.put("to", Integer.toString(anOffset+commentList.size()-1));
297
298       ServletHelper.generateResponse(aResponse.getWriter(), responseData, listGenerator);
299     }
300     catch (Throwable e) {
301       throw new ServletModuleFailure(e);
302     }
303   }
304
305   public void update(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc
306   {
307     try {
308       HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
309
310       String returnUrl = requestParser.getParameter("returnurl");
311
312       String idParam = aRequest.getParameter("id");
313       if (idParam == null)
314         throw new ServletModuleExc("Wrong call: (id) is missing");
315
316       Map withValues = getIntersectingValues(aRequest, DatabaseComment.getInstance());
317
318       if (!withValues.containsKey("is_published"))
319         withValues.put("is_published","0");
320       if (!withValues.containsKey("is_html"))
321         withValues.put("is_html","0");
322
323       String webdbCreate = (String) withValues.get("webdb_create");
324       if (webdbCreate==null || webdbCreate.trim().length()==0)
325         withValues.remove("webdb_create");
326
327       String id = mainModule.set(withValues);
328
329       logAdminUsage(aRequest, id, "object modified");
330
331       if (returnUrl!=null){
332         ServletHelper.redirect(aResponse, returnUrl);
333       }
334       else
335         editObject(aRequest, aResponse, idParam);
336     }
337     catch (Throwable e) {
338       throw new ServletModuleFailure(e);
339     }
340   }
341 }