KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > process > SvrProcess


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.process;
15
16 import java.sql.*;
17 import java.util.*;
18 import java.math.*;
19 import java.lang.reflect.*;
20
21 import org.compiere.util.*;
22
23 /**
24  * Server Process Template
25  *
26  * @author Jorg Janke
27  * @version $Id: SvrProcess.java,v 1.15 2003/10/10 01:01:06 jjanke Exp $
28  */

29 public abstract class SvrProcess implements ProcessCall
30 {
31     /**
32      * Server Process
33      */

34     public SvrProcess()
35     {
36     } // SvrProcess
37

38     private Properties m_ctx;
39     private ProcessInfo m_pi;
40
41     /** Logger */
42     protected Logger log = Logger.getCLogger (getClass());
43
44     /** Common Error Message */
45     protected static String JavaDoc MSG_SaveErrorRowNotFound = "@SaveErrorRowNotFound@";
46     protected static String JavaDoc MSG_InvalidArguments = "@InvalidArguments@";
47
48
49     /**
50      * Start the process.
51      * Calls the abstract methods <code>process</code>.
52      * It should only return false, if the function could not be performed
53      * as this causes the process to abort.
54      *
55      * @param ctx Context
56      * @param pi Process Info
57      * @return true if the next process should be performed
58      */

59     public final boolean startProcess (Properties ctx, ProcessInfo pi)
60     {
61         // Preparation
62
m_ctx = ctx == null ? Env.getCtx() : ctx;
63         m_pi = pi;
64         lock();
65         //
66
process();
67         unlock();
68         return !m_pi.isError();
69     } // startProcess
70

71     /*************************************************************************/
72
73     /**
74      * Process - called when pressing the button ... in ...
75      */

76     protected void process()
77     {
78         prepare();
79         String JavaDoc msg = null;
80         boolean error = false;
81         try
82         {
83             msg = doIt();
84         }
85         catch (Exception JavaDoc e)
86         {
87             msg = e.getMessage();
88             if (msg == null)
89                 msg = e.toString();
90             if (e.getCause() != null)
91                 log.error("process - " + msg, e.getCause());
92             else
93                 log.error("process - " + msg);
94             error = true;
95         }
96         msg = Msg.parseTranslation(m_ctx, msg);
97         m_pi.setSummary(msg, error);
98         ProcessInfoUtil.saveLogToDB(m_pi);
99     } // process
100

101     /**
102      * Prepare - e.g., get Parameters.
103      */

104     abstract protected void prepare();
105
106     /**
107      * Perrform process.
108      * @return Message (clear text)
109      * @throws Exception if not successful
110      */

111     abstract protected String JavaDoc doIt() throws Exception JavaDoc;
112
113     /*************************************************************************/
114
115     /**
116      * Get Process Info
117      * @return Process Info
118      */

119     public ProcessInfo getProcessInfo()
120     {
121         return m_pi;
122     } // getProcessInfo
123

124     /**
125      * Get Properties
126      * @return Properties
127      */

128     public Properties getCtx()
129     {
130         return m_ctx;
131     } // getCtx
132

133     /**
134      * Get Name
135      * @return Name
136      */

137     protected String JavaDoc getName()
138     {
139         return m_pi.getTitle();
140     } // getName
141

142     /**
143      * Get Process Instance
144      * @return Process Instance
145      */

146     protected int getAD_PInstance_ID()
147     {
148         return m_pi.getAD_PInstance_ID();
149     } // getAD_PInstance_ID
150

151     /**
152      * Get Record_ID
153      * @return Record_ID
154      */

155     protected int getRecord_ID()
156     {
157         return m_pi.getRecord_ID();
158     } // getRecord_ID
159

160     /**
161      * Get AD_User_ID
162      * @return AD_User_ID of Process owner
163      */

164     protected int getAD_User_ID()
165     {
166         if (m_pi.getAD_User_ID() == null)
167         {
168             String JavaDoc sql = "SELECT AD_User_ID, AD_Client_ID FROM AD_PInstance WHERE AD_PInstance_ID=?";
169             try
170             {
171                 PreparedStatement pstmt = DB.prepareStatement(sql);
172                 pstmt.setInt(1, m_pi.getAD_PInstance_ID());
173                 ResultSet rs = pstmt.executeQuery();
174                 if (rs.next())
175                 {
176                     m_pi.setAD_User_ID (rs.getInt (1));
177                     m_pi.setAD_Client_ID (rs.getInt(2));
178                 }
179                 rs.close();
180                 pstmt.close();
181             }
182             catch (SQLException e)
183             {
184                 log.error("getAD_User_ID", e);
185             }
186         }
187         if (m_pi.getAD_User_ID() == null)
188             return 0;
189         return m_pi.getAD_User_ID().intValue();
190     } // getAD_User_ID
191

192     /**
193      * Get AD_User_ID
194      * @return AD_User_ID of Process owner
195      */

196     protected int getAD_Client_ID()
197     {
198         if (m_pi.getAD_Client_ID() == null)
199         {
200             getAD_User_ID(); // sets also Client
201
if (m_pi.getAD_Client_ID() == null)
202                 return 0;
203         }
204         return m_pi.getAD_Client_ID().intValue();
205     } // getAD_Client_ID
206

207     /*************************************************************************/
208
209     /**
210      * Get Parameter
211      * @return parameter
212      */

213     protected ProcessInfoParameter[] getParameter()
214     {
215         ProcessInfoParameter[] retValue = m_pi.getParameter();
216         if (retValue == null)
217         {
218             ProcessInfoUtil.setParameterFromDB(m_pi);
219             retValue = m_pi.getParameter();
220         }
221         return retValue;
222     } // getParameter
223

224     /*************************************************************************/
225
226     /**
227      * Add Log Entry
228      * @param date date or null
229      * @param id record id or 0
230      * @param number number or null
231      * @param msg message or null
232      */

233     public void addLog (int id, Timestamp date, BigDecimal number, String JavaDoc msg)
234     {
235         m_pi.addLog(id, date, number, msg);
236     } // addLog
237

238     /*************************************************************************/
239
240     /**
241      * Execute function
242      * @param className class
243      * @param methodName method
244      * @param args arguments
245      * @return result
246      */

247     public Object JavaDoc doIt (String JavaDoc className, String JavaDoc methodName, Object JavaDoc args[])
248     {
249         try
250         {
251             Class JavaDoc clazz = Class.forName(className);
252             Object JavaDoc object = clazz.newInstance();
253             Method[] methods = clazz.getMethods();
254             for (int i = 0; i < methods.length; i++)
255             {
256                 if (methods[i].getName().equals(methodName))
257                     return methods[i].invoke(object, args);
258             }
259         }
260         catch (Exception JavaDoc ex)
261         {
262             log.error("doIt", ex);
263         }
264         return null;
265     } // doIt
266

267     /*************************************************************************/
268
269     /**
270      * Lock Process Instance
271      */

272     private void lock()
273     {
274         log.debug("lock");
275         DB.executeUpdate("UPDATE AD_PInstance SET IsProcessing='Y' WHERE AD_PInstance_ID=" + m_pi.getAD_PInstance_ID());
276     } // lock
277

278     /**
279      * Unlock Process Instance.
280      * Update Process Instance DB and write option return message
281      */

282     private void unlock ()
283     {
284         StringBuffer JavaDoc sql = new StringBuffer JavaDoc("UPDATE AD_PInstance SET IsProcessing='N',Updated=SysDate,Result=");
285         sql.append(m_pi.isError() ? "0" : "1");
286         String JavaDoc msg = m_pi.getSummary();
287         if (msg != null && msg.length() > 0)
288             sql.append(", ErrorMsg=").append(DB.TO_STRING(msg, 2000));
289         sql.append(" WHERE AD_PInstance_ID=").append(m_pi.getAD_PInstance_ID());
290         DB.executeUpdate(sql.toString());
291         log.debug("unlock - Error=" + m_pi.isError() + " - " + msg);
292     } // unlock
293

294 } // SvrProcess
295
Popular Tags