📄 importbpartner.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 Smart Business Solution. The Initial
* Developer of the Original Code is Jorg Janke. Portions created by Jorg Janke
* are Copyright (C) 1999-2005 Jorg Janke.
* All parts are Copyright (C) 1999-2005 ComPiere, Inc. All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.process;
import java.math.*;
import java.sql.*;
import java.util.logging.*;
import org.compiere.model.*;
import org.compiere.util.*;
/**
* Import BPartners from I_BPartner
*
* @author Jorg Janke
* @version $Id: ImportBPartner.java,v 1.21 2006/01/24 05:16:35 jjanke Exp $
*/
public class ImportBPartner extends SvrProcess
{
/** Client to be imported to */
private int m_AD_Client_ID = 0;
/** Delete old Imported */
private boolean m_deleteOldImported = false;
/** Organization to be imported to */
private int m_AD_Org_ID = 0;
/** Effective */
private Timestamp m_DateValue = null;
/**
* Prepare - e.g., get Parameters.
*/
protected void prepare()
{
ProcessInfoParameter[] para = getParameter();
for (int i = 0; i < para.length; i++)
{
String name = para[i].getParameterName();
if (name.equals("AD_Client_ID"))
m_AD_Client_ID = ((BigDecimal)para[i].getParameter()).intValue();
else if (name.equals("DeleteOldImported"))
m_deleteOldImported = "Y".equals(para[i].getParameter());
else
log.log(Level.SEVERE, "Unknown Parameter: " + name);
}
if (m_DateValue == null)
m_DateValue = new Timestamp (System.currentTimeMillis());
} // prepare
/**
* Perrform process.
* @return Message
* @throws Exception
*/
protected String doIt() throws java.lang.Exception
{
StringBuffer sql = null;
int no = 0;
String clientCheck = " AND AD_Client_ID=" + m_AD_Client_ID;
// **** Prepare ****
// Delete Old Imported
if (m_deleteOldImported)
{
sql = new StringBuffer ("DELETE I_BPartner "
+ "WHERE I_IsImported='Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.fine("Delete Old Impored =" + no);
}
// Set Client, Org, IsActive, Created/Updated
sql = new StringBuffer ("UPDATE I_BPartner "
+ "SET AD_Client_ID = COALESCE (AD_Client_ID, ").append(m_AD_Client_ID).append("),"
+ " AD_Org_ID = COALESCE (AD_Org_ID, 0),"
+ " IsActive = COALESCE (IsActive, 'Y'),"
+ " Created = COALESCE (Created, SysDate),"
+ " CreatedBy = COALESCE (CreatedBy, 0),"
+ " Updated = COALESCE (Updated, SysDate),"
+ " UpdatedBy = COALESCE (UpdatedBy, 0),"
+ " I_ErrorMsg = NULL,"
+ " I_IsImported = 'N' "
+ "WHERE I_IsImported<>'Y' OR I_IsImported IS NULL");
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.fine("Reset=" + no);
// Set BP_Group
sql = new StringBuffer ("UPDATE I_BPartner i "
+ "SET GroupValue=(SELECT Value FROM C_BP_Group g WHERE g.IsDefault='Y'"
+ " AND g.AD_Client_ID=i.AD_Client_ID AND ROWNUM=1) "
+ "WHERE GroupValue IS NULL AND C_BP_Group_ID IS NULL"
+ " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.fine("Set Group Default=" + no);
//
sql = new StringBuffer ("UPDATE I_BPartner i "
+ "SET C_BP_Group_ID=(SELECT C_BP_Group_ID FROM C_BP_Group g"
+ " WHERE i.GroupValue=g.Value AND g.AD_Client_ID=i.AD_Client_ID) "
+ "WHERE C_BP_Group_ID IS NULL"
+ " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.fine("Set Group=" + no);
//
sql = new StringBuffer ("UPDATE I_BPartner "
+ "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid Group, ' "
+ "WHERE C_BP_Group_ID IS NULL"
+ " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.config("Invalid Group=" + no);
// Set Country
/**
sql = new StringBuffer ("UPDATE I_BPartner i "
+ "SET CountryCode=(SELECT CountryCode FROM C_Country c WHERE c.IsDefault='Y'"
+ " AND c.AD_Client_ID IN (0, i.AD_Client_ID) AND ROWNUM=1) "
+ "WHERE CountryCode IS NULL AND C_Country_ID IS NULL"
+ " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.fine("Set Country Default=" + no);
**/
//
sql = new StringBuffer ("UPDATE I_BPartner i "
+ "SET C_Country_ID=(SELECT C_Country_ID FROM C_Country c"
+ " WHERE i.CountryCode=c.CountryCode AND c.AD_Client_ID IN (0, i.AD_Client_ID)) "
+ "WHERE C_Country_ID IS NULL"
+ " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.fine("Set Country=" + no);
//
sql = new StringBuffer ("UPDATE I_BPartner "
+ "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid Country, ' "
+ "WHERE C_Country_ID IS NULL AND (City IS NOT NULL OR Address1 IS NOT NULL)"
+ " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.config("Invalid Country=" + no);
// Set Region
sql = new StringBuffer ("UPDATE I_BPartner i "
+ "Set RegionName=(SELECT Name FROM C_Region r"
+ " WHERE r.IsDefault='Y' AND r.C_Country_ID=i.C_Country_ID"
+ " AND r.AD_Client_ID IN (0, i.AD_Client_ID) AND ROWNUM=1) "
+ "WHERE RegionName IS NULL AND C_Region_ID IS NULL"
+ " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.fine("Set Region Default=" + no);
//
sql = new StringBuffer ("UPDATE I_BPartner i "
+ "Set C_Region_ID=(SELECT C_Region_ID FROM C_Region r"
+ " WHERE r.Name=i.RegionName AND r.C_Country_ID=i.C_Country_ID"
+ " AND r.AD_Client_ID IN (0, i.AD_Client_ID)) "
+ "WHERE C_Region_ID IS NULL"
+ " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.fine("Set Region=" + no);
//
sql = new StringBuffer ("UPDATE I_BPartner i "
+ "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid Region, ' "
+ "WHERE C_Region_ID IS NULL "
+ " AND EXISTS (SELECT * FROM C_Country c"
+ " WHERE c.C_Country_ID=i.C_Country_ID AND c.HasRegion='Y')"
+ " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.config("Invalid Region=" + no);
// Set Greeting
sql = new StringBuffer ("UPDATE I_BPartner i "
+ "SET C_Greeting_ID=(SELECT C_Greeting_ID FROM C_Greeting g"
+ " WHERE i.BPContactGreeting=g.Name AND g.AD_Client_ID IN (0, i.AD_Client_ID)) "
+ "WHERE C_Greeting_ID IS NULL AND BPContactGreeting IS NOT NULL"
+ " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.fine("Set Greeting=" + no);
//
sql = new StringBuffer ("UPDATE I_BPartner i "
+ "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid Greeting, ' "
+ "WHERE C_Greeting_ID IS NULL AND BPContactGreeting IS NOT NULL"
+ " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.config("Invalid Greeting=" + no);
// Existing User ?
sql = new StringBuffer ("UPDATE I_BPartner i "
+ "SET (C_BPartner_ID,AD_User_ID)="
+ "(SELECT C_BPartner_ID,AD_User_ID FROM AD_User u "
+ "WHERE i.EMail=u.EMail AND u.AD_Client_ID=i.AD_Client_ID) "
+ "WHERE i.EMail IS NOT NULL AND I_IsImported='N'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.fine("Found EMail User=" + no);
// Existing BPartner ? Match Value
sql = new StringBuffer ("UPDATE I_BPartner i "
+ "SET C_BPartner_ID=(SELECT C_BPartner_ID FROM C_BPartner p"
+ " WHERE i.Value=p.Value AND p.AD_Client_ID=i.AD_Client_ID) "
+ "WHERE C_BPartner_ID IS NULL AND Value IS NOT NULL"
+ " AND I_IsImported='N'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.fine("Found BPartner=" + no);
// Existing Contact ? Match Name
sql = new StringBuffer ("UPDATE I_BPartner i "
+ "SET AD_User_ID=(SELECT AD_User_ID FROM AD_User c"
+ " WHERE i.ContactName=c.Name AND i.C_BPartner_ID=c.C_BPartner_ID AND c.AD_Client_ID=i.AD_Client_ID) "
+ "WHERE C_BPartner_ID IS NOT NULL AND AD_User_ID IS NULL AND ContactName IS NOT NULL"
+ " AND I_IsImported='N'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.fine("Found Contact=" + no);
// Existing Location ? Exact Match
sql = new StringBuffer ("UPDATE I_BPartner i "
+ "SET C_BPartner_Location_ID=(SELECT C_BPartner_Location_ID"
+ " FROM C_BPartner_Location bpl INNER JOIN C_Location l ON (bpl.C_Location_ID=l.C_Location_ID)"
+ " WHERE i.C_BPartner_ID=bpl.C_BPartner_ID AND bpl.AD_Client_ID=i.AD_Client_ID"
+ " AND DUMP(i.Address1)=DUMP(l.Address1) AND DUMP(i.Address2)=DUMP(l.Address2)"
+ " AND DUMP(i.City)=DUMP(l.City) AND DUMP(i.Postal)=DUMP(l.Postal) AND DUMP(i.Postal_Add)=DUMP(l.Postal_Add)"
+ " AND DUMP(i.C_Region_ID)=DUMP(l.C_Region_ID) AND DUMP(i.C_Country_ID)=DUMP(l.C_Country_ID)) "
+ "WHERE C_BPartner_ID IS NOT NULL AND C_BPartner_Location_ID IS NULL"
+ " AND I_IsImported='N'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.fine("Found Location=" + no);
// Interest Area
sql = new StringBuffer ("UPDATE I_BPartner i "
+ "SET R_InterestArea_ID=(SELECT R_InterestArea_ID FROM R_InterestArea ia "
+ "WHERE i.InterestAreaName=ia.Name AND ia.AD_Client_ID=i.AD_Client_ID) "
+ "WHERE R_InterestArea_ID IS NULL AND InterestAreaName IS NOT NULL"
+ " AND I_IsImported='N'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.fine("Set Interest Area=" + no);
commit();
// -------------------------------------------------------------------
int noInsert = 0;
int noUpdate = 0;
// Go through Records
sql = new StringBuffer ("SELECT * FROM I_BPartner "
+ "WHERE I_IsImported='N'").append(clientCheck);
try
{
PreparedStatement pstmt = DB.prepareStatement(sql.toString(), get_TrxName());
ResultSet rs = pstmt.executeQuery();
while (rs.next())
{
X_I_BPartner impBP = new X_I_BPartner (getCtx(), rs, get_TrxName());
log.fine("I_BPartner_ID=" + impBP.getI_BPartner_ID()
+ ", C_BPartner_ID=" + impBP.getC_BPartner_ID()
+ ", C_BPartner_Location_ID=" + impBP.getC_BPartner_Location_ID()
+ ", AD_User_ID=" + impBP.getAD_User_ID());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -