1 package mircoders.servlet;
7 import javax.servlet.*;
8 import javax.servlet.http.*;
9 import org.apache.struts.util.MessageResources;
11 import freemarker.template.*;
18 import mir.generator.*;
20 import mir.entity.adapter.*;
23 import mircoders.storage.*;
24 import mircoders.global.*;
25 import mircoders.localizer.*;
26 import mircoders.module.*;
29 * ServletModuleComment - controls navigation for Comments
35 public class ServletModuleComment extends ServletModule
38 private ModuleContent moduleContent;
40 // Singelton / Kontruktor
41 private static ServletModuleComment instance = new ServletModuleComment();
42 public static ServletModule getInstance() { return instance; }
44 private ServletModuleComment() {
45 theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("ServletModule.Comment.Logfile"));
46 templateListString = MirConfig.getProp("ServletModule.Comment.ListTemplate");
47 templateObjektString = MirConfig.getProp("ServletModule.Comment.ObjektTemplate");
48 templateConfirmString = MirConfig.getProp("ServletModule.Comment.ConfirmTemplate");
50 mainModule = new ModuleComment(DatabaseComment.getInstance());
51 moduleContent = new ModuleContent(DatabaseContent.getInstance());
53 catch (StorageObjectException e) {
54 theLog.printError("servletmodule: comment could not be initialized");
58 public void list(HttpServletRequest req, HttpServletResponse res)
59 throws ServletModuleException
61 // Parameter auswerten
62 SimpleHash mergeData = new SimpleHash();
63 String query_text = req.getParameter("query_text");
64 mergeData.put("query_text",query_text);
65 if (query_text!=null) mergeData.put("query_text_encoded",URLEncoder.encode(query_text));
66 String query_field = req.getParameter("query_field");
67 mergeData.put("query_field",query_field);
68 String query_is_published = req.getParameter("query_is_published");
69 mergeData.put("query_is_published",query_is_published);
71 String offset = req.getParameter("offset");
72 if (offset==null || offset.equals("")) offset="0";
73 mergeData.put("offset",offset);
76 String order = req.getParameter("order");
78 mergeData.put("order", order);
79 mergeData.put("order_encoded", URLEncoder.encode(order));
80 if (order.equals("webdb_create")) order="webdb_create desc";
84 String whereClause=""; boolean isFirst=true;
85 if (query_text!=null && !query_text.equalsIgnoreCase("")) {
86 whereClause += "lower("+query_field+") like lower('%"+query_text+"%')"; isFirst=false;}
87 if (query_is_published != null && !query_is_published.equals("")) {
88 if (isFirst==false) whereClause+=" and ";
89 whereClause += "is_published='"+query_is_published+"'";
93 theLog.printDebugInfo("sql-whereclause: " + whereClause + " order: " + order + " offset: " + offset);
95 // fetch und ausliefern
98 if (query_text!=null || query_is_published!=null ) {
99 EntityList theList = mainModule.getByWhereClause(whereClause, order, (new Integer(offset)).intValue());
100 if (theList!=null && theList.size()>0) {
102 //make articleHash for comment
103 StringBuffer buf= new StringBuffer("id in (");boolean first=true;
104 for(int i=0;i<theList.size();i++) {
105 if (first==false) buf.append(",");
107 buf.append(theList.elementAt(i).getValue("to_media"));
110 SimpleHash articleHash = HTMLTemplateProcessor.makeSimpleHash(moduleContent.getByWhereClause(buf.toString(),-1));
111 mergeData.put("articleHash", articleHash);
114 mergeData.put("contentlist",theList);
115 mergeData.put("count", (new Integer(theList.getCount())).toString());
116 mergeData.put("from", (new Integer(theList.getFrom())).toString());
117 mergeData.put("to", (new Integer(theList.getTo())).toString());
118 if (theList.hasNextBatch())
119 mergeData.put("next", (new Integer(theList.getNextBatch())).toString());
120 if (theList.hasPrevBatch())
121 mergeData.put("prev", (new Integer(theList.getPrevBatch())).toString());
125 HTMLTemplateProcessor.process(res, templateListString, mergeData, res.getWriter(), getLocale(req));
127 catch (ModuleException e) {throw new ServletModuleException(e.toString());}
128 catch (IOException e) {throw new ServletModuleException(e.toString());}
129 catch (Exception e) {throw new ServletModuleException(e.toString());}
132 public void showArticleCommentList(Object aWriter, int anOffset, int anArticleId, Locale aLocale) throws ServletModuleException {
133 int nrCommentsPerPage = 20;
139 EntityAdapterModel model;
140 MessageResources messages = MessageResources.getMessageResources("bundles.admin");
143 generator = MirGlobal.localizer().generators().makeGeneratorLibrary().makeGenerator("admin/commentlist2.template");
144 model = MirGlobal.localizer().dataModel().adapterModel();
146 // commentList = mainModule.getByWhereClause(whereClause, order, offset);
148 generationData = new HashMap();
149 MirGlobal.localizer().producerAssistant().initializeGenerationValueSet(generationData);
152 new CachingRewindableIterator(
153 new EntityIteratorAdapter( "to_media = " + anArticleId,
163 totalNrComments = model.getMappingForName("comment").getStorage().getSize("to_media = " + anArticleId);
165 generationData.put( "comments", comments);
166 generationData.put( "offset", new Integer(anOffset));
167 generationData.put( "articleid", new Integer(anArticleId));
168 generationData.put( "lang", new MessageMethodModel(aLocale, messages) );
169 generationData.put( "thisurl", "module=Comment&do=listarticlecomments&offset="+anOffset+"&articleid="+anArticleId);
172 generationData.put( "previousurl", "module=Comment&do=listarticlecomments&offset="+
173 Math.max( 0, anOffset - nrCommentsPerPage )+"&articleid="+anArticleId);
174 generationData.put("previous", new Integer(Math.max( 0, anOffset - nrCommentsPerPage )));
177 if (anOffset + nrCommentsPerPage < totalNrComments) {
178 generationData.put( "nexturl", "module=Comment&do=listarticlecomments&offset="+
179 Math.min( anOffset + nrCommentsPerPage, totalNrComments-1 )+"&articleid="+anArticleId);
180 generationData.put("next", new Integer(Math.min( anOffset + nrCommentsPerPage, totalNrComments-1 )));
183 generator.generate(aWriter, generationData, new PrintWriter(new NullWriter()));
185 catch (Throwable t) {
186 t.printStackTrace(System.out);
187 throw new ServletModuleException(t.getMessage());
191 public void listarticlecomments(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
193 String articleIdString = req.getParameter("articleid");
194 String offsetString = req.getParameter("offset");
199 offset = Integer.parseInt(offsetString);
201 catch (Throwable t) {
205 articleId = Integer.parseInt(articleIdString);
207 showArticleCommentList( res.getWriter(), offset, articleId, getLocale(req));
209 catch (ServletModuleException e) {
212 catch (Throwable e) {
213 e.printStackTrace(System.out);
214 throw new ServletModuleException(e.getMessage());
218 public void performarticlecommentsoperation(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException {
219 String commentIdString = req.getParameter("commentid");
220 String articleIdString = req.getParameter("articleid");
221 String offsetString = req.getParameter("offset");
222 String operation = req.getParameter("operation");
227 articleId = Integer.parseInt(articleIdString);
229 showArticleCommentList( res.getWriter(), offset, articleId, getLocale(req));
231 catch (ServletModuleException e) {
234 catch (Throwable e) {
235 e.printStackTrace(System.out);
236 throw new ServletModuleException(e.getMessage());