cleanup + misc. fixes
[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().makeAdminGeneratorLibrary().makeGenerator("producerqueue.template");
64
65       Map generationData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)});
66       generationData.put( "thisurl", "module=Producer&do=showProducerQueueStatus");
67
68       List producersData = new ArrayList();
69       Iterator i = MirGlobal.getProducerEngine().getFactories().iterator();
70       while (i.hasNext()) {
71         ProducerFactory factory = (ProducerFactory) i.next();
72
73         List producerVerbs = new ArrayList();
74         Iterator j = factory.verbs().iterator();
75         while (j.hasNext()) {
76           Map verbData = new HashMap();
77           ProducerFactory.ProducerVerb verb = (ProducerFactory.ProducerVerb) j.next();
78           verbData.put("name", verb.getName());
79           verbData.put("description", verb.getDescription());
80
81           producerVerbs.add(verbData);
82         }
83
84         Map producerData = new HashMap();
85         producerData.put("name", factory.getName());
86         producerData.put("verbs", producerVerbs);
87
88         producersData.add(producerData);
89       }
90       generationData.put("producers", producersData);
91
92       generationData.put("queue", MirGlobal.getProducerEngine().getQueueStatus());
93       generator.generate(aResponse.getWriter(), generationData, getLogger());
94     }
95     catch (Throwable t) {
96       throw new ServletModuleFailure(t);
97     }
98   }
99
100   /**
101    * This method will only be called by external scripts (e.g. from cron jobs).
102    * The output therefore is very simple.
103    */
104   public void produce(HttpServletRequest aRequest, HttpServletResponse aResponse) {
105     PrintWriter out;
106     try {
107       out = aResponse.getWriter();
108     }
109     catch (IOException e) {
110       throw new ServletModuleFailure(e);
111     }
112
113     if (aRequest.getParameter("producer")!=null) {
114       String producerParam = aRequest.getParameter("producer");
115       String verbParam = aRequest.getParameter("verb");
116
117       try {
118         MirGlobal.getProducerEngine().addJob(producerParam, verbParam);
119
120         out.println("job added");
121       }
122       catch (Throwable t) {
123
124         out.println("Can't add job: " + t.getMessage());
125       }
126     }
127   }
128
129   /**
130    * Servlet action to propduce a preconfigured combination of producers
131    * (e.g. "generate all new")
132    */
133   public void producerecipe(HttpServletRequest aRequest, HttpServletResponse aResponse) {
134     try {
135       String recipe = aRequest.getParameter("recipe");
136       MirGlobal.localizer().producers().produceRecipe(recipe);
137
138       ServletHelper.generateInfoMessage(aRequest, aResponse,
139           new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)},
140            "bundles/admin", "etc/bundles/adminlocal", "recipeAddedToQueue", recipe, "");
141     }
142     catch (Throwable t) {
143       throw new ServletModuleFailure(t);
144     }
145   }
146
147   /**
148    * Servlet action to enqueue a producer job
149    */
150   public void enqueue(HttpServletRequest aRequest, HttpServletResponse aResponse) {
151     try {
152       if (aRequest.getParameter("producer")!=null) {
153         String producerParam = aRequest.getParameter("producer");
154         String verbParam = aRequest.getParameter("verb");
155
156         MirGlobal.getProducerEngine().addJob(producerParam, verbParam);
157
158         ServletHelper.redirect(aResponse, "Producer", "showProducerQueueStatus");
159       }
160     }
161     catch (Throwable t) {
162       throw new ServletModuleFailure(t);
163     }
164   }
165
166   public void cancel(HttpServletRequest aRequest, HttpServletResponse aResponse)  {
167     try {
168       HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);
169
170       if (requestParser.getParameter("cancelall") != null) {
171         MirGlobal.getProducerEngine().cancelAllJobs();
172       }
173       else {
174         List jobs = new ArrayList(requestParser.getParameterList("jobid"));
175
176         MirGlobal.getProducerEngine().cancelJobs(jobs);
177       }
178       ServletHelper.redirect(aResponse, "Producer", "showProducerQueueStatus");
179     }
180     catch (Throwable t) {
181       throw new ServletModuleFailure(t);
182     }
183   }
184 }