KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > util > EMailUtil


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

14 package org.compiere.util;
15
16 import java.sql.*;
17 import java.util.*;
18
19 /**
20  * Mail Utilities
21  *
22  * @author Jorg Janke
23  * @version $Id: EMailUtil.java,v 1.1 2003/10/11 05:20:32 jjanke Exp $
24  */

25 public class EMailUtil
26 {
27     /** Log */
28     private static Logger s_log = Logger.getCLogger(EMailUtil.class);
29     
30     /**
31      * Get the EMail Address of current user or request
32      * @param ctx Context
33      * @param strict no bogous email address
34      * @return EMail Address
35      */

36     public static String JavaDoc getEMail (Properties ctx, boolean strict)
37     {
38         String JavaDoc from = Env.getContext(ctx, EMail.CTX_EMAIL);
39         if (from.length() != 0)
40             return from;
41
42         // Current User
43
int AD_User_ID = Env.getContextAsInt (ctx, "#AD_User_ID");
44         if (AD_User_ID != 0)
45             from = getEMail_User(AD_User_ID, strict, ctx);
46         // Request
47
if (from == null || from.length() == 0)
48             from = getEMail_Request (ctx);
49         // Bogus
50
if (from == null || from.length() == 0)
51         {
52             if (strict)
53                 return null;
54             from = getEMail_Bogus(ctx);
55         }
56         return from;
57     } // getEMail
58

59     
60     /**
61      * Get Email Address from Business Partner (Contact)
62      * @param C_BPartner_ID Business Partner
63      * @param AD_User_ID Contact (optional)
64      * @return EMail Address or null
65      */

66     public static String JavaDoc getEMail_BPartner (int C_BPartner_ID, int AD_User_ID)
67     {
68         String JavaDoc email = null;
69         String JavaDoc sql = "SELECT bpc.EMail "
70             + "FROM C_BPartner bp"
71             + " INNER JOIN AD_User bpc ON (bp.C_BPartner_ID=bpc.C_BPartner_ID) "
72             + "WHERE bp.C_BPartner_ID=?";
73         if (AD_User_ID != 0)
74             sql += " AND bpc.AD_User_ID=?";
75         try
76         {
77             PreparedStatement pstmt = DB.prepareStatement(sql);
78             pstmt.setInt(1, C_BPartner_ID);
79             if (AD_User_ID != 0)
80                 pstmt.setInt(2, AD_User_ID);
81             ResultSet rs = pstmt.executeQuery();
82             if (rs.next())
83                 email = rs.getString(1);
84             else
85                 s_log.warn("getEMail_BPartner - None for C_BPartner_ID=" + C_BPartner_ID + ", AD_User_ID=" + AD_User_ID);
86             rs.close();
87             pstmt.close();
88         }
89         catch (SQLException e)
90         {
91             s_log.error("getEMail_BPartner", e);
92         }
93         return email;
94     } // getEMail_BPartner
95

96
97     /**
98      * Get Email Address of AD_User
99      * @param AD_User_ID user
100      * @param strict no bogous email address
101      * @param ctx optional context
102      * @return EMail Address
103      */

104     public static String JavaDoc getEMail_User (int AD_User_ID, boolean strict, Properties ctx)
105     {
106         String JavaDoc email = null;
107         // Get ID
108
String JavaDoc sql = "SELECT EMail, EMailUser, EMailUserPw, Name "
109             + "FROM AD_User "
110             + "WHERE AD_User_ID=?";
111         try
112         {
113             PreparedStatement pstmt = DB.prepareStatement(sql);
114             pstmt.setInt(1, AD_User_ID);
115             ResultSet rs = pstmt.executeQuery();
116             if (rs.next())
117             {
118                 email = rs.getString(1);
119                 if (email != null)
120                 {
121                     email = cleanUpEMail(email);
122                     if (ctx != null)
123                     {
124                         Env.setContext (ctx, EMail.CTX_EMAIL, email);
125                         Env.setContext (ctx, EMail.CTX_EMAIL_USER, rs.getString (2));
126                         Env.setContext (ctx, EMail.CTX_EMAIL_USERPW, rs.getString (3));
127                     }
128                 }
129             }
130             rs.close();
131             pstmt.close();
132         }
133         catch (SQLException e)
134         {
135             s_log.error("getEMail_User - " + sql, e);
136         }
137         if (email == null || email.length() == 0)
138         {
139             s_log.warn("getEMail_User - EMail not found - AD_User_ID=" + AD_User_ID);
140             if (strict)
141                 return null;
142             email = getEMail_Bogus(ctx == null ? Env.getCtx() : ctx);
143         }
144         return email;
145     } // getEMail_User
146

147     /**
148      * Get Email Address of AD_User
149      * @param AD_User_ID user
150      * @return EMail Address
151      */

152     public static String JavaDoc getEMail_User (int AD_User_ID)
153     {
154         return getEMail_User(AD_User_ID, false, null);
155     } // getEMail_User
156

157     /**
158      * Get Email Address current AD_User
159      * @param ctx Context
160      * @param strict no bogous email address
161      * @return EMail Address
162      */

163     public static String JavaDoc getEMail_User (Properties ctx, boolean strict)
164     {
165         String JavaDoc from = Env.getContext(ctx, EMail.CTX_EMAIL);
166         if (from.length() != 0)
167             return from;
168
169         int AD_User_ID = Env.getContextAsInt (ctx, "#AD_User_ID");
170         from = getEMail_User(AD_User_ID, strict, ctx);
171         return from;
172     } // getEMail_User
173

174     /**
175      * Construct Bogos email
176      * @param ctx Context
177      * @return userName.ClientName.com
178      */

179     public static String JavaDoc getEMail_Bogus (Properties ctx)
180     {
181         String JavaDoc email = System.getProperty("user.name") + "@"
182             + Env.getContext(ctx, "#AD_Client_Name") + ".com";
183         email = cleanUpEMail(email);
184         return email;
185     } // getBogusEMail
186

187     /**
188      * Get Client Request EMail
189      * @param ctx Context
190      * @return Request EMail Address
191      */

192     public static String JavaDoc getEMail_Request (Properties ctx)
193     {
194         String JavaDoc email = Env.getContext(ctx, EMail.CTX_REQUEST_EMAIL);
195         if (email.length() != 0)
196             return email;
197
198         String JavaDoc sql = "SELECT RequestEMail, RequestUser, RequestUserPw "
199             + "FROM AD_Client "
200             + "WHERE AD_Client_ID=?";
201         PreparedStatement pstmt = null;
202         try
203         {
204             pstmt = DB.prepareStatement(sql);
205             pstmt.setInt(1, Env.getContextAsInt(ctx, "#AD_Client_ID"));
206             ResultSet rs = pstmt.executeQuery();
207             if (rs.next())
208             {
209                 email = rs.getString (1);
210                 email = cleanUpEMail(email);
211                 Env.setContext(ctx, EMail.CTX_REQUEST_EMAIL, email);
212                 Env.setContext(ctx, EMail.CTX_REQUEST_EMAIL_USER, rs.getString(2));
213                 Env.setContext(ctx, EMail.CTX_REQUEST_EMAIL_USERPW, rs.getString(3));
214             }
215             rs.close();
216             pstmt.close();
217             pstmt = null;
218         }
219         catch (Exception JavaDoc e)
220         {
221             s_log.error("getRequestEMail", e);
222         }
223         finally
224         {
225             try
226             {
227                 if (pstmt != null)
228                     pstmt.close ();
229             }
230             catch (Exception JavaDoc e)
231             {}
232             pstmt = null;
233         }
234         return email;
235     } // getRequestEMail
236

237
238     /*************************************************************************/
239
240     /**
241      * Clean up EMail address
242      * @param email email address
243      * @return lower case email w/o spaces
244      */

245     private static String JavaDoc cleanUpEMail (String JavaDoc email)
246     {
247         if (email == null || email.length() == 0)
248             return "";
249         //
250
email = email.trim().toLowerCase();
251         // Delete all spaces
252
int pos = email.indexOf(" ");
253         while (pos != -1)
254         {
255             email = email.substring(0, pos) + email.substring(pos+1);
256             pos = email.indexOf(" ");
257         }
258         return email;
259     } // cleanUpEMail
260

261
262     /**
263      * Get Name of AD_User
264      * @param AD_User_ID System User
265      * @return Name of user
266      */

267     public static String JavaDoc getNameOfUser (int AD_User_ID)
268     {
269         String JavaDoc name = null;
270         // Get ID
271
String JavaDoc sql = "SELECT Name FROM AD_User WHERE AD_User_ID=?";
272         try
273         {
274             PreparedStatement pstmt = DB.prepareStatement(sql);
275             pstmt.setInt(1, AD_User_ID);
276             ResultSet rs = pstmt.executeQuery();
277             if (rs.next())
278                 name = rs.getString(1);
279             rs.close();
280             pstmt.close();
281         }
282         catch (SQLException e)
283         {
284             s_log.error("getNameOfUser", e);
285         }
286         return name;
287     } // getNameOfUser
288

289
290
291     /**************************************************************************
292      * Get the current Client EMail SMTP Host
293      * @param ctx Context
294      * @return Mail Host
295      */

296     public static String JavaDoc getSmtpHost (Properties ctx)
297     {
298         String JavaDoc SMTP = Env.getContext(ctx, EMail.CTX_SMTP);
299         if (SMTP.length() != 0)
300             return SMTP;
301         // Get SMTP name
302
SMTP = getSmtpHost (Env.getContextAsInt(ctx, "#AD_Client_ID"));
303         if (SMTP == null)
304             SMTP = "localhost";
305         Env.setContext(ctx, EMail.CTX_SMTP, SMTP);
306         return SMTP;
307     } // getCurrentSmtpHost
308

309     /**
310      * Get SMTP Host of Client
311      * @param AD_Client_ID Client
312      * @return Mail Host
313      */

314     public static String JavaDoc getSmtpHost (int AD_Client_ID)
315     {
316         String JavaDoc SMTP = null;
317         String JavaDoc sql = "SELECT SMTPHost FROM AD_Client "
318             + "WHERE AD_Client_ID=?";
319         try
320         {
321             PreparedStatement pstmt = DB.prepareStatement(sql);
322             pstmt.setInt(1, AD_Client_ID);
323             ResultSet rs = pstmt.executeQuery();
324             if (rs.next())
325                 SMTP = rs.getString(1);
326             rs.close();
327             pstmt.close();
328         }
329         catch (SQLException e)
330         {
331             s_log.error ("getSmtpHost", e);
332         }
333         //
334
return SMTP;
335     } // getCurrentSMTPHost
336

337 } // EMailUtil
338
Popular Tags