8d10cf47f4f3cdaef7e8deb951a10b34a2d420df
[mir.git] / source / mircoders / servlet / ServletModuleMessage.java
1 /*\r
2  * Copyright (C) 2001, 2002  The Mir-coders group\r
3  *\r
4  * This file is part of Mir.\r
5  *\r
6  * Mir is free software; you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation; either version 2 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * Mir is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with Mir; if not, write to the Free Software\r
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19  *\r
20  * In addition, as a special exception, The Mir-coders gives permission to link\r
21  * the code of this program with the com.oreilly.servlet library, any library\r
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced\r
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of\r
24  * the above that use the same license as the above), and distribute linked\r
25  * combinations including the two.  You must obey the GNU General Public\r
26  * License in all respects for all of the code used other than the above\r
27  * mentioned libraries.  If you modify this file, you may extend this exception\r
28  * to your version of the file, but you are not obligated to do so.  If you do\r
29  * not wish to do so, delete this exception statement from your version.\r
30  */\r
31 \r
32 package mircoders.servlet;\r
33 \r
34 import java.net.URLEncoder;\r
35 import javax.servlet.http.HttpServletRequest;\r
36 import javax.servlet.http.HttpServletResponse;\r
37 \r
38 import freemarker.template.SimpleHash;\r
39 import mir.entity.EntityList;\r
40 import mir.log.LoggerWrapper;\r
41 import mir.misc.HTMLTemplateProcessor;\r
42 import mir.servlet.ServletModule;\r
43 import mir.servlet.ServletModuleExc;\r
44 import mir.servlet.ServletModuleFailure;\r
45 import mir.storage.StorageObjectFailure;\r
46 import mircoders.module.ModuleMessage;\r
47 import mircoders.storage.DatabaseMessages;\r
48 \r
49 /**\r
50  * Title:       ServletModuleMessage\r
51  * Description:\r
52  * Copyright:   Copyright (c) 2001-2002\r
53  * Company:     mir-coders\r
54  * @author\r
55  * @version 1.0\r
56  */\r
57 \r
58 \r
59 public class ServletModuleMessage extends ServletModule\r
60 {\r
61 \r
62   // Singelton / Kontruktor\r
63 \r
64   private static ServletModuleMessage instance = new ServletModuleMessage();\r
65   public static ServletModule getInstance() { return instance; }\r
66 \r
67   private ServletModuleMessage() {\r
68     super();\r
69     logger = new LoggerWrapper("ServletModule.Messages");\r
70 \r
71     templateListString = configuration.getString("ServletModule.Messages.ListTemplate");\r
72     templateObjektString = configuration.getString("ServletModule.Messages.ObjektTemplate");\r
73     templateConfirmString = configuration.getString("ServletModule.Messages.ConfirmTemplate");\r
74 \r
75     try {\r
76       mainModule = new ModuleMessage(DatabaseMessages.getInstance());\r
77     }\r
78     catch (StorageObjectFailure e) {\r
79       logger.error("initialization of ServletModuleMessage failed!: " + e.getMessage());\r
80 \r
81       throw new ServletModuleFailure(e);\r
82     }\r
83   }\r
84 \r
85   public void list(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc\r
86   {\r
87 // fetch and deliver\r
88     try {\r
89       SimpleHash mergeData = new SimpleHash();\r
90       String offset = req.getParameter("offset");\r
91       if (offset==null || offset.equals("")) offset="0";\r
92       mergeData.put("offset",offset);\r
93       EntityList theList = mainModule.getByWhereClause(null, "webdb_create desc", (new Integer(offset)).intValue());\r
94       mergeData.put("contentlist",theList);\r
95       if(theList.getOrder()!=null) {\r
96         mergeData.put("order", theList.getOrder());\r
97         mergeData.put("order_encoded", URLEncoder.encode(theList.getOrder()));\r
98       }\r
99       mergeData.put("count", (new Integer(theList.getCount())).toString());\r
100       mergeData.put("from", (new Integer(theList.getFrom())).toString());\r
101       mergeData.put("to", (new Integer(theList.getTo())).toString());\r
102       if (theList.hasNextBatch())\r
103         mergeData.put("next", (new Integer(theList.getNextBatch())).toString());\r
104       if (theList.hasPrevBatch())\r
105         mergeData.put("prev", (new Integer(theList.getPrevBatch())).toString());\r
106 \r
107       HTMLTemplateProcessor.process(res, templateListString, mergeData, res.getWriter(), getLocale(req), getFallbackLocale(req));\r
108 \r
109     }\r
110     catch (Throwable e) {\r
111       throw new ServletModuleFailure(e);\r
112     }\r
113   }\r
114 \r
115 \r
116 }\r