📄 importaccount.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 org.compiere.util.*;
import org.compiere.model.*;
/**
* Import Accounts from I_ElementValue
*
* @author Jorg Janke
* @version $Id: ImportAccount.java,v 1.9 2003/03/17 20:30:59 jjanke Exp $
*/
public class ImportAccount extends SvrProcess
{
/**
* Import Account Constructor
*/
public ImportAccount()
{
super();
Log.trace(Log.l1_User, "ImportAccount");
} // ImportAccount
/** Client to be imported to */
private int m_AD_Client_ID = 0;
/** Default Element */
private int m_C_Element_ID = 0;
/** Update Default Accounts */
private boolean m_updateDefaultAccounts = false;
/** Create New Combination */
private boolean m_createNewCombination = true;
/** 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()
{
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("AD_Client_ID"))
m_AD_Client_ID = ((BigDecimal)para[i].Parameter).intValue();
else if (name.equals("C_Element_ID"))
m_C_Element_ID = ((BigDecimal)para[i].Parameter).intValue();
else if (name.equals("UpdateDefaultAccounts"))
m_updateDefaultAccounts = "Y".equals(para[i].Parameter);
else if (name.equals("CreateNewCombination"))
m_createNewCombination = "Y".equals(para[i].Parameter);
else if (name.equals("DeleteOldImported"))
m_deleteOldImported = "Y".equals(para[i].Parameter);
else
Log.error("ImportAccount.prepare - 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_ElementValue "
+ "WHERE I_IsImported='Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString());
Log.trace(Log.l5_DData, "ImportAccount.doIt", "Delete Old Impored =" + no);
}
// Set Client, Org, IsActive, Created/Updated
sql = new StringBuffer ("UPDATE I_ElementValue "
+ "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());
Log.trace(Log.l5_DData, "ImportAccount.doIt", "Reset=" + no);
// **** Prepare ****
// Set Element
if (m_C_Element_ID != 0)
{
sql = new StringBuffer ("UPDATE I_ElementValue "
+ "SET ElementName=(SELECT Name FROM C_Element WHERE C_Element_ID=").append(m_C_Element_ID).append(") "
+ "WHERE ElementName IS NULL AND C_Element_ID IS NULL"
+ " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString());
Log.trace(Log.l5_DData, "ImportAccount.doIt", "Set Element Default=" + no);
}
//
sql = new StringBuffer ("UPDATE I_ElementValue i "
+ "SET C_Element_ID = (SELECT C_Element_ID FROM C_Element e"
+ " WHERE i.ElementName=e.Name AND i.AD_Client_ID=e.AD_Client_ID)"
+ "WHERE C_Element_ID IS NULL"
+ " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString());
Log.trace(Log.l5_DData, "ImportAccount.doIt", "Set Element=" + no);
//
sql = new StringBuffer ("UPDATE I_ElementValue "
+ "SET I_IsImported='E', I_ErrorMsg='ERR=Invalid Element, ' "
+ "WHERE C_Element_ID IS NULL"
+ " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString());
Log.trace(Log.l3_Util, "ImportAccount.doIt", "Invalid Element=" + no);
// Set Column
sql = new StringBuffer ("UPDATE I_ElementValue i "
+ "SET AD_Column_ID = (SELECT AD_Column_ID FROM AD_Column c"
+ " WHERE UPPER(i.Default_Account)=UPPER(c.ColumnName)"
+ " AND c.AD_Table_ID IN (315,266) AND AD_Reference_ID=25) "
+ "WHERE Default_Account IS NOT NULL AND AD_Column_ID IS NULL"
+ " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString());
Log.trace(Log.l5_DData, "ImportAccount.doIt", "Set Column=" + no);
//
sql = new StringBuffer ("UPDATE I_ElementValue "
+ "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid Column, ' "
+ "WHERE AD_Column_ID IS NULL AND Default_Account IS NOT NULL"
+ " AND UPPER(Default_Account)<>'DEFAULT_ACCT'" // ignore default account
+ " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString());
Log.trace(Log.l3_Util, "ImportAccount.doIt", "Invalid Column=" + no);
// Set Post* Defaults (ignore errors)
String[] yColumns = new String[] {"PostActual", "PostBudget", "PostStatistical", "PostEncumbrance"};
for (int i = 0; i < yColumns.length; i++)
{
sql = new StringBuffer ("UPDATE I_ElementValue SET ")
.append(yColumns[i]).append("='Y' WHERE ")
.append(yColumns[i]).append(" IS NULL OR ")
.append(yColumns[i]).append(" NOT IN ('Y','N')"
+ " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString());
Log.trace(Log.l5_DData, "ImportAccount.doIt", "Set " + yColumns[i] + " Default=" + no);
}
// Summary
sql = new StringBuffer ("UPDATE I_ElementValue "
+ "SET IsSummary='N' "
+ "WHERE IsSummary IS NULL OR IsSummary NOT IN ('Y','N')"
+ " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString());
Log.trace(Log.l5_DData, "ImportAccount.doIt", "Set IsSummary Default=" + no);
// Doc Controlled
sql = new StringBuffer ("UPDATE I_ElementValue "
+ "SET IsDocControlled = CASE WHEN AD_Column_ID IS NOT NULL THEN 'Y' ELSE 'N' END "
+ "WHERE IsDocControlled IS NULL OR IsDocControlled NOT IN ('Y','N')"
+ " AND I_IsImported='N'").append(clientCheck);
no = DB.executeUpdate(sql.toString());
Log.trace(Log.l5_DData, "ImportAccount.doIt", "Set IsDocumentControlled Default=" + no);
// Check Account Type A (E) L M O R
sql = new StringBuffer ("UPDATE I_ElementValue "
+ "SET AccountType='E' "
+ "WHERE AccountType IS NULL"
+ " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString());
Log.trace(Log.l5_DData, "ImportAccount.doIt", "Set AccountType Default=" + no);
//
sql = new StringBuffer ("UPDATE I_ElementValue "
+ "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid AccountType, ' "
+ "WHERE AccountType NOT IN ('A','E','L','M','O','R')"
+ " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString());
Log.trace(Log.l3_Util, "ImportAccount.doIt", "Invalid AccountType=" + no);
// Check Account Sign (N) C B
sql = new StringBuffer ("UPDATE I_ElementValue "
+ "SET AccountSign='N' "
+ "WHERE AccountSign IS NULL"
+ " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString());
Log.trace(Log.l5_DData, "ImportAccount.doIt", "Set AccountSign Default=" + no);
//
sql = new StringBuffer ("UPDATE I_ElementValue "
+ "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid AccountSign, ' "
+ "WHERE AccountSign NOT IN ('N','C','B')"
+ " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString());
Log.trace(Log.l3_Util, "ImportAccount.doIt", "Invalid AccountSign=" + no);
// **** Update ElementValue from existing
sql = new StringBuffer ("UPDATE I_ElementValue i "
+ "SET C_ElementValue_ID=(SELECT C_ElementValue_ID FROM C_ElementValue ev"
+ " INNER JOIN C_Element e ON (ev.C_Element_ID=e.C_Element_ID)"
+ " WHERE i.C_Element_ID=e.C_Element_ID AND i.AD_Client_ID=e.AD_Client_ID"
+ " AND i.Value=ev.Value) "
+ "WHERE C_ElementValue_ID IS NULL"
+ " AND I_IsImported='N'").append(clientCheck);
no = DB.executeUpdate(sql.toString());
Log.trace(Log.l5_DData, "ImportAccount.doIt", "Found ElementValue=" + no);
// Disable Trigger Updateing Description
no = DB.executeUpdate("ALTER TABLE C_ValidCombination DISABLE ALL TRIGGERS");
Log.trace(Log.l5_DData, "ImportAccount.doIt", "Disable Description Update =" + no);
// -------------------------------------------------------------------
int noInsert = 0;
int noUpdate = 0;
// Go through Records
sql = new StringBuffer ("SELECT I_ElementValue_ID, C_ElementValue_ID "
+ "FROM I_ElementValue "
+ "WHERE I_IsImported='N'").append(clientCheck)
.append(" ORDER BY I_ElementValue_ID");
Connection conn = DB.createConnection(false, Connection.TRANSACTION_READ_COMMITTED);
try
{
// Insert ElementValue
PreparedStatement pstmt_insertElementValue = conn.prepareStatement
("INSERT INTO C_ElementValue (C_ElementValue_ID,C_Element_ID,"
+ "AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy,"
+ "Value,Name,Description, AccountType,AccountSign,IsDocControlled,"
+ "IsSummary,PostActual,PostBudget,PostEncumbrance,PostStatistical) "
+ "SELECT ?,C_Element_ID,"
+ "AD_Client_ID,AD_Org_ID,'Y',SysDate,CreatedBy,SysDate,UpdatedBy,"
+ "Value,Name,Description, AccountType,AccountSign,IsDocControlled,"
+ "IsSummary,PostActual,PostBudget,PostEncumbrance,PostStatistical "
+ "FROM I_ElementValue "
+ "WHERE I_ElementValue_ID=?");
// Update ElementValue
PreparedStatement pstmt_updateElementValue = conn.prepareStatement
("UPDATE C_ElementValue "
+ "SET (Value,Name,Description, AccountType,AccountSign,IsDocControlled,"
+ "IsSummary,PostActual,PostBudget,PostEncumbrance,PostStatistical,Updated,UpdatedBy)="
+ " (SELECT Value,Name,Description, AccountType,AccountSign,IsDocControlled,"
+ " IsSummary,PostActual,PostBudget,PostEncumbrance,PostStatistical,SysDate,UpdatedBy"
+ " FROM I_ElementValue"
+ " WHERE I_ElementValue_ID=?) "
+ "WHERE C_ElementValue_ID=?");
// Set Imported = Y & Processing = 'Y'
PreparedStatement pstmt_setImported = conn.prepareStatement
("UPDATE I_ElementValue SET I_IsImported='Y',"
+ " C_ElementValue_ID=?, "
+ " Updated=SysDate, Processed='Y', Processing='Y' WHERE I_ElementValue_ID=?");
//
PreparedStatement pstmt = DB.prepareStatement(sql.toString());
ResultSet rs = pstmt.executeQuery();
while (rs.next())
{
int I_ElementValue_ID = rs.getInt(1);
int C_ElementValue_ID = rs.getInt(2);
Log.trace(Log.l6_Database, "I_ElementValue_ID=" + I_ElementValue_ID
+ ", C_ElementValue_ID=" + C_ElementValue_ID);
// **** Create/Update ElementValue
if (C_ElementValue_ID == 0) // New
{
C_ElementValue_ID = DB.getKeyNextNo(m_AD_Client_ID, "N", "C_ElementValue");
pstmt_insertElementValue.setInt(1, C_ElementValue_ID);
pstmt_insertElementValue.setInt(2, I_ElementValue_ID);
try
{
no = pstmt_insertElementValue.executeUpdate();
Log.trace(10, "Insert ElementValue = " + no);
noInsert++;
}
catch (SQLException ex)
{
Log.trace(10, "Insert ElementValue - " + ex.toString());
sql = new StringBuffer ("UPDATE I_ElementValue i "
+ "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||").append(DB.TO_STRING("Insert ElementValue: " + ex.toString()))
.append("WHERE I_ElementValue_ID=").append(I_ElementValue_ID);
DB.executeUpdate(sql.toString());
continue;
}
}
else // Update existing
{
pstmt_updateElementValue.setInt(1, I_ElementValue_ID);
pstmt_updateElementValue.setInt(2, C_ElementValue_ID);
try
{
no = pstmt_updateElementValue.executeUpdate();
Log.trace(10, "Update ElementValue = " + no);
noUpdate++;
}
catch (SQLException ex)
{
Log.trace(10, "Update ElementValue - " + ex.toString());
sql = new StringBuffer ("UPDATE I_ElementValue i "
+ "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||").append(DB.TO_STRING("Update ElementValue: " + ex.toString()))
.append("WHERE I_ElementValue_ID=").append(I_ElementValue_ID);
DB.executeUpdate(sql.toString());
continue;
}
}
// Update Element
pstmt_setImported.setInt(1, C_ElementValue_ID);
pstmt_setImported.setInt(2, I_ElementValue_ID);
no = pstmt_setImported.executeUpdate();
if (no != 1)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -