IDEMPIERE-3996 Implement DisplayLogic for PrintFormatItem

This commit is contained in:
Heng Sin Low 2019-06-28 17:39:43 +08:00
parent df72426ab1
commit 2af8ec1a39
6 changed files with 264 additions and 78 deletions

View File

@ -23,6 +23,8 @@ import java.util.ArrayList;
import java.util.Properties;
import org.compiere.model.MQuery;
import org.compiere.util.Evaluator;
import org.compiere.util.Util;
/**
* Header Footer
@ -49,6 +51,8 @@ public class HeaderFooter
/** Header/Footer content as Array */
private PrintElement[] m_pe = null;
private Page m_currentPage;
/**
* Add Print Element to Page
* @param element print element
@ -86,7 +90,17 @@ public class HeaderFooter
Point pageStart = new Point(bounds.getLocation());
getElements();
for (int i = 0; i < m_pe.length; i++)
{
if (m_currentPage != null && !Util.isEmpty(m_pe[i].getPageLogic(), true) && m_pe[i].getPrintData() != null
&& m_pe[i].getRowIndex() >= 0)
{
PrintDataEvaluatee evaluatee = new PrintDataEvaluatee(m_currentPage, m_pe[i].getPrintData());
boolean display = Evaluator.evaluateLogic(evaluatee, m_pe[i].getPageLogic());
if (!display)
continue;
}
m_pe[i].paint(g2D, 0, pageStart, m_ctx, isView);
}
} // paint
/**
@ -105,4 +119,9 @@ public class HeaderFooter
return retValue;
} // getDrillDown
public void setCurrentPage(Page page)
{
m_currentPage = page;
}
} // HeaderFooter

View File

@ -50,7 +50,6 @@ import javax.print.DocFlavor;
import javax.print.attribute.DocAttributeSet;
import org.compiere.model.MClientInfo;
import org.compiere.model.MColumn;
import org.compiere.model.MQuery;
import org.compiere.model.MSysConfig;
import org.compiere.model.MTable;
@ -72,7 +71,6 @@ import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.DisplayType;
import org.compiere.util.Env;
import org.compiere.util.Evaluatee;
import org.compiere.util.Evaluator;
import org.compiere.util.KeyNamePair;
import org.compiere.util.Msg;
@ -1101,7 +1099,7 @@ public class LayoutEngine implements Pageable, Printable, Doc
// Type
PrintElement element = null;
if ( !isDisplayed(item) )
if ( !PrintDataEvaluatee.hasPageLogic(item.getDisplayLogic()) && !isDisplayed(m_data, item) )
{
;
}
@ -1214,6 +1212,14 @@ public class LayoutEngine implements Pageable, Printable, Doc
m_currPage.addElement (element);
else
m_headerFooter.addElement (element);
if (PrintDataEvaluatee.hasPageLogic(item.getDisplayLogic()))
{
element.setPrintData(m_data);
element.setRowIndex(row);
element.setPageLogic(item.getDisplayLogic());
}
//
if (m_lastHeight[m_area] > m_maxHeightSinceNewLine[m_area])
m_maxHeightSinceNewLine[m_area] = m_lastHeight[m_area];
@ -1623,6 +1629,8 @@ public class LayoutEngine implements Pageable, Printable, Doc
Boolean [] colSuppressRepeats = new Boolean[columnCount];
String[] columnJustification = new String[columnCount];
HashMap<Integer,Integer> additionalLines = new HashMap<Integer,Integer>();
ArrayList<String> pageLogics = new ArrayList<String>();
boolean hasPageLogic = false;
int col = 0;
for (int c = 0; c < format.getItemCount(); c++)
@ -1669,6 +1677,16 @@ public class LayoutEngine implements Pageable, Printable, Doc
rowColColor.put(new Point(TableElement.ALL, col), color.getColor());
}
//
if (PrintDataEvaluatee.hasPageLogic(item.getDisplayLogic()))
{
pageLogics.add(item.getDisplayLogic());
hasPageLogic = true;
}
else
{
pageLogics.add(null);
}
//
col++;
}
}
@ -1679,7 +1697,7 @@ public class LayoutEngine implements Pageable, Printable, Doc
KeyNamePair[] pk = new KeyNamePair[rows];
String pkColumnName = null;
ArrayList<Integer> functionRows = new ArrayList<Integer>();
ArrayList<Integer> pageBreak = new ArrayList<Integer>();
ArrayList<Integer> pageBreak = new ArrayList<Integer>();
// for all rows
for (int row = 0; row < rows; row++)
@ -1724,7 +1742,7 @@ public class LayoutEngine implements Pageable, Printable, Doc
Serializable dataElement = null;
if (item.isPrinted()) // Text Columns
{
if ( !isDisplayed(item) )
if ( !PrintDataEvaluatee.hasPageLogic(item.getDisplayLogic()) && !isDisplayed(printData, item) )
{
;
}
@ -1802,6 +1820,13 @@ public class LayoutEngine implements Pageable, Printable, Doc
if (format == m_format)
this.colSuppressRepeats = colSuppressRepeats;
if (hasPageLogic)
{
table.setPageLogics(pageLogics);
table.setTablePrintData(printData);
}
return table;
} // layoutTable
@ -1891,7 +1916,9 @@ public class LayoutEngine implements Pageable, Printable, Doc
//
// log.fine("#" + m_id, "PageIndex=" + pageIndex + ", Copy=" + m_isCopy);
page.paint((Graphics2D)graphics, r, false, m_isCopy); // sets context
getHeaderFooter().setCurrentPage(page);
getHeaderFooter().paint((Graphics2D)graphics, r, false);
getHeaderFooter().setCurrentPage(null);
//
return Printable.PAGE_EXISTS;
} // print
@ -2005,81 +2032,10 @@ public class LayoutEngine implements Pageable, Printable, Doc
return childPrintFormatDetails;
}
private boolean isDisplayed(MPrintFormatItem item) {
private boolean isDisplayed(PrintData data, MPrintFormatItem item) {
if ( Util.isEmpty(item.getDisplayLogic() ))
return true;
boolean display = Evaluator.evaluateLogic(new Evaluatee() {
@Override
public String get_ValueAsString(String variableName) {
if (Page.CONTEXT_PAGE.equals(variableName)) {
return String.valueOf(getPageNo());
} else if (Page.CONTEXT_PAGECOUNT.equals(variableName)) {
return String.valueOf(getNumberOfPages());
}
//ref column
String foreignColumn = "";
int f = variableName.indexOf('.');
if (f > 0) {
foreignColumn = variableName.substring(f+1, variableName.length());
variableName = variableName.substring(0, f);
}
Object obj = m_data.getNode(variableName);
if ( obj == null || !(obj instanceof PrintDataElement))
return "";
PrintDataElement data = (PrintDataElement) obj;
if (data.isNull() )
return "";
String value = null;
if (data.getValue() instanceof Boolean)
value = ((Boolean)data.getValue()).booleanValue() ? "Y" : "N";
else
value = data.getValueAsString();
if (!Util.isEmpty(value) && !Util.isEmpty(foreignColumn) && variableName.endsWith("_ID")) {
String refValue = "";
int id = 0;
try {
id = Integer.parseInt(value);
} catch (Exception e){}
if (id > 0) {
String tableName = null;
if (!Util.isEmpty(m_data.getTableName()))
tableName = m_data.getTableName();
else
tableName = variableName.substring(0, variableName.length()-3);
MColumn column = MColumn.get(m_data.getCtx(), tableName, variableName);
if (column != null) {
String foreignTable = column.getReferenceTableName();
refValue = DB.getSQLValueString(null,
"SELECT " + foreignColumn + " FROM " + foreignTable + " WHERE "
+ foreignTable + "_ID = ?", id);
} else {
if (variableName.startsWith("#") || variableName.startsWith("$")) {
variableName = variableName.substring(1);
} else if (variableName.indexOf("|") > 0) {
variableName = variableName.substring(variableName.lastIndexOf("|")+1);
}
String foreignTable = null;
if (foreignColumn.indexOf(".") > 0) {
foreignTable = foreignColumn.substring(0, foreignColumn.indexOf("."));
} else {
foreignTable = variableName.substring(0, variableName.length()-3);
}
MTable t = MTable.get(Env.getCtx(), foreignTable);
if (t != null) {
refValue = DB.getSQLValueString(null,
"SELECT " + foreignColumn + " FROM " + foreignTable + " WHERE "
+ foreignTable + "_ID = ?", id);
}
}
}
return refValue;
}
return value;
}
}, item.getDisplayLogic());
boolean display = Evaluator.evaluateLogic(new PrintDataEvaluatee(getPage(getPageNo()), data), item.getDisplayLogic());
return display;
}

View File

@ -25,7 +25,9 @@ import java.util.ArrayList;
import java.util.Properties;
import org.compiere.model.MQuery;
import org.compiere.util.Evaluator;
import org.compiere.util.Msg;
import org.compiere.util.Util;
/**
* Layout Page
@ -118,6 +120,11 @@ public class Page
m_pageCount = pageCount;
} // setPageCount
public int getPageCount()
{
return m_pageCount;
}
/**
* Add Print Element to Page
* @param element print element
@ -180,7 +187,17 @@ public class Page
for (int i = 0; i < m_elements.size(); i++)
{
PrintElement e = (PrintElement)m_elements.get(i);
if (e.getPrintData() != null && !Util.isEmpty(e.getPageLogic(), true) && e.getRowIndex() >= 0)
{
e.getPrintData().setRowIndex(e.getRowIndex());
PrintDataEvaluatee evaluatee = new PrintDataEvaluatee(this, e.getPrintData());
boolean display = Evaluator.evaluateLogic(evaluatee, e.getPageLogic());
if (!display)
continue;
}
e.setCurrentPage(this);
e.paint(g2D, m_pageNo, pageStart, m_ctx, isView);
e.setCurrentPage(null);
}
} // paint

View File

@ -0,0 +1,116 @@
/******************************************************************************
* Project: Trek Global ERP *
* Copyright (C) 2009-2018 Trek Global Corporation *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms version 2 of the GNU General Public License as published *
* by the Free Software Foundation. This program is distributed in the hope *
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU General Public License for more details. *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
*****************************************************************************/
package org.compiere.print.layout;
import org.compiere.model.MColumn;
import org.compiere.model.MTable;
import org.compiere.print.PrintData;
import org.compiere.print.PrintDataElement;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Evaluatee;
import org.compiere.util.Util;
/**
*
* @author hengsin
*
*/
public class PrintDataEvaluatee implements Evaluatee {
private PrintData m_data;
private Page m_page;
public PrintDataEvaluatee(Page page, PrintData data) {
m_data = data;
m_page = page;
}
@Override
public String get_ValueAsString(String variableName) {
if (Page.CONTEXT_PAGE.equals(variableName)) {
return String.valueOf(m_page.getPageNo());
} else if (Page.CONTEXT_PAGECOUNT.equals(variableName)) {
return String.valueOf(m_page.getPageCount());
}
//ref column
String foreignColumn = "";
int f = variableName.indexOf('.');
if (f > 0) {
foreignColumn = variableName.substring(f+1, variableName.length());
variableName = variableName.substring(0, f);
}
Object obj = m_data.getNode(variableName);
if ( obj == null || !(obj instanceof PrintDataElement))
return "";
PrintDataElement data = (PrintDataElement) obj;
if (data.isNull() )
return "";
String value = null;
if (data.getValue() instanceof Boolean)
value = ((Boolean)data.getValue()).booleanValue() ? "Y" : "N";
else
value = data.getValueAsString();
if (!Util.isEmpty(value) && !Util.isEmpty(foreignColumn) && variableName.endsWith("_ID")) {
String refValue = "";
int id = 0;
try {
id = Integer.parseInt(value);
} catch (Exception e){}
if (id > 0) {
String tableName = null;
if (!Util.isEmpty(m_data.getTableName()))
tableName = m_data.getTableName();
else
tableName = variableName.substring(0, variableName.length()-3);
MColumn column = MColumn.get(m_data.getCtx(), tableName, variableName);
if (column != null) {
String foreignTable = column.getReferenceTableName();
refValue = DB.getSQLValueString(null,
"SELECT " + foreignColumn + " FROM " + foreignTable + " WHERE "
+ foreignTable + "_ID = ?", id);
} else {
if (variableName.startsWith("#") || variableName.startsWith("$")) {
variableName = variableName.substring(1);
} else if (variableName.indexOf("|") > 0) {
variableName = variableName.substring(variableName.lastIndexOf("|")+1);
}
String foreignTable = null;
if (foreignColumn.indexOf(".") > 0) {
foreignTable = foreignColumn.substring(0, foreignColumn.indexOf("."));
} else {
foreignTable = variableName.substring(0, variableName.length()-3);
}
MTable t = MTable.get(Env.getCtx(), foreignTable);
if (t != null) {
refValue = DB.getSQLValueString(null,
"SELECT " + foreignColumn + " FROM " + foreignTable + " WHERE "
+ foreignTable + "_ID = ?", id);
}
}
}
return refValue;
}
return value;
}
public static boolean hasPageLogic(String displayLogic) {
if (Util.isEmpty(displayLogic, true))
return false;
return displayLogic.contains("@*Page");
}
}

