KickJava   Java API By Example, From Geeks To Geeks.

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


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-2002 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.sql.*;
17 import java.util.*;
18
19 import org.compiere.util.*;
20
21 /**
22  * Customer Model
23  * Based on Defaults of
24  *
25  * @author Jorg Janke
26  * @version $Id: MCustomer.java,v 1.5 2003/11/06 07:08:06 jjanke Exp $
27  */

28 public class MCustomer
29 {
30
31     /**
32      * Get MCustomer
33      * @param EMail search criteris
34      * @param Password optional search criteria
35      * @return MCustomer if found or null
36      */

37     public static MCustomer get (String JavaDoc EMail, String JavaDoc Password)
38     {
39         int C_BPartner_ID = 0;
40         int AD_User_ID = 0;
41         //
42
String JavaDoc sql = "SELECT C_BPartner_ID, AD_User_ID "
43             + "FROM AD_User WHERE EMail=?";
44         if (Password != null)
45         sql += " AND Password=?";
46         try
47         {
48             PreparedStatement pstmt = DB.prepareStatement(sql);
49             pstmt.setString (1, EMail);
50             if (Password != null)
51                 pstmt.setString (2, Password);
52             ResultSet rs = pstmt.executeQuery();
53             while (rs.next())
54             {
55                 C_BPartner_ID = rs.getInt(1);
56                 AD_User_ID = rs.getInt(2);
57             }
58             rs.close();
59             pstmt.close();
60         }
61         catch (SQLException e)
62         {
63             s_log.error("get", e);
64             return null;
65         }
66         if (C_BPartner_ID == 0)
67             return null;
68
69         // Load it
70
MCustomer customer = new MCustomer (0);
71         customer.load(C_BPartner_ID, AD_User_ID);
72         return customer;
73     } // get
74

75
76
77     /**
78      * Constructor
79      * @param C_BPartner_ID
80      */

81     public MCustomer (int C_BPartner_ID)
82     {
83         load (C_BPartner_ID);
84     } // MCustomer
85

86     /** Logger */
87     private static Logger s_log = Logger.getCLogger(MCustomer.class);
88     /** Logger */
89     private Logger log = Logger.getCLogger(getClass());
90
91     /** AD_Message */
92     private String JavaDoc m_errorMessage = null;
93
94     private int C_BPartner_ID = 0;
95     private int AD_User_ID = 0;
96     private int C_BPartner_Location_ID = 0;
97     private int C_GreetingBP_ID = 0;
98     private int C_Greeting_ID = 0;
99     private int C_Location_ID = 0;
100
101     private String JavaDoc Value="", Name="", Name2="", Contact="", Title="",
102         Phone="", Fax="", Phone2="", EMail="", Password="";
103
104     private String JavaDoc Loc_Phone="", Loc_Phone2="", Loc_Fax="", BP_EMail="";
105
106     /**
107      * Load BPartner
108      * @para C_BPartner_ID - existing BPartner or 0 for new
109      */

110     private boolean load (int C_BPartner_ID)
111     {
112         this.C_BPartner_ID = C_BPartner_ID;
113         m_errorMessage = null;
114
115         // we have it already or new
116
if (C_BPartner_ID == this.C_BPartner_ID
117             || C_BPartner_ID == 0)
118             return true;
119
120         log.info("load - " + C_BPartner_ID);
121         String JavaDoc SQL = "SELECT p.Value,p.C_Greeting_ID AS C_GreetingBP_ID,p.Name,p.Name2,"
122             + "c.Name AS Contact,c.C_Greeting_ID,c.Title,COALESCE(c.EMail,p.EMail),c.Password,"
123             + "COALESCE(c.Phone,l.Phone) AS Phone, COALESCE(c.Phone2,l.Phone2) AS Phone2, COALESCE(c.Fax,l.Fax) AS Fax, "
124             + "l.C_Location_ID,c.AD_User_ID,l.C_BPartner_Location_ID "
125             + "FROM C_BPartner p, AD_User c, C_BPartner_Location l "
126             + "WHERE p.C_BPartner_ID=c.C_BPartner_ID(+)"
127             + " AND p.C_BPartner_ID=l.C_BPartner_ID(+)"
128             + " AND p.C_BPartner_ID=?" // 1
129
+ " ORDER BY l.IsBillTo Desc"; // BillTo first
130

131         try
132         {
133             PreparedStatement pstmt = DB.prepareStatement(SQL);
134             pstmt.setInt(1, C_BPartner_ID);
135             ResultSet rs = pstmt.executeQuery();
136             if (rs.next())
137                 load (rs);
138             else
139                 m_errorMessage = "BPartnerNotFound";
140             rs.close();
141             pstmt.close();
142         }
143         catch (SQLException e)
144         {
145             m_errorMessage = "BPartnerNotFound";
146             log.error("load", e);
147         }
148         if (m_errorMessage == null)
149             this.C_BPartner_ID = 0;
150         return m_errorMessage == null;
151     } // load
152

153     /**
154      * Load BPartner
155      * @para C_BPartner_ID - existing BPartner or 0 for new
156      */

157     private boolean load (int C_BPartner_ID, int AD_User_ID)
158     {
159         this.C_BPartner_ID = C_BPartner_ID;
160         m_errorMessage = null;
161
162         log.info("load - " + C_BPartner_ID + ", Contact=" + AD_User_ID);
163         String JavaDoc SQL = "SELECT p.Value,p.C_Greeting_ID AS C_GreetingBP_ID,p.Name,p.Name2, "
164             + "c.Name AS Contact,c.C_Greeting_ID,c.Title,COALESCE(c.EMail,p.EMail) AS EMail,c.Password, "
165             + "COALESCE(c.Phone,l.Phone) AS Phone, COALESCE(c.Phone2,l.Phone2) AS Phone2, COALESCE(c.Fax,l.Fax) AS Fax, "
166             + "l.C_Location_ID,c.AD_User_ID,l.C_BPartner_Location_ID "
167             + "FROM C_BPartner p, AD_User c, C_BPartner_Location l "
168             + "WHERE p.C_BPartner_ID=c.C_BPartner_ID(+)"
169             + " AND p.C_BPartner_ID=l.C_BPartner_ID(+)"
170             + " AND p.C_BPartner_ID=?" // #1
171
+ " AND AD_User_ID=?" // #2
172
+ " ORDER BY l.IsBillTo Desc"; // BillTo first
173

174         try
175         {
176             PreparedStatement pstmt = DB.prepareStatement(SQL);
177             pstmt.setInt(1, C_BPartner_ID);
178             pstmt.setInt(2, AD_User_ID);
179             ResultSet rs = pstmt.executeQuery();
180             if (rs.next())
181                 load (rs);
182             else
183                 m_errorMessage = "BPartnerNotFound";
184             rs.close();
185             pstmt.close();
186         }
187         catch (SQLException e)
188         {
189             m_errorMessage = "BPartnerNotFound";
190             log.error("load", e);
191         }
192         return m_errorMessage == null;
193     } // load
194

195
196     /**
197      * Load BPartner
198      * @para C_BPartner_ID - existing BPartner or 0 for new
199      */

200     private void load (ResultSet rs) throws SQLException
201     {
202         setValue (rs.getString("Value"));
203         setC_GreetingBP_ID (rs.getInt("C_GreetingBP_ID"));
204         setName (rs.getString("Name"));
205         setName2 (rs.getString("Name2"));
206         setC_Greeting_ID (rs.getInt("C_Greeting_ID"));
207         setContact (rs.getString("Contact"));
208         setTitle (rs.getString("Title"));
209         setPhone (rs.getString("Phone"));
210         setPhone2 (rs.getString("Phone2"));
211         setFax (rs.getString("Fax"));
212         setEMail (rs.getString("EMail"));
213         setPassword (rs.getString("Password"));
214         setC_Location_ID (rs.getInt("C_Location_ID"));
215         setAD_User_ID (rs.getInt("AD_User_ID"));
216         setC_BPartner_Location_ID (rs.getInt("C_BPartner_Location_ID"));
217         //
218
Loc_Phone = Phone;
219         Loc_Phone2 = Phone2;
220         Loc_Fax = Fax;
221         BP_EMail = EMail;
222     } // load
223

224     /**
225      * Save
226      * Checks mandatory fields and saves Partner, Contact and Location
227      */

228     private boolean save (Properties ctx)
229     {
230         log.info("save " + C_BPartner_ID);
231
232         m_errorMessage = null;
233
234         // Check Mandatory fields
235
if (Value == null)
236             Value = Name;
237         if (Name == null || Value.length() == 0 || Name.length() == 0)
238         {
239             m_errorMessage = "FillMandatory";
240             return false;
241         }
242
243         String JavaDoc SQL = "";
244         try
245         {
246             Statement stmt = DB.createStatement();
247             int AD_Client_ID = Integer.parseInt(Env.getContext(ctx, "#AD_Client_ID"));
248             int AD_Org_ID = Integer.parseInt(Env.getContext(ctx, "#AD_Org_ID"));
249             int UpdatedBy = Integer.parseInt(Env.getContext(ctx, "#AD_User_ID"));
250
251             // === Insert Business Partner ===
252
if (C_BPartner_ID == 0)
253             {
254                 C_BPartner_ID = DB.getKeyNextNo(AD_Client_ID, "C_BPartner");
255                 log.debug("save - Insert BPartner " + C_BPartner_ID);
256                 String JavaDoc gr = "NULL";
257                 if (C_GreetingBP_ID > 0)
258                     gr = String.valueOf(C_GreetingBP_ID);
259                 SQL = "INSERT INTO C_BPartner"
260                     + "(C_BPartner_ID,"
261                     + "AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy,"
262                     + "Value,Name,Name2,C_Greeting_ID,IsSummary,IsOneTime,IsProspect,IsVendor,IsCustomer,IsEmployee,IsSalesRep,"
263                     + "AD_Language,C_BP_Group_ID,C_InvoiceSchedule_ID,C_PaymentTerm_ID,"
264                     + "M_PriceList_ID,M_DiscountSchema_ID,C_Dunning_ID,DocumentCopies,PaymentRule,"
265                     + "SO_CreditLimit, SO_CreditUsed, EMail) "
266                     //
267
+ "SELECT " + C_BPartner_ID + ","
268                     + AD_Client_ID + "," + AD_Org_ID + ",'Y',SysDate," + UpdatedBy + ",SysDate," + UpdatedBy + ","
269                     + "'" + Value + "','" + Name + "','" + Name2 + "'," + gr + ",'N','N','N','N','Y','N','N',"
270                     + "AD_Language,C_BP_Group_ID,C_InvoiceSchedule_ID,C_PaymentTerm_ID,"
271                     + "M_PriceList_ID,M_DiscountSchema_ID,C_Dunning_ID,DocumentCopies,PaymentRule,"
272                     + "0,0,'" + EMail + "' "
273                     + "FROM C_BPartner p, AD_ClientInfo c "
274                     + "WHERE c.AD_Client_ID=" + AD_Client_ID
275                     + " AND p.C_BPartner_ID(+)=c.C_BPartnerCashTrx_ID";
276                 int i = stmt.executeUpdate(SQL);
277                 if (i != 1)
278                 {
279                     m_errorMessage = "BPartnerNotSaved";
280                     log.error("Partner Insert #=" + i + ", SQL=" + SQL);
281                     return false;
282                 }
283             }
284             // === Update Business Partner === (value/greeting/name/name2/email)
285
else
286             {
287                 log.debug("save - Update BPartner " + C_BPartner_ID);
288                 String JavaDoc gr = "NULL";
289                 if (C_GreetingBP_ID > 0)
290                     gr = String.valueOf(C_GreetingBP_ID);
291                 SQL = "UPDATE C_BPartner SET "
292                     + "Value='" + Value + "',"
293                     + "C_Greeting_ID=" + gr + ","
294                     + "Name='" + Name + "',"
295                     + "Name2='" + Name2 + "',"
296                     + "EMail='" + EMail + "',"
297                     + "Updated=SysDate, UpdatedBy=" + UpdatedBy
298                     + " WHERE C_BPartner_ID=" + C_BPartner_ID;
299                 int i = stmt.executeUpdate(SQL);
300                 if (i != 1)
301                 {
302                     m_errorMessage = "BPartnerNotSaved";
303                     log.error ("Partner Update #=" + i + ", SQL=" + SQL);
304                     return false;
305                 }
306             }
307
308             // === Insert New BPartner_Location ===
309
if (C_BPartner_Location_ID == 0 && C_Location_ID != 0)
310             {
311                 C_BPartner_Location_ID = DB.getKeyNextNo(AD_Client_ID, "C_BPartner_Location");
312                 log.debug("Insert BPartner Location " + C_BPartner_Location_ID + " (" + C_Location_ID + ")");
313                 SQL = "INSERT INTO C_BPartner_Location"
314                     + "(C_BPartner_Location_ID, C_BPartner_ID,"
315                     + "AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy,"
316                     + "Name, C_Location_ID, Phone, Phone2, Fax)"
317                     + " VALUES "
318                     + "(" + C_BPartner_Location_ID + "," + C_BPartner_ID + ","
319                     + AD_Client_ID + "," + AD_Org_ID + ",'Y',SysDate," + UpdatedBy + ",SysDate," + UpdatedBy + ","
320                     + "'.'," + C_Location_ID + ",'" + Phone + "','" + Phone
321                     + "','" + Fax + "')";
322                 int i = stmt.executeUpdate(SQL);
323                 if (i != 1)
324                 {
325                     m_errorMessage = "BPartnerNotSaved";
326                     log.error ("Location Insert #=" + i + ", SQL=" + SQL);
327                     return false;
328                 }
329             }
330             // === Update BPartner_Location ===
331
else if (C_BPartner_Location_ID != 0)
332             {
333                 log.debug("Update BPartner Location " + C_BPartner_Location_ID);
334                 SQL = "UPDATE C_BPartner_Location SET "
335                     + "C_Location_ID=" + C_Location_ID + ","
336                     + "Phone='" + Phone + "'," // ?? ok for one contact
337
+ "Phone2='" + Phone2 + "'," // ??
338
+ "Fax='" + Fax + "'," // ??
339
+ "Updated=SysDate, UpdatedBy=" + UpdatedBy
340                     + " WHERE C_BPartner_Location_ID=" + C_BPartner_Location_ID;
341                 int i = stmt.executeUpdate(SQL);
342                 if (i != 1)
343                 {
344                     m_errorMessage = "BPartnerNotSaved";
345                     log.error ("Location Update #=" + i + ", SQL=" + SQL);
346                     return false;
347                 }
348             }
349
350             // === Insert new BPartner_Contact === only if contact entered
351
if (AD_User_ID == 0 && Contact.length() != 0)
352             {
353                 AD_User_ID = DB.getKeyNextNo (AD_Client_ID, "AD_User");
354                 log.debug("Insert BPartner User/Contact " + AD_User_ID);
355                 String JavaDoc gr = "NULL";
356                 if (C_Greeting_ID > 0)
357                     gr = String.valueOf(C_Greeting_ID);
358                 SQL = "INSERT INTO AD_User"
359                     + "(AD_User_ID, C_BPartner_ID,"
360                     + "AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy,"
361                     + "Name, C_Greeting_ID, Title, Phone, Phone2, Fax, EMail, Password)"
362                     + " VALUES "
363                     + "(" + AD_User_ID + "," + C_BPartner_ID + ","
364                     + AD_Client_ID + "," + AD_Org_ID + ",'Y',SysDate," + UpdatedBy + ",SysDate," + UpdatedBy + ","
365                     + "'" + Contact + "'," + gr + ",'" + Title + "','" + Phone + "','" + Phone2 + "','"
366                     + Fax + "','" + EMail + "','" + Password +"')";
367                 int i = stmt.executeUpdate(SQL);
368                 if (i != 1)
369                 {
370                     m_errorMessage = "BPartnerNotSaved";
371                     log.error ("Contact Insert #=" + i + ", SQL=" + SQL);
372                     return false;
373                 }
374             }
375             // === Update BPartner_Contact ===
376
else if (AD_User_ID != 0)
377             {
378                 log.debug("Update BPartner User/Contact " + AD_User_ID);
379                 // We need to have a Name
380
if (Contact.length() == 0)
381                     Contact = Name;
382                 String JavaDoc gr = "NULL";
383                 if (C_Greeting_ID > 0)
384                     gr = String.valueOf(C_Greeting_ID);
385                 SQL = "UPDATE AD_User SET "
386                     + "Name='" + Contact + "',"
387                     + "C_Greeting_ID=" + gr + ","
388                     + "Title='" + Title + "',"
389                     + "Phone='" + Phone + "',"
390                     + "Phone2='" + Phone2 + "',"
391                     + "Fax='" + Fax + "',"
392                     + "EMail='" + EMail + "',"
393                     + "Password='" + Password + "',"
394                     // location ??
395
+ "Updated=SysDate, UpdatedBy=" + UpdatedBy
396                     + " WHERE AD_User_ID=" + AD_User_ID;
397                 int i = stmt.executeUpdate(SQL);
398                 if (i != 1)
399                 {
400                     m_errorMessage = "BPartnerNotSaved";
401                     log.error ("Contact Update #=" + i + ", SQL=" + SQL);
402                     return false;
403                 }
404             }
405         }
406         catch (SQLException e)
407         {
408             m_errorMessage = "BPartnerNotSaved";
409             log.error("save " + SQL, e);
410             C_BPartner_ID = 0;
411         }
412         return m_errorMessage == null;
413     } // save
414

415     public String JavaDoc getErrorMessage()
416     {
417         return m_errorMessage;
418     }
419
420     /*************************************************************************/
421
422     public int getAD_User_ID()
423     {
424         return AD_User_ID;
425     }
426     public void setAD_User_ID(int AD_User_ID)
427     {
428         this.AD_User_ID = AD_User_ID;
429     }
430     public int getC_BPartner_ID()
431     {
432         return C_BPartner_ID;
433     }
434     public void setC_BPartner_ID(int C_BPartner_ID)
435     {
436         this.C_BPartner_ID = C_BPartner_ID;
437     }
438     public int getC_BPartner_Location_ID()
439     {
440         return C_BPartner_Location_ID;
441     }
442     public void setC_BPartner_Location_ID(int C_BPartner_Location_ID)
443     {
444         this.C_BPartner_Location_ID = C_BPartner_Location_ID;
445     }
446     public int getC_Greeting_ID()
447     {
448         return C_Greeting_ID;
449     }
450     public void setC_Greeting_ID(int C_Greeting_ID)
451     {
452         this.C_Greeting_ID = C_Greeting_ID;
453     }
454     public int getC_GreetingBP_ID()
455     {
456         return C_GreetingBP_ID;
457     }
458     public void setC_GreetingBP_ID(int C_GreetingBP_ID)
459     {
460         this.C_GreetingBP_ID = C_GreetingBP_ID;
461     }
462     public int getC_Location_ID()
463     {
464         return C_Location_ID;
465     }
466     public void setC_Location_ID(int C_Location_ID)
467     {
468         this.C_Location_ID = C_Location_ID;
469     }
470     //
471
public String JavaDoc getContact()
472     {
473         return Contact;
474     }
475     public void setContact(String JavaDoc Contact)
476     {
477         if (Contact != null)
478             this.Contact = Contact;
479     }
480     public String JavaDoc getEMail()
481     {
482         return EMail;
483     }
484     public void setEMail(String JavaDoc EMail)
485     {
486         if (EMail != null)
487             this.EMail = EMail;
488     }
489     public String JavaDoc getPassword()
490     {
491         return Password;
492     }
493     public void setPassword(String JavaDoc Password)
494     {
495         if (Password != null)
496             this.Password = Password;
497     }
498     public String JavaDoc getFax()
499     {
500         return Fax;
501     }
502     public void setFax(String JavaDoc Fax)
503     {
504         if (Fax != null)
505             this.Fax = Fax;
506     }
507     public String JavaDoc getName()
508     {
509         return Name;
510     }
511     public void setName(String JavaDoc Name)
512     {
513         if (Name != null)
514             this.Name = Name;
515     }
516     public String JavaDoc getName2()
517     {
518         return Name2;
519     }
520     public void setName2(String JavaDoc Name2)
521     {
522         if (Name2 != null)
523             this.Name2 = Name2;
524     }
525     public String JavaDoc getPhone()
526     {
527         return Phone;
528     }
529     public void setPhone(String JavaDoc Phone)
530     {
531         if (Phone != null)
532             this.Phone = Phone;
533     }
534     public String JavaDoc getPhone2()
535     {
536         return Phone2;
537     }
538     public void setPhone2(String JavaDoc Phone2)
539     {
540         if (Phone2 != null)
541             this.Phone2 = Phone2;
542     }
543     public String JavaDoc getTitle()
544     {
545         return Title;
546     }
547     public void setTitle(String JavaDoc Title)
548     {
549         if (Title != null)
550             this.Title = Title;
551     }
552     public String JavaDoc getValue()
553     {
554         return Value;
555     }
556     public void setValue(String JavaDoc Value)
557     {
558         if (Value != null)
559             this.Value = Value;
560     }
561
562 } // MCustomer
563
Popular Tags