- experimental opensessions
[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 mircoders.global.MirGlobal;\r
48 \r
49 \r
50 \r
51 public class ServletHelper {\r
52   public static Map makeGenerationData(Locale[] aLocales) throws ServletModuleExc {\r
53     return makeGenerationData(aLocales, "bundles.adminlocal", "bundles.admin");\r
54   }\r
55 \r
56   public static Map makeGenerationData(Locale[] aLocales, String aBundle) throws ServletModuleExc {\r
57     return makeGenerationData(aLocales, aBundle, aBundle);\r
58   }\r
59 \r
60   public static Map makeGenerationData(Locale[] aLocales, String aBundle, String aDefaultBundle) throws ServletModuleExc {\r
61 \r
62     try {\r
63       Map result = new HashMap();\r
64 \r
65       MirGlobal.localizer().producerAssistant().initializeGenerationValueSet(result);\r
66 \r
67       result.put("returnurl", null);\r
68 \r
69       Object languages =\r
70           new CachingRewindableIterator(\r
71             new EntityIteratorAdapter( "", "id", 30,\r
72                MirGlobal.localizer().dataModel().adapterModel(), "language"));\r
73       Object topics =\r
74           new CachingRewindableIterator(\r
75             new EntityIteratorAdapter("", "id", 30,\r
76                MirGlobal.localizer().dataModel().adapterModel(), "topic"));\r
77 \r
78       Object articleTypes =\r
79           new CachingRewindableIterator(\r
80             new EntityIteratorAdapter( "", "id", 30,\r
81                MirGlobal.localizer().dataModel().adapterModel(), "articleType"));\r
82 \r
83       Object commentStatuses =\r
84           new CachingRewindableIterator(\r
85             new EntityIteratorAdapter( "", "id", 30,\r
86                MirGlobal.localizer().dataModel().adapterModel(), "commentStatus"));\r
87 \r
88       result.put("commentstatuses", commentStatuses);\r
89       result.put("articletypes", articleTypes);\r
90       result.put("languages", languages);\r
91       result.put("topics", topics);\r
92 \r
93       result.put( "lang",\r
94           new ResourceBundleGeneratorFunction( aLocales,\r
95              new MessageResources[] { MessageResources.getMessageResources(aBundle),\r
96                                    MessageResources.getMessageResources(aDefaultBundle)}));\r
97 \r
98       return result;\r
99     }\r
100     catch (Throwable t) {\r
101       throw new ServletModuleFailure(t);\r
102     }\r
103   }\r
104 \r
105   public static void generateResponse(PrintWriter aWriter, Map aGenerationData, String aGenerator) throws ServletModuleExc {\r
106     Generator generator;\r
107 \r
108     try {\r
109       generator = MirGlobal.localizer().generators().makeAdminGeneratorLibrary().makeGenerator(aGenerator);\r
110 \r
111       generator.generate(aWriter, aGenerationData, new PrintWriter(new NullWriter()));\r
112     }\r
113     catch (Throwable t) {\r
114       throw new ServletModuleFailure(t);\r
115     }\r
116   }\r
117 \r
118   public static void generateOpenPostingResponse(PrintWriter aWriter, Map aGenerationData, String aGenerator) throws ServletModuleExc {\r
119     Generator generator;\r
120 \r
121     try {\r
122       generator = MirGlobal.localizer().generators().makeOpenPostingGeneratorLibrary().makeGenerator(aGenerator);\r
123 \r
124       generator.generate(aWriter, aGenerationData, new PrintWriter(new NullWriter()));\r
125     }\r
126     catch (Throwable t) {\r
127       throw new ServletModuleFailure(t);\r
128     }\r
129   }\r
130 }\r