1 14 package org.compiere.util; 15 16 import java.sql.*; 17 import java.util.*; 18 19 25 public class EMailUtil 26 { 27 28 private static Logger s_log = Logger.getCLogger(EMailUtil.class); 29 30 36 public static String getEMail (Properties ctx, boolean strict) 37 { 38 String from = Env.getContext(ctx, EMail.CTX_EMAIL); 39 if (from.length() != 0) 40 return from; 41 42 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 if (from == null || from.length() == 0) 48 from = getEMail_Request (ctx); 49 if (from == null || from.length() == 0) 51 { 52 if (strict) 53 return null; 54 from = getEMail_Bogus(ctx); 55 } 56 return from; 57 } 59 60 66 public static String getEMail_BPartner (int C_BPartner_ID, int AD_User_ID) 67 { 68 String email = null; 69 String 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 } 96 97 104 public static String getEMail_User (int AD_User_ID, boolean strict, Properties ctx) 105 { 106 String email = null; 107 String 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 } 147 152 public static String getEMail_User (int AD_User_ID) 153 { 154 return getEMail_User(AD_User_ID, false, null); 155 } 157 163 public static String getEMail_User (Properties ctx, boolean strict) 164 { 165 String 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 } 174 179 public static String getEMail_Bogus (Properties ctx) 180 { 181 String email = System.getProperty("user.name") + "@" 182 + Env.getContext(ctx, "#AD_Client_Name") + ".com"; 183 email = cleanUpEMail(email); 184 return email; 185 } 187 192 public static String getEMail_Request (Properties ctx) 193 { 194 String email = Env.getContext(ctx, EMail.CTX_REQUEST_EMAIL); 195 if (email.length() != 0) 196 return email; 197 198 String 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 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 e) 231 {} 232 pstmt = null; 233 } 234 return email; 235 } 237 238 239 240 245 private static String cleanUpEMail (String email) 246 { 247 if (email == null || email.length() == 0) 248 return ""; 249 email = email.trim().toLowerCase(); 251 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 } 261 262 267 public static String getNameOfUser (int AD_User_ID) 268 { 269 String name = null; 270 String 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 } 289 290 291 296 public static String getSmtpHost (Properties ctx) 297 { 298 String SMTP = Env.getContext(ctx, EMail.CTX_SMTP); 299 if (SMTP.length() != 0) 300 return SMTP; 301 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 } 309 314 public static String getSmtpHost (int AD_Client_ID) 315 { 316 String SMTP = null; 317 String 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 return SMTP; 335 } 337 } | Popular Tags |