scripts/mir-setup/README: update with link to new doc on wiki
[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  any library licensed under the Apache Software License,
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23  * (or with modified versions of the above that use the same license as the above),
24  * and distribute linked combinations including the two.  You must obey the
25  * GNU General Public License in all respects for all of the code used other than
26  * the above mentioned libraries.  If you modify this file, you may extend this
27  * exception to your version of the file, but you are not obligated to do so.
28  * If you do not wish to do so, delete this exception statement from your version.
29  */
30 package mircoders.servlet;
31
32 import mir.generator.Generator;
33 import mir.producer.ProducerFactory;
34 import mir.servlet.AdminServletModule;
35 import mir.servlet.ServletModuleFailure;
36 import mir.util.HTTPRequestParser;
37 import mircoders.global.MirGlobal;
38
39 import javax.servlet.http.HttpServletRequest;
40 import javax.servlet.http.HttpServletResponse;
41 import java.io.IOException;
42 import java.io.PrintWriter;
43 import java.util.ArrayList;
44 import java.util.HashMap;
45 import java.util.Iterator;
46 import java.util.List;
47 import java.util.Locale;
48 import java.util.Map;
49
50 public class ServletModuleProducer extends AdminServletModule {
51   public void defaultAction(HttpServletRequest aRequest, HttpServletResponse aResponse) {
52     showProducerQueueStatus(aRequest, aResponse);
53   }
54
55   /**
56    *
57    * @param aRequest
58    * @param aResponse
59    */
60   public void showProducerQueueStatus(HttpServletRequest aRequest, HttpServletResponse aResponse) {
61     try {
62       // TODO: use ServletHelper
63       Generator generator = MirGlobal.localizer().generators().
64           makeAdminGeneratorLibrary().makeGenerator("producerqueue.template", null);
65
66       Map generationData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
67       generationData.put( "thisurl", "module=Producer&do=showProducerQueueStatus");
68
69       List producersData = new ArrayList();
70       Iterator i = MirGlobal.getProducerEngine().getFactories().iterator();
71       while (i.hasNext()) {
72         ProducerFactory factory = (ProducerFactory) i.next();
73
74         List producerVerbs = new ArrayList();
75         Iterator j = factory.verbs().iterator();
76         while (j.hasNext()) {
77           Map verbData = new HashMap();
78           ProducerFactory.ProducerVerb verb = (ProducerFactory.ProducerVerb) j.next();
79           verbData.put("name", verb.getName());
80           verbData.put("description", verb.getDescription());
81
82           producerVerbs.add(verbData);
83         }
84
85         Map producerData = new HashMap();
86         producerData.put("name", factory.getName());
87         producerData.put("verbs", producerVerbs);
88
89         producersData.add(producerData);
90       }
91       generationData.put("producers", producersData);
92
93       generationData.put("queue", MirGlobal.getProducerEngine().getQueueStatus());
94       generator.generate(aResponse.getWriter(), generationData, getLogger());
95     }
96     catch (Throwable t) {
97       throw new ServletModuleFailure(t);
98     }
99   }
100
101   /**
102    * This method will only be called by external scripts (e.g. from cron jobs).
103    * The output therefore is very simple.
104    */
105   public void produce(HttpServletRequest aRequest, HttpServletResponse aResponse) {
106     PrintWriter out;
107     try {
108       out = aResponse.getWriter();
109     }
110     catch (IOException e) {
111       throw new ServletModuleFailure(e);
112     }
113
114     if (aRequest.getParameter("producer")!=null) {
115       String producerParam = aRequest.getParameter("producer");
116       String verbParam = aRequest.getParameter("verb");
117
118       try {
119         MirGlobal.getProducerEngine().addJob(producerParam, verbParam);
120
121         out.println("job added");
122       }
123       catch (Throwable t) {
124
125         out.println("Can't add job: " + t.getMessage());
126       }
127     }
128   }
129
130   /**
131    * Servlet action to propduce a preconfigured combination of producers
132    * (e.g. "generate all new")
133    */
134   public void producerecipe(HttpServletRequest aRequest, HttpServletResponse aResponse) {
135     try {
136       String recipe = aRequest.getParameter("recipe");
137       MirGlobal.localizer().producers().produceRecipe(recipe);
138
139       ServletHelper.generateInfoMessage(aRequest, aResponse,
140           new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)},
141            "bundles/admin", "etc/bundles/adminlocal", "recipeAddedToQueue", recipe, "");
142     }
143     catch (Throwable t) {
144       throw new ServletModuleFailure(t);
145     }
146   }
147
148   /**
149    * Servlet action to enqueue a producer job
150    */
151   public void enqueue(HttpServletRequest aRequest, HttpServletResponse aResponse) {
152     try {
153       if (aRequest.getParameter("producer")!=null) {
154         String producerParam = aRequest.getParameter("producer");
155         String verbParam = aRequest.getParameter("verb");
156
157         MirGlobal.getProducerEngine().addJob(producerParam, verbParam);
158
159         ServletHelper.redirect(aResponse, "Producer", "showProducerQueueStatus");
160       }
161     }
162     catch (Throwable t) {
163       throw new ServletModuleFailure(t);
164     }
165   }
166
167   public void cancel(HttpServletRequest aRequest, HttpServletResponse aResponse)  {
168     try {
169       HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
170
171       if (requestParser.getParameter("cancelall") != null) {
172         MirGlobal.getProducerEngine().cancelAllJobs();
173       }
174       else {
175         List jobs = new ArrayList(requestParser.getParameterList("jobid"));
176
177         MirGlobal.getProducerEngine().cancelJobs(jobs);
178       }
179       ServletHelper.redirect(aResponse, "Producer", "showProducerQueueStatus");
180     }
181     catch (Throwable t) {
182       throw new ServletModuleFailure(t);
183     }
184   }
185 }