📄 reportengine.java
字号:
m_printData.setRowIndex(row);
// for all columns
for (int col = 0; col < m_printFormat.getItemCount(); col++)
{
MPrintFormatItem item = m_printFormat.getItem(col);
if (item.isPrinted())
{
// header row
if (row == -1)
{
th th = new th();
tr.addElement(th);
th.addElement(Util.maskHTML(item.getPrintName(language)));
}
else
{
td td = new td();
tr.addElement(td);
Object obj = m_printData.getNode(new Integer(item.getAD_Column_ID()));
if (obj == null)
td.addElement(" ");
else if (obj instanceof PrintDataElement)
{
String value = ((PrintDataElement)obj).getValueDisplay(language); // formatted
td.addElement(Util.maskHTML(value));
}
else if (obj instanceof PrintData)
{
// ignore contained Data
}
else
log.log(Level.SEVERE, "Element not PrintData(Element) " + obj.getClass());
}
} // printed
} // for all columns
} // for all rows
//
PrintWriter w = new PrintWriter(writer);
if (onlyTable)
table.output(w);
else
{
XhtmlDocument doc = new XhtmlDocument();
doc.appendBody(table);
doc.output(w);
}
w.flush();
w.close();
}
catch (Exception e)
{
log.log(Level.SEVERE, "(w)", e);
}
return false;
} // createHTML
/**************************************************************************
* Create CSV File
* @param file file
* @param delimiter delimiter, e.g. comma, tab
* @param language translation language
* @return true if success
*/
public boolean createCSV (File file, char delimiter, Language language)
{
try
{
FileWriter fw = new FileWriter (file, false);
return createCSV (new BufferedWriter(fw), delimiter, language);
}
catch (FileNotFoundException fnfe)
{
log.log(Level.SEVERE, "(f) - " + fnfe.toString());
}
catch (Exception e)
{
log.log(Level.SEVERE, "(f)", e);
}
return false;
} // createCSV
/**
* Write CSV to writer
* @param writer writer
* @param delimiter delimiter, e.g. comma, tab
* @param language translation language
* @return true if success
*/
public boolean createCSV (Writer writer, char delimiter, Language language)
{
if (delimiter == 0)
delimiter = '\t';
try
{
// for all rows (-1 = header row)
for (int row = -1; row < m_printData.getRowCount(); row++)
{
StringBuffer sb = new StringBuffer();
if (row != -1)
m_printData.setRowIndex(row);
// for all columns
boolean first = true; // first column to print
for (int col = 0; col < m_printFormat.getItemCount(); col++)
{
MPrintFormatItem item = m_printFormat.getItem(col);
if (item.isPrinted())
{
// column delimiter (comma or tab)
if (first)
first = false;
else
sb.append(delimiter);
// header row
if (row == -1)
createCSVvalue (sb, delimiter,
m_printFormat.getItem(col).getPrintName(language));
else
{
Object obj = m_printData.getNode(new Integer(item.getAD_Column_ID()));
String data = "";
if (obj == null)
;
else if (obj instanceof PrintDataElement)
{
PrintDataElement pde = (PrintDataElement)obj;
if (pde.isPKey())
data = pde.getValueAsString();
else
data = pde.getValueDisplay(language); // formatted
}
else if (obj instanceof PrintData)
{
}
else
log.log(Level.SEVERE, "Element not PrintData(Element) " + obj.getClass());
createCSVvalue (sb, delimiter, data);
}
} // printed
} // for all columns
writer.write(sb.toString());
writer.write(Env.NL);
} // for all rows
//
writer.flush();
writer.close();
}
catch (Exception e)
{
log.log(Level.SEVERE, "(w)", e);
}
return false;
} // createCSV
/**
* Add Content to CSV string.
* Encapsulate/mask content in " if required
* @param sb StringBuffer to add to
* @param delimiter delimiter
* @param content column value
*/
private void createCSVvalue (StringBuffer sb, char delimiter, String content)
{
// nothing to add
if (content == null || content.length() == 0)
return;
//
boolean needMask = false;
StringBuffer buff = new StringBuffer();
char chars[] = content.toCharArray();
for (int i = 0; i < chars.length; i++)
{
char c = chars[i];
if (c == '"')
{
needMask = true;
buff.append(c); // repeat twice
} // mask if any control character
else if (!needMask && (c == delimiter || !Character.isLetterOrDigit(c)))
needMask = true;
buff.append(c);
}
// Optionally mask value
if (needMask)
sb.append('"').append(buff).append('"');
else
sb.append(buff);
} // addCSVColumnValue
/**************************************************************************
* Create XML File
* @param file file
* @return true if success
*/
public boolean createXML (File file)
{
try
{
FileWriter fw = new FileWriter (file, false);
return createXML (new BufferedWriter(fw));
}
catch (FileNotFoundException fnfe)
{
log.log(Level.SEVERE, "(f) - " + fnfe.toString());
}
catch (Exception e)
{
log.log(Level.SEVERE, "(f)", e);
}
return false;
} // createXML
/**
* Write XML to writer
* @param writer writer
* @return true if success
*/
public boolean createXML (Writer writer)
{
try
{
m_printData.createXML(new StreamResult(writer));
writer.flush();
writer.close();
}
catch (Exception e)
{
log.log(Level.SEVERE, "(w)", e);
}
return false;
} // createXML
/**************************************************************************
* Create PDF file.
* (created in temporary storage)
* @return PDF file
*/
public File getPDF()
{
return getPDF (null);
} // getPDF
/**
* Create PDF file.
* @param file file
* @return PDF file
*/
public File getPDF (File file)
{
try
{
if (file == null)
file = File.createTempFile ("ReportEngine", ".pdf");
}
catch (IOException e)
{
log.log(Level.SEVERE, "", e);
}
if (createPDF (file))
return file;
return null;
} // getPDF
/**
* Create PDF File
* @param file file
* @return true if success
*/
public boolean createPDF (File file)
{
String fileName = null;
URI uri = null;
try
{
if (file == null)
file = File.createTempFile ("ReportEngine", ".pdf");
fileName = file.getAbsolutePath();
uri = file.toURI();
if (file.exists())
file.delete();
}
catch (Exception e)
{
log.log(Level.SEVERE, "file", e);
return false;
}
log.fine(uri.toString());
try
{
if (m_layout == null)
layout ();
ArchiveEngine.get().archive(m_layout, m_info);
Document.getPDFAsFile(fileName, m_layout.getPageable(false));
}
catch (Exception e)
{
log.log(Level.SEVERE, "PDF", e);
return false;
}
File file2 = new File(fileName);
log.info(file2.getAbsolutePath() + " - " + file2.length());
return file2.exists();
} // createPDF
/**
* Create PDF as Data array
* @return pdf data
*/
public byte[] createPDFData ()
{
try
{
if (m_layout == null)
layout ();
return Document.getPDFAsArray(m_layout.getPageable(false));
}
catch (Exception e)
{
log.log(Level.SEVERE, "PDF", e);
}
return null;
} // createPDFData
/**************************************************************************
* Create PostScript File
* @param file file
* @return true if success
*/
public boolean createPS (File file)
{
try
{
return createPS (new FileOutputStream(file));
}
catch (FileNotFoundException fnfe)
{
log.log(Level.SEVERE, "(f) - " + fnfe.toString());
}
catch (Exception e)
{
log.log(Level.SEVERE, "(f)", e);
}
return false;
} // createPS
/**
* Write PostScript to writer
* @param fos file output stream
* @return true if success
*/
public boolean createPS (FileOutputStream fos)
{
try
{
String outputMimeType = DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType();
DocFlavor docFlavor = DocFlavor.SERVICE_FORMATTED.PAGEABLE;
StreamPrintServiceFactory[] spsfactories =
StreamPrintServiceFactory.lookupStreamPrintServiceFactories(docFlavor, outputMimeType);
if (spsfactories.length == 0)
{
log.log(Level.SEVERE, "(fos) - No StreamPrintService");
return false;
}
// just use first one - sun.print.PSStreamPrinterFactory
// System.out.println("- " + spsfactories[0]);
StreamPrintService sps = spsfactories[0].getPrintService(fos);
// get format
if (m_layout == null)
layout();
// print it
sps.createPrintJob().print(m_layout.getPageable(false),
new HashPrintRequestAttributeSet());
//
fos.flush();
fos.close();
}
catch (Exception e)
{
log.log(Level.SEVERE, "(fos)", e);
}
return false;
} // createPS
/**************************************************************************
* Get Report Engine for process info
* @param ctx context
* @param pi process info with AD_PInstance_ID
* @return report engine or null
*/
static public ReportEngine get (Properties ctx, ProcessInfo pi)
{
int AD_Client_ID = Env.getAD_Client_ID(ctx);
//
int AD_Table_ID = 0;
int AD_ReportView_ID = 0;
String TableName = null;
String whereClause = "";
int AD_PrintFormat_ID = 0;
boolean IsForm = false;
int Client_ID = -1;
// Get AD_Table_ID and TableName
String sql = "SELECT rv.AD_ReportView_ID,rv.WhereClause,"
+ " t.AD_Table_ID,t.TableName, pf.AD_PrintFormat_ID, pf.IsForm, pf.AD_Client_ID "
+ "FROM AD_PInstance pi"
+ " INNER JOIN AD_Process p ON (pi.AD_Process_ID=p.AD_Process_ID)"
+ " INNER JOIN AD_ReportView rv ON (p.AD_ReportView_ID=rv.AD_ReportView_ID)"
+ " INNER JOIN AD_Table t ON (rv.AD_Table_ID=t.AD_Table_ID)"
+ " LEFT OUTER JOIN AD_PrintFormat pf ON (p.AD_ReportView_ID=pf.AD_ReportView_ID AND pf.AD_Client_ID IN (0,?)) "
+ "WHERE pi.AD_PInstance_ID=? " // #2
+ "ORDER BY pf.AD_Client_ID DESC, pf.IsDefault DESC"; // own first
try
{
PreparedStatement pstmt = DB.prepareStatement(sql, null);
pstmt.setInt(1, AD_Client_ID);
pstmt.setInt(2, pi.getAD_PInstance_ID());
ResultSet rs = pstmt.executeQuery();
// Just get first
if (rs.next())
{
AD_ReportView_ID = rs.getInt(1); // required
whereClause = rs.getString(2);
if (rs.wasNull())
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -