KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > apps > ProcessCtl


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.apps;
15
16 import java.awt.*;
17 import java.sql.*;
18 import java.util.*;
19 import javax.swing.*;
20
21 import org.compiere.util.*;
22 import org.compiere.process.*;
23 import org.compiere.print.*;
24
25 /**
26  * Process Interface Controller.
27  *
28  * @author Jorg Janke
29  * @version $Id: ProcessCtl.java,v 1.32 2003/08/18 15:49:30 jjanke Exp $
30  */

31 public class ProcessCtl extends Thread JavaDoc
32 {
33     /**
34      * Async Process - Do it all.
35      * <code>
36      * - Get Instance ID
37      * - Get Parameters
38      * - execute (lock - start process - unlock)
39      * </code>
40      * Creates a ProcessCtl instance, which calls
41      * lockUI and unlockUI if parent is a ASyncProcess
42      * <br>
43      * Called from ProcessStart.startProcess, ProcessDialog.actionPerformed,
44      * APanel.cmd_print, APanel.actionButton, VPaySelect.cmd_generate
45      *
46      * @param parent ASyncProcess & Container
47      * @param WindowNo window no
48      * @param pi ProcessInfo process info
49      * @return worker started ProcessCtl instance
50      */

51     public static ProcessCtl process (ASyncProcess parent, int WindowNo, ProcessInfo pi)
52     {
53         Log.trace(Log.l2_Sub, "ProcessCtl.process - WindowNo=" + WindowNo, pi);
54
55         // Get Instance
56
pi.setAD_PInstance_ID (getInstanceID(WindowNo, pi.getAD_Process_ID(), pi.getRecord_ID()));
57         if (pi.getAD_PInstance_ID() == 0)
58         {
59             pi.setSummary (Msg.getMsg(Env.getCtx(), "ProcessNoInstance"));
60             pi.setError (true);
61             return null;
62         }
63
64         // Get Parameters
65
ProcessParameter para = new ProcessParameter (Env.getFrame((Container)parent), WindowNo, pi);
66         if (para.initDialog())
67         {
68             para.show();
69             if (!para.isOK())
70             {
71                 pi.setSummary (Msg.getMsg(Env.getCtx(), "ProcessCancelled"));
72                 pi.setError (true);
73                 return null;
74             }
75         }
76
77         // execute
78
ProcessCtl worker = new ProcessCtl(parent, pi);
79         worker.start();
80         return worker;
81     } // execute
82

83
84     /*************************************************************************/
85
86     /**
87      * Create Process Instance.
88      * @param WindowNo window no
89      * @param AD_Process_ID process
90      * @param Record_ID record
91      * @param AD_Client_ID client
92      * @param AD_Org_ID org
93      * @return AD_PInstance_ID instance
94      */

95     public static int getInstanceID (int WindowNo, int AD_Process_ID, int Record_ID, int AD_Client_ID, int AD_Org_ID)
96     {
97     // Log.trace(Log.l3_Util, "ProcessCtl.getInstanceID for " + AD_Process_ID + " with " + Record_ID);
98

99         // Get AD_PInstance_ID
100
int AD_PInstance_ID = DB.getKeyNextNo(Env.getCtx(), WindowNo, "AD_PInstance");
101         if (AD_PInstance_ID == 0)
102             return 0;
103         // Get User
104
int AD_User_ID = Env.getContextAsInt(Env.getCtx(), "#AD_User_ID");
105
106         // Create Instance Record
107
String JavaDoc sql = "INSERT INTO AD_PInstance (AD_Client_ID,AD_Org_ID, IsActive, Created,CreatedBy,Updated,UpdatedBy,"
108             + " AD_PInstance_ID, AD_Process_ID, Record_ID, AD_User_ID) "
109             + "VALUES (" + AD_Client_ID + "," + AD_Org_ID
110             + ",'Y',SysDate," + AD_User_ID + ",SysDate," + AD_User_ID + ","
111             + AD_PInstance_ID + "," + AD_Process_ID + "," + Record_ID + "," + AD_User_ID + ")";
112         try
113         {
114             Statement stmt = DB.createStatement();
115             int n = stmt.executeUpdate(sql.toString());
116             stmt.close();
117         }
118         catch (SQLException e)
119         {
120             Log.error("ProcessCtl.getInstanceID", e);
121             return 0;
122         }
123         //
124
Log.trace(Log.l4_Data, "ProcessCtl.getInstanceID = " + AD_PInstance_ID);
125         return AD_PInstance_ID;
126     } // getInstanceID
127

128     /**
129      * Create Process Instance.
130      * @param WindowNo window no
131      * @param AD_Process_ID process
132      * @param Record_ID record
133      * @return AD_PInstance_ID instance
134      */

135     public static int getInstanceID (int WindowNo, int AD_Process_ID, int Record_ID)
136     {
137         return getInstanceID (WindowNo, AD_Process_ID, Record_ID,
138           Env.getContextAsInt(Env.getCtx(), WindowNo, "AD_Client_ID"),
139           Env.getContextAsInt(Env.getCtx(), WindowNo, "AD_Org_ID"));
140     } // getInstanceID
141

142     /*************************************************************************/
143
144     /**
145      * Start Java Class.
146      * instanciate the class implementing the interface ProcessCall.
147      * The class can be a Server/Client class (when in Package
148      * org compiere.process or org.compiere.model) or a client only class
149      * (e.g. in org.compiere.report)
150      *
151      * @param ctx Context
152      * @param ClassName name of the class to call
153      * @param pi process info
154      * @return true if success
155      * @see org.compiere.model.ProcessCall
156      */

157     private static boolean startClass (Properties ctx, String JavaDoc ClassName, ProcessInfo pi)
158     {
159         Log.trace(Log.l4_Data, "ProcessCtl.startClass - " + ClassName, pi);
160         boolean retValue = false;
161         ProcessCall myObject = null;
162         try
163         {
164             Class JavaDoc myClass = Class.forName(ClassName);
165             myObject = (ProcessCall)myClass.newInstance();
166             if (myObject == null)
167                 retValue = false;
168             else
169                 retValue = myObject.startProcess(ctx, pi);
170         }
171         catch (Exception JavaDoc e)
172         {
173             pi.setSummary("Error Start Class " + ClassName, true);
174             Log.error("ProcessCtl.startClass - " + ClassName, e);
175             retValue = false;
176         }
177
178         return retValue;
179     } // startClass
180

181
182     /*************************************************************************/
183
184     /**
185      * Start Database Process
186      * @param ProcedureName PL/SQL procedure name
187      * @param pi orocess info
188      * @return true if success
189      */

190     private static boolean startProcess (String JavaDoc ProcedureName, ProcessInfo pi)
191     {
192         // execute on this thread/connection
193
Log.trace(Log.l4_Data, "ProcessCtl.startProcess", ProcedureName + "(" + pi.getAD_PInstance_ID() + ")");
194         try
195         {
196             String JavaDoc sql = "{CALL " + ProcedureName + "(?)}";
197             CallableStatement cstmt = DB.prepareCall(sql); // ro??
198
cstmt.setInt(1, pi.getAD_PInstance_ID());
199             cstmt.executeUpdate();
200             cstmt.close();
201         }
202         catch (Exception JavaDoc e)
203         {
204             Log.error("ProcessCtl.startProcess", e);
205             pi.setSummary (Msg.getMsg(Env.getCtx(), "ProcessRunError") + " " + e.getLocalizedMessage());
206             pi.setError (true);
207             return false;
208         }
209     // Log.trace(Log.l4_Data, "ProcessCtl.startProcess - done");
210
return true;
211     } // startProcess
212

213     /*************************************************************************/
214
215     /**
216      * Constructor
217      * @param parent Container & ASyncProcess
218      * @param pi
219      * Created in process(), VInvoiceGen.generateInvoices
220      */

221     public ProcessCtl (ASyncProcess parent, ProcessInfo pi)
222     {
223         m_parent = parent;
224         m_pi = pi;
225     } // ProcessCtl
226

227     private ASyncProcess m_parent;
228     private ProcessInfo m_pi;
229     private Waiting m_waiting;
230
231     /**
232      * Execute Process Instance and Lock UI.
233      * Calls lockUI and unlockUI if parent is a ASyncProcess
234      * <pre>
235      * - Get Process Information
236      * - Call Class
237      * - Submit SQL Procedure
238      * - Run SQL Procedure
239      * </pre>
240      */

241     public void run ()
242     {
243         Log.trace(Log.l3_Util, "ProcessCtl.run", "AD_PInstance_ID=" + m_pi.getAD_PInstance_ID()
244             + ", Record_ID=" + m_pi.getRecord_ID());
245
246         // Lock
247
lock();
248     // try {System.out.println(">> sleeping ..");sleep(20000);System.out.println(".. sleeping <<");} catch (Exception e) {}
249

250         // Get Process Information: Name, Procedure Name, ClassName, IsReport, IsDirectPrint
251
String JavaDoc ProcedureName = "";
252         String JavaDoc ClassName = "";
253         int AD_ReportView_ID = 0;
254         boolean IsReport = false;
255         boolean IsDirectPrint = false;
256         //
257
String JavaDoc SQL = "SELECT p.Name, p.ProcedureName,p.ClassName, p.AD_Process_ID,"
258             + " p.isReport,p.IsDirectPrint,p.AD_ReportView_ID,"
259             + " DECODE(p.Statistic_Count,0,0, p.Statistic_Seconds/p.Statistic_Count) "
260             + "FROM AD_Process p, AD_PInstance i "
261             + "WHERE p.AD_Process_ID=i.AD_Process_ID AND p.IsActive='Y'"
262             + " AND i.AD_PInstance_ID=?";
263         if (!Env.isBaseLanguage(Env.getCtx(), "AD_Process"))
264             SQL = "SELECT t.Name, p.ProcedureName,p.ClassName, p.AD_Process_ID,"
265                 + " p.isReport, p.IsDirectPrint,AD_ReportView_ID,"
266                 + " DECODE(p.Statistic_Count,0,0, p.Statistic_Seconds/p.Statistic_Count) "
267                 + "FROM AD_Process p, AD_Process_Trl t, AD_PInstance i "
268                 + "WHERE p.AD_Process_ID=i.AD_Process_ID"
269                 + " AND p.AD_Process_ID=t.AD_Process_ID AND p.IsActive='Y'"
270                 + " AND i.AD_PInstance_ID=?"
271                 + " AND t.AD_Language='" + Env.getAD_Language(Env.getCtx()) + "'";
272         //
273
try
274         {
275             PreparedStatement pstmt = DB.prepareStatement(SQL, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
276             pstmt.setInt(1, m_pi.getAD_PInstance_ID());
277             ResultSet rs = pstmt.executeQuery();
278             if (rs.next())
279             {
280                 m_pi.setTitle (rs.getString(1));
281                 if (m_waiting != null)
282                     m_waiting.setTitle(m_pi.getTitle());
283                 ProcedureName = rs.getString(2);
284                 ClassName = rs.getString(3);
285                 m_pi.setAD_Process_ID (rs.getInt(4));
286                 // Report
287
if ("Y".equals(rs.getString(5)))
288                 {
289                     IsReport = true;
290                     if ("Y".equals(rs.getString(6)) && !Ini.getPropertyBool(Ini.P_PRINTPREVIEW))
291                         IsDirectPrint = true;
292                 }
293                 AD_ReportView_ID = rs.getInt(7);
294                 //
295
int estimate = rs.getInt(8);
296                 if (estimate != 0)
297                 {
298                     m_pi.setEstSeconds (estimate + 1); // admin overhead
299
if (m_waiting != null)
300                         m_waiting.setTimerEstimate(m_pi.getEstSeconds());
301                 }
302             }
303             else
304                 Log.error("ProcessCtrl.run - no AD_PInstance_ID=" + m_pi.getAD_PInstance_ID());
305             rs.close();
306             pstmt.close();
307         }
308         catch (SQLException e)
309         {
310             m_pi.setSummary (Msg.getMsg(Env.getCtx(), "ProcessNoProcedure") + " " + e.getLocalizedMessage(), true);
311             unlock();
312             Log.error("ProcessCtrl.run", e);
313             return;
314         }
315
316         // No Procedure
317
if (ProcedureName == null)
318             ProcedureName = "";
319
320         // Start Optional Class
321
if (ClassName != null && ClassName.length() > 0)
322         {
323             // Run Class
324
if (!startClass (Env.getCtx(), ClassName, m_pi))
325             {
326                 unlock();
327                 return;
328             }
329
330             // No Optional SQL procedure ... done
331
if (!IsReport && ProcedureName.length() == 0)
332             {
333                 unlock ();
334                 return;
335             }
336             // No Optional Report ... done
337
if (IsReport && AD_ReportView_ID == 0)
338             {
339                 unlock ();
340                 return;
341             }
342         }
343
344         // If not a report, we need a prodedure name
345
if (!IsReport && ProcedureName.length() == 0)
346         {
347             m_pi.setSummary (Msg.getMsg(Env.getCtx(), "ProcessNoProcedure"), true);
348             unlock();
349             return;
350         }
351
352         /**********************************************************************
353          * Report submission
354          */

355         if (IsReport)
356         {
357             // Optional Pre-Report Process
358
if (ProcedureName.length() > 0)
359             {
360                 if (!startProcess(ProcedureName, m_pi))
361                 {
362                     unlock();
363                     return;
364                 }
365             } // Pre-Report
366

367             // Start Report -----------------------------------------------
368
boolean ok = ReportCtl.start(m_pi, IsDirectPrint);
369             m_pi.setSummary("Report", !ok);
370             unlock ();
371         }
372         /**********************************************************************
373          * Process submission
374          */

375         else
376         {
377             if (!startProcess(ProcedureName, m_pi))
378             {
379                 unlock();
380                 return;
381             }
382             // Success - getResult
383
ProcessInfoUtil.setSummaryFromDB(m_pi);
384             unlock();
385         } // *** Process submission ***
386
// Log.trace(Log.l3_Util, "ProcessCtl.run - done");
387
} // run
388

389     /**
390      * Lock & show Waiting
391      */

392     private void lock ()
393     {
394     // Log.trace(Log.l5_DData, "ProcessCtl.locking ...");
395
JFrame frame = Env.getFrame((Container)m_parent);
396         if (frame instanceof AWindow)
397             ((AWindow)frame).setBusyTimer(m_pi.getEstSeconds());
398         else
399             m_waiting = new Waiting (frame, Msg.getMsg(Env.getCtx(), "Processing"), false, m_pi.getEstSeconds());
400         SwingUtilities.invokeLater(new Runnable JavaDoc()
401         {
402             public void run()
403             {
404                 Log.trace(Log.l5_DData, "ProcessCtl.lock");
405                 m_parent.lockUI(m_pi);
406             }
407         });
408         if (m_waiting != null)
409             m_waiting.toFront();
410     } // lock
411

412     /**
413      * Unlock & dispose Waiting
414      */

415     private void unlock ()
416     {
417         if (m_pi.getSummary().indexOf("@") != -1)
418             m_pi.setSummary(Msg.parseTranslation(Env.getCtx(), m_pi.getSummary()));
419         SwingUtilities.invokeLater(new Runnable JavaDoc()
420         {
421             public void run()
422             {
423                 Log.trace(Log.l5_DData, "ProcessCtl.unlock");
424                 m_parent.unlockUI(m_pi);
425             }
426         });
427         if (m_waiting != null)
428             m_waiting.dispose();
429         m_waiting = null;
430     } // unlock
431

432 } // ProcessCtl
433
Popular Tags