KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > model > NaturalAccountMap


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.model;
15
16 import java.io.*;
17 import java.util.*;
18
19 import org.compiere.util.*;
20
21 /**
22  * Natural Account (HashMap) Management.
23  * <pre>
24  * The key is a String of the column name (e.g. SUSPENSEBALANCING_ACCT)
25  * The value is an NaturalAccount
26  *
27  * a) Account information are loaded via the parse functions
28  * b) Accounts are created via the createAccounts function
29  * c) retrieve the C_ElementValue_ID for the given key
30  * </pre>
31  *
32  * @author Jorg Janke
33  * @version $Id: NaturalAccountMap.java,v 1.10 2003/11/06 07:08:05 jjanke Exp $
34  */

35 public final class NaturalAccountMap extends CCache
36 {
37     /**
38      * Constructor
39      */

40     public NaturalAccountMap(Properties ctx)
41     {
42         super("naturalAccountMap", 100);
43         m_ctx = ctx;
44     } // NaturalAccountMap
45

46     /** Delimiter */
47 // private String m_delim = ",";
48
/** KeyNo */
49     private static int s_keyNo = 0;
50     /** Context */
51     private Properties m_ctx = null;
52
53
54     /**
55      * Read and Parse File
56      * @param file Accounts file
57      * @return error message or "" if OK
58      */

59     public String JavaDoc parseFile (File file)
60     {
61         Log.trace(Log.l3_Util, "NaturalAccountMap.parseFile - " + file.getAbsolutePath());
62         try
63         {
64             // see FileImport
65
BufferedReader in = new BufferedReader(new FileReader(file), 10240);
66             // not safe see p108 Network pgm
67
String JavaDoc s = null;
68             String JavaDoc errMsg = "";
69
70             // read lines
71
while ((s = in.readLine()) != null && errMsg.length() == 0)
72                 errMsg = parseLine(s);
73             in.close();
74
75             // Error
76
if (errMsg.length() != 0)
77                 return errMsg;
78         }
79         catch (IOException ioe)
80         {
81             String JavaDoc s = ioe.getLocalizedMessage();
82             if (s == null || s.length() == 0)
83                 s = ioe.toString();
84             return s;
85         }
86         return "";
87     } // parse
88

89     /**
90      * Create Account Entry for Default Accounts only.
91      * @param line line with info
92      * Line format (9 fields)
93      * 1 A [Account Value]
94      * 2 B [Account Name]
95      * 3 C [Description]
96      * 4 D [Account Type]
97      * 5 E [Account Sign]
98      * 6 F [Document Controlled]
99      * 7 G [Summary Account]
100      * 8 H [Default_Account]
101      * 9 I [Parent Value] - ignored
102      *
103      * @return error message or "" if OK
104      */

105     public String JavaDoc parseLine (String JavaDoc line)
106     {
107         Log.trace(Log.l3_Util, "NaturalAccountMap.parseLine - " + line);
108
109         // Fields with ',' are enclosed in "
110
StringBuffer JavaDoc newLine = new StringBuffer JavaDoc();
111         StringTokenizer st = new StringTokenizer(line, "\"", false);
112         newLine.append(st.nextToken()); // first part
113
while (st.hasMoreElements())
114         {
115             String JavaDoc s = st.nextToken(); // enclosed part
116
newLine.append(s.replace(',',' ')); // remove ',' with space
117
if (st.hasMoreTokens())
118                 newLine.append(st.nextToken()); // unenclosed
119
}
120         // add space at the end - tokenizer does not count empty fields
121
newLine.append(" ");
122
123         // Parse Line - replace ",," with ", ," - tokenizer does not count empty fields
124
String JavaDoc pLine = Util.replace(newLine.toString(), ",,", ", ,");
125         pLine = Util.replace(pLine, ",,", ", ,");
126         st = new StringTokenizer(pLine, ",", false);
127         // All fields there ?
128
if (st.countTokens() < 9)
129         {
130             Log.error("NaturalAccountMap.parseLine - FieldNumber wrong: " + st.countTokens() + " - " + pLine);
131             return "";
132         }
133
134         // Fill variables
135
String JavaDoc Value = null, Name = null, Description = null,
136             AccountType = null, AccountSign = null, IsDocControlled = null,
137             IsSummary = null, Default_Account = null;
138         //
139
for (int i = 0; i < 8 && st.hasMoreTokens(); i++)
140         {
141             String JavaDoc s = st.nextToken().trim();
142             // Ignore, if is it header line
143
if (s.startsWith("[") && s.endsWith("]"))
144                 return "";
145             if (s == null)
146                 s = "";
147             //
148
if (i == 0) // A - Value
149
Value = s;
150             else if (i == 1) // B - Name
151
Name = s;
152             else if (i == 2) // C - Description
153
Description = s;
154             else if (i == 3) // D - Type
155
AccountType = s.length()>0 ? String.valueOf(s.charAt(0)) : "E";
156             else if (i == 4) // E - Sign
157
AccountSign = s.length()>0 ? String.valueOf(s.charAt(0)) : "N";
158             else if (i == 5) // F - DocControlled
159
IsDocControlled = s.length()>0 ? String.valueOf(s.charAt(0)) : "N";
160             else if (i == 6) // G - IsSummary
161
IsSummary = s.length()>0 ? String.valueOf(s.charAt(0)) : "N";
162             else if (i == 7) // H - Default_Account
163
Default_Account = s;
164         }
165
166         // Ignore if Value & Name are empty (no error message)
167
if ((Value == null || Value.length() == 0) && (Name == null || Name.length() == 0))
168             return "";
169
170         // Default Account may be blank
171
if (Default_Account == null || Default_Account.length() == 0)
172         // Default_Account = String.valueOf(s_keyNo++);
173
return "";
174
175         // No Summary Account
176
if (IsSummary == null || IsSummary.length() == 0)
177             IsSummary = "N";
178         if (!IsSummary.equals("N"))
179             return "";
180             
181         // Validation
182
if (AccountType == null || AccountType.length() == 0)
183             AccountType = "E";
184             
185         if (AccountSign == null || AccountSign.length() == 0)
186             AccountSign = "N";
187         if (IsDocControlled == null || IsDocControlled.length() == 0)
188             IsDocControlled = "N";
189
190
191     // Log.trace(Log.l4_Data, "Value=" + Value + ", AcctType=" + AccountType
192
// + ", Sign=" + AccountSign + ", Doc=" + docControlled
193
// + ", Summary=" + summary + " - " + Name + " - " + Description);
194

195         try
196         {
197             // Create Account
198
MElementValue na = new MElementValue(m_ctx, Value, Name, Description,
199                 AccountType, AccountSign,
200                 IsDocControlled.toUpperCase().startsWith("Y"), IsSummary.toUpperCase().startsWith("Y"));
201             // Add to ArrayList
202
put(Default_Account, na);
203         }
204         catch (Exception JavaDoc e)
205         {
206             return (e.getMessage());
207         }
208
209         return "";
210     } // parseLine
211

212     /**
213      * Create all Accounts
214      *
215      * @param AD_Client_ID client
216      * @param AD_Org_ID org
217      * @param C_Element_ID element
218      * @return true if created
219      */

220     public boolean createAccounts (int AD_Client_ID, int AD_Org_ID, int C_Element_ID)
221     {
222         Log.trace(Log.l3_Util, "NaturalAccountMap.createAccounts");
223         boolean OK = true;
224         Iterator iterator = this.values().iterator();
225         while (iterator.hasNext())
226         {
227             MElementValue na = (MElementValue)iterator.next();
228             na.setAD_Client_ID(AD_Client_ID);
229             na.setAD_Org_ID(AD_Org_ID);
230             na.setC_Element_ID(C_Element_ID);
231             boolean ok = na.save();
232             if (!ok)
233                 OK = ok;
234         }
235         return OK;
236     } // createAccounts
237

238     /**
239      * Get ElementValue
240      * @param key key
241      * @return 0 if error
242      */

243     public int getC_ElementValue_ID (String JavaDoc key)
244     {
245         MElementValue na = (MElementValue)this.get(key);
246         if (na == null)
247             return 0;
248         return na.getC_ElementValue_ID();
249     } // getC_ElementValue_ID
250

251 } // NaturalAccountMap
252
Popular Tags