KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > process > SendMailText


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.process;
15
16 import java.sql.*;
17 import java.math.*;
18
19 import org.compiere.util.*;
20 import org.compiere.model.*;
21
22 /**
23  * Send Mail to Interest Area Subscribers
24  *
25  * @author Jorg Janke
26  * @version $Id: SendMailText.java,v 1.5 2003/10/08 03:36:21 jjanke Exp $
27  */

28 public class SendMailText extends SvrProcess
29 {
30     /** What to send */
31     private int m_R_MailText_ID = -1;
32     /** Mail Text */
33     private MMailText m_MailText = null;
34
35     /** From (sender) */
36     private int m_AD_User_ID = -1;
37     /** Client Info */
38     private MClient m_Client = null;
39     /** User Info */
40     private MUser m_User = null;
41     /** From */
42     private String JavaDoc m_from = null;
43     private String JavaDoc m_fromID = null;
44     private String JavaDoc m_fromPW = null;
45
46     
47     /** To Subscribers */
48     private int m_R_InterestArea_ID = -1;
49     /** To Customer Type */
50     private int m_C_BP_Group_ID = -1;
51     /** To Purchaser of Product */
52     // comes here
53

54
55     /**
56      * Prepare - e.g., get Parameters.
57      */

58     protected void prepare()
59     {
60         ProcessInfoParameter[] para = getParameter();
61         for (int i = 0; i < para.length; i++)
62         {
63             String JavaDoc name = para[i].getParameterName();
64             if (para[i].getParameter() == null)
65                 ;
66             else if (name.equals("R_InterestArea_ID"))
67                 m_R_InterestArea_ID = ((BigDecimal)para[i].getParameter()).intValue();
68             else if (name.equals("R_MailText_ID"))
69                 m_R_MailText_ID = ((BigDecimal)para[i].getParameter()).intValue();
70             else if (name.equals("C_BP_Group_ID"))
71                 m_C_BP_Group_ID = ((BigDecimal)para[i].getParameter()).intValue();
72             else if (name.equals("AD_User_ID"))
73                 m_AD_User_ID = ((BigDecimal)para[i].getParameter()).intValue();
74             else
75                 log.error("prepare - Unknown Parameter: " + name);
76         }
77     } // prepare
78

79     /**
80      * Perrform process.
81      * @return Message
82      * @throws Exception
83      */

84     protected String JavaDoc doIt() throws Exception JavaDoc
85     {
86         log.info("doIt - R_MailText_ID=" + m_R_MailText_ID);
87         // Mail Test
88
m_MailText = new MMailText (getCtx(), m_R_MailText_ID);
89         if (m_MailText.getR_MailText_ID() == 0)
90             throw new Exception JavaDoc ("Not found @R_MailText_ID@=" + m_R_MailText_ID);
91         // Client Info
92
m_Client = MClient.get (getCtx());
93         if (m_Client.getAD_Client_ID() == 0)
94             throw new Exception JavaDoc ("Not found @AD_Client_ID@");
95         if (m_Client.getSMTPHost() == null || m_Client.getSMTPHost().length() == 0)
96             throw new Exception JavaDoc ("No SMTP Host found");
97         //
98
if (m_AD_User_ID != -1)
99         {
100             m_User = new MUser (getCtx(), m_AD_User_ID);
101             if (m_User.getAD_User_ID() == 0)
102                 throw new Exception JavaDoc ("No found @AD_User_ID@=" + m_AD_User_ID);
103         }
104         m_from = m_User == null ? m_Client.getRequestEMail() : m_User.getEmail();
105         m_fromID = m_User == null ? m_Client.getRequestUser() : m_User.getEmailUser();
106         m_fromPW = m_User == null ? m_Client.getRequestUserPW() : m_User.getEmailUserPW();
107         if (m_from == null || m_from.length() == 0 || m_fromID == null || m_fromID.length() == 0 || m_fromPW == null || m_fromPW.length() == 0)
108             throw new Exception JavaDoc ("From EMail not complete - " + m_from + "(" + m_fromID + "/" + m_fromPW + ")");
109         log.debug("doIt - send from " + m_from + "(" + m_fromID + "/" + m_fromPW + ")");
110
111         long start = System.currentTimeMillis();
112         // Loop to Interest Area Subscribers *******************************
113
log.info("doIt - Send to R_InterestArea_ID=" + m_R_InterestArea_ID);
114         String JavaDoc sql = "SELECT u.Name, u.EMail, u.AD_User_ID "
115             + "FROM R_ContactInterest ci"
116             + " INNER JOIN AD_User u ON (ci.AD_User_ID=u.AD_User_ID) "
117             + "WHERE ci.IsActive='Y' AND u.IsActive='Y'"
118             + " AND ci.OptOutDate IS NULL"
119             + " AND u.EMail IS NOT NULL"
120             + " AND ci.R_InterestArea_ID=?";
121         PreparedStatement pstmt = null;
122         int counter = 0;
123         int errors = 0;
124         try
125         {
126             pstmt = DB.prepareStatement(sql);
127             pstmt.setInt(1, m_R_InterestArea_ID);
128             ResultSet rs = pstmt.executeQuery();
129             while (rs.next())
130             {
131                 if (sendIndividualMail (rs.getString(1), rs.getString(2), rs.getInt(3)))
132                     counter++;
133                 else
134                     errors++;
135             }
136             rs.close();
137             pstmt.close();
138             pstmt = null;
139         }
140         catch (SQLException ex)
141         {
142             log.error("doIt - InterestArea", ex);
143         }
144         // Loop to Interest Area Subscribers *******************************
145
log.info("doIt - Send to C_BP_Group_ID=" + m_C_BP_Group_ID);
146         sql = "SELECT u.Name, u.EMail, u.AD_User_ID "
147             + "FROM AD_User u"
148             + " INNER JOIN C_BPartner bp ON (u.C_BPartner_ID=bp.C_BPartner_ID) "
149             + "WHERE u.IsActive='Y' AND bp.IsActive='Y'"
150             + " AND u.EMail IS NOT NULL"
151             + " AND bp.C_BP_Group_ID=?";
152         try
153         {
154             pstmt = DB.prepareStatement(sql);
155             pstmt.setInt(1, m_C_BP_Group_ID);
156             ResultSet rs = pstmt.executeQuery();
157             while (rs.next())
158             {
159                 if (sendIndividualMail (rs.getString(1), rs.getString(2), rs.getInt(3)))
160                     counter++;
161                 else
162                     errors++;
163             }
164             rs.close();
165             pstmt.close();
166             pstmt = null;
167         }
168         catch (SQLException ex)
169         {
170             log.error("doIt - BP_Group", ex);
171         }
172         // Cleanup
173
try
174         {
175             if (pstmt != null)
176                 pstmt.close();
177         }
178         catch (SQLException ex1)
179         {
180         }
181         pstmt = null;
182
183         return "@Created@=" + counter + ", @Errors@=" + errors + " - "
184             + (System.currentTimeMillis()-start) + "ms";
185     } // doIt
186

187     /**
188      * Send Individual Mail
189      * @param Name name
190      * @param EMailAddress email
191      * @return true if mail has been sent
192      */

193     private boolean sendIndividualMail (String JavaDoc Name, String JavaDoc EMailAddress, int AD_User_ID)
194     {
195         String JavaDoc subject = m_MailText.getMailHeader();
196         String JavaDoc message = m_MailText.getMailText();
197         //
198
EMail mail = new EMail (m_Client.getSMTPHost(), m_from, EMailAddress, subject, message);
199         if (!mail.isValid())
200         {
201             log.warn("sendIndividualMail NOT VALID - " + EMailAddress);
202             deactivate (AD_User_ID);
203             return false;
204         }
205         mail.setEMailUser(m_fromID, m_fromPW);
206         boolean OK = EMail.SENT_OK.equals(mail.send());
207         if (OK)
208             log.debug("sendIndividualMail - " + EMailAddress);
209         else
210             log.warn("sendIndividualMail FAILURE - " + EMailAddress);
211         addLog(0, null, null, (OK ? "OK" : "ERROR") + " - " + EMailAddress);
212         return OK;
213     } // sendIndividualMail
214

215     /**
216      * Deactivate Invalid User ID
217      * @param AD_User_ID
218      */

219     private void deactivate (int AD_User_ID)
220     {
221         String JavaDoc sql = "UPDATE AD_User "
222             + "SET IsActive='N', Help=Help||' - Invalid User EMail' "
223             + "WHERE AD_User_ID=" + AD_User_ID;
224         DB.executeUpdate(sql);
225     } // deactivate
226

227 } // SendMailText
228
Popular Tags