KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.math.*;
19 import java.io.Serializable JavaDoc;
20
21 import org.compiere.util.*;
22
23 /**
24  * Business Partner Model
25  *
26  * @author Jorg Janke
27  * @version $Id: MBPartner.java,v 1.15 2003/10/04 03:51:51 jjanke Exp $
28  */

29 public class MBPartner extends X_C_BPartner
30 {
31     /**
32      * Get Template Business Partner
33      * @param ctx context
34      * @param AD_Client_ID client
35      * @return Template Business Partner or null
36      */

37     public static MBPartner getTemplate (Properties ctx, int AD_Client_ID)
38     {
39         if (s_template != null)
40             return s_template;
41         //
42
String JavaDoc sql = "SELECT * FROM C_BPartner "
43             + "WHERE C_BPartner_ID=(SELECT C_BPartnerCashTrx_ID FROM AD_ClientInfo WHERE AD_Client_ID=?)";
44         PreparedStatement pstmt = null;
45         try
46         {
47             pstmt = DB.prepareStatement(sql);
48             pstmt.setInt(1, AD_Client_ID);
49             ResultSet rs = pstmt.executeQuery();
50             if (rs.next())
51                 s_template = new MBPartner (ctx, rs);
52             else
53                 s_log.error("getTemplate - No Template BP for AD_Client_ID=" + AD_Client_ID);
54             rs.close();
55             pstmt.close();
56             pstmt = null;
57         }
58         catch (Exception JavaDoc e)
59         {
60             s_log.error("getTemplate", e);
61         }
62         finally
63         {
64             try
65             {
66                 if (pstmt != null)
67                     pstmt.close ();
68             }
69             catch (Exception JavaDoc e)
70             {}
71             pstmt = null;
72         }
73         // Reset
74
if (s_template != null)
75         {
76             s_template.setValueNoCheck ("C_BPartner_ID", null);
77             s_template.setValueNoCheck ("Value", "");
78             s_template.setValueNoCheck ("Name", "");
79             s_template.setValueNoCheck ("Name2", "");
80             s_template.setSO_CreditLimit (Env.ZERO);
81             s_template.setSO_CreditUsed (Env.ZERO);
82         }
83         return s_template;
84     } // getTemplate
85

86     /** Static Logger */
87     private static Logger s_log = Logger.getCLogger (PO.class);
88     /** Tamplate BPartner */
89     private static MBPartner s_template;
90
91     /*************************************************************************/
92
93     /**
94      * Constructor for new BPartner from Template
95      * @param ctx context
96      */

97     public MBPartner (Properties ctx)
98     {
99         this (ctx, -1);
100     }
101
102     /**
103      * Default Constructor
104      * @param ctx context
105      * @param rs ResultSet to load from
106      */

107     public MBPartner (Properties ctx, ResultSet rs)
108     {
109         super (ctx, rs);
110     } // MBPartner
111

112     /**
113      * Default Constructor
114      * @param ctx context
115      * @param C_BPartner_ID partner or 0 or -1 (load from template)
116      */

117     public MBPartner (Properties ctx, int C_BPartner_ID)
118     {
119         super (ctx, C_BPartner_ID);
120         //
121
if (C_BPartner_ID == -1)
122         {
123             getTemplate (Env.getContextAsInt(ctx, "AD_Client_ID"));
124             C_BPartner_ID = 0;
125         }
126         if (C_BPartner_ID == 0)
127         {
128             setValueNoCheck ("Value", "");
129             setValueNoCheck ("Name", "");
130             setName2("");
131             setDUNS("");
132             setFirstSale(null);
133             //
134
setIsCustomer (true);
135             setIsProspect (true);
136             //
137
setSendEMail (false);
138             setIsOneTime (false);
139             setIsVendor (false);
140             setIsSummary (false);
141             setIsEmployee (false);
142             setIsSalesRep (false);
143             //
144
setSO_CreditLimit (Env.ZERO);
145             setSO_CreditUsed (Env.ZERO);
146         }
147         log.debug(toString());
148     } // MBPartner
149

150     private MUser[] m_contacts = null;
151     private MBPartner_Location[] m_locations = null;
152     private MBP_BankAccount[] m_accounts = null;
153
154     /**
155      * Load Default BPartner
156      * @param AD_Client_ID client
157      */

158     private void getTemplate (int AD_Client_ID)
159     {
160         if (AD_Client_ID == 0)
161             throw new IllegalArgumentException JavaDoc ("MBPartner new - Client_ID=0");
162
163         String JavaDoc sql = "SELECT * FROM C_BPartner "
164             + "WHERE C_BPartner_ID=(SELECT C_BPartnerCashTrx_ID FROM AD_ClientInfo WHERE AD_Client_ID=?)";
165         PreparedStatement pstmt = null;
166         try
167         {
168             pstmt = DB.prepareStatement(sql);
169             pstmt.setInt(1, AD_Client_ID);
170             ResultSet rs = pstmt.executeQuery();
171             if (rs.next())
172                 load (rs);
173             else
174             {
175                 load(0);
176                 log.error ("getTemplate - None found");
177             }
178             rs.close();
179             pstmt.close();
180             pstmt = null;
181         }
182         catch (Exception JavaDoc e)
183         {
184             log.error("getTemplate", e);
185         }
186         finally
187         {
188             try
189             {
190                 if (pstmt != null)
191                     pstmt.close ();
192             }
193             catch (Exception JavaDoc e)
194             {}
195             pstmt = null;
196         }
197         setStandardDefaults();
198         setValueNoCheck("C_BPartner_ID", new Integer JavaDoc(0));
199         setValueNoCheck("Value", null);
200         setValueNoCheck("Name", null);
201         setName2(null);
202     } // getTemplate
203

204
205     /**
206      * Get Contacts
207      * @return contacts
208      */

209     public MUser[] getContacts()
210     {
211         if (m_contacts == null || m_contacts.length == 0)
212             ;
213         else
214             return m_contacts;
215         //
216
ArrayList list = new ArrayList();
217         String JavaDoc sql = "SELECT * FROM AD_User WHERE C_BPartner_ID=?";
218         PreparedStatement pstmt = null;
219         try
220         {
221             pstmt = DB.prepareStatement(sql);
222             pstmt.setInt(1, getC_BPartner_ID());
223             ResultSet rs = pstmt.executeQuery();
224             while (rs.next())
225                 list.add(new MUser (getCtx(), rs));
226             rs.close();
227             pstmt.close();
228             pstmt = null;
229         }
230         catch (Exception JavaDoc e)
231         {
232             log.error("getContacts", e);
233         }
234         finally
235         {
236             try
237             {
238                 if (pstmt != null)
239                     pstmt.close ();
240             }
241             catch (Exception JavaDoc e)
242             {}
243             pstmt = null;
244         }
245
246         m_contacts = new MUser[list.size()];
247         list.toArray(m_contacts);
248         return m_contacts;
249     } // getContacts
250

251     /**
252      * Get Locations
253      * @return locations
254      */

255     public MBPartner_Location[] getLocations()
256     {
257         if (m_locations == null || m_locations.length == 0)
258             ;
259         else
260             return m_locations;
261         //
262
ArrayList list = new ArrayList();
263         String JavaDoc sql = "SELECT * FROM C_BPartner_Location WHERE C_BPartner_ID=?";
264         PreparedStatement pstmt = null;
265         try
266         {
267             pstmt = DB.prepareStatement(sql);
268             pstmt.setInt(1, getC_BPartner_ID());
269             ResultSet rs = pstmt.executeQuery();
270             while (rs.next())
271                 list.add(new MBPartner_Location (getCtx(), rs));
272             rs.close();
273             pstmt.close();
274             pstmt = null;
275         }
276         catch (Exception JavaDoc e)
277         {
278             log.error("getLocations", e);
279         }
280         finally
281         {
282             try
283             {
284                 if (pstmt != null)
285                     pstmt.close ();
286             }
287             catch (Exception JavaDoc e)
288             {}
289             pstmt = null;
290         }
291
292         m_locations = new MBPartner_Location[list.size()];
293         list.toArray(m_locations);
294         return m_locations;
295     } // getLocations
296

297     /**
298      * Get Bank Accounts
299      * @return Bank Accounts
300      */

301     public MBP_BankAccount[] getBankAccounts()
302     {
303         if (m_accounts == null || m_accounts.length == 0) // re-load
304
;
305         else
306             return m_accounts;
307         //
308
ArrayList list = new ArrayList();
309         String JavaDoc sql = "SELECT * FROM C_BP_BankAccount WHERE C_BPartner_ID=?";
310         PreparedStatement pstmt = null;
311         try
312         {
313             pstmt = DB.prepareStatement(sql);
314             pstmt.setInt(1, getC_BPartner_ID());
315             ResultSet rs = pstmt.executeQuery();
316             while (rs.next())
317                 list.add(new MBP_BankAccount (getCtx(), rs));
318             rs.close();
319             pstmt.close();
320             pstmt = null;
321         }
322         catch (Exception JavaDoc e)
323         {
324             log.error("getBankAccounts", e);
325         }
326         finally
327         {
328             try
329             {
330                 if (pstmt != null)
331                     pstmt.close ();
332             }
333             catch (Exception JavaDoc e)
334             {}
335             pstmt = null;
336         }
337
338         m_accounts = new MBP_BankAccount[list.size()];
339         list.toArray(m_accounts);
340         return m_accounts;
341     } // getBankAccounts
342

343     /*************************************************************************/
344
345     /**
346      * String Representation
347      * @return info
348      */

349     public String JavaDoc toString ()
350     {
351         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ("MBPartner[ID=")
352             .append(getID())
353             .append(",Value=").append(getValue())
354             .append(",Name=").append(getName())
355             .append ("]");
356         return sb.toString ();
357     } // toString
358

359     /**
360      * Set Client/Org
361      * @param AD_Client_ID client
362      * @param AD_Org_ID org
363      */

364     public void setClientOrg (int AD_Client_ID, int AD_Org_ID)
365     {
366         setAD_Client_ID(AD_Client_ID);
367         setAD_Org_ID(AD_Org_ID);
368     } // setClientOrg
369

370 } // MBPartner
371
Popular Tags