add in advanced query parameters used by media and comment search to mergeData so...
[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
41 import freemarker.template.*;
42
43 import mir.servlet.*;
44 import mir.module.*;
45 import mir.misc.*;
46 import mir.entity.*;
47 import mir.storage.*;
48
49 import mir.entity.*;
50 import mircoders.storage.*;
51 import mircoders.module.*;
52
53 /*
54  *  ServletModuleComment - controls navigation for Comments
55  *
56  *
57  * @author RK
58  */
59
60 public class ServletModuleComment extends ServletModule
61 {
62
63         private ModuleContent     moduleContent;
64
65         // Singelton / Kontruktor
66         private static ServletModuleComment instance = new ServletModuleComment();
67         public static ServletModule getInstance() { return instance; }
68
69         private ServletModuleComment() {
70                 theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("ServletModule.Comment.Logfile"));
71                 templateListString = MirConfig.getProp("ServletModule.Comment.ListTemplate");
72                 templateObjektString = MirConfig.getProp("ServletModule.Comment.ObjektTemplate");
73                 templateConfirmString = MirConfig.getProp("ServletModule.Comment.ConfirmTemplate");
74                 try {
75                         mainModule = new ModuleComment(DatabaseComment.getInstance());
76                         moduleContent = new ModuleContent(DatabaseContent.getInstance());
77                 }
78                 catch (StorageObjectException e) {
79                         theLog.printError("servletmodule: comment could not be initialized");
80                 }
81         }
82
83
84         public void list(HttpServletRequest req, HttpServletResponse res)
85                 throws ServletModuleException
86         {
87                         // Parameter auswerten
88                         SimpleHash mergeData = new SimpleHash();
89                         String query_text = req.getParameter("query_text");
90                         mergeData.put("query_text",query_text);
91                         if (query_text!=null) mergeData.put("query_text_encoded",URLEncoder.encode(query_text));
92                         String query_field = req.getParameter("query_field");
93                         mergeData.put("query_field",query_field);
94                         String query_is_published = req.getParameter("query_is_published");
95                         mergeData.put("query_is_published",query_is_published);
96
97                         String offset = req.getParameter("offset");
98                         if (offset==null || offset.equals("")) offset="0";
99                         mergeData.put("offset",offset);
100
101                         // patching order
102                         String order = req.getParameter("order");
103                         if(order!=null) {
104                                 mergeData.put("order", order);
105                                 mergeData.put("order_encoded", URLEncoder.encode(order));
106                                 if (order.equals("webdb_create")) order="webdb_create desc";
107                         }
108
109                         // sql basteln
110                         String whereClause=""; boolean isFirst=true;
111                         if (query_text!=null && !query_text.equalsIgnoreCase("")) {
112                                 whereClause += "lower("+query_field+") like lower('%"+query_text+"%')"; isFirst=false;}
113                         if (query_is_published != null && !query_is_published.equals("")) {
114                                 if (isFirst==false) whereClause+=" and ";
115                                 whereClause += "is_published='"+query_is_published+"'";
116                                 isFirst=false;
117                         }
118
119                         theLog.printDebugInfo("sql-whereclause: " + whereClause + " order: " + order + " offset: " + offset);
120
121                         // fetch und ausliefern
122                         try {
123
124                                 if (query_text!=null || query_is_published!=null ) {
125                                         EntityList theList = mainModule.getByWhereClause(whereClause, order, (new Integer(offset)).intValue());
126                                         if (theList!=null && theList.size()>0) {
127
128                                                 //make articleHash for comment
129                                                 StringBuffer buf= new StringBuffer("id in (");boolean first=true;
130                                                 for(int i=0;i<theList.size();i++) {
131                                                         if (first==false) buf.append(",");
132                                                         first=false;
133                                                         buf.append(theList.elementAt(i).getValue("to_media"));
134                                                 }
135                                                 buf.append(")");
136                                                 SimpleHash articleHash = HTMLTemplateProcessor.makeSimpleHash(moduleContent.getByWhereClause(buf.toString(),-1));
137                                                 mergeData.put("articleHash", articleHash);
138
139                                                 // get comment
140                                                 mergeData.put("contentlist",theList);
141                                                 mergeData.put("count", (new Integer(theList.getCount())).toString());
142                                                 mergeData.put("from", (new Integer(theList.getFrom())).toString());
143                                                 mergeData.put("to", (new Integer(theList.getTo())).toString());
144                                                 if (theList.hasNextBatch())
145                                                         mergeData.put("next", (new Integer(theList.getNextBatch())).toString());
146                                                 if (theList.hasPrevBatch())
147                                                         mergeData.put("prev", (new Integer(theList.getPrevBatch())).toString());
148                                         }
149                                 }
150                                 // raus damit
151                                 HTMLTemplateProcessor.process(res, templateListString, mergeData, res.getWriter(), getLocale(req));
152                         }
153                         catch (ModuleException e) {throw new ServletModuleException(e.toString());}
154                         catch (IOException e) {throw new ServletModuleException(e.toString());}
155                         catch (Exception e) {throw new ServletModuleException(e.toString());}
156         }
157 }