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;
39 import javax.servlet.*;
40 import javax.servlet.http.*;
42 import org.apache.struts.util.MessageResources;
44 import freemarker.template.*;
51 import mir.generator.*;
53 import mir.entity.adapter.*;
57 import mircoders.storage.*;
58 import mircoders.global.*;
59 import mircoders.localizer.*;
60 import mircoders.module.*;
63 * ServletModuleComment - controls navigation for Comments
69 public class ServletModuleComment extends ServletModule
72 private ModuleContent moduleContent;
74 // Singelton / Kontruktor
75 private static ServletModuleComment instance = new ServletModuleComment();
76 public static ServletModule getInstance() { return instance; }
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");
84 mainModule = new ModuleComment(DatabaseComment.getInstance());
85 moduleContent = new ModuleContent(DatabaseContent.getInstance());
87 catch (StorageObjectException e) {
88 logger.error("servletmodule comment could not be initialized:" + e.getMessage());
92 public void list(HttpServletRequest req, HttpServletResponse res)
93 throws ServletModuleException
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);
105 String offset = req.getParameter("offset");
106 if (offset==null || offset.equals("")) offset="0";
107 mergeData.put("offset",offset);
110 String order = req.getParameter("order");
112 mergeData.put("order", order);
113 mergeData.put("order_encoded", URLEncoder.encode(order));
114 if (order.equals("webdb_create")) order="webdb_create desc";
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+"'";
127 logger.debug("list comments: whereclause = " + whereClause + ", order = " + order + ", offset = " + offset);
129 // fetch und ausliefern
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) {
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(",");
141 buf.append(theList.elementAt(i).getValue("to_media"));
144 SimpleHash articleHash = HTMLTemplateProcessor.makeSimpleHash(moduleContent.getByWhereClause(buf.toString(),-1));
145 mergeData.put("articleHash", articleHash);
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());
159 HTMLTemplateProcessor.process(res, templateListString, mergeData, res.getWriter(), getLocale(req));
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());}
166 public void showArticleCommentList(Object aWriter, int anOffset, int anArticleId, Locale aLocale) throws ServletModuleException {
167 int nrCommentsPerPage = 20;
173 EntityAdapterModel model;
174 MessageResources messages = MessageResources.getMessageResources("bundles.admin");
177 generator = MirGlobal.localizer().generators().makeAdminGeneratorLibrary().makeGenerator("admin/commentlist2.template");
178 model = MirGlobal.localizer().dataModel().adapterModel();
180 generationData = new HashMap();
181 MirGlobal.localizer().producerAssistant().initializeGenerationValueSet(generationData);
184 new CachingRewindableIterator(
185 new EntityIteratorAdapter( "to_media = " + anArticleId,
195 totalNrComments = model.getMappingForName("comment").getStorage().getSize("to_media = " + anArticleId);
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);
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 )));
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 )));
215 generator.generate(aWriter, generationData, new PrintWriter(new NullWriter()));
217 catch (Throwable t) {
218 t.printStackTrace(System.out);
219 throw new ServletModuleException(t.getMessage());
223 public void listarticlecomments(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
225 String articleIdString = req.getParameter("articleid");
226 String offsetString = req.getParameter("offset");
231 offset = Integer.parseInt(offsetString);
233 catch (Throwable t) {
237 articleId = Integer.parseInt(articleIdString);
239 showArticleCommentList( res.getWriter(), offset, articleId, getLocale(req));
241 catch (ServletModuleException e) {
244 catch (Throwable e) {
245 e.printStackTrace(System.out);
246 throw new ServletModuleException(e.getMessage());
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");
259 articleId = Integer.parseInt(articleIdString);
261 showArticleCommentList( res.getWriter(), offset, articleId, getLocale(req));
263 catch (ServletModuleException e) {
266 catch (Throwable e) {
267 e.printStackTrace(System.out);
268 throw new ServletModuleException(e.getMessage());