merge of localization branch into HEAD. mh and zap
[mir.git] / source / mircoders / servlet / ServletModuleComment.java
1 package mircoders.servlet;
2
3 import java.io.*;
4 import java.sql.*;
5 import java.util.*;
6 import java.net.*;
7 import javax.servlet.*;
8 import javax.servlet.http.*;
9 import org.apache.struts.util.MessageResources;
10
11 import freemarker.template.*;
12
13 import mir.servlet.*;
14 import mir.module.*;
15 import mir.misc.*;
16 import mir.entity.*;
17 import mir.storage.*;
18 import mir.generator.*;
19 import mir.entity.*;
20 import mir.entity.adapter.*;
21 import mir.util.*;
22
23 import mircoders.storage.*;
24 import mircoders.global.*;
25 import mircoders.localizer.*;
26 import mircoders.module.*;
27
28 /*
29  *  ServletModuleComment - controls navigation for Comments
30  *
31  *
32  * @author RK
33  */
34
35 public class ServletModuleComment extends ServletModule
36 {
37
38   private ModuleContent     moduleContent;
39
40   // Singelton / Kontruktor
41   private static ServletModuleComment instance = new ServletModuleComment();
42   public static ServletModule getInstance() { return instance; }
43
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");
49     try {
50       mainModule = new ModuleComment(DatabaseComment.getInstance());
51       moduleContent = new ModuleContent(DatabaseContent.getInstance());
52     }
53     catch (StorageObjectException e) {
54       theLog.printError("servletmodule: comment could not be initialized");
55     }
56   }
57
58   public void list(HttpServletRequest req, HttpServletResponse res)
59       throws ServletModuleException
60   {
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);
70
71     String offset = req.getParameter("offset");
72     if (offset==null || offset.equals("")) offset="0";
73     mergeData.put("offset",offset);
74
75     // patching order
76     String order = req.getParameter("order");
77     if(order!=null) {
78       mergeData.put("order", order);
79       mergeData.put("order_encoded", URLEncoder.encode(order));
80       if (order.equals("webdb_create")) order="webdb_create desc";
81     }
82
83     // sql basteln
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+"'";
90       isFirst=false;
91     }
92
93     theLog.printDebugInfo("sql-whereclause: " + whereClause + " order: " + order + " offset: " + offset);
94
95     // fetch und ausliefern
96     try {
97
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) {
101
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(",");
106             first=false;
107             buf.append(theList.elementAt(i).getValue("to_media"));
108           }
109           buf.append(")");
110           SimpleHash articleHash = HTMLTemplateProcessor.makeSimpleHash(moduleContent.getByWhereClause(buf.toString(),-1));
111           mergeData.put("articleHash", articleHash);
112
113           // get comment
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());
122         }
123       }
124       // raus damit
125       HTMLTemplateProcessor.process(res, templateListString, mergeData, res.getWriter(), getLocale(req));
126     }
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());}
130   }
131
132   public void showArticleCommentList(Object aWriter, int anOffset, int anArticleId, Locale aLocale) throws ServletModuleException {
133     int nrCommentsPerPage = 20;
134
135     Object comments;
136     Map generationData;
137     Generator generator;
138     int totalNrComments;
139     EntityAdapterModel model;
140     MessageResources messages = MessageResources.getMessageResources("bundles.admin");
141
142     try {
143       generator = MirGlobal.localizer().generators().makeGeneratorLibrary().makeGenerator("admin/commentlist2.template");
144       model = MirGlobal.localizer().dataModel().adapterModel();
145
146 //    commentList = mainModule.getByWhereClause(whereClause, order, offset);
147
148       generationData = new HashMap();
149       MirGlobal.localizer().producerAssistant().initializeGenerationValueSet(generationData);
150
151       comments =
152         new CachingRewindableIterator(
153           new EntityIteratorAdapter( "to_media = " + anArticleId,
154                "webdb_create desc",
155                nrCommentsPerPage,
156                model,
157               "comment",
158               nrCommentsPerPage,
159               anOffset
160           )
161         );
162
163       totalNrComments = model.getMappingForName("comment").getStorage().getSize("to_media = " + anArticleId);
164
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);
170
171       if (anOffset>0) {
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 )));
175       }
176
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 )));
181       }
182
183       generator.generate(aWriter, generationData, new PrintWriter(new NullWriter()));
184     }
185     catch (Throwable t) {
186       t.printStackTrace(System.out);
187       throw new ServletModuleException(t.getMessage());
188     }
189   }
190
191   public void listarticlecomments(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
192   {
193     String articleIdString = req.getParameter("articleid");
194     String offsetString = req.getParameter("offset");
195     int offset = 0;
196     int articleId = 0;
197
198     try {
199       offset = Integer.parseInt(offsetString);
200     }
201     catch (Throwable t) {
202     }
203
204     try {
205       articleId  = Integer.parseInt(articleIdString);
206
207       showArticleCommentList( res.getWriter(), offset, articleId, getLocale(req));
208     }
209     catch (ServletModuleException e) {
210       throw e;
211     }
212     catch (Throwable e) {
213       e.printStackTrace(System.out);
214       throw new ServletModuleException(e.getMessage());
215     }
216   }
217
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");
223     int offset = 0;
224     int articleId = 0;
225
226     try {
227       articleId  = Integer.parseInt(articleIdString);
228
229       showArticleCommentList( res.getWriter(), offset, articleId, getLocale(req));
230     }
231     catch (ServletModuleException e) {
232       throw e;
233     }
234     catch (Throwable e) {
235       e.printStackTrace(System.out);
236       throw new ServletModuleException(e.getMessage());
237     }
238   }
239 }