}
final TypedTableModel resultSet = new TypedTableModel();
final int queryLimit = calculateQueryLimit(parameters);
final OpenERPHelper helper = new OpenERPHelper();
final OpenERPConfiguration targetConfig = config.clone();
final ArrayList<OpenERPFilterInfo> configFilters = targetConfig.getFilters();
// Build a hashmap to pass all parameters as a dictionary to a custom OpenERP procedure
final HashMap<String, Object> openERPParams = new HashMap<String, Object>();
for (final String paramName : parameters.getColumnNames())
{
Object value = parameters.get(paramName);
if (value == null)
{
value = false;
}
openERPParams.put(paramName, value);
}
// Can't get selected fields from config, because we may be calling a custom function
ArrayList<OpenERPFieldInfo> selectedFields = null;
try
{
selectedFields = helper.getFields(targetConfig, openERPParams);
}
catch (Exception e1)
{
throw new ReportDataFactoryException("Failed to select field", e1);
}
// Build a field list
for (final OpenERPFieldInfo selectedFld : selectedFields)
{
resultSet.addColumn(selectedFld.getRenamedFieldName(), convertFieldType(selectedFld.getFieldType()));
}
// Called by the designer to get column layout, return a empty resultSet with columns already set
if (queryLimit == 1)
{
return resultSet;
}
// Parameter parser to replace parameters in strings
final PropertyLookupParser parameterParser = new PropertyLookupParser()
{
private static final long serialVersionUID = -7264648195698966110L;
@Override
protected String lookupVariable(final String property)
{
return parameters.get(property).toString();
}
};
// Replace parameterized filters with values from parameters
if (configFilters != null)
{
for (final OpenERPFilterInfo filter : configFilters)
{
// You could have set a filter without using the Designer. Then the filter could be any data type that should not be converted to a String.
if (filter.getValue() instanceof String)
{
try
{
final String realFilterValue = filter.getValue().toString();
// If you specify the filter on its own, try in get the object value
// Not all parameter values are a string. Could be an Object[] of ids for example in a multi-select parameter
final Object filterValue;
if (realFilterValue.length() >= 4
&& realFilterValue.substring(0, 2).equals("${")
&& realFilterValue.endsWith("}"))
{
final String parameterName = realFilterValue.substring(2, realFilterValue.length() - 1);
filterValue = parameters.get(parameterName);
}
// Cater for cases where users specify compound filer: "name" "like" "some${post_fix}"
else
{
filterValue = parameterParser.translateAndLookup(realFilterValue, parameters);
}
// If the value is null, this may be a dependent query and it is waiting for a parameter.
// just return and wait
if (filterValue == null)
{
return resultSet;
}
filter.setValue(filterValue);
}
catch (Exception e)
{
throw new ReportDataFactoryException(e.getMessage(), e);
}
}
}
}
// Get the data
final Object[][] rows;
try
{
rows = helper.getData(targetConfig, openERPParams);
}
catch (Exception e)
{
throw new ReportDataFactoryException(e.getMessage(), e);
}