📄 assetdelivery.java
字号:
/******************************************************************************
* The contents of this file are subject to the Compiere License Version 1.1
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.compiere.org/license.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Original Code is Compiere ERP & CRM Business Solution
* The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
* Portions created by Jorg Janke are Copyright (C) 1999-2003 Jorg Janke, parts
* created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.process;
import java.sql.*;
import java.math.*;
import java.net.*;
import org.compiere.util.*;
/**
* Deliver Assets Electronically
*
* @author Jorg Janke
* @version $Id: AssetDelivery.java,v 1.4 2003/02/11 06:22:10 jjanke Exp $
*/
public class AssetDelivery extends SvrProcess
{
/**
* Constructor
*/
public AssetDelivery()
{
super();
Log.trace(Log.l1_User, "AssetDelivery");
} // AssetDelivery
private int m_A_Asset_Group_ID = 0;
private int m_M_Product_ID = 0;
private int m_C_BPartner_ID = 0;
private int m_A_Asset_ID = 0;
private Timestamp m_GuaranteeDate = null;
private int m_NoGuarantee_MailText_ID = 0;
/**
* Prepare - e.g., get Parameters.
*/
protected void prepare()
{
Parameter[] para = getParameter();
for (int i = 0; i < para.length; i++)
{
String name = para[i].ParameterName;
if (para[i].Parameter == null)
;
else if (name.equals("A_Asset_Group_ID"))
m_A_Asset_Group_ID = ((BigDecimal)para[i].Parameter).intValue();
else if (name.equals("M_Product_ID"))
m_M_Product_ID = ((BigDecimal)para[i].Parameter).intValue();
else if (name.equals("C_BPartner_ID"))
m_C_BPartner_ID = ((BigDecimal)para[i].Parameter).intValue();
else if (name.equals("A_Asset_ID"))
m_A_Asset_ID = ((BigDecimal)para[i].Parameter).intValue();
else if (name.equals("GuaranteeDate"))
m_GuaranteeDate = (Timestamp)para[i].Parameter;
else if (name.equals("NoGuarantee_MailText_ID"))
m_NoGuarantee_MailText_ID = ((BigDecimal)para[i].Parameter).intValue();
else
Log.error("AssetDelivery.prepare - Unknown Parameter: " + name);
}
if (m_GuaranteeDate == null)
m_GuaranteeDate = new Timestamp (System.currentTimeMillis());
} // prepare
/**
* Perrform process.
* @return Message to be translated
* @throws Exception
*/
protected String doIt() throws java.lang.Exception
{
// Asset selected
if (m_A_Asset_ID != 0)
{
String msg = deliverIt (m_A_Asset_ID, 0);
addLog (null, m_A_Asset_ID, null, msg);
return msg;
}
//
StringBuffer sql = new StringBuffer ("SELECT A_Asset_ID, GuaranteeDate "
+ "FROM A_Asset a"
+ " INNER JOIN M_Product p ON (a.M_Product_ID=p.M_Product_ID) "
+ "WHERE ");
if (m_A_Asset_Group_ID != 0)
sql.append("a.A_Asset_Group_ID=").append(m_A_Asset_Group_ID).append(" AND ");
if (m_M_Product_ID != 0)
sql.append("p.M_Product_ID=").append(m_M_Product_ID).append(" AND ");
if (m_C_BPartner_ID != 0)
sql.append("a.C_BPartner_ID=").append(m_C_BPartner_ID).append(" AND ");
String s = sql.toString();
if (s.endsWith(" WHERE "))
throw new Exception ("@RestrictSelection@");
// No mail to expired
if (m_NoGuarantee_MailText_ID == 0)
{
sql.append("TRUNC(GuaranteeDate) >= ").append(DB.TO_DATE(m_GuaranteeDate, true));
s = sql.toString();
}
if (s.endsWith(" AND "))
s = sql.substring(0, sql.length()-5);
//
Statement stmt = null;
int count = 0;
int errors = 0;
try
{
stmt = DB.createStatement();
ResultSet rs = stmt.executeQuery(s);
while (rs.next())
{
int A_Asset_ID = rs.getInt(1);
Timestamp GuaranteeDate = rs.getTimestamp(2);
// Guarantee Expired
if (GuaranteeDate != null && GuaranteeDate.before(m_GuaranteeDate))
{
if (m_NoGuarantee_MailText_ID != 0)
sendNoGuaranteeMail (A_Asset_ID, m_NoGuarantee_MailText_ID);
}
else // Guarantee valid
{
String msg = deliverIt (A_Asset_ID, 0);
addLog (null, A_Asset_ID, null, msg);
if (msg.startsWith ("** "))
errors++;
else
count++;
}
}
rs.close();
stmt.close();
stmt = null;
}
catch (Exception e)
{
Log.error("AssetDelivery.doIt - " + s, e);
}
finally
{
try
{
if (stmt != null)
stmt.close ();
}
catch (Exception e)
{}
stmt = null;
}
return "@Sent@=" + count + " - @Errors@=" + errors;
} // doIt
/**
* Send No Guarantee EMail
* @param A_Asset_ID asset
* @param R_MailText_ID mail to send
* @return message - delivery errors start with **
*/
public static String sendNoGuaranteeMail (int A_Asset_ID, int R_MailText_ID)
{
String smtpHost = null;
String from = null;
String uid = null;
String pwd = null;
String to = null;
String subject = null;
String message = null;
String productName = null;
StringBuffer sql = new StringBuffer ("SELECT c.SMTPHost, c.RequestEMail, c.RequestUser,c.RequestUserPW," // 1..4
+ " bpc.EMail, m.MailHeader,m.MailText, p.Name " // 5..8
+ "FROM AD_Client c"
+ " INNER JOIN A_Asset a ON (c.AD_Client_ID=a.AD_Client_ID)"
+ " INNER JOIN M_Product p ON (a.M_Product_ID=p.M_Product_ID)"
+ " INNER JOIN R_MailText m ON (c.AD_Client_ID=m.AD_Client_ID)"
+ " LEFT OUTER JOIN C_BPartner_Contact bpc ON (a.C_BPartner_Contact_ID=bpc.C_BPartner_Contact_ID) "
+ "WHERE a.A_Asset_ID=? AND m.R_MailText_ID=?");
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement (sql.toString());
pstmt.setInt (1, A_Asset_ID);
pstmt.setInt (2, R_MailText_ID);
ResultSet rs = pstmt.executeQuery ();
String urlString = null;
if (rs.next ())
{
smtpHost = rs.getString(1);
from = rs.getString(2);
uid = rs.getString(3);
pwd = rs.getString(4);
//
to = rs.getString(5);
subject = rs.getString(6);
message = rs.getString(7);
productName = rs.getString(8);
}
rs.close ();
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
Log.error ("AssetDelivery.sendNoGuaranteeMail", e);
}
finally
{
try
{
if (pstmt != null)
pstmt.close ();
}
catch (Exception e)
{}
pstmt = null;
}
// Test
if (smtpHost == null)
return "** No SMTP - " + to;
else if (from == null)
return "** No From - " + to;
else if (to == null)
return "** No To";
else if (subject == null)
return "** No Subject - " + to;
// Create Mail
EMail mail = new EMail(smtpHost, from, to);
if (uid != null)
mail.setEMailUser(uid, pwd);
String hdr = productName + ": " + subject;
mail.setMessageHTML (hdr, message);
String msg = mail.send();
if (!EMail.SENT_OK.equals(msg))
return "** Not delivered: " + to + " - " + msg;
// String MessageID = mail.getMessageID();
return to + " - " + subject;
} // sendNoGuaranteeMail
/*************************************************************************/
/**
* Deliver Asset
* @param A_Asset_ID asset
* @param A_Asset_Delivery_ID optional delivery id (-1 = update existing record)
* @return message - delivery errors start with **
*/
public static String deliverIt (int A_Asset_ID, int A_Asset_Delivery_ID)
{
Log.trace(Log.l3_Util, "AssetDelivery.deliverIt", "A_Asset_ID=" + A_Asset_ID + ", Delivery_ID=" + A_Asset_Delivery_ID);
long start = System.currentTimeMillis();
//
String smtpHost = null;
String from = null;
String uid = null;
String pwd = null;
String to = null;
String subject = null;
String message = null;
URL url = null;
int AD_Client_ID = 0;
int C_BPartner_Contact_ID = 0;
String versionNo = null;
Timestamp GuaranteeDate = null;
StringBuffer sql = new StringBuffer ("SELECT c.SMTPHost, c.RequestEMail, c.RequestUser,c.RequestUserPW," // 1..4
+ " bpc.EMail, m.MailHeader,m.MailText, p.DescriptionURL," // 5..8
+ " c.AD_Client_ID,bpc.C_BPartner_Contact_ID,p.VersionNo " // 9..12
+ "FROM AD_Client c"
+ " INNER JOIN A_Asset a ON (c.AD_Client_ID=a.AD_Client_ID)"
+ " INNER JOIN M_Product p ON (a.M_Product_ID=p.M_Product_ID)"
+ " LEFT OUTER JOIN R_MailText m ON (p.R_MailText_ID=m.R_MailText_ID)"
+ " LEFT OUTER JOIN C_BPartner_Contact bpc ON (a.C_BPartner_Contact_ID=bpc.C_BPartner_Contact_ID) "
+ "WHERE a.A_Asset_ID=?");
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement (sql.toString());
pstmt.setInt (1, A_Asset_ID);
ResultSet rs = pstmt.executeQuery ();
String urlString = null;
if (rs.next ())
{
smtpHost = rs.getString(1);
from = rs.getString(2);
uid = rs.getString(3);
pwd = rs.getString(4);
//
to = rs.getString(5);
subject = rs.getString(6);
message = rs.getString(7);
urlString = rs.getString(8);
//
AD_Client_ID = rs.getInt(9);
C_BPartner_Contact_ID = rs.getInt(10);
versionNo = rs.getString(11);
}
rs.close ();
pstmt.close ();
pstmt = null;
if (urlString != null)
url = new URL (urlString);
}
catch (Exception e)
{
Log.error ("AssetDelivery.deliverIt", e);
}
finally
{
try
{
if (pstmt != null)
pstmt.close ();
}
catch (Exception e)
{}
pstmt = null;
}
// Test
if (smtpHost == null)
return "** No SMTP - " + to;
else if (from == null)
return "** No From - " + to;
else if (to == null)
return "** No To";
else if (subject == null)
return "** No Subject - " + to;
else if (url == null)
return "** No URL - " + to;
// Create Mail
EMail mail = new EMail(smtpHost, from, to);
if (uid != null)
mail.setEMailUser(uid, pwd);
mail.setMessageHTML(subject, message);
mail.addAttachment(url);
String msg = mail.send();
if (!EMail.SENT_OK.equals(msg))
return "** Not delivered: " + to + " - " + msg;
String MessageID = mail.getMessageID();
// Create new Asset_Delivery
if (A_Asset_Delivery_ID == 0)
{
A_Asset_Delivery_ID = DB.getKeyNextNo (AD_Client_ID, "N", "A_Asset_Delivery");
sql = new StringBuffer ("INSERT INTO A_Asset_Delivery "
+ "(A_Asset_Delivery_ID, AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy,"
+ "A_Asset_ID,MovementDate,SerNo,Lot,VersionNo,"
+ "C_BPartner_Contact_ID,EMail,MessageID,DeliveryConfirmation) "
//
+ "SELECT ").append (A_Asset_Delivery_ID).append (", a.AD_Client_ID,a.AD_Org_ID,'Y',SysDate,0,SysDate,0,"
+ "a.A_Asset_ID,SysDate,a.SerNo,a.Lot,p.VersionNo,")
// C_BPartner_Contact_ID,EMail,MessageID,DeliveryConfirmation
.append(C_BPartner_Contact_ID).append(",'").append(to).append("','").append(MessageID).append("',null "
+ "FROM A_Asset a"
+ " INNER JOIN M_Product p ON (a.M_Product_ID=p.M_Product_ID) "
+ "WHERE a.A_Asset_ID=").append(A_Asset_ID);
int no = DB.executeUpdate(sql.toString());
if (no != 1)
return "Not Inserted - " + to;
}
// overwrite Asset_Delivery
else if (A_Asset_Delivery_ID == -1)
{
sql = new StringBuffer ("UPDATE A_Asset_Delivery "
+ "SET MovementDate=SysDate,Updated=SysDate,UpdatedBy=0,"
+ " C_BPartner_Contact_ID=").append(C_BPartner_Contact_ID).append(","
+ " EMail='").append(to).append("',"
+ " MessageID='").append(MessageID).append("',");
if (versionNo != null)
sql.append(" VersionNo='").append(versionNo).append("',");
sql.append(" DeliveryConfirmation=null "
+ "WHERE A_Asset_ID=").append(A_Asset_ID);
int no = DB.executeUpdate(sql.toString());
if (no == 0)
return "No Deliveries - " + to;
}
// Update Asset_Delivery
else
{
sql = new StringBuffer ("UPDATE A_Asset_Delivery "
+ "SET MovementDate=SysDate,Updated=SysDate,UpdatedBy=0,"
+ " C_BPartner_Contact_ID=").append(C_BPartner_Contact_ID).append(","
+ " EMail='").append(to).append("',"
+ " MessageID='").append(MessageID).append("',");
if (versionNo != null)
sql.append(" VersionNo='").append(versionNo).append("',");
sql.append(" DeliveryConfirmation=null "
+ "WHERE A_Asset_Delivery_ID=").append(A_Asset_Delivery_ID);
int no = DB.executeUpdate(sql.toString());
if (no != 1)
return "Not Updated - " + to;
}
// Update Asset with latest Info
sql = new StringBuffer ("UPDATE A_Asset a "
+ "SET (SerNo,Lot,VersionNo)= "
+ "(SELECT COALESCE(ad.SerNo,a.SerNo), COALESCE(ad.Lot,a.Lot), COALESCE(ad.VersionNo,a.VersionNo) "
+ "FROM A_Asset_Delivery ad "
+ "WHERE ad.A_Asset_ID=a.A_Asset_ID"
+ " AND A_Asset_Delivery_ID= "
+ "(SELECT MAX(A_Asset_Delivery_ID) FROM A_Asset_Delivery xx "
+ "WHERE xx.A_Asset_ID=a.A_Asset_ID)), "
+ "Updated=SysDate "
+ "WHERE A_Asset_ID=").append(A_Asset_ID);
int no = DB.executeUpdate(sql.toString());
if (no != 1)
Log.error("AssetDelivery.deliverIt - Asset not updated");
//
Log.trace(Log.l6_Database, "AssetDelivery.deliverIt "
+ (System.currentTimeMillis()-start) + " ms");
// success
return to + " - " + versionNo;
} // deliverIt
} // AssetDelivery
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -