1 14 package org.compiere.impexp; 15 16 import java.math.*; 17 import java.sql.*; 18 import java.text.*; 19 20 import org.compiere.util.*; 21 import org.compiere.model.*; 22 23 29 public final class ImpFormatRow 30 { 31 40 public ImpFormatRow(int seqNo, String columnName, int startNo, int endNo, String dataType, int maxLength) 41 { 42 m_seqNo = seqNo; 43 setColumnName(columnName); 44 m_startNo = startNo; 45 m_endNo = endNo; 46 setDataType (dataType); 47 setMaxLength (maxLength); 48 } 50 57 public ImpFormatRow(int seqNo, String columnName, String dataType, int maxLength) 58 { 59 m_seqNo = seqNo; 60 setColumnName(columnName); 61 setDataType (dataType); 62 setMaxLength (maxLength); 63 } 65 private int m_seqNo; 66 private String m_columnName; 67 private int m_startNo = 0; 68 private int m_endNo = 0; 69 private String m_dataType; 70 private String m_dataFormat = ""; 71 private String m_decimalPoint = "."; 72 private boolean m_divideBy100 = false; 73 private String m_constantValue = ""; 74 private boolean m_constantIsString = true; 75 private String m_callout = ""; 76 private SimpleDateFormat m_dformat = null; 77 private int m_maxLength = 0; 78 79 83 public int getSeqNo () 84 { 85 return m_seqNo; 86 } 88 92 public void setSeqNo (int newSeqNo) 93 { 94 m_seqNo = newSeqNo; 95 } 97 101 public void setStartNo (int newStartNo) 102 { 103 m_startNo = newStartNo; 104 } 106 110 public int getStartNo() 111 { 112 return m_startNo; 113 } 115 119 public void setEndNo (int newEndNo) 120 { 121 m_endNo = newEndNo; 122 } 124 128 public int getEndNo () 129 { 130 return m_endNo; 131 } 133 137 public void setColumnName (String columnName) 138 { 139 if (columnName == null || columnName.length() == 0) 140 throw new IllegalArgumentException ("ColumnName must be at least 1 char"); 141 else 142 m_columnName = columnName; 143 } 145 149 public String getColumnName() 150 { 151 return m_columnName; 152 } 154 158 public void setDataType (String dataType) 159 { 160 if (dataType.equals(DATATYPE_String) || dataType.equals(DATATYPE_Date) 161 || dataType.equals(DATATYPE_Number) || dataType.equals(DATATYPE_Constant)) 162 m_dataType = dataType; 163 else 164 throw new IllegalArgumentException ("DataType must be S/D/N/C"); 165 } 167 168 public static final String DATATYPE_String = "S"; 169 170 public static final String DATATYPE_Date = "D"; 171 172 public static final String DATATYPE_Number = "N"; 173 174 public static final String DATATYPE_Constant = "C"; 175 176 180 public String getDataType() 181 { 182 return m_dataType; 183 } 185 189 public boolean isString() 190 { 191 if (m_dataType.equals(DATATYPE_Constant)) 192 return m_constantIsString; 193 return m_dataType.equals(DATATYPE_String); 194 } 196 200 public boolean isNumber() 201 { 202 return m_dataType.equals(DATATYPE_Number); 203 } 204 205 209 public boolean isDate() 210 { 211 return m_dataType.equals(DATATYPE_Date); 212 } 213 214 218 public boolean isConstant() 219 { 220 return m_dataType.equals(DATATYPE_Constant); 221 } 222 223 231 public void setFormatInfo (String dataFormat, String decimalPoint, boolean divideBy100, 232 String constantValue, String callout) 233 { 234 if (dataFormat == null) 235 m_dataFormat = ""; 236 else 237 m_dataFormat = dataFormat; 238 if (decimalPoint == null || !decimalPoint.equals(",")) 240 m_decimalPoint = "."; 241 else 242 m_decimalPoint = ","; 243 m_divideBy100 = divideBy100; 244 if (constantValue == null || constantValue.length() == 0 || !m_dataType.equals(DATATYPE_Constant)) 246 { 247 m_constantValue = ""; 248 m_constantIsString = true; 249 } 250 else 251 { 252 m_constantValue = constantValue; 253 m_constantIsString = false; 254 for (int i = 0; i < m_constantValue.length(); i++) 255 { 256 char c = m_constantValue.charAt(i); 257 if (!(Character.isDigit(c) || c == '.')) { 259 m_constantIsString = true; 260 break; 261 } 262 } 263 } 264 if (callout == null) 266 m_callout = ""; 267 else 268 m_callout = callout; 269 } 271 275 public String getDataFormat() 276 { 277 return m_dataFormat; 278 } 279 280 284 public String getDecimalPoint() 285 { 286 return m_decimalPoint; 287 } 288 289 293 public boolean isDivideBy100() 294 { 295 return m_divideBy100; 296 } 297 298 302 public String getConstantValue() 303 { 304 return m_constantValue; 305 } 306 307 311 public String getCallout() 312 { 313 return m_callout; 314 } 316 321 public void setMaxLength (int maxLength) 322 { 323 m_maxLength = maxLength; 324 } 326 327 328 334 public String parse (String info) 335 { 336 if (info == null || info.length() == 0) 337 return ""; 338 339 if (info.startsWith("[") && info.endsWith("]")) 341 return ""; 342 String retValue = null; 344 if (isNumber()) 345 retValue = parseNumber (info); 346 else if (isDate()) 347 retValue = parseDate (info); 348 else if (isConstant()) 349 retValue = m_constantIsString ? parseString (m_constantValue) : m_constantValue; 350 else 351 retValue = parseString (info); 352 if (m_callout.length() > 2) 354 { 355 Callout call = null; 356 String method = m_callout.substring(2); 357 358 if (m_callout.charAt(0) == 'S' && m_callout.charAt(1) == '_') 359 { 360 call = new CalloutSystem(); 361 } 362 else 363 { 364 try 365 { 366 Class cClass = Class.forName("com.compiere.custom.CalloutUser"); 367 call = (Callout)cClass.newInstance(); 368 } 369 catch (Exception e) 370 { 371 Log.error("ImpFormatRow.parse", e); 372 } 373 } 374 if (call == null) 375 Log.error("ImpFormatRow.parse - No Class"); 376 else 377 retValue = call.convert (method, retValue); 378 } 379 if (retValue == null) 381 retValue = ""; 382 return retValue.trim(); 383 } 385 390 private String parseDate (String info) 391 { 392 if (m_dformat == null) 393 { 394 try 395 { 396 m_dformat = new SimpleDateFormat(m_dataFormat); 397 } 398 catch (Exception e) 399 { 400 Log.error ("ImpFormatRow.parseDate Format=" + m_dataFormat, e); 401 } 402 if (m_dformat == null) 403 m_dformat = (SimpleDateFormat)DateFormat.getDateInstance(); 404 m_dformat.setLenient(true); 405 } 406 407 Timestamp ts = null; 408 try 409 { 410 ts = new Timestamp (m_dformat.parse(info).getTime()); 411 } 412 catch (ParseException pe) 413 { 414 Log.error ("ImpFormatRow.parseDate - " + info, pe); 415 } 416 if (ts == null) 417 ts = new Timestamp (System.currentTimeMillis()); 418 String dateString = ts.toString(); 420 return dateString.substring(0, dateString.indexOf(".")); } 423 430 private String parseString (String info) 431 { 432 String retValue = info; 433 if (m_maxLength > 0 && retValue.length() > m_maxLength) 435 retValue = retValue.substring(0, m_maxLength); 436 437 StringBuffer out = new StringBuffer (retValue.length()); 439 for (int i = 0; i < retValue.length(); i++) 440 { 441 char c = retValue.charAt(i); 442 if (c == '\'') 443 out.append("''"); 444 else if (c == '\\') 445 out.append("\\\\"); 446 else 447 out.append(c); 448 } 449 return out.toString(); 450 } 452 457 private String parseNumber (String info) 458 { 459 boolean hasPoint = info.indexOf(".") != -1; 460 boolean hasComma = info.indexOf(",") != -1; 461 if (hasComma && m_decimalPoint.equals(".")) 463 info = info.replace(',', ' '); 464 if (hasPoint && m_decimalPoint.equals(",")) 465 info = info.replace('.', ' '); 466 hasComma = info.indexOf(",") != -1; 467 468 if (hasComma && m_decimalPoint.equals(",")) 470 info = info.replace(',', '.'); 471 472 char[] charArray = info.toCharArray(); 474 StringBuffer sb = new StringBuffer (); 475 for (int i = 0; i < charArray.length; i++) 476 if (Character.isDigit(charArray[i]) || charArray[i] == '.') 477 sb.append(charArray[i]); 478 479 if (sb.length() == 0) 480 return "0"; 481 BigDecimal bd = new BigDecimal(sb.toString()); 482 if (m_divideBy100) bd = bd.divide(new BigDecimal(100.0), 2, BigDecimal.ROUND_HALF_UP); 484 return bd.toString(); 485 } 487 } | Popular Tags |