8894213a3d9fc7a94bd8de54317020f3d7e64786
[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 the com.oreilly.servlet library, any library
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24  * the above that use the same license as the above), and distribute linked
25  * combinations including the two.  You must obey the GNU General Public
26  * License in all respects for all of the code used other than the above
27  * mentioned libraries.  If you modify this file, you may extend this exception
28  * to your version of the file, but you are not obligated to do so.  If you do
29  * not wish to do so, delete this exception statement from your version.
30  */
31
32 package mircoders.servlet;
33
34 import java.io.*;
35 import java.sql.*;
36 import java.util.*;
37 import java.net.*;
38
39 import javax.servlet.*;
40 import javax.servlet.http.*;
41
42 import org.apache.struts.util.MessageResources;
43
44 import freemarker.template.*;
45
46 import mir.servlet.*;
47 import mir.module.*;
48 import mir.misc.*;
49 import mir.entity.*;
50 import mir.storage.*;
51 import mir.generator.*;
52 import mir.entity.*;
53 import mir.entity.adapter.*;
54 import mir.util.*;
55 import mir.log.*;
56
57 import mircoders.storage.*;
58 import mircoders.global.*;
59 import mircoders.localizer.*;
60 import mircoders.module.*;
61
62 /*
63  *  ServletModuleComment - controls navigation for Comments
64  *
65  *
66  * @author RK
67  */
68
69 public class ServletModuleComment extends ServletModule
70 {
71
72   private ModuleContent     moduleContent;
73
74   // Singelton / Kontruktor
75   private static ServletModuleComment instance = new ServletModuleComment();
76   public static ServletModule getInstance() { return instance; }
77
78   private ServletModuleComment() {
79     logger = new LoggerWrapper("ServletModule.Comment");
80     templateListString = MirConfig.getProp("ServletModule.Comment.ListTemplate");
81     templateObjektString = MirConfig.getProp("ServletModule.Comment.ObjektTemplate");
82     templateConfirmString = MirConfig.getProp("ServletModule.Comment.ConfirmTemplate");
83     try {
84       mainModule = new ModuleComment(DatabaseComment.getInstance());
85       moduleContent = new ModuleContent(DatabaseContent.getInstance());
86     }
87     catch (StorageObjectException e) {
88       logger.error("servletmodule comment could not be initialized:" + e.getMessage());
89     }
90   }
91
92   public void list(HttpServletRequest req, HttpServletResponse res)
93       throws ServletModuleException
94   {
95     // Parameter auswerten
96     SimpleHash mergeData = new SimpleHash();
97     String query_text = req.getParameter("query_text");
98     mergeData.put("query_text",query_text);
99     if (query_text!=null) mergeData.put("query_text_encoded",URLEncoder.encode(query_text));
100     String query_field = req.getParameter("query_field");
101     mergeData.put("query_field",query_field);
102     String query_is_published = req.getParameter("query_is_published");
103     mergeData.put("query_is_published",query_is_published);
104
105     String offset = req.getParameter("offset");
106     if (offset==null || offset.equals("")) offset="0";
107     mergeData.put("offset",offset);
108
109     // patching order
110     String order = req.getParameter("order");
111     if(order!=null) {
112       mergeData.put("order", order);
113       mergeData.put("order_encoded", URLEncoder.encode(order));
114       if (order.equals("webdb_create")) order="webdb_create desc";
115     }
116
117     // sql basteln
118     String whereClause=""; boolean isFirst=true;
119     if (query_text!=null && !query_text.equalsIgnoreCase("")) {
120     whereClause += "lower("+query_field+") like lower('%"+query_text+"%')"; isFirst=false;}
121     if (query_is_published != null && !query_is_published.equals("")) {
122       if (isFirst==false) whereClause+=" and ";
123       whereClause += "is_published='"+query_is_published+"'";
124       isFirst=false;
125     }
126
127     logger.debug("list comments: whereclause = " + whereClause + ", order = " + order + ", offset = " + offset);
128
129     // fetch und ausliefern
130     try {
131
132       if (query_text!=null || query_is_published!=null ) {
133         EntityList theList = mainModule.getByWhereClause(whereClause, order, (new Integer(offset)).intValue());
134         if (theList!=null && theList.size()>0) {
135
136           //make articleHash for comment
137           StringBuffer buf= new StringBuffer("id in (");boolean first=true;
138           for(int i=0;i<theList.size();i++) {
139             if (first==false) buf.append(",");
140             first=false;
141             buf.append(theList.elementAt(i).getValue("to_media"));
142           }
143           buf.append(")");
144           SimpleHash articleHash = HTMLTemplateProcessor.makeSimpleHash(moduleContent.getByWhereClause(buf.toString(),-1));
145           mergeData.put("articleHash", articleHash);
146
147           // get comment
148           mergeData.put("contentlist",theList);
149           mergeData.put("count", (new Integer(theList.getCount())).toString());
150           mergeData.put("from", (new Integer(theList.getFrom())).toString());
151           mergeData.put("to", (new Integer(theList.getTo())).toString());
152           if (theList.hasNextBatch())
153             mergeData.put("next", (new Integer(theList.getNextBatch())).toString());
154           if (theList.hasPrevBatch())
155             mergeData.put("prev", (new Integer(theList.getPrevBatch())).toString());
156         }
157       }
158       // raus damit
159       HTMLTemplateProcessor.process(res, templateListString, mergeData, res.getWriter(), getLocale(req));
160     }
161     catch (ModuleException e) {throw new ServletModuleException(e.toString());}
162     catch (IOException e) {throw new ServletModuleException(e.toString());}
163     catch (Exception e) {throw new ServletModuleException(e.toString());}
164   }
165
166   public void showArticleCommentList(Object aWriter, int anOffset, int anArticleId, Locale aLocale) throws ServletModuleException {
167     int nrCommentsPerPage = 20;
168
169     Object comments;
170     Map generationData;
171     Generator generator;
172     int totalNrComments;
173     EntityAdapterModel model;
174     MessageResources messages = MessageResources.getMessageResources("bundles.admin");
175
176     try {
177       generator = MirGlobal.localizer().generators().makeAdminGeneratorLibrary().makeGenerator("admin/commentlist2.template");
178       model = MirGlobal.localizer().dataModel().adapterModel();
179
180       generationData = new HashMap();
181       MirGlobal.localizer().producerAssistant().initializeGenerationValueSet(generationData);
182
183       comments =
184         new CachingRewindableIterator(
185           new EntityIteratorAdapter( "to_media = " + anArticleId,
186                "webdb_create desc",
187                nrCommentsPerPage,
188                model,
189               "comment",
190               nrCommentsPerPage,
191               anOffset
192           )
193         );
194
195       totalNrComments = model.getMappingForName("comment").getStorage().getSize("to_media = " + anArticleId);
196
197       generationData.put( "comments", comments);
198       generationData.put( "offset", new Integer(anOffset));
199       generationData.put( "articleid", new Integer(anArticleId));
200       generationData.put( "lang", new MessageMethodModel(aLocale, messages) );
201       generationData.put( "thisurl", "module=Comment&do=listarticlecomments&offset="+anOffset+"&articleid="+anArticleId);
202
203       if (anOffset>0) {
204         generationData.put( "previousurl", "module=Comment&do=listarticlecomments&offset="+
205                             Math.max( 0, anOffset - nrCommentsPerPage )+"&articleid="+anArticleId);
206         generationData.put("previous", new Integer(Math.max( 0, anOffset - nrCommentsPerPage )));
207       }
208
209       if (anOffset + nrCommentsPerPage < totalNrComments) {
210         generationData.put( "nexturl", "module=Comment&do=listarticlecomments&offset="+
211                             Math.min( anOffset + nrCommentsPerPage, totalNrComments-1 )+"&articleid="+anArticleId);
212         generationData.put("next", new Integer(Math.min( anOffset + nrCommentsPerPage, totalNrComments-1 )));
213       }
214
215       generator.generate(aWriter, generationData, new PrintWriter(new NullWriter()));
216     }
217     catch (Throwable t) {
218       t.printStackTrace(System.out);
219       throw new ServletModuleException(t.getMessage());
220     }
221   }
222
223   public void listarticlecomments(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
224   {
225     String articleIdString = req.getParameter("articleid");
226     String offsetString = req.getParameter("offset");
227     int offset = 0;
228     int articleId = 0;
229
230     try {
231       offset = Integer.parseInt(offsetString);
232     }
233     catch (Throwable t) {
234     }
235
236     try {
237       articleId  = Integer.parseInt(articleIdString);
238
239       showArticleCommentList( res.getWriter(), offset, articleId, getLocale(req));
240     }
241     catch (ServletModuleException e) {
242       throw e;
243     }
244     catch (Throwable e) {
245       e.printStackTrace(System.out);
246       throw new ServletModuleException(e.getMessage());
247     }
248   }
249
250   public void performarticlecommentsoperation(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException {
251     String commentIdString = req.getParameter("commentid");
252     String articleIdString = req.getParameter("articleid");
253     String offsetString = req.getParameter("offset");
254     String operation = req.getParameter("operation");
255     int offset = 0;
256     int articleId = 0;
257
258     try {
259       articleId  = Integer.parseInt(articleIdString);
260
261       showArticleCommentList( res.getWriter(), offset, articleId, getLocale(req));
262     }
263     catch (ServletModuleException e) {
264       throw e;
265     }
266     catch (Throwable e) {
267       e.printStackTrace(System.out);
268       throw new ServletModuleException(e.getMessage());
269     }
270   }
271 }