📄 naturalaccountmap.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-2001 Jorg Janke, parts
* created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.model;
import java.io.*;
import java.util.*;
import org.compiere.util.*;
/**
* Natural Account (HashMap) Management.
* <pre>
* The key is a String of the column name (e.g. SUSPENSEBALANCING_ACCT)
* The value is an NaturalAccount
*
* a) Account information are loaded via the parse functions
* b) Accounts are created via the createAccounts function
* c) retrieve the C_ElementValue_ID for the given key
* </pre>
*
* @author Jorg Janke
* @version $Id: NaturalAccountMap.java,v 1.7 2003/01/20 05:39:20 jjanke Exp $
*/
public final class NaturalAccountMap extends HashMap implements Serializable
{
/**
* Constructor
*/
public NaturalAccountMap()
{
super();
} // NaturalAccountMap
/** Delimiter */
private String m_delim = ",";
/** KeyNo */
private static int s_keyNo = 0;
/**
* Read and Parse File
* @param file Accounts file
* @return error message or "" if OK
*/
public String parseFile (File file)
{
Log.trace(Log.l3_Util, "NaturalAccountMap.parseFile - " + file.getAbsolutePath());
try
{
// see FileImport
BufferedReader in = new BufferedReader(new FileReader(file), 10240);
// not safe see p108 Network pgm
String s = null;
String errMsg = "";
// read lines
while ((s = in.readLine()) != null && errMsg.length() == 0)
errMsg = parseLine(s);
in.close();
// Error
if (errMsg.length() != 0)
return errMsg;
}
catch (IOException ioe)
{
String s = ioe.getLocalizedMessage();
if (s == null || s.length() == 0)
s = ioe.toString();
return s;
}
return "";
} // parse
/**
* Create Account Entry.
* @param line line with info
* Line format (9 fields)
* A [Account Value]
* B [Account Name]
* C [Description]
* D [Account Type]
* E [Account Sign]
* F [Document Controlled]
* G [Summary Account]
* H [Default_Account]
* I [Parent Value] - ignored
*
* @return error message or "" if OK
*/
public String parseLine (String line)
{
Log.trace(Log.l3_Util, "NaturalAccountMap.parseLine - " + line);
// Fields with ',' are enclosed in "
StringBuffer newLine = new StringBuffer();
StringTokenizer st = new StringTokenizer(line, "\"", false);
newLine.append(st.nextToken()); // first part
while (st.hasMoreElements())
{
String s = st.nextToken(); // enclosed part
newLine.append(s.replace(',',' ')); // remove ',' with space
if (st.hasMoreTokens())
newLine.append(st.nextToken()); // unenclosed
}
// add space at the end - tokenizer does not count empty fields
newLine.append(" ");
// Parse Line - replace ",," with ", ," - tokenizer does not count empty fields
st = new StringTokenizer(Util.replace(newLine.toString(), ",,", ", ,"), m_delim, false);
// All fields there ?
if (st.countTokens() < 9)
{
Log.error("NaturalAccountMap.parseLine - FieldNumber wrong: " + st.countTokens());
return "";
}
// Fill variables
String Value = null, Name = null, Description = null,
AccountType = null, AccountSign = null, IsDocControlled = null,
IsSummary = null, Default_Account = null;
//
for (int i = 0; i < 8 && st.hasMoreTokens(); i++)
{
String s = st.nextToken().trim();
// Ignore, if is it header line
if (s.startsWith("[") && s.endsWith("]"))
return "";
if (s == null)
s = "";
//
if (i == 0) // A - Value
Value = s;
else if (i == 1) // B - Name
Name = s;
else if (i == 2) // C - Description
Description = s;
else if (i == 3) // D - Type
AccountType = s.length()>0 ? String.valueOf(s.charAt(0)) : "E";
else if (i == 4) // E - Sign
AccountSign = s.length()>0 ? String.valueOf(s.charAt(0)) : "N";
else if (i == 5) // F - DocControlled
IsDocControlled = s.length()>0 ? String.valueOf(s.charAt(0)) : "N";
else if (i == 6) // G - IsSummary
IsSummary = s.length()>0 ? String.valueOf(s.charAt(0)) : "N";
else if (i == 7) // H - Default_Account
Default_Account = s;
}
// Ignore if Value & Name are empty (no error message)
if ((Value == null || Value.length() == 0) && (Name == null || Name.length() == 0))
return "";
// Default Account may be blank
if (Default_Account == null || Default_Account.length() == 0)
Default_Account = String.valueOf(s_keyNo++);
// Validation
if (AccountType == null || AccountType.length() == 0)
AccountType = "E";
if (IsSummary == null || IsSummary.length() == 0)
IsSummary = "N";
if (AccountSign == null || AccountSign.length() == 0)
AccountSign = "N";
if (IsDocControlled == null || IsDocControlled.length() == 0)
IsDocControlled = "N";
// Log.trace(Log.l4_Data, "Value=" + Value + ", AcctType=" + AccountType
// + ", Sign=" + AccountSign + ", Doc=" + docControlled
// + ", Summary=" + summary + " - " + Name + " - " + Description);
try
{
// Create Account
NaturalAccount na = new NaturalAccount(Value, Name, Description,
AccountType, AccountSign,
IsDocControlled.toUpperCase().startsWith("Y"), IsSummary.toUpperCase().startsWith("Y"));
// Add to ArrayList
put(Default_Account, na);
}
catch (Exception e)
{
return (e.getMessage());
}
return "";
} // parseLine
/**
* Create all Accounts
*
* @param AD_Client_ID client
* @param AD_Org_ID org
* @param C_Element_ID element
* @return true if created
*/
public boolean createAccounts(int AD_Client_ID, int AD_Org_ID, int C_Element_ID)
{
Log.trace(Log.l3_Util, "NaturalAccountMap.createAccounts");
boolean OK = true;
Iterator iterator = this.values().iterator();
while (iterator.hasNext())
{
NaturalAccount na = (NaturalAccount)iterator.next();
na.setC_Element_ID(C_Element_ID);
int no = na.save(AD_Client_ID, AD_Org_ID);
if (no == 0)
OK = false;
}
return OK;
} // createAccounts
/**
* Get ElementValue
* @param key key
* @return 0 if error
*/
public int getC_ElementValue_ID (String key)
{
NaturalAccount na = (NaturalAccount)this.get(key);
if (na == null)
return 0;
return na.getC_ElementValue_ID();
} // getC_ElementValue_ID
} // NaturalAccountMap
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -