📄 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 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 Accounts from I_ElementValue
*
* @author Jorg Janke
* @version $Id: ImportAccount.java,v 1.23 2005/10/28 00:59:28 jjanke Exp $
*/
public class ImportAccount extends SvrProcess
{
/** 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()
{
ProcessInfoParameter[] para = getParameter();
for (int i = 0; i < para.length; i++)
{
String name = para[i].getParameterName();
if (para[i].getParameter() == null)
;
else if (name.equals("AD_Client_ID"))
m_AD_Client_ID = ((BigDecimal)para[i].getParameter()).intValue();
else if (name.equals("C_Element_ID"))
m_C_Element_ID = ((BigDecimal)para[i].getParameter()).intValue();
else if (name.equals("UpdateDefaultAccounts"))
m_updateDefaultAccounts = "Y".equals(para[i].getParameter());
else if (name.equals("CreateNewCombination"))
m_createNewCombination = "Y".equals(para[i].getParameter());
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_ElementValue "
+ "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_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(), get_TrxName());
log.fine("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(), get_TrxName());
log.fine("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(), get_TrxName());
log.fine("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(), get_TrxName());
log.config("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(), get_TrxName());
log.fine("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(), get_TrxName());
log.config("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(), get_TrxName());
log.fine("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(), get_TrxName());
log.fine("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(), get_TrxName());
log.fine("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(), get_TrxName());
log.fine("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(), get_TrxName());
log.config("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(), get_TrxName());
log.fine("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','D')"
+ " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.config("Invalid AccountSign=" + no);
// No Value
sql = new StringBuffer ("UPDATE I_ElementValue "
+ "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||'ERR=No Key, ' "
+ "WHERE (Value IS NULL OR Value='')"
+ " AND I_IsImported<>'Y'").append(clientCheck);
no = DB.executeUpdate(sql.toString(), get_TrxName());
log.config("Invalid Key=" + 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(), get_TrxName());
log.fine("Found ElementValue=" + no);
commit();
// -------------------------------------------------------------------
int noInsert = 0;
int noUpdate = 0;
// Go through Records
sql = new StringBuffer ("SELECT * "
+ "FROM I_ElementValue "
+ "WHERE I_IsImported='N'").append(clientCheck)
.append(" ORDER BY I_ElementValue_ID");
try
{
PreparedStatement pstmt = DB.prepareStatement(sql.toString(), get_TrxName());
ResultSet rs = pstmt.executeQuery();
while (rs.next())
{
X_I_ElementValue impEV = new X_I_ElementValue(getCtx(), rs, get_TrxName());
int C_ElementValue_ID = impEV.getC_ElementValue_ID();
int I_ElementValue_ID = impEV.getI_ElementValue_ID();
// **** Create/Update ElementValue
if (C_ElementValue_ID == 0) // New
{
MElementValue ev = new MElementValue(impEV);
if (ev.save())
{
noInsert++;
impEV.setC_ElementValue_ID(ev.getC_ElementValue_ID());
impEV.setI_IsImported(true);
impEV.save();
}
else
{
sql = new StringBuffer ("UPDATE I_ElementValue i "
+ "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||").append(DB.TO_STRING("Insert ElementValue "))
.append("WHERE I_ElementValue_ID=").append(I_ElementValue_ID);
DB.executeUpdate(sql.toString(), get_TrxName());
}
}
else // Update existing
{
MElementValue ev = new MElementValue (getCtx(), C_ElementValue_ID, null);
if (ev.get_ID() != C_ElementValue_ID)
{
}
ev.set(impEV);
if (ev.save())
{
noUpdate++;
impEV.setI_IsImported(true);
impEV.save();
}
else
{
sql = new StringBuffer ("UPDATE I_ElementValue i "
+ "SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||").append(DB.TO_STRING("Update ElementValue"))
.append("WHERE I_ElementValue_ID=").append(I_ElementValue_ID);
DB.executeUpdate(sql.toString(), get_TrxName());
}
}
} // for all I_Product
rs.close();
pstmt.close();
}
catch (SQLException e)
{
throw new Exception ("create", e);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -