KickJava   Java API By Example, From Geeks To Geeks.

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


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.process;
15
16 import java.io.*;
17 import java.sql.*;
18 import java.math.*;
19 import java.net.*;
20
21 import org.compiere.model.*;
22 import org.compiere.util.DB;
23 import org.compiere.util.EMail;
24
25 /**
26  * Deliver Assets Electronically
27  *
28  * @author Jorg Janke
29  * @version $Id: AssetDelivery.java,v 1.11 2003/10/31 05:28:35 jjanke Exp $
30  */

31 public class AssetDelivery extends SvrProcess
32 {
33     /**
34      * Constructor
35      */

36     public AssetDelivery()
37     {
38         super();
39     } // AssetDelivery
40

41     private MClient m_client = null;
42
43     private int m_A_Asset_Group_ID = 0;
44     private int m_M_Product_ID = 0;
45     private int m_C_BPartner_ID = 0;
46     private int m_A_Asset_ID = 0;
47     private Timestamp m_GuaranteeDate = null;
48     private int m_NoGuarantee_MailText_ID = 0;
49     private boolean m_AttachAsset = false;
50     //
51
private MMailText m_MailText = null;
52
53
54     /**
55      * Prepare - e.g., get Parameters.
56      */

57     protected void prepare()
58     {
59         ProcessInfoParameter[] para = getParameter();
60         for (int i = 0; i < para.length; i++)
61         {
62             String JavaDoc name = para[i].getParameterName();
63             if (para[i].getParameter() == null)
64                 ;
65             else if (name.equals("A_Asset_Group_ID"))
66                 m_A_Asset_Group_ID = ((BigDecimal)para[i].getParameter()).intValue();
67             else if (name.equals("M_Product_ID"))
68                 m_M_Product_ID = ((BigDecimal)para[i].getParameter()).intValue();
69             else if (name.equals("C_BPartner_ID"))
70                 m_C_BPartner_ID = ((BigDecimal)para[i].getParameter()).intValue();
71             else if (name.equals("A_Asset_ID"))
72                 m_A_Asset_ID = ((BigDecimal)para[i].getParameter()).intValue();
73             else if (name.equals("GuaranteeDate"))
74                 m_GuaranteeDate = (Timestamp)para[i].getParameter();
75             else if (name.equals("NoGuarantee_MailText_ID"))
76                 m_NoGuarantee_MailText_ID = ((BigDecimal)para[i].getParameter()).intValue();
77             else if (name.equals("AttachAsset"))
78                 m_AttachAsset = "Y".equals(para[i].getParameter());
79             else
80                 log.error("prepare - Unknown Parameter: " + name);
81         }
82         if (m_GuaranteeDate == null)
83             m_GuaranteeDate = new Timestamp (System.currentTimeMillis());
84         //
85
m_client = MClient.get(getCtx());
86     } // prepare
87

88     /**
89      * Perrform process.
90      * @return Message to be translated
91      * @throws Exception
92      */

93     protected String JavaDoc doIt() throws java.lang.Exception JavaDoc
94     {
95         log.info("doIt");
96         long start = System.currentTimeMillis();
97
98         // Test
99
if (m_client.getSMTPHost() == null)
100             throw new Exception JavaDoc ("No Client SMTP Info");
101         if (m_client.getRequestEMail() == null)
102             throw new Exception JavaDoc ("No Client Request User");
103
104         // Asset selected
105
if (m_A_Asset_ID != 0)
106         {
107             String JavaDoc msg = deliverIt (m_A_Asset_ID);
108             addLog (m_A_Asset_ID, null, null, msg);
109             return msg;
110         }
111         //
112
StringBuffer JavaDoc sql = new StringBuffer JavaDoc ("SELECT A_Asset_ID, GuaranteeDate "
113             + "FROM A_Asset a"
114             + " INNER JOIN M_Product p ON (a.M_Product_ID=p.M_Product_ID) "
115             + "WHERE ");
116         if (m_A_Asset_Group_ID != 0)
117             sql.append("a.A_Asset_Group_ID=").append(m_A_Asset_Group_ID).append(" AND ");
118         if (m_M_Product_ID != 0)
119             sql.append("p.M_Product_ID=").append(m_M_Product_ID).append(" AND ");
120         if (m_C_BPartner_ID != 0)
121             sql.append("a.C_BPartner_ID=").append(m_C_BPartner_ID).append(" AND ");
122         String JavaDoc s = sql.toString();
123         if (s.endsWith(" WHERE "))
124             throw new Exception JavaDoc ("@RestrictSelection@");
125         // No mail to expired
126
if (m_NoGuarantee_MailText_ID == 0)
127         {
128             sql.append("TRUNC(GuaranteeDate) >= ").append(DB.TO_DATE(m_GuaranteeDate, true));
129             s = sql.toString();
130         }
131         // Clean up
132
if (s.endsWith(" AND "))
133             s = sql.substring(0, sql.length()-5);
134         //
135
Statement stmt = null;
136         int count = 0;
137         int errors = 0;
138         int reminders = 0;
139         try
140         {
141             stmt = DB.createStatement();
142             ResultSet rs = stmt.executeQuery(s);
143             while (rs.next())
144             {
145                 int A_Asset_ID = rs.getInt(1);
146                 Timestamp GuaranteeDate = rs.getTimestamp(2);
147
148                 // Guarantee Expired
149
if (GuaranteeDate != null && GuaranteeDate.before(m_GuaranteeDate))
150                 {
151                     if (m_NoGuarantee_MailText_ID != 0)
152                     {
153                         sendNoGuaranteeMail (A_Asset_ID, m_NoGuarantee_MailText_ID);
154                         reminders++;
155                     }
156                 }
157                 else // Guarantee valid
158
{
159                     String JavaDoc msg = deliverIt (A_Asset_ID);
160                     addLog (A_Asset_ID, null, null, msg);
161                     if (msg.startsWith ("** "))
162                         errors++;
163                     else
164                         count++;
165                 }
166             }
167             rs.close();
168             stmt.close();
169             stmt = null;
170         }
171         catch (Exception JavaDoc e)
172         {
173             log.error("doIt - " + s, e);
174         }
175         finally
176         {
177             try
178             {
179                 if (stmt != null)
180                     stmt.close ();
181             }
182             catch (Exception JavaDoc e)
183             {}
184             stmt = null;
185         }
186         log.info("doIt - done - Count=" + count + ", Errors=" + errors + ", Reminder=" + reminders
187             + " - " + (System.currentTimeMillis()-start) + "ms");
188         return "@Sent@=" + count + " - @Errors@=" + errors;
189     } // doIt
190

191
192     /**
193      * Send No Guarantee EMail
194      * @param A_Asset_ID asset
195      * @param R_MailText_ID mail to send
196      * @return message - delivery errors start with **
197      */

198     private String JavaDoc sendNoGuaranteeMail (int A_Asset_ID, int R_MailText_ID)
199     {
200         MAsset asset = new MAsset (getCtx(), A_Asset_ID);
201         if (asset.getAD_User_ID() == 0)
202             return "** No Asset User";
203         MUser user = new MUser (getCtx(), asset.getAD_User_ID());
204         if (user.getEmail() == null || user.getEmail().length() == 0)
205             return "** No Asset User Email";
206         if (m_MailText == null || m_MailText.getR_MailText_ID() != R_MailText_ID)
207             m_MailText = new MMailText (getCtx(), R_MailText_ID);
208         if (m_MailText.getMailHeader() == null || m_MailText.getMailHeader().length() == 0)
209             return "** No Subject";
210
211         // Create Mail
212
EMail email = new EMail(m_client.getSMTPHost(), m_client.getRequestEMail(), user.getEmail());
213         if (m_client.isSmtpAuthorization())
214             email.setEMailUser(m_client.getRequestUser(), m_client.getRequestUserPW());
215         if (m_MailText.isHtml())
216             email.setMessageHTML(m_MailText.getMailHeader(), m_MailText.getMailText());
217         else
218         {
219             email.setSubject (m_MailText.getMailHeader());
220             email.setMessageText (m_MailText.getMailText());
221         }
222         String JavaDoc msg = email.send();
223         if (!EMail.SENT_OK.equals(msg))
224             return "** Not delivered: " + user.getEmail() + " - " + msg;
225         //
226
return user.getEmail();
227     } // sendNoGuaranteeMail
228

229     /*************************************************************************/
230
231     /**
232      * Deliver Asset
233      * @param A_Asset_ID asset
234      * @return message - delivery errors start with **
235      */

236     private String JavaDoc deliverIt (int A_Asset_ID)
237     {
238         log.debug("deliverIt - A_Asset_ID=" + A_Asset_ID);
239         long start = System.currentTimeMillis();
240         //
241
MAsset asset = new MAsset (getCtx(), A_Asset_ID);
242         if (asset.getAD_User_ID() == 0)
243             return "** No Asset User";
244         MUser user = new MUser (getCtx(), asset.getAD_User_ID());
245         if (user.getEmail() == null || user.getEmail().length() == 0)
246             return "** No Asset User Email";
247         if (asset.getProductR_MailText_ID() == 0)
248             return "** Product Mail Text";
249         if (m_MailText == null || m_MailText.getR_MailText_ID() != asset.getProductR_MailText_ID())
250             m_MailText = new MMailText (getCtx(), asset.getProductR_MailText_ID());
251         if (m_MailText.getMailHeader() == null || m_MailText.getMailHeader().length() == 0)
252             return "** No Subject";
253
254         // Create Mail
255
EMail email = new EMail(m_client.getSMTPHost(), m_client.getRequestEMail(), user.getEmail());
256         if (!email.isValid())
257         {
258             asset.setHelp(asset.getHelp() + " - Invalid EMail");
259             asset.setIsActive(false);
260             return "** Invalid EMail: " + user.getEmail();
261         }
262         if (m_client.isSmtpAuthorization())
263             email.setEMailUser(m_client.getRequestUser(), m_client.getRequestUserPW());
264         if (m_MailText.isHtml() || m_AttachAsset)
265             email.setMessageHTML(m_MailText.getMailHeader(), m_MailText.getMailText());
266         else
267         {
268             email.setSubject (m_MailText.getMailHeader());
269             email.setMessageText (m_MailText.getMailText());
270         }
271         if (m_AttachAsset)
272         {
273             URL url = asset.getDownloadURL(m_client.getDocumentDir());
274             if (url != null)
275                 email.addAttachment(url);
276             else
277                 log.warn("deliverIt - no DowloadURL for A_Asset_ID=" + A_Asset_ID);
278         }
279         String JavaDoc msg = email.send();
280         if (!EMail.SENT_OK.equals(msg))
281             return "** Not delivered: " + user.getEmail() + " - " + msg;
282
283         asset.confirmDelivery(email, user.getAD_User_ID());
284         asset.save();
285         //
286
log.debug("deliverIt " + (System.currentTimeMillis()-start) + " ms");
287         // success
288
return user.getEmail() + " - " + asset.getProductVersionNo();
289     } // deliverIt
290

291 } // AssetDelivery
292
Popular Tags