cos removed from the open posting system + misc. updates here and there
[mir.git] / source / mircoders / servlet / ServletHelper.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.io.PrintWriter;\r
35 import java.util.HashMap;\r
36 import java.util.Locale;\r
37 import java.util.Map;\r
38 \r
39 import org.apache.struts.util.MessageResources;\r
40 import mir.entity.adapter.EntityIteratorAdapter;\r
41 import mir.generator.Generator;\r
42 import mir.servlet.ServletModuleExc;\r
43 import mir.servlet.ServletModuleFailure;\r
44 import mir.util.CachingRewindableIterator;\r
45 import mir.util.NullWriter;\r
46 import mir.util.ResourceBundleGeneratorFunction;\r
47 import mir.log.*;\r
48 import mircoders.global.MirGlobal;\r
49 \r
50 \r
51 \r
52 public class ServletHelper {\r
53   static LoggerWrapper logger = new LoggerWrapper("ServletModule.Helper");\r
54 \r
55 \r
56   public static Map makeGenerationData(Locale[] aLocales) throws ServletModuleExc {\r
57     return makeGenerationData(aLocales, "bundles.adminlocal", "bundles.admin");\r
58   }\r
59 \r
60   public static Map makeGenerationData(Locale[] aLocales, String aBundle) throws ServletModuleExc {\r
61     return makeGenerationData(aLocales, aBundle, aBundle);\r
62   }\r
63 \r
64   public static Map makeGenerationData(Locale[] aLocales, String aBundle, String aDefaultBundle) throws ServletModuleExc {\r
65 \r
66     try {\r
67       Map result = new HashMap();\r
68 \r
69       MirGlobal.localizer().producerAssistant().initializeGenerationValueSet(result);\r
70 \r
71       result.put("returnurl", null);\r
72 \r
73       Object languages =\r
74           new CachingRewindableIterator(\r
75             new EntityIteratorAdapter( "", "id", 30,\r
76                MirGlobal.localizer().dataModel().adapterModel(), "language"));\r
77       Object topics =\r
78           new CachingRewindableIterator(\r
79             new EntityIteratorAdapter("", "id", 30,\r
80                MirGlobal.localizer().dataModel().adapterModel(), "topic"));\r
81 \r
82       Object articleTypes =\r
83           new CachingRewindableIterator(\r
84             new EntityIteratorAdapter( "", "id", 30,\r
85                MirGlobal.localizer().dataModel().adapterModel(), "articleType"));\r
86 \r
87       Object commentStatuses =\r
88           new CachingRewindableIterator(\r
89             new EntityIteratorAdapter( "", "id", 30,\r
90                MirGlobal.localizer().dataModel().adapterModel(), "commentStatus"));\r
91 \r
92       result.put("commentstatuses", commentStatuses);\r
93       result.put("articletypes", articleTypes);\r
94       result.put("languages", languages);\r
95       result.put("topics", topics);\r
96 \r
97       result.put( "lang",\r
98           new ResourceBundleGeneratorFunction( aLocales,\r
99              new MessageResources[] { MessageResources.getMessageResources(aBundle),\r
100                                    MessageResources.getMessageResources(aDefaultBundle)}));\r
101 \r
102       return result;\r
103     }\r
104     catch (Throwable t) {\r
105       throw new ServletModuleFailure(t);\r
106     }\r
107   }\r
108 \r
109   public static void generateResponse(PrintWriter aWriter, Map aGenerationData, String aGenerator) throws ServletModuleExc {\r
110     Generator generator;\r
111 \r
112     try {\r
113       generator = MirGlobal.localizer().generators().makeAdminGeneratorLibrary().makeGenerator(aGenerator);\r
114 \r
115       generator.generate(aWriter, aGenerationData, logger);\r
116     }\r
117     catch (Throwable t) {\r
118       throw new ServletModuleFailure(t);\r
119     }\r
120   }\r
121 \r
122   public static void generateOpenPostingResponse(PrintWriter aWriter, Map aGenerationData, String aGenerator) throws ServletModuleExc {\r
123     Generator generator;\r
124 \r
125     try {\r
126       generator = MirGlobal.localizer().generators().makeOpenPostingGeneratorLibrary().makeGenerator(aGenerator);\r
127 \r
128       generator.generate(aWriter, aGenerationData, logger);\r
129     }\r
130     catch (Throwable t) {\r
131       throw new ServletModuleFailure(t);\r
132     }\r
133   }\r
134 }\r