KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > erpCommon > utility > ExecuteTask


1 /*
2  *************************************************************************
3  * The contents of this file are subject to the Openbravo Public License
4  * Version 1.0 (the "License"), being the Mozilla Public License
5  * Version 1.1 with a permitted attribution clause; you may not use this
6  * file except in compliance with the License. You may obtain a copy of
7  * the License at http://www.openbravo.com/legal/license.html
8  * Software distributed under the License is distributed on an "AS IS"
9  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
10  * License for the specific language governing rights and limitations
11  * under the License.
12  * The Original Code is Openbravo ERP.
13  * The Initial Developer of the Original Code is Openbravo SL
14  * All portions are Copyright (C) 2001-2006 Openbravo SL
15  * All Rights Reserved.
16  * Contributor(s): ______________________________________.
17  ************************************************************************
18 */

19 package org.openbravo.erpCommon.utility;
20
21 import org.openbravo.base.secureApp.*;
22 import org.openbravo.xmlEngine.XmlDocument;
23 import java.io.*;
24 import javax.servlet.*;
25 import javax.servlet.http.*;
26
27
28
29 public class ExecuteTask extends HttpSecureAppServlet {
30   
31   
32   public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException {
33     VariablesSecureApp vars = new VariablesSecureApp(request);
34     
35     if (vars.commandIn("DEFAULT")) {
36       String JavaDoc strTaskId = vars.getGlobalVariable("inpadTaskId", "ExecuteTask|taskId");
37       printPageDefault(response, vars, strTaskId);
38     } else if (vars.commandIn("EXECUTE")) {
39       String JavaDoc strTaskId = vars.getGlobalVariable("inpadTaskId", "ExecuteTask|taskId");
40       ExecuteTaskData[] data=null;
41       if (vars.getLanguage().equals("en_US")) {
42         data = ExecuteTaskData.select(this, strTaskId);
43       } else {
44         data = ExecuteTaskData.selectTrl(this, vars.getLanguage(), strTaskId);
45       }
46       if (data==null || data.length==0) throw new ServletException("Task not found: " + strTaskId);
47       if (!Utility.canViewInsert(this, vars, data[0].accesslevel, "ExecuteTask")) {
48         bdError(response, "AccessTableNoView", vars.getLanguage());
49         return;
50       } else if (!Utility.hasTaskAccess(this, vars, strTaskId)) {
51         bdError(response, "AccessTableNoView", vars.getLanguage());
52         return;
53       }
54       String JavaDoc command = Utility.parseTranslation(this, vars, vars.getLanguage(), data[0].osCommand);
55       String JavaDoc taskinstance = SequenceIdData.getSequence(this, "AD_TaskInstance", vars.getClient());
56       ExecuteTaskData.insert(this, taskinstance, vars.getClient(), vars.getOrg(), vars.getUser(), strTaskId);
57       executeCommand(response, vars, data[0].name, command);
58     } else if (vars.commandIn("EXECUTE_CMD")) {
59       String JavaDoc command = vars.getGlobalVariable("inpCommandText", "ExecuteTask|commandtext");
60       // The permissions of the tasks executor must be checked
61
command = Utility.parseTranslation(this, vars, vars.getLanguage(), command);
62       executeCommand(response, vars, command, command);
63     } else pageError(response);
64   }
65
66   void printPageDefault(HttpServletResponse response, VariablesSecureApp vars, String JavaDoc strTaskId) throws IOException, ServletException {
67     if (log4j.isDebugEnabled()) log4j.debug("Output: Default");
68     response.setContentType("text/html; charset=UTF-8");
69     PrintWriter out = response.getWriter();
70     XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/utility/TaskDefault").createXmlDocument();
71     xmlDocument.setParameter("taskId", strTaskId);
72
73     out.println(xmlDocument.print());
74     out.close();
75   }
76
77   void executeCommand(HttpServletResponse response, VariablesSecureApp vars, String JavaDoc Tittle, String JavaDoc command) throws IOException, ServletException {
78     if (log4j.isDebugEnabled()) log4j.debug("Output: Default");
79     response.setContentType("text/html; charset=UTF-8");
80     PrintWriter out = response.getWriter();
81
82     out.println("<html>");
83     out.println("<head>");
84     out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
85     out.println("<title>Aviso</title>");
86     out.println("<link REL=\"stylesheet\" TYPE=\"text/css\" HREF=\"" + strDireccion + "/web/skins/" + vars.getTheme() + "/Popup/Popup.css\" TITLE=\"Style\">");
87     out.println("<script language=\"JavaScript\" type=\"text/javascript\" SRC=\"" + strDireccion + "/web/js/mensaje.js\"></script>");
88     out.println("<script language=\"JavaScript\" type=\"text/javascript\" SRC=\"" + strDireccion + "/web/js/utils.js\"></script>");
89     out.println("<script language=\"JavaScript\" type=\"text/javascript\"> LNG_POR_DEFECTO = \"" + vars.getLanguage() + "\";</script>");
90     out.println("</head>");
91     out.println("<body>");
92     out.println("<form name=\"form1\" method=\"post\" action=\"ExecuteTask.html\">");
93     out.println("<div class=\"Popup_ContentPane_CircleLogo\">");
94     out.println(" <div class=\"Popup_WindowLogo\">");
95     out.println(" <img class=\"Popup_WindowLogo_Icon Popup_WindowLogo_Icon_task\" SRC=\"" + strDireccion + "/web/images/blank.gif\" border=0/></img>");
96     out.println(" </div>");
97     out.println("</div>");
98     out.println("");
99     out.println("<table cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">");
100     out.println("");
101     out.println(" <tr>");
102     out.println(" <td>");
103     out.println(" <table cellspacing=\"0\" cellpadding=\"0\" class=\"Popup_ContentPane_NavBar\">");
104     out.println(" <tr class=\"Popup_NavBar_bg\"><td></td>");
105     out.println(" <td class=\"Popup_NavBar_separator_cell\"></td>");
106     out.println(" <td class=\"Popup_NavBar_bg_logo_left\"></td>");
107     out.println(" <td class=\"Popup_NavBar_bg_logo\" width=\"1\" onclick=\"abrirNuevoBrowser('http://www.openbravo.com', 'Openbravo');return false;\"><IMG SRC=\"" + strDireccion + "/web/images/blank.gif\" alt=\"Openbravo\" title=\"Openbravo\" border=\"0\" id=\"openbravoLogo\" class=\"Popup_NavBar_logo\" /></td>");
108     out.println(" <td class=\"Popup_NavBar_bg_logo_right\"></td>");
109     out.println(" <td class=\"Popup_NavBar_Popup_title_cell\"><span>" + Tittle + "</span></td>");
110     out.println(" <td class=\"Popup_NavBar_separator_cell\"></td>");
111     out.println(" </tr>");
112     out.println(" </table>");
113     out.println(" </td>");
114     out.println(" </tr>");
115     out.println("");
116     out.println(" <tr>");
117     out.println(" <td>");
118     out.println(" <table cellspacing=\"0\" cellpadding=\"0\" class=\"Popup_ContentPane_SeparatorBar\">");
119     out.println(" <tr>");
120     out.println(" <td class=\"Popup_SeparatorBar_bg\"></td>");
121     out.println(" </tr>");
122     out.println(" </table>");
123     out.println(" </td>");
124     out.println(" </tr>");
125     out.println("");
126     out.println(" <tr>");
127     out.println(" <td>");
128     out.println(" <table cellspacing=\"0\" cellpadding=\"0\" class=\"Popup_ContentPane_InfoBar\">");
129     out.println(" <tr>");
130     out.println(" <td class=\"Popup_InfoBar_Icon_cell\"><IMG SRC=\"" + strDireccion + "/web/images/blank.gif\" border=\"0\" class=\"Popup_InfoBar_Icon_info\" /></td>");
131     out.println(" <td class=\"Popup_InfoBar_text_table\">");
132     out.println(" <table>");
133     out.println(" <tr>");
134     out.println(" <td class=\"Popup_InfoBar_text\" id=\"processMessage\">" + Utility.messageBD(this, "Processing", vars.getLanguage()) + "</td>");
135     out.println(" </tr>");
136     out.println(" </table>");
137     out.println(" </td>");
138     out.println(" </tr>");
139     out.println(" </table>");
140     out.println(" </td>");
141     out.println(" </tr>");
142     out.println("");
143     out.println(" <tr>");
144     out.println(" <td>");
145     out.println(" <div class=\"Popup_ContentPane_Client\" style=\"overflow: auto;\">");
146     out.println(" <table cellspacing=\"0\" cellpadding=\"0\" class=\"Popup_Client_TablePopup\">");
147     out.println("");
148     out.println(" <tr>");
149     out.println("");
150     out.println(" <td class=\"TableEdition_FourCells_width\">");
151     out.flush();
152     out.println(" <p class=\"Regular_Paragraph\">");
153     executeCommandTask(out, vars, command);
154     out.println(" </p>");
155     out.println(" </td>");
156     out.println(" </tr>");
157     out.println("");
158     out.println(" </table>");
159     out.println(" </div>");
160     out.println(" </td>");
161     out.println(" </tr>");
162     out.println("</table>");
163     out.println("<script language=\"JavaScript\" type=\"text/javascript\">layer('processMessage', '" + Utility.messageBD(this, "ProcessOK", vars.getLanguage()) + "', true, false);</script>");
164     out.println("</form>");
165     out.println("</body>");
166     out.println("</html>");
167     out.close();
168   }
169
170   public void executeCommandTask(PrintWriter out, VariablesSecureApp vars, String JavaDoc cmd) throws IOException, ServletException {
171     if (log4j.isDebugEnabled()) log4j.debug("ExecuteTask.executeCommand: " + cmd);
172     if(cmd == null || cmd.equals("")) {
173       out.println(Utility.messageBD(this, "EmptyCommand", vars.getLanguage()));
174       return;
175     }
176     //if(m_task != null && m_task.isAlive()) m_task.interrupt();
177
Task m_task = new Task(cmd);
178     m_task.start();
179     do {
180       try {
181         Thread.sleep(500L);
182       } catch(InterruptedException JavaDoc ioe) {
183         log4j.error("ExecuteTask.executeCommand: " + ioe);
184       }
185       if (!m_task.getOut().toString().equals("")) out.println("<span>" + m_task.getOut().toString() + "</span><br>");
186       if (!m_task.getErr().toString().equals("")) out.println("<span class=\"error\">" + m_task.getErr().toString() + "</span><br>");
187       out.flush();
188     } while(m_task.isAlive());
189     if (log4j.isDebugEnabled()) log4j.debug("ExecuteTask.executeCommand - done");
190   }
191
192
193   public String JavaDoc getServletInfo() {
194     return "Task executorServlet";
195   } // end of getServletInfo() method
196
}
197
Popular Tags