1 14 package org.compiere.model; 15 16 import java.io.*; 17 import java.util.*; 18 19 import org.compiere.util.*; 20 21 35 public final class NaturalAccountMap extends CCache 36 { 37 40 public NaturalAccountMap(Properties ctx) 41 { 42 super("naturalAccountMap", 100); 43 m_ctx = ctx; 44 } 46 47 49 private static int s_keyNo = 0; 50 51 private Properties m_ctx = null; 52 53 54 59 public String parseFile (File file) 60 { 61 Log.trace(Log.l3_Util, "NaturalAccountMap.parseFile - " + file.getAbsolutePath()); 62 try 63 { 64 BufferedReader in = new BufferedReader(new FileReader(file), 10240); 66 String s = null; 68 String errMsg = ""; 69 70 while ((s = in.readLine()) != null && errMsg.length() == 0) 72 errMsg = parseLine(s); 73 in.close(); 74 75 if (errMsg.length() != 0) 77 return errMsg; 78 } 79 catch (IOException ioe) 80 { 81 String s = ioe.getLocalizedMessage(); 82 if (s == null || s.length() == 0) 83 s = ioe.toString(); 84 return s; 85 } 86 return ""; 87 } 89 105 public String parseLine (String line) 106 { 107 Log.trace(Log.l3_Util, "NaturalAccountMap.parseLine - " + line); 108 109 StringBuffer newLine = new StringBuffer (); 111 StringTokenizer st = new StringTokenizer(line, "\"", false); 112 newLine.append(st.nextToken()); while (st.hasMoreElements()) 114 { 115 String s = st.nextToken(); newLine.append(s.replace(',',' ')); if (st.hasMoreTokens()) 118 newLine.append(st.nextToken()); } 120 newLine.append(" "); 122 123 String pLine = Util.replace(newLine.toString(), ",,", ", ,"); 125 pLine = Util.replace(pLine, ",,", ", ,"); 126 st = new StringTokenizer(pLine, ",", false); 127 if (st.countTokens() < 9) 129 { 130 Log.error("NaturalAccountMap.parseLine - FieldNumber wrong: " + st.countTokens() + " - " + pLine); 131 return ""; 132 } 133 134 String Value = null, Name = null, Description = null, 136 AccountType = null, AccountSign = null, IsDocControlled = null, 137 IsSummary = null, Default_Account = null; 138 for (int i = 0; i < 8 && st.hasMoreTokens(); i++) 140 { 141 String s = st.nextToken().trim(); 142 if (s.startsWith("[") && s.endsWith("]")) 144 return ""; 145 if (s == null) 146 s = ""; 147 if (i == 0) Value = s; 150 else if (i == 1) Name = s; 152 else if (i == 2) Description = s; 154 else if (i == 3) AccountType = s.length()>0 ? String.valueOf(s.charAt(0)) : "E"; 156 else if (i == 4) AccountSign = s.length()>0 ? String.valueOf(s.charAt(0)) : "N"; 158 else if (i == 5) IsDocControlled = s.length()>0 ? String.valueOf(s.charAt(0)) : "N"; 160 else if (i == 6) IsSummary = s.length()>0 ? String.valueOf(s.charAt(0)) : "N"; 162 else if (i == 7) Default_Account = s; 164 } 165 166 if ((Value == null || Value.length() == 0) && (Name == null || Name.length() == 0)) 168 return ""; 169 170 if (Default_Account == null || Default_Account.length() == 0) 172 return ""; 174 175 if (IsSummary == null || IsSummary.length() == 0) 177 IsSummary = "N"; 178 if (!IsSummary.equals("N")) 179 return ""; 180 181 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 195 try 196 { 197 MElementValue na = new MElementValue(m_ctx, Value, Name, Description, 199 AccountType, AccountSign, 200 IsDocControlled.toUpperCase().startsWith("Y"), IsSummary.toUpperCase().startsWith("Y")); 201 put(Default_Account, na); 203 } 204 catch (Exception e) 205 { 206 return (e.getMessage()); 207 } 208 209 return ""; 210 } 212 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 } 238 243 public int getC_ElementValue_ID (String key) 244 { 245 MElementValue na = (MElementValue)this.get(key); 246 if (na == null) 247 return 0; 248 return na.getC_ElementValue_ID(); 249 } 251 } | Popular Tags |