1 14 package org.compiere.impexp; 15 16 import java.util.*; 17 import java.sql.*; 18 19 import org.compiere.util.*; 20 21 27 public final class ImpFormat 28 { 29 35 public ImpFormat (String name, int AD_Table_ID, String formatType) 36 { 37 setName(name); 38 setTable(AD_Table_ID); 39 setFormatType(formatType); 40 } 42 private String m_name; 43 private String m_formatType; 44 45 46 private int m_AD_Table_ID; 47 private String m_tableName; 48 private String m_tablePK; 49 private String m_tableUnique1; 50 private String m_tableUnique2; 51 private String m_tableUniqueParent; 52 private String m_tableUniqueChild; 53 private String m_BPartner; 55 private ArrayList m_rows = new ArrayList(); 56 57 61 public void setName(String newName) 62 { 63 if (newName == null || newName.length() == 0) 64 throw new IllegalArgumentException ("Name must be at least 1 char"); 65 else 66 m_name = newName; 67 } 68 69 73 public String getName() 74 { 75 return m_name; 76 } 78 82 public void setTable (int AD_Table_ID) 83 { 84 m_AD_Table_ID = AD_Table_ID; 85 m_tableName = null; 86 m_tablePK = null; 87 String sql = "SELECT t.TableName,c.ColumnName " 88 + "FROM AD_Table t INNER JOIN AD_Column c ON (t.AD_Table_ID=c.AD_Table_ID AND c.IsKey='Y') " 89 + "WHERE t.AD_Table_ID=?"; 90 try 91 { 92 PreparedStatement pstmt = DB.prepareStatement(sql); 93 pstmt.setInt(1, AD_Table_ID); 94 ResultSet rs = pstmt.executeQuery(); 95 if (rs.next()) 96 { 97 m_tableName = rs.getString(1); 98 m_tablePK = rs.getString(2); 99 } 100 rs.close(); 101 pstmt.close(); 102 } 103 catch (SQLException e) 104 { 105 Log.error("ImpFormat.setTable", e); 106 } 107 if (m_tableName == null || m_tablePK == null) 108 Log.error("ImpFormat.setTable - Data not forund for AD_Table_ID=" + AD_Table_ID); 109 110 m_tableUnique1 = ""; 112 m_tableUnique2 = ""; 113 m_tableUniqueParent = ""; 114 m_tableUniqueChild = ""; 115 116 if (m_AD_Table_ID == 311) { 118 m_tableUnique1 = "H_UPC"; m_tableUnique2 = "Value"; 120 m_tableUniqueChild = "H_Commodity1"; m_tableUniqueParent = "H_PartnrID"; } 123 else if (m_AD_Table_ID == 532) { 125 m_tableUnique1 = "UPC"; m_tableUnique2 = "Value"; 127 m_tableUniqueChild = "VendorProductNo"; m_tableUniqueParent = "BPartner_Value"; } 130 else if (m_AD_Table_ID == 533) { 132 m_tableUnique1 = "Value"; } 134 else if (m_AD_Table_ID == 534) { 136 m_tableUniqueParent = "ElementName"; m_tableUniqueChild = "Value"; } 139 else if (m_AD_Table_ID == 535) { 141 m_tableUniqueParent = "ReportLineSetName"; m_tableUniqueChild = "Name"; } 144 } 146 150 public int getAD_Table_ID() 151 { 152 return m_AD_Table_ID; 153 } 155 158 public static final String FORMATTYPE_FIXED = "F"; 159 public static final String FORMATTYPE_COMMA = "C"; 160 public static final String FORMATTYPE_TAB = "T"; 161 public static final String FORMATTYPE_XML = "X"; 162 163 167 public void setFormatType(String newFormatType) 168 { 169 if (newFormatType.equals(FORMATTYPE_FIXED) || newFormatType.equals(FORMATTYPE_COMMA) 170 || newFormatType.equals(FORMATTYPE_TAB) || newFormatType.equals(FORMATTYPE_XML)) 171 m_formatType = newFormatType; 172 else 173 throw new IllegalArgumentException ("FormatType must be F/C/T/X"); 174 } 176 180 public String getFormatType() 181 { 182 return m_formatType; 183 } 185 189 public void setBPartner(String newBPartner) 190 { 191 m_BPartner = newBPartner; 192 } 194 198 public String getBPartner() 199 { 200 return m_BPartner; 201 } 203 204 205 209 public void addRow (ImpFormatRow row) 210 { 211 m_rows.add (row); 212 } 214 219 public ImpFormatRow getRow (int index) 220 { 221 if (index >=0 && index < m_rows.size()) 222 return (ImpFormatRow)m_rows.get(index); 223 return null; 224 } 226 230 public int getRowCount() 231 { 232 return m_rows.size(); 233 } 235 236 237 242 public static ImpFormat load (String name) 243 { 244 Log.trace(Log.l3_Util, "ImpFormat.load - " + name); 245 ImpFormat retValue = null; 246 String SQL = "SELECT * FROM AD_ImpFormat WHERE Name=?"; 247 int ID = 0; 248 try 249 { 250 PreparedStatement pstmt = DB.prepareStatement(SQL); 251 pstmt.setString (1, name); 252 ResultSet rs = pstmt.executeQuery(); 253 if (rs.next()) 254 { 255 retValue = new ImpFormat (name, rs.getInt("AD_Table_ID"), rs.getString("FormatType")); 256 ID = rs.getInt ("AD_ImpFormat_ID"); 257 } 258 rs.close(); 259 pstmt.close(); 260 } 261 catch (SQLException e) 262 { 263 Log.error ("ImpFormat.load", e); 264 return null; 265 } 266 loadRows (retValue, ID); 267 return retValue; 268 } 270 275 private static void loadRows (ImpFormat format, int ID) 276 { 277 String SQL = "SELECT f.SeqNo,c.ColumnName,f.StartNo,f.EndNo,f.DataType,c.FieldLength," + "f.DataFormat,f.DecimalPoint,f.DivideBy100,f.ConstantValue,f.Callout " + "FROM AD_ImpFormat_Row f,AD_Column c " 280 + "WHERE AD_ImpFormat_ID=? AND f.AD_Column_ID=c.AD_Column_ID " 281 + "ORDER BY SeqNo"; 282 try 283 { 284 PreparedStatement pstmt = DB.prepareStatement(SQL); 285 pstmt.setInt (1, ID); 286 ResultSet rs = pstmt.executeQuery(); 287 while (rs.next()) 288 { 289 ImpFormatRow row = new ImpFormatRow (rs.getInt(1), 290 rs.getString(2), rs.getInt(3), rs.getInt(4), rs.getString(5), rs.getInt(6)); 291 row.setFormatInfo(rs.getString(7), rs.getString(8), 293 rs.getString(9).equals("Y"), 294 rs.getString(10), rs.getString(11)); 295 format.addRow (row); 297 } 298 rs.close(); 299 pstmt.close(); 300 } 301 catch (SQLException e) 302 { 303 Log.error ("ImpFormat.loadRows", e); 304 } 305 } 307 308 309 318 public String [] parseLine (String line, boolean withLabel, boolean trace, boolean ignoreEmpty) 319 { 320 if (trace) 321 Log.trace(Log.l4_Data, "ImpFormat.parseLine - " + line); 322 323 ArrayList list = new ArrayList(); 324 for (int i = 0; i < m_rows.size(); i++) 326 { 327 ImpFormatRow row = (ImpFormatRow)m_rows.get(i); 328 StringBuffer entry = new StringBuffer (); 329 if (withLabel) 331 { 332 entry.append(row.getColumnName()); 333 entry.append("="); 334 if (row.isString()) 335 entry.append("'"); 336 else if (row.isDate()) 337 entry.append("TO_DATE('"); 338 } 339 340 String info = null; 342 if (row.isConstant()) 343 info = "Constant"; 344 else if (m_formatType.equals(FORMATTYPE_FIXED)) 345 { 346 if (row.getStartNo() > 0 && row.getEndNo() <= line.length()) 348 info = line.substring(row.getStartNo()-1, row.getEndNo()); 349 } 350 else 351 { 352 info = parseFlexFormat (line, m_formatType, row.getStartNo()); 353 } 354 355 if (info == null) 356 info = ""; 357 358 entry.append(row.parse(info)); 360 361 if (withLabel) 363 { 364 if (row.isString()) 365 entry.append("'"); 366 else if (row.isDate()) 367 entry.append("','YYYY-MM-DD HH24:MI:SS')"); } 369 370 if (!ignoreEmpty || (ignoreEmpty && info.length() != 0)) 371 list.add(entry.toString()); 372 if (trace) 374 Log.trace(Log.l5_DData, info + "=>" + entry.toString() + " (Length=" + info.length() + ")"); 375 } 377 String [] retValue = new String [list.size()]; 378 list.toArray(retValue); 379 return retValue; 380 } 382 392 private String parseFlexFormat (String line, String formatType, int fieldNo) 393 { 394 final char QUOTE = '"'; 395 char delimiter = ' '; 397 if (formatType.equals(FORMATTYPE_COMMA)) 398 delimiter = ','; 399 else if (formatType.equals(FORMATTYPE_TAB)) 400 delimiter = '\t'; 401 else 402 throw new IllegalArgumentException ("ImpFormat.parseFlexFormat - unknown format: " + formatType); 403 if (line == null || line.length() == 0 || fieldNo < 0) 404 return ""; 405 406 int pos = 0; 411 int length = line.length(); 412 for (int field = 1; field <= fieldNo && pos < length; field++) 413 { 414 StringBuffer content = new StringBuffer (); 415 if (line.charAt(pos) == delimiter) 417 { 418 pos++; 419 continue; 420 } 421 if (line.charAt(pos) == QUOTE) 423 { 424 pos++; while (pos < length) 426 { 427 if (line.charAt(pos) == QUOTE && pos+1 < length && line.charAt(pos+1) == QUOTE) 429 { 430 content.append(line.charAt(pos++)); 431 pos++; 432 } 433 else if (line.charAt(pos) == QUOTE) 435 { 436 pos++; 437 break; 438 } 439 else 441 content.append(line.charAt(pos++)); 442 } 443 if (pos < length && line.charAt(pos) != delimiter) 445 Log.trace(Log.l1_User, "ImpFormat.parseFlexFormat - Did not find delimiter at pos " + pos, line); 446 pos++; } 448 else { 450 while (pos < length && line.charAt(pos) != delimiter) 451 content.append(line.charAt(pos++)); 452 pos++; } 454 if (field == fieldNo) 455 return content.toString(); 456 } 457 458 return ""; 460 } 462 463 464 470 public boolean updateDB (Properties ctx, String line) 471 { 472 if (line == null || line.trim().length() == 0) 473 { 474 Log.trace(10, "No Line"); 475 return false; 476 } 477 String [] nodes = parseLine (line, true, false, true); if (nodes.length == 0) 479 { 480 Log.trace(10, "Nothing parsed from: " + line); 481 return false; 482 } 483 485 int AD_Client_ID = Env.getContextAsInt(ctx, "#AD_Client_ID"); 487 int AD_Org_ID = Env.getContextAsInt(ctx, "#AD_Org_ID"); 488 int UpdatedBy = Env.getContextAsInt(ctx, "#AD_User_ID"); 489 490 491 StringBuffer sql = new StringBuffer ("SELECT COUNT(*), MAX(") 493 .append(m_tablePK).append(") FROM ").append(m_tableName) 494 .append(" WHERE AD_Client_ID=").append(AD_Client_ID).append(" AND ("); 495 String where1 = null; 497 String where2 = null; 498 String whereParentChild = null; 499 for (int i = 0; i < nodes.length; i++) 500 { 501 if (nodes[i].endsWith("=''") || nodes[i].endsWith("=0")) 502 ; 503 else if (nodes[i].startsWith(m_tableUnique1 + "=")) 504 where1 = nodes[i]; 505 else if (nodes[i].startsWith(m_tableUnique2 + "=")) 506 where2 = nodes[i]; 507 else if (nodes[i].startsWith(m_tableUniqueParent + "=") || nodes[i].startsWith(m_tableUniqueChild + "=")) 508 { 509 if (whereParentChild == null) 510 whereParentChild = nodes[i]; 511 else 512 whereParentChild += " AND " + nodes[i]; 513 } 514 } 515 StringBuffer find = new StringBuffer (); 516 if (where1 != null) 517 find.append(where1); 518 if (where2 != null) 519 { 520 if (find.length() > 0) 521 find.append(" OR "); 522 find.append(where2); 523 } 524 if (whereParentChild != null && whereParentChild.indexOf(" AND ") != -1) { 526 if (find.length() > 0) 527 find.append(" OR (").append(whereParentChild).append(")"); else 529 find.append(whereParentChild); 530 } 531 sql.append(find).append(")"); 532 int count = 0; 533 int ID = 0; 534 try 535 { 536 if (find.length() > 0) 537 { 538 PreparedStatement pstmt = DB.prepareStatement(sql.toString()); 539 ResultSet rs = pstmt.executeQuery(); 540 if (rs.next()) 541 { 542 count = rs.getInt(1); 543 if (count == 1) 544 ID = rs.getInt(2); 545 } 546 rs.close(); 547 pstmt.close(); 548 } 549 } 550 catch (SQLException e) 551 { 552 Log.error("ImpFormat.updateDB - " + sql.toString(), e); 553 return false; 554 } 555 556 557 if (ID == 0) 559 { 560 ID = DB.getKeyNextNo(ctx, 0, m_tableName); sql = new StringBuffer ("INSERT INTO ") 562 .append(m_tableName).append("(").append(m_tablePK).append(",") 563 .append("AD_Client_ID,AD_Org_ID,Created,CreatedBy,Updated,UpdatedBy,IsActive") .append(") VALUES (").append(ID).append(",") 565 .append(AD_Client_ID).append(",").append(AD_Org_ID).append(",SysDate,").append(UpdatedBy) 566 .append(",SysDate,").append(UpdatedBy).append(",'Y'") 567 .append(")"); 568 int no = DB.executeUpdate(sql.toString()); 570 if (no != 1) 571 { 572 Log.error("ImpFormat.updateDB - Insert records=" + no + "; SQL=" + sql.toString()); 573 return false; 574 } 575 Log.trace(Log.l6_Database, "New ID=" + ID, find); 576 } 577 else 578 Log.trace(Log.l6_Database, "Old ID=" + ID, find); 579 580 sql = new StringBuffer ("UPDATE ") 582 .append(m_tableName).append(" SET "); 583 for (int i = 0; i < nodes.length; i++) 584 sql.append(nodes[i]).append(","); sql.append("IsActive='Y',Processed='N',I_IsImported='N',Updated=SysDate,UpdatedBy=").append(UpdatedBy); 586 sql.append(" WHERE ").append(m_tablePK).append("=").append(ID); 587 int no = DB.executeUpdate(sql.toString()); 589 if (no != 1) 590 { 591 Log.error("ImpFormat.updateDB - ID=" + ID + " - rows updated=" + no); 592 return true; 593 } 594 return true; 595 } 597 } | Popular Tags |