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