2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
32 package mircoders.servlet;
38 import javax.servlet.*;
39 import javax.servlet.http.*;
40 import org.apache.struts.util.MessageResources;
42 import freemarker.template.*;
49 import mir.generator.*;
51 import mir.entity.adapter.*;
54 import mircoders.storage.*;
55 import mircoders.global.*;
56 import mircoders.localizer.*;
57 import mircoders.module.*;
60 * ServletModuleComment - controls navigation for Comments
66 public class ServletModuleComment extends ServletModule
69 private ModuleContent moduleContent;
71 // Singelton / Kontruktor
72 private static ServletModuleComment instance = new ServletModuleComment();
73 public static ServletModule getInstance() { return instance; }
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");
81 mainModule = new ModuleComment(DatabaseComment.getInstance());
82 moduleContent = new ModuleContent(DatabaseContent.getInstance());
84 catch (StorageObjectException e) {
85 theLog.printError("servletmodule: comment could not be initialized");
89 public void list(HttpServletRequest req, HttpServletResponse res)
90 throws ServletModuleException
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);
102 String offset = req.getParameter("offset");
103 if (offset==null || offset.equals("")) offset="0";
104 mergeData.put("offset",offset);
107 String order = req.getParameter("order");
109 mergeData.put("order", order);
110 mergeData.put("order_encoded", URLEncoder.encode(order));
111 if (order.equals("webdb_create")) order="webdb_create desc";
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+"'";
124 theLog.printDebugInfo("sql-whereclause: " + whereClause + " order: " + order + " offset: " + offset);
126 // fetch und ausliefern
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) {
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(",");
138 buf.append(theList.elementAt(i).getValue("to_media"));
141 SimpleHash articleHash = HTMLTemplateProcessor.makeSimpleHash(moduleContent.getByWhereClause(buf.toString(),-1));
142 mergeData.put("articleHash", articleHash);
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());
156 HTMLTemplateProcessor.process(res, templateListString, mergeData, res.getWriter(), getLocale(req));
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());}
163 public void showArticleCommentList(Object aWriter, int anOffset, int anArticleId, Locale aLocale) throws ServletModuleException {
164 int nrCommentsPerPage = 20;
170 EntityAdapterModel model;
171 MessageResources messages = MessageResources.getMessageResources("bundles.admin");
174 generator = MirGlobal.localizer().generators().makeGeneratorLibrary().makeGenerator("admin/commentlist2.template");
175 model = MirGlobal.localizer().dataModel().adapterModel();
177 // commentList = mainModule.getByWhereClause(whereClause, order, offset);
179 generationData = new HashMap();
180 MirGlobal.localizer().producerAssistant().initializeGenerationValueSet(generationData);
183 new CachingRewindableIterator(
184 new EntityIteratorAdapter( "to_media = " + anArticleId,
194 totalNrComments = model.getMappingForName("comment").getStorage().getSize("to_media = " + anArticleId);
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);
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 )));
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 )));
214 generator.generate(aWriter, generationData, new PrintWriter(new NullWriter()));
216 catch (Throwable t) {
217 t.printStackTrace(System.out);
218 throw new ServletModuleException(t.getMessage());
222 public void listarticlecomments(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
224 String articleIdString = req.getParameter("articleid");
225 String offsetString = req.getParameter("offset");
230 offset = Integer.parseInt(offsetString);
232 catch (Throwable t) {
236 articleId = Integer.parseInt(articleIdString);
238 showArticleCommentList( res.getWriter(), offset, articleId, getLocale(req));
240 catch (ServletModuleException e) {
243 catch (Throwable e) {
244 e.printStackTrace(System.out);
245 throw new ServletModuleException(e.getMessage());
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");
258 articleId = Integer.parseInt(articleIdString);
260 showArticleCommentList( res.getWriter(), offset, articleId, getLocale(req));
262 catch (ServletModuleException e) {
265 catch (Throwable e) {
266 e.printStackTrace(System.out);
267 throw new ServletModuleException(e.getMessage());