📄 mprintformat.java
字号:
/**
* Get Table Format
* @return Table Format
*/
public MPrintTableFormat getTableFormat()
{
if (m_tFormat == null)
m_tFormat = MPrintTableFormat.get(getAD_PrintTableFormat_ID(), getAD_PrintFont_ID());
return m_tFormat;
} // getTableFormat
/*************************************************************************/
/**
* Load Special data (images, ..).
* To be extended by sub-classes
* @param rs result set
* @param index zero based index
* @return value value
* @throws SQLException
*/
protected Object loadSpecial (ResultSet rs, int index) throws SQLException
{
// CreateCopy
// Log.trace(Log.l4_Data, "MPrintFormat.loadSpecial", p_info.getColumnName(index));
return null;
} // loadSpecial
/**
* Save Special Data.
* To be extended by sub-classes
* @param value value
* @param index index
* @return SQL code for INSERT VALUES clause
*/
protected String saveNewSpecial (Object value, int index)
{
// CreateCopy
// String colName = p_info.getColumnName(index);
// String colClass = p_info.getColumnClass(index).toString();
// String colValue = value == null ? "null" : value.getClass().toString();
// Log.error("PO.saveNewSpecial - Unknown class for column " + colName + " (" + colClass + ") - Value=" + colValue);
if (value == null)
return "NULL";
return value.toString();
} // saveNewSpecial
/*************************************************************************/
/**
* Create MPrintFormat for Table
* @param ctx context
* @param AD_Table_ID table
* @return print format
*/
static public MPrintFormat createFromTable (Properties ctx, int AD_Table_ID)
{
return createFromTable(ctx, AD_Table_ID, 0);
} // createFromTable
/**
* Create MPrintFormat for Table
* @param ctx context
* @param AD_Table_ID table
* @param AD_PrintFormat_ID 0 or existing PrintFormat
* @return print format
*/
static public MPrintFormat createFromTable (Properties ctx, int AD_Table_ID, int AD_PrintFormat_ID)
{
int AD_Client_ID = Env.getContextAsInt (ctx, "#AD_Client_ID");
Log.trace(Log.l4_Data, "MPrintFormat.createFromTable", "AD_Table_ID=" + AD_Table_ID
+ " - AD_Client_ID=" + AD_Client_ID);
MPrintFormat pf = new MPrintFormat(ctx, AD_PrintFormat_ID);
pf.setValueNoCheck("AD_Table_ID", new Integer(AD_Table_ID));
// Get Info
String sql = "SELECT TableName," // 1
+ " (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,"
+ " COALESCE (cpc.AD_PrintColor_ID, pc.AD_PrintColor_ID) AS AD_PrintColor_ID," // 3
+ " COALESCE (cpf.AD_PrintFont_ID, pf.AD_PrintFont_ID) AS AD_PrintFont_ID,"
+ " COALESCE (cpp.AD_PrintPaper_ID, pp.AD_PrintPaper_ID) AS AD_PrintPaper_ID "
+ "FROM AD_Table t, AD_Client c"
+ " LEFT OUTER JOIN AD_PrintColor cpc ON (cpc.AD_Client_ID=c.AD_Client_ID AND cpc.IsDefault='Y')"
+ " LEFT OUTER JOIN AD_PrintFont cpf ON (cpf.AD_Client_ID=c.AD_Client_ID AND cpf.IsDefault='Y')"
+ " LEFT OUTER JOIN AD_PrintPaper cpp ON (cpp.AD_Client_ID=c.AD_Client_ID AND cpp.IsDefault='Y'),"
+ " AD_PrintColor pc, AD_PrintFont pf, AD_PrintPaper pp "
+ "WHERE t.AD_Table_ID=? AND c.AD_Client_ID=?" // #1/2
+ " AND pc.IsDefault='Y' AND pf.IsDefault='Y' AND pp.IsDefault='Y'";
boolean error = true;
try
{
PreparedStatement pstmt = DB.prepareStatement(sql);
pstmt.setInt(1, AD_Table_ID);
pstmt.setInt(2, AD_Client_ID);
ResultSet rs = pstmt.executeQuery();
if (rs.next())
{
// Name
String TableName = rs.getString(1);
String ColumnName = TableName + "_ID";
String s = ColumnName;
if (!ColumnName.equals("T_Report_ID"))
{
s = Msg.translate (ctx, ColumnName);
if (ColumnName.equals (s)) // not found
s = Msg.translate (ctx, TableName);
}
int count = rs.getInt(2);
if (count > 0)
s += " " + count;
pf.setName(s);
//
pf.setAD_PrintColor_ID(rs.getInt(3));
pf.setAD_PrintFont_ID(rs.getInt(4));
pf.setAD_PrintPaper_ID(rs.getInt(5));
//
error = false;
}
else
Log.error("MPrintFormat.createFromTable - no info found " + AD_Table_ID);
rs.close();
pstmt.close();
}
catch (SQLException e)
{
Log.error("MPrintFormat.createFromTable", e);
}
if (error)
return null;
// Save & complete
if (!pf.save())
return null;
// pf.dump();
pf.setItems (createItems(ctx, pf));
//
return pf;
} // createFromTable
/**
* Create MPrintFormat for ReportView
* @param ctx context
* @param AD_ReportView_ID ReportView
* @param ReportName - optional Report Name
* @return print format
*/
static public MPrintFormat createFromReportView (Properties ctx, int AD_ReportView_ID, String ReportName)
{
int AD_Client_ID = Env.getContextAsInt (ctx, "#AD_Client_ID");
Log.trace(Log.l4_Data, "MPrintFormat.createFromReportView", "AD_ReportView_ID=" + AD_ReportView_ID
+ " - AD_Client_ID=" + AD_Client_ID + " " + ReportName);
MPrintFormat pf = new MPrintFormat(ctx, 0);
pf.setValueNoCheck("AD_ReportView_ID", new Integer(AD_ReportView_ID));
// Get Info
String sql = "SELECT t.TableName,"
+ " (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,"
+ " 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,"
+ " COALESCE (cpp.AD_PrintPaper_ID, pp.AD_PrintPaper_ID) AS AD_PrintPaper_ID,"
+ " t.AD_Table_ID "
+ "FROM AD_ReportView rv"
+ " INNER JOIN AD_Table t ON (rv.AD_Table_ID=t.AD_Table_ID),"
+ " AD_Client c"
+ " LEFT OUTER JOIN AD_PrintColor cpc ON (cpc.AD_Client_ID=c.AD_Client_ID AND cpc.IsDefault='Y')"
+ " LEFT OUTER JOIN AD_PrintFont cpf ON (cpf.AD_Client_ID=c.AD_Client_ID AND cpf.IsDefault='Y')"
+ " LEFT OUTER JOIN AD_PrintPaper cpp ON (cpp.AD_Client_ID=c.AD_Client_ID AND cpp.IsDefault='Y'),"
+ " AD_PrintColor pc, AD_PrintFont pf, AD_PrintPaper pp "
+ "WHERE rv.AD_ReportView_ID=? AND c.AD_Client_ID=?"
+ " AND pc.IsDefault='Y' AND pf.IsDefault='Y' AND pp.IsDefault='Y'";
boolean error = true;
try
{
PreparedStatement pstmt = DB.prepareStatement(sql);
pstmt.setInt(1, AD_ReportView_ID);
pstmt.setInt(2, AD_Client_ID);
ResultSet rs = pstmt.executeQuery();
if (rs.next())
{
// Name
String name = ReportName;
// String TableName = rs.getString(1);
int count = rs.getInt(2);
if (count > 0)
name += " " + count;
pf.setName(name);
//
pf.setAD_PrintColor_ID(rs.getInt(3));
pf.setAD_PrintFont_ID(rs.getInt(4));
pf.setAD_PrintPaper_ID(rs.getInt(5));
//
pf.setValueNoCheck("AD_Table_ID", new Integer(rs.getInt(6)));
error = false;
}
else
Log.error("MPrintFormat.createFromReportView - no info found " + AD_ReportView_ID);
rs.close();
pstmt.close();
}
catch (SQLException e)
{
Log.error("MPrintFormat.createFromReportView", e);
}
if (error)
return null;
// Save & complete
if (!pf.save())
return null;
// pf.dump();
pf.setItems (createItems(ctx, pf));
//
return pf;
} // createFromReportView
/**
* Create Items.
* Using the display order of Fields in some Tab
* @param ctx context
* @param format print format
* @return items
*/
static private MPrintFormatItem[] createItems (Properties ctx, MPrintFormat format)
{
Log.trace(Log.l4_Data, "MPrintFormat.createItems");
ArrayList list = new ArrayList();
String sql = "SELECT c.AD_Column_ID "
+ "FROM AD_Column c"
+ " 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))"
+ " LEFT OUTER JOIN AD_Field f ON (tab.AD_Tab_ID=f.AD_Tab_ID AND c.AD_Column_ID=f.AD_Column_ID) "
+ "WHERE c.AD_Table_ID=? "
+ "ORDER BY f.SeqNo, c.Name";
try
{
PreparedStatement pstmt = DB.prepareStatement(sql);
pstmt.setInt(1, format.getAD_Table_ID());
ResultSet rs = pstmt.executeQuery();
int seqNo = 1;
while (rs.next())
{
MPrintFormatItem pfi = MPrintFormatItem.createFromColumn (ctx, rs.getInt(1), format, seqNo++);
if (pfi != null)
list.add (pfi);
}
rs.close();
pstmt.close();
}
catch (SQLException e)
{
Log.error("MPrintFormat.getItems", e);
}
//
MPrintFormatItem[] retValue = new MPrintFormatItem[list.size()];
list.toArray(retValue);
return retValue;
} // createItems
/**
* Copy Items
* @param fromFormat from print format
* @param toFormat to print format (client, id)
* @return items
*/
static private MPrintFormatItem[] copyItems (MPrintFormat fromFormat, MPrintFormat toFormat)
{
Log.trace(Log.l4_Data, "MPrintFormat.copyItems");
ArrayList list = new ArrayList();
MPrintFormatItem[] items = fromFormat.getItems();
for (int i = 0; i < items.length; i++)
{
MPrintFormatItem pfi = items[i].copyToClient (toFormat.getAD_Client_ID(), toFormat.getID());
if (pfi != null)
list.add (pfi);
}
//
MPrintFormatItem[] retValue = new MPrintFormatItem[list.size()];
list.toArray(retValue);
return retValue;
} // copyItems
/*************************************************************************/
/**
* Copy existing Definition To Client
* @param ctx context
* @param from_AD_PrintFormat_ID format
* @param to_AD_PrintFormat_ID format
* @return print format
*/
public static MPrintFormat copy (Properties ctx, int from_AD_PrintFormat_ID, int to_AD_PrintFormat_ID)
{
return copy (ctx, from_AD_PrintFormat_ID, to_AD_PrintFormat_ID, -1);
}
/**
* Copy existing Definition To Client
* @param ctx context
* @param AD_PrintFormat_ID format
* @param To_Client_ID to client
* @return print format
*/
public static MPrintFormat copyToClient (Properties ctx, int AD_PrintFormat_ID, int To_Client_ID)
{
return copy (ctx, AD_PrintFormat_ID, 0, To_Client_ID);
}
/**
* Copy existing Definition To Client
* @param ctx context
* @param from_AD_PrintFormat_ID format
* @param to_AD_PrintFormat_ID to format
* @param to_Client_ID to client (ignored, if to_AD_PrintFormat_ID <> 0)
* @return print format
*/
private static MPrintFormat copy (Properties ctx, int from_AD_PrintFormat_ID,
int to_AD_PrintFormat_ID, int to_Client_ID)
{
Log.trace(Log.l4_Data, "MPrintFormat.copyToClient", "From_AD_PrintFormat_ID=" + from_AD_PrintFormat_ID
+ "To_AD_PrintFormat_ID=" + to_AD_PrintFormat_ID + ", To_Client_ID=" + to_Client_ID);
if (from_AD_PrintFormat_ID == 0)
throw new IllegalArgumentException ("MPrintFormat.copyToClient - from_AD_PrintFormat_ID is 0");
//
MPrintFormat from = new MPrintFormat(ctx, from_AD_PrintFormat_ID);
MPrintFormat to = new MPrintFormat (ctx, to_AD_PrintFormat_ID); // could be 0
MPrintFormat.copyValues (from, to);
// New
if (to_AD_PrintFormat_ID == 0)
{
to.setValueNoCheck ("AD_Client_ID", new Integer(to_Client_ID));
to.setValueNoCheck ("AD_Org_ID", new Integer(0));
}
// Remove TEMPLATE - add copy
to.setName(Util.replace(to.getName(), "TEMPLATE", String.valueOf(to_Client_ID)));
to.setName(to.getName() + " " + Msg.getMsg(ctx, "Copy"));
//
to.save();
// Copy Items
to.setItems(copyItems(from,to));
return to;
} // copyToClient
/*************************************************************************/
/** Cached Colors */
static private HashMap s_formats = new HashMap();
/**
* Get Format from disk
* @param AD_PrintFormat_ID id
* @return Format
*/
static public MPrintFormat get (int AD_PrintFormat_ID)
{
return new MPrintFormat (Env.getCtx(), AD_PrintFormat_ID);
} // get
/**
* Get Format
* @param AD_PrintFormat_ID id
* @param readFromDisk refresh from disk
* @return Format
*/
static public MPrintFormat get (int AD_PrintFormat_ID, boolean readFromDisk)
{
Integer key = new Integer(AD_PrintFormat_ID);
MPrintFormat pf = null;
if (!readFromDisk)
pf = (MPrintFormat)s_formats.get(key);
if (pf == null)
{
pf = new MPrintFormat (Env.getCtx(), AD_PrintFormat_ID);
s_formats.put(key, pf);
}
return pf;
} // get
/**
* Delete Format from Cache
* @param AD_PrintFormat_ID id
*/
static public void deleteFromCache (int AD_PrintFormat_ID)
{
Integer key = new Integer(AD_PrintFormat_ID);
s_formats.put(key, null);
} // get
/*************************************************************************/
/**
* Test
* @param args arga
*/
static public void main (String[] args)
{
org.compiere.Compiere.startupClient();
/**
MPrintFormat.createFromTable(Env.getCtx(), 496); // Order
MPrintFormat.createFromTable(Env.getCtx(), 497);
MPrintFormat.createFromTable(Env.getCtx(), 516); // Invoice
MPrintFormat.createFromTable(Env.getCtx(), 495);
MPrintFormat.createFromTable(Env.getCtx(), 500); // Shipment
MPrintFormat.createFromTable(Env.getCtx(), 501);
MPrintFormat.createFromTable(Env.getCtx(), 498); // Check
MPrintFormat.createFromTable(Env.getCtx(), 499);
MPrintFormat.createFromTable(Env.getCtx(), 498); // Remittance
**/
} // main
} // MPrintFormat
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -