- producer links are moved to an "advanced" page, not intended for normal
[mir.git] / source / mircoders / servlet / ServletModuleProducer.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 javax.servlet.*;
38 import javax.servlet.http.*;
39
40 import org.apache.struts.util.MessageResources;
41
42 import freemarker.template.*;
43
44 import mir.servlet.*;
45 import mir.misc.*;
46 import mir.producer.*;
47 import mir.generator.*;
48 import mir.producer.*;
49 import mir.entity.adapter.*;
50 import mir.util.*;
51
52 import mircoders.global.*;
53
54 /* Verteilerservlet, dass je nach Parameter task die Klasse Producer"TASK"
55  * ueber die Methode handle(); aufruft
56  *
57  * @author RK
58  */
59
60 public class ServletModuleProducer extends ServletModule
61 {
62
63   private static ServletModuleProducer instance = new ServletModuleProducer();
64   public static ServletModule getInstance() { return instance; }
65
66   Object comments;
67   Map generationData;
68   Generator generator;
69   int totalNrComments;
70   List producersData;
71
72   void generateResponse(String aGeneratorIdentifier, PrintWriter aWriter, Map aResponseData, Locale aLocale) throws ServletModuleException {
73     try {
74       generator = MirGlobal.localizer().generators().makeAdminGeneratorLibrary().makeGenerator(aGeneratorIdentifier);
75       MirGlobal.localizer().producerAssistant().initializeGenerationValueSet(aResponseData);
76       aResponseData.put( "lang", new ResourceBundleGeneratorFunction( aLocale, MessageResources.getMessageResources("bundles.admin")));
77       generator.generate(aWriter, aResponseData, new PrintWriter(new NullWriter()));
78     }
79     catch (Throwable t) {
80       throw new ServletModuleException(t.getMessage());
81     }
82   }
83
84   private ServletModuleProducer() {
85     theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("ServletModule.Producer.Logfile"));
86     defaultAction="showProducerQueueStatus";
87   }
88
89   public void showMessage(PrintWriter aWriter, Locale aLocale, String aMessage, String anArgument1, String anArgument2) throws ServletModuleException {
90     Map responseData;
91     try {
92       responseData = new HashMap();
93       responseData.put("message", aMessage);
94       responseData.put("argument1", anArgument1);
95       responseData.put("argument2", anArgument2);
96       generateResponse("infomessage.template", aWriter, responseData, aLocale);
97     }
98     catch (Throwable t) {
99       throw new ServletModuleException(t.getMessage());
100     }
101   }
102
103
104   public void showProducerQueueStatus(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException {
105     Object comments;
106     Map generationData;
107     Generator generator;
108     int totalNrComments;
109     List producersData;
110
111     try {
112       generator = MirGlobal.localizer().generators().makeAdminGeneratorLibrary().makeGenerator("producerqueue.template");
113
114       generationData = new HashMap();
115       MirGlobal.localizer().producerAssistant().initializeGenerationValueSet(generationData);
116
117       generationData.put( "lang", new ResourceBundleGeneratorFunction( getLocale(aRequest), MessageResources.getMessageResources("bundles.admin")));
118       generationData.put( "thisurl", "module=Producer&do=showProducerQueueStatus");
119
120       producersData = new Vector();
121       Iterator i = MirGlobal.localizer().producers().factories().iterator();
122       while (i.hasNext()) {
123         ProducerFactory factory = (ProducerFactory) i.next();
124
125         List producerVerbs = new Vector();
126         Iterator j = factory.verbs();
127         while (j.hasNext()) {
128           Map verbData = new HashMap();
129           ProducerFactory.ProducerVerb verb = (ProducerFactory.ProducerVerb) j.next();
130           verbData.put("name", verb.getName());
131           verbData.put("description", verb.getDescription());
132
133           producerVerbs.add(verbData);
134         }
135
136         Map producerData = new HashMap();
137         producerData.put("name", factory.getName());
138         producerData.put("verbs", producerVerbs);
139
140         producersData.add(producerData);
141       }
142       generationData.put("producers", producersData);
143
144       generationData.put("queue", MirGlobal.producerEngine().getQueueStatus());
145       generator.generate(aResponse.getWriter(), generationData, new PrintWriter(new NullWriter()));
146     }
147     catch (Throwable t) {
148       throw new ServletModuleException(t.getMessage());
149     }
150   }
151
152   public void produce(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException {
153     /*
154      * This method will only be called by external scripts (e.g. from cron jobs).
155      * The output therefore is very simple.
156      *
157      */
158
159     try {
160       PrintWriter out = res.getWriter();
161
162       if (req.getParameter("producer")!=null) {
163         String producerParam = req.getParameter("producer");
164         String verbParam = req.getParameter("verb");
165
166         MirGlobal.producerEngine().addJob(producerParam, verbParam);
167         out.println("job added");
168       }
169     }
170     catch (Exception e) {
171       throw new ServletModuleException(e.getMessage());
172     }
173   }
174
175   public void produceAllNew(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException {
176     try {
177       MirGlobal.localizer().producers().produceAllNew();
178       showMessage(aResponse.getWriter(), getLocale(aRequest), "produceAllNewAddedToQueue", "", "");
179     }
180     catch (Exception e) {
181       throw new ServletModuleException(e.getMessage());
182     }
183   }
184
185   public void enqueue(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException {
186     try {
187       if (aRequest.getParameter("producer")!=null) {
188         String producerParam = aRequest.getParameter("producer");
189         String verbParam = aRequest.getParameter("verb");
190
191         MirGlobal.producerEngine().addJob(producerParam, verbParam);
192
193         showProducerQueueStatus(aRequest, aResponse);
194       }
195     }
196     catch (Exception e) {
197       throw new ServletModuleException(e.getMessage());
198     }
199   }
200
201   public void cancelAbortJob(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException {
202     // ML: to be coded
203   }
204 }