1 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 31 public class AssetDelivery extends SvrProcess 32 { 33 36 public AssetDelivery() 37 { 38 super(); 39 } 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 private MMailText m_MailText = null; 52 53 54 57 protected void prepare() 58 { 59 ProcessInfoParameter[] para = getParameter(); 60 for (int i = 0; i < para.length; i++) 61 { 62 String 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 m_client = MClient.get(getCtx()); 86 } 88 93 protected String doIt() throws java.lang.Exception 94 { 95 log.info("doIt"); 96 long start = System.currentTimeMillis(); 97 98 if (m_client.getSMTPHost() == null) 100 throw new Exception ("No Client SMTP Info"); 101 if (m_client.getRequestEMail() == null) 102 throw new Exception ("No Client Request User"); 103 104 if (m_A_Asset_ID != 0) 106 { 107 String msg = deliverIt (m_A_Asset_ID); 108 addLog (m_A_Asset_ID, null, null, msg); 109 return msg; 110 } 111 StringBuffer sql = new StringBuffer ("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 s = sql.toString(); 123 if (s.endsWith(" WHERE ")) 124 throw new Exception ("@RestrictSelection@"); 125 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 if (s.endsWith(" AND ")) 133 s = sql.substring(0, sql.length()-5); 134 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 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 { 159 String 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 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 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 } 191 192 198 private String 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 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 msg = email.send(); 223 if (!EMail.SENT_OK.equals(msg)) 224 return "** Not delivered: " + user.getEmail() + " - " + msg; 225 return user.getEmail(); 227 } 229 230 231 236 private String deliverIt (int A_Asset_ID) 237 { 238 log.debug("deliverIt - A_Asset_ID=" + A_Asset_ID); 239 long start = System.currentTimeMillis(); 240 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 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 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 log.debug("deliverIt " + (System.currentTimeMillis()-start) + " ms"); 287 return user.getEmail() + " - " + asset.getProductVersionNo(); 289 } 291 } | Popular Tags |