1 14 package org.compiere.print; 15 16 import java.util.*; 17 import java.sql.*; 18 19 import org.apache.log4j.Logger; 20 21 import org.compiere.model.*; 22 import org.compiere.util.DB; 23 import org.compiere.util.Env; 24 import org.compiere.util.Msg; 25 import org.compiere.util.Util; 26 import org.compiere.util.CCache; 27 import org.compiere.util.Language; 28 29 35 public class MPrintFormat extends X_AD_PrintFormat 36 { 37 43 private MPrintFormat (Properties ctx, int AD_PrintFormat_ID) 44 { 45 super (ctx, AD_PrintFormat_ID); 46 m_language = Env.getLanguage(ctx); 47 if (AD_PrintFormat_ID == 0) 48 { 49 setStandardHeaderFooter(true); 50 setIsTableBased(true); 51 setIsForm(false); 52 setIsDefault(false); 53 } 54 m_items = getItems(); 55 } 57 58 private MPrintFormatItem[] m_items = null; 59 60 private String m_translationViewLanguage = null; 61 62 private Language m_language; 63 64 private MPrintTableFormat m_tFormat; 65 66 private static Logger s_log = Logger.getLogger (PO.class); 67 68 72 public Language getLanguage() 73 { 74 return m_language; 75 } 77 81 public void setLanguage(Language language) 82 { 83 if (language != null) 84 m_language = language; 85 m_translationViewLanguage = null; 86 } 88 92 public int[] getOrderAD_Column_IDs() 93 { 94 HashMap map = new HashMap(); for (int i = 0; i < m_items.length; i++) 96 { 97 if (m_items[i].getSortNo() != 0 && m_items[i].getAD_Column_ID() != 0) 99 map.put(new Integer (m_items[i].getSortNo()), new Integer (m_items[i].getAD_Column_ID())); 100 } 101 Integer [] keys = new Integer [map.keySet().size()]; 103 map.keySet().toArray(keys); 104 Arrays.sort(keys); 105 106 int[] retValue = new int[keys.length]; 108 for (int i = 0; i < keys.length; i++) 109 { 110 Integer value = (Integer )map.get(keys[i]); 111 retValue[i] = value.intValue(); 112 } 113 return retValue; 114 } 116 120 public int[] getAD_Column_IDs() 121 { 122 ArrayList list = new ArrayList(); 123 for (int i = 0; i < m_items.length; i++) 124 { 125 if (m_items[i].getAD_Column_ID() != 0 && m_items[i].isPrinted()) 126 list.add(new Integer (m_items[i].getAD_Column_ID())); 127 } 128 int[] retValue = new int[list.size()]; 130 for (int i = 0; i < list.size(); i++) 131 retValue[i] = ((Integer )list.get(i)).intValue(); 132 return retValue; 133 } 135 139 private void setItems (MPrintFormatItem[] items) 140 { 141 if (items != null) 142 m_items = items; 143 } 145 149 private MPrintFormatItem[] getItems() 150 { 151 ArrayList list = new ArrayList(); 152 String sql = "SELECT * FROM AD_PrintFormatItem pfi " 153 + "WHERE pfi.AD_PrintFormat_ID=? AND pfi.IsActive='Y'" 154 + " AND NOT EXISTS (SELECT * FROM AD_Column c WHERE pfi.AD_Column_ID=c.AD_Column_ID AND c.ColumnName='Password') " 155 + "ORDER BY SeqNo"; 156 MRole role = MRole.getDefault(getCtx(), false); 157 try 158 { 159 PreparedStatement pstmt = DB.prepareStatement(sql); 160 pstmt.setInt(1, getID()); 161 ResultSet rs = pstmt.executeQuery(); 162 while (rs.next()) 163 { 164 MPrintFormatItem pfi = new MPrintFormatItem(p_ctx, rs); 165 if (role.isColumnAccess(getAD_Table_ID(), pfi.getAD_Column_ID(), true)) 166 list.add (pfi); 167 } 168 rs.close(); 169 pstmt.close(); 170 } 171 catch (SQLException e) 172 { 173 log.error("getItems", e); 174 } 175 MPrintFormatItem[] retValue = new MPrintFormatItem[list.size()]; 177 list.toArray(retValue); 178 return retValue; 179 } 181 185 public int getItemCount() 186 { 187 return m_items.length; 188 } 190 195 public MPrintFormatItem getItem (int index) 196 { 197 if (index < 0 || index >= m_items.length) 198 throw new ArrayIndexOutOfBoundsException ("Index=" + index + " - Length=" + m_items.length); 199 return m_items[index]; 200 } 202 205 public void setTranslation() 206 { 207 StringBuffer sb = new StringBuffer ("UPDATE AD_PrintFormatItem_Trl t" 208 + " SET (PrintName, PrintNameSuffix)=" 209 + " (SELECT PrintName, PrintNameSuffix FROM AD_PrintFormatItem i WHERE i.AD_PrintFormatItem_ID=t.AD_PrintFormatItem_ID) " 210 + "WHERE AD_PrintFormatItem_ID IN" 211 + " (SELECT AD_PrintFormatItem_ID FROM AD_PrintFormatItem WHERE AD_PrintFormat_ID=").append(getID()).append(")"); 212 int no = DB.executeUpdate(sb.toString()); 213 log.debug("setTranslation #" + no); 214 } 216 217 218 222 public void setStandardHeaderFooter (boolean standardHeaderFooter) 223 { 224 super.setIsStandardHeaderFooter(standardHeaderFooter); 225 if (standardHeaderFooter) 226 { 227 setFooterMargin(0); 228 setHeaderMargin(0); 229 } 230 } 232 237 protected void setIsTableBased (boolean tableBased) 238 { 239 setValueNoCheck("IsTableBased", new Boolean (tableBased)); 240 if (tableBased) 241 setIsForm(false); 242 } 244 245 246 250 public void setTranslationLanguage (Language language) 251 { 252 if (language == null || language.isBaseLanguage()) 253 { 254 log.info("setTranslationLanguage - ignored - " + language); 255 m_translationViewLanguage = null; 256 } 257 else 258 { 259 log.info("setTranslationLanguage - " + language.getAD_Language()); 260 m_translationViewLanguage = language.getAD_Language(); 261 m_language = language; 262 } 263 } 265 269 public boolean isTranslationView() 270 { 271 return m_translationViewLanguage != null; 272 } 274 279 public void setTranslationViewQuery (MQuery query) 280 { 281 if (m_translationViewLanguage != null && query != null && query.getTableName().toUpperCase().endsWith("_V")) 283 { 284 query.setTableName(query.getTableName() + "t"); 285 query.addRestriction("AD_Language", MQuery.EQUAL, m_translationViewLanguage); 286 } 287 } 289 290 291 295 public void setAD_PrintTableFormat_ID (int AD_PrintTableFormat_ID) 296 { 297 super.setAD_PrintTableFormat_ID(AD_PrintTableFormat_ID); 298 m_tFormat = MPrintTableFormat.get (AD_PrintTableFormat_ID, getAD_PrintFont_ID()); 299 } 301 305 public MPrintTableFormat getTableFormat() 306 { 307 if (m_tFormat == null) 308 m_tFormat = MPrintTableFormat.get(getAD_PrintTableFormat_ID(), getAD_PrintFont_ID()); 309 return m_tFormat; 310 } 312 316 public String toString() 317 { 318 StringBuffer sb = new StringBuffer ("MPrintFormat[").append(getID()) 319 .append(",").append(getName()) 320 .append("]"); 321 return sb.toString(); 322 } 324 325 326 334 protected Object loadSpecial (ResultSet rs, int index) throws SQLException 335 { 336 return null; 339 } 341 348 protected String saveNewSpecial (Object value, int index) 349 { 350 if (value == null) 356 return "NULL"; 357 return value.toString(); 358 } 360 361 362 363 369 static public MPrintFormat createFromTable (Properties ctx, int AD_Table_ID) 370 { 371 return createFromTable(ctx, AD_Table_ID, 0); 372 } 374 381 static public MPrintFormat createFromTable (Properties ctx, int AD_Table_ID, int AD_PrintFormat_ID) 382 { 383 int AD_Client_ID = Env.getContextAsInt (ctx, "#AD_Client_ID"); 384 s_log.info ("createFromTable - AD_Table_ID=" + AD_Table_ID + " - AD_Client_ID=" + AD_Client_ID); 385 386 MPrintFormat pf = new MPrintFormat(ctx, AD_PrintFormat_ID); 387 pf.setValueNoCheck("AD_Table_ID", new Integer (AD_Table_ID)); 388 389 String sql = "SELECT TableName," + " (SELECT COUNT(*) FROM AD_PrintFormat x WHERE x.AD_Table_ID=t.AD_Table_ID AND x.AD_Client_ID=c.AD_Client_ID) AS Count," 392 + " COALESCE (cpc.AD_PrintColor_ID, pc.AD_PrintColor_ID) AS AD_PrintColor_ID," + " COALESCE (cpf.AD_PrintFont_ID, pf.AD_PrintFont_ID) AS AD_PrintFont_ID," 394 + " COALESCE (cpp.AD_PrintPaper_ID, pp.AD_PrintPaper_ID) AS AD_PrintPaper_ID " 395 + "FROM AD_Table t, AD_Client c" 396 + " LEFT OUTER JOIN AD_PrintColor cpc ON (cpc.AD_Client_ID=c.AD_Client_ID AND cpc.IsDefault='Y')" 397 + " LEFT OUTER JOIN AD_PrintFont cpf ON (cpf.AD_Client_ID=c.AD_Client_ID AND cpf.IsDefault='Y')" 398 + " LEFT OUTER JOIN AD_PrintPaper cpp ON (cpp.AD_Client_ID=c.AD_Client_ID AND cpp.IsDefault='Y')," 399 + " AD_PrintColor pc, AD_PrintFont pf, AD_PrintPaper pp " 400 + "WHERE t.AD_Table_ID=? AND c.AD_Client_ID=?" + " AND pc.IsDefault='Y' AND pf.IsDefault='Y' AND pp.IsDefault='Y'"; 402 boolean error = true; 403 try 404 { 405 PreparedStatement pstmt = DB.prepareStatement(sql); 406 pstmt.setInt(1, AD_Table_ID); 407 pstmt.setInt(2, AD_Client_ID); 408 ResultSet rs = pstmt.executeQuery(); 409 if (rs.next()) 410 { 411 String TableName = rs.getString(1); 413 String ColumnName = TableName + "_ID"; 414 String s = ColumnName; 415 if (!ColumnName.equals("T_Report_ID")) 416 { 417 s = Msg.translate (ctx, ColumnName); 418 if (ColumnName.equals (s)) s = Msg.translate (ctx, TableName); 420 } 421 int count = rs.getInt(2); 422 if (count > 0) 423 s += " " + count; 424 pf.setName(s); 425 pf.setAD_PrintColor_ID(rs.getInt(3)); 427 pf.setAD_PrintFont_ID(rs.getInt(4)); 428 pf.setAD_PrintPaper_ID(rs.getInt(5)); 429 error = false; 431 } 432 else 433 s_log.error("createFromTable - no info found " + AD_Table_ID); 434 rs.close(); 435 pstmt.close(); 436 } 437 catch (SQLException e) 438 { 439 s_log.error("createFromTable", e); 440 } 441 if (error) 442 return null; 443 444 if (!pf.save()) 446 return null; 447 pf.setItems (createItems(ctx, pf)); 449 return pf; 451 } 453 460 static public MPrintFormat createFromReportView (Properties ctx, int AD_ReportView_ID, String ReportName) 461 { 462 int AD_Client_ID = Env.getContextAsInt (ctx, "#AD_Client_ID"); 463 s_log.info ("createFromReportView - AD_ReportView_ID=" + AD_ReportView_ID + " - AD_Client_ID=" + AD_Client_ID + " - " + ReportName); 464 465 MPrintFormat pf = new MPrintFormat(ctx, 0); 466 pf.setValueNoCheck("AD_ReportView_ID", new Integer (AD_ReportView_ID)); 467 468 String sql = "SELECT t.TableName," 470 + " (SELECT COUNT(*) FROM AD_PrintFormat x WHERE x.AD_ReportView_ID=rv.AD_ReportView_ID AND x.AD_Client_ID=c.AD_Client_ID) AS Count," 471 + " COALESCE (cpc.AD_PrintColor_ID, pc.AD_PrintColor_ID) AS AD_PrintColor_ID," 472 + " COALESCE (cpf.AD_PrintFont_ID, pf.AD_PrintFont_ID) AS AD_PrintFont_ID," 473 + " COALESCE (cpp.AD_PrintPaper_ID, pp.AD_PrintPaper_ID) AS AD_PrintPaper_ID," 474 + " t.AD_Table_ID " 475 + "FROM AD_ReportView rv" 476 + " INNER JOIN AD_Table t ON (rv.AD_Table_ID=t.AD_Table_ID)," 477 + " AD_Client c" 478 + " LEFT OUTER JOIN AD_PrintColor cpc ON (cpc.AD_Client_ID=c.AD_Client_ID AND cpc.IsDefault='Y')" 479 + " LEFT OUTER JOIN AD_PrintFont cpf ON (cpf.AD_Client_ID=c.AD_Client_ID AND cpf.IsDefault='Y')" 480 + " LEFT OUTER JOIN AD_PrintPaper cpp ON (cpp.AD_Client_ID=c.AD_Client_ID AND cpp.IsDefault='Y')," 481 + " AD_PrintColor pc, AD_PrintFont pf, AD_PrintPaper pp " 482 + "WHERE rv.AD_ReportView_ID=? AND c.AD_Client_ID=?" 483 + " AND pc.IsDefault='Y' AND pf.IsDefault='Y' AND pp.IsDefault='Y'"; 484 boolean error = true; 485 try 486 { 487 PreparedStatement pstmt = DB.prepareStatement(sql); 488 pstmt.setInt(1, AD_ReportView_ID); 489 pstmt.setInt(2, AD_Client_ID); 490 ResultSet rs = pstmt.executeQuery(); 491 if (rs.next()) 492 { 493 String name = ReportName; 495 int count = rs.getInt(2); 497 if (count > 0) 498 name += " " + count; 499 pf.setName(name); 500 pf.setAD_PrintColor_ID(rs.getInt(3)); 502 pf.setAD_PrintFont_ID(rs.getInt(4)); 503 pf.setAD_PrintPaper_ID(rs.getInt(5)); 504 pf.setValueNoCheck("AD_Table_ID", new Integer (rs.getInt(6))); 506 error = false; 507 } 508 else 509 s_log.error("createFromReportView - no info found " + AD_ReportView_ID); 510 rs.close(); 511 pstmt.close(); 512 } 513 catch (SQLException e) 514 { 515 s_log.error("createFromReportView", e); 516 } 517 if (error) 518 return null; 519 520 if (!pf.save()) 522 return null; 523 pf.setItems (createItems(ctx, pf)); 525 return pf; 527 } 529 530 537 static private MPrintFormatItem[] createItems (Properties ctx, MPrintFormat format) 538 { 539 s_log.info ("createItems"); 540 ArrayList list = new ArrayList(); 541 String sql = "SELECT c.AD_Column_ID " 542 + "FROM AD_Column c" 543 + " LEFT OUTER JOIN AD_Tab tab ON (c.AD_Table_ID=tab.AD_Table_ID AND tab.AD_Tab_ID=(SELECT AD_Tab_ID FROM AD_Tab tt WHERE tt.AD_Table_ID=c.AD_Table_ID AND ROWNUM=1))" 544 + " LEFT OUTER JOIN AD_Field f ON (tab.AD_Tab_ID=f.AD_Tab_ID AND c.AD_Column_ID=f.AD_Column_ID) " 545 + "WHERE c.AD_Table_ID=?" 546 + " AND c.ColumnName<>'Password' " 547 + "ORDER BY f.SeqNo, c.Name"; 548 try 549 { 550 PreparedStatement pstmt = DB.prepareStatement(sql); 551 pstmt.setInt(1, format.getAD_Table_ID()); 552 ResultSet rs = pstmt.executeQuery(); 553 int seqNo = 1; 554 while (rs.next()) 555 { 556 MPrintFormatItem pfi = MPrintFormatItem.createFromColumn (ctx, rs.getInt(1), format, seqNo++); 557 if (pfi != null) 558 list.add (pfi); 559 } 560 rs.close(); 561 pstmt.close(); 562 } 563 catch (SQLException e) 564 { 565 s_log.error("getItems", e); 566 } 567 MPrintFormatItem[] retValue = new MPrintFormatItem[list.size()]; 569 list.toArray(retValue); 570 return retValue; 571 } 573 579 static private MPrintFormatItem[] copyItems (MPrintFormat fromFormat, MPrintFormat toFormat) 580 { 581 s_log.info("copyItems - from " + fromFormat); 582 ArrayList list = new ArrayList(); 583 584 MPrintFormatItem[] items = fromFormat.getItems(); 585 for (int i = 0; i < items.length; i++) 586 { 587 MPrintFormatItem pfi = items[i].copyToClient (toFormat.getAD_Client_ID(), toFormat.getID()); 588 if (pfi != null) 589 list.add (pfi); 590 } 591 MPrintFormatItem[] retValue = new MPrintFormatItem[list.size()]; 593 list.toArray(retValue); 594 return retValue; 595 } 597 598 599 606 public static MPrintFormat copy (Properties ctx, int from_AD_PrintFormat_ID, int to_AD_PrintFormat_ID) 607 { 608 return copy (ctx, from_AD_PrintFormat_ID, to_AD_PrintFormat_ID, -1); 609 } 610 611 618 public static MPrintFormat copyToClient (Properties ctx, int AD_PrintFormat_ID, int To_Client_ID) 619 { 620 return copy (ctx, AD_PrintFormat_ID, 0, To_Client_ID); 621 } 622 623 631 private static MPrintFormat copy (Properties ctx, int from_AD_PrintFormat_ID, 632 int to_AD_PrintFormat_ID, int to_Client_ID) 633 { 634 s_log.info ("copyToClient - From_AD_PrintFormat_ID=" + from_AD_PrintFormat_ID 635 + ", To_AD_PrintFormat_ID=" + to_AD_PrintFormat_ID + ", To_Client_ID=" + to_Client_ID); 636 if (from_AD_PrintFormat_ID == 0) 637 throw new IllegalArgumentException ("MPrintFormat.copyToClient - from_AD_PrintFormat_ID is 0"); 638 MPrintFormat from = new MPrintFormat(ctx, from_AD_PrintFormat_ID); 640 MPrintFormat to = new MPrintFormat (ctx, to_AD_PrintFormat_ID); MPrintFormat.copyValues (from, to); 642 if (to_AD_PrintFormat_ID == 0) 644 { 645 to.setValueNoCheck ("AD_Client_ID", new Integer (to_Client_ID)); 646 to.setValueNoCheck ("AD_Org_ID", new Integer (0)); 647 } 648 to.setName(Util.replace(to.getName(), "TEMPLATE", String.valueOf(to_Client_ID))); 650 to.setName(to.getName() + " " + Msg.getMsg(ctx, "Copy")); 651 to.save(); 653 654 to.setItems(copyItems(from,to)); 656 return to; 657 } 659 660 661 662 static private CCache s_formats = new CCache("printFormat", 30); 663 664 669 static public MPrintFormat get (int AD_PrintFormat_ID) 670 { 671 return new MPrintFormat (Env.getCtx(), AD_PrintFormat_ID); 672 } 674 680 static public MPrintFormat get (int AD_PrintFormat_ID, boolean readFromDisk) 681 { 682 Integer key = new Integer (AD_PrintFormat_ID); 683 MPrintFormat pf = null; 684 if (!readFromDisk) 685 pf = (MPrintFormat)s_formats.get(key); 686 if (pf == null) 687 { 688 pf = new MPrintFormat (Env.getCtx(), AD_PrintFormat_ID); 689 s_formats.put(key, pf); 690 } 691 return pf; 692 } 694 698 static public void deleteFromCache (int AD_PrintFormat_ID) 699 { 700 Integer key = new Integer (AD_PrintFormat_ID); 701 s_formats.put(key, null); 702 } 704 705 706 710 static public void main (String [] args) 711 { 712 org.compiere.Compiere.startupClient(); 713 725 } 727 728 } | Popular Tags |