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