View File

@ -30,6 +30,7 @@ import java.util.logging.Level;
import org.compiere.model.MQuery;
import org.compiere.print.MPrintFormatItem;
import org.compiere.print.PrintData;
import org.compiere.util.CLogger;
/**
@ -72,6 +73,14 @@ public abstract class PrintElement implements ImageObserver, Serializable
/** Logger */
protected CLogger log = CLogger.getCLogger(getClass());
private Page m_currentPage;
private PrintData m_printData;
private int m_rowIndex = -1;
private String m_pageLogic;
/**************************************************************************
@ -357,4 +366,43 @@ public abstract class PrintElement implements ImageObserver, Serializable
return sb.toString();
} // toString
public void setCurrentPage(Page page)
{
m_currentPage = page;
}
protected Page getCurrentPage()
{
return m_currentPage;
}
public void setPrintData(PrintData printData)
{
m_printData = printData;
}
public PrintData getPrintData()
{
return m_printData;
}
public void setRowIndex(int row)
{
m_rowIndex = row;
}
public int getRowIndex()
{
return m_rowIndex;
}
public void setPageLogic(String displayLogic)
{
m_pageLogic = displayLogic;
}
public String getPageLogic()
{
return m_pageLogic;
}
} // PrintElement

View File

@ -43,8 +43,10 @@ import java.util.regex.Pattern;
import org.compiere.model.MQuery;
import org.compiere.print.MPrintFormatItem;
import org.compiere.print.MPrintTableFormat;
import org.compiere.print.PrintData;
import org.compiere.print.util.SerializableMatrix;
import org.compiere.print.util.SerializableMatrixImpl;
import org.compiere.util.Evaluator;
import org.compiere.util.KeyNamePair;
import org.compiere.util.NamePair;
import org.compiere.util.Util;
@ -1449,6 +1451,22 @@ public class TableElement extends PrintElement
if (Arrays.equals(lastItems,printItems) )
suppress = true;
}
if (!suppress)
{
if (m_tablePrintData != null && m_pageLogics != null && col < m_pageLogics.size())
{
String pageLogic = m_pageLogics.get(col);
if (!Util.isEmpty(pageLogic, true))
{
m_tablePrintData.setRowIndex(row);
PrintDataEvaluatee evaluatee = new PrintDataEvaluatee(getCurrentPage(), m_tablePrintData);
boolean display = Evaluator.evaluateLogic(evaluatee, pageLogic);
if (!display)
suppress = true;
}
}
}
if ( !suppress )
{
@ -1669,6 +1687,8 @@ public class TableElement extends PrintElement
/** Print Data */
private SerializableMatrix<ArrayList<Serializable>> m_printRows = new SerializableMatrixImpl<ArrayList<Serializable>>("PrintRows");
private ArrayList<String> m_pageLogics;
private PrintData m_tablePrintData;
/**
* Insert empty Row after current Row
@ -1705,4 +1725,14 @@ public class TableElement extends PrintElement
return coordinate.toArray();
} // getPrintItems
public void setPageLogics(ArrayList<String> pageLogics)
{
m_pageLogics = pageLogics;
}
public void setTablePrintData(PrintData printData)
{
m_tablePrintData = printData;
}
}