KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > model > MProcess


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 Smart 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-2003 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.model;
15
16 import java.util.*;
17 import java.sql.*;
18
19 import org.compiere.util.DB;
20
21
22 /**
23  * Process Model
24  *
25  * @author Jorg Janke
26  * @version $Id: MProcess.java,v 1.2 2003/09/27 01:22:19 jjanke Exp $
27  */

28 public class MProcess extends X_AD_Process
29 {
30     /**
31      * Standard Constructor
32      * @param ctx context
33      * @param AD_Process_ID process
34      */

35     public MProcess (Properties ctx, int AD_Process_ID)
36     {
37         super (ctx, AD_Process_ID);
38         if (AD_Process_ID == 0)
39         {
40             setIsReport (false);
41             setAccessLevel (null);
42             setEntityType (null);
43             setValue (null);
44             setIsUserStartable (false);
45             setName (null);
46             setAD_Process_ID (0);
47         }
48     } // MProcess
49

50     /**
51      * Load Contsructor
52      * @param ctx context
53      * @param rs result set
54      */

55     public MProcess (Properties ctx, ResultSet rs)
56     {
57         super (ctx, rs);
58     } // MProcess
59

60     protected POInfo initPO (Properties ctx)
61     {
62         int AD_Table_ID = 284;
63         POInfo poi = POInfo.getPOInfo (ctx, AD_Table_ID);
64         return poi;
65     }
66
67     private MProcess_Para[] m_parameters = null;
68
69     public MProcess_Para[] getParameter()
70     {
71         if (m_parameters != null)
72             return m_parameters;
73         ArrayList list = new ArrayList();
74         //
75
String JavaDoc sql = "SELECT * FROM AD_Process_Para WHERE AD_Process_ID=?";
76         PreparedStatement pstmt = null;
77         try
78         {
79             pstmt = DB.prepareStatement(sql);
80             pstmt.setInt(1, getAD_Process_ID());
81             ResultSet rs = pstmt.executeQuery();
82             while (rs.next())
83             {
84                 list.add(new MProcess_Para(getCtx(), rs));
85             }
86             rs.close();
87             pstmt.close();
88             pstmt = null;
89         }
90         catch (Exception JavaDoc e)
91         {
92             log.error("getParameter", e);
93         }
94         finally
95         {
96             try
97             {
98                 if (pstmt != null)
99                     pstmt.close ();
100             }
101             catch (Exception JavaDoc e)
102             {}
103             pstmt = null;
104         }
105         //
106
m_parameters = new MProcess_Para[list.size()];
107         list.toArray(m_parameters);
108         return m_parameters;
109     } // getParameters
110

111
112     public String JavaDoc toString ()
113     {
114         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ("MProcess[")
115             .append (getID ())
116             .append ("]");
117         return sb.toString ();
118     }
119
120     /*************************************************************************/
121
122     /**
123      * Process w/o parameter
124      * @param Record_ID record
125      * @return Process Instance
126      */

127     public MPInstance process (int Record_ID)
128     {
129         MPInstance pi = new MPInstance (this, Record_ID);
130         // Lock
131
pi.setIsProcessing(true);
132         pi.save();
133
134         boolean ok = true;
135
136         // PL/SQL Procedure
137
String JavaDoc ProcedureName = getProcedureName();
138         if (ProcedureName != null && ProcedureName.length() > 0)
139         {
140             ok = startProcess (ProcedureName, pi);
141         }
142
143         // Unlock
144
pi.setIsProcessing(false);
145         pi.save();
146         //
147
pi.log();
148         return pi;
149     } // process
150

151     /**
152      * Start Database Process
153      * @param ProcedureName PL/SQL procedure name
154      * @param pi process instance
155      * @return true if success
156      */

157     private boolean startProcess (String JavaDoc ProcedureName, MPInstance pi)
158     {
159         int AD_PInstance_ID = pi.getAD_PInstance_ID();
160         // execute on this thread/connection
161
log.info("startProcess - " + ProcedureName + "(" + AD_PInstance_ID + ")");
162         try
163         {
164             String JavaDoc sql = "{CALL " + ProcedureName + "(?)}";
165             CallableStatement cstmt = DB.prepareCall(sql); // ro??
166
cstmt.setInt(1, AD_PInstance_ID);
167             cstmt.executeUpdate();
168             cstmt.close();
169         }
170         catch (Exception JavaDoc e)
171         {
172             log.error("startProcess", e);
173             pi.setResult(MPInstance.RESULT_ERROR);
174             pi.setErrorMsg(e.getLocalizedMessage());
175             return false;
176         }
177         pi.setResult(MPInstance.RESULT_OK);
178         return true;
179     } // startProcess
180

181
182 } // MProcess
183
Popular Tags