net.sf.jasperreports.engine.query.JRXlsxQueryExecuter Maven / Gradle / Ivy
/*
* JasperReports - Free Java Reporting Library.
* Copyright (C) 2001 - 2011 Jaspersoft Corporation. All rights reserved.
* http://www.jaspersoft.com
*
* Unless you have purchased a commercial license agreement from Jaspersoft,
* the following license terms apply:
*
* This program is part of JasperReports.
*
* JasperReports is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JasperReports 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JasperReports. If not, see .
*/
package net.sf.jasperreports.engine.query;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import net.sf.jasperreports.engine.DefaultJasperReportsContext;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRDataset;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;
import net.sf.jasperreports.engine.JRParameter;
import net.sf.jasperreports.engine.JRPropertiesUtil.PropertySuffix;
import net.sf.jasperreports.engine.JRValueParameter;
import net.sf.jasperreports.engine.JasperReportsContext;
import net.sf.jasperreports.engine.data.JRXlsxDataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.ss.usermodel.Workbook;
/**
* XLS query executer implementation.
*
* @author sanda zaharia ([email protected])
* @version $Id: JRXlsxQueryExecuter.java 5180 2012-03-29 13:23:12Z teodord $
*/
public class JRXlsxQueryExecuter extends JRAbstractQueryExecuter {
private static final Log log = LogFactory.getLog(JRXlsxQueryExecuter.class);
private JRXlsxDataSource datasource;
/**
*
*/
protected JRXlsxQueryExecuter(
JasperReportsContext jasperReportsContext,
JRDataset dataset,
Map parametersMap
)
{
super(jasperReportsContext, dataset, parametersMap);
}
/**
* @deprecated Replaced by {@link #JRXlsxQueryExecuter(JasperReportsContext, JRDataset, Map)}.
*/
protected JRXlsxQueryExecuter(JRDataset dataset, Map parametersMap)
{
this(DefaultJasperReportsContext.getInstance(), dataset, parametersMap);
}
public JRDataSource createDatasource() throws JRException {
try {
Workbook workbook = (Workbook) getParameterValue(JRXlsxQueryExecuterFactory.XLSX_WORKBOOK);
if (workbook != null) {
datasource = new JRXlsxDataSource(workbook);
} else {
InputStream xlsxInputStream = (InputStream) getParameterValue(JRXlsxQueryExecuterFactory.XLSX_INPUT_STREAM);
if (xlsxInputStream != null) {
datasource = new JRXlsxDataSource(xlsxInputStream);
} else {
File xlsxFile = (File) getParameterValue(JRXlsxQueryExecuterFactory.XLSX_FILE);
if (xlsxFile != null) {
datasource = new JRXlsxDataSource(xlsxFile);
} else {
String xlsxSource = getStringParameterOrProperty(JRXlsxQueryExecuterFactory.XLSX_SOURCE);
if (xlsxSource != null) {
datasource = new JRXlsxDataSource(getJasperReportsContext(), xlsxSource);
} else {
if (log.isWarnEnabled()){
log.warn("No XLS source was provided.");
}
}
}
}
}
} catch (IOException e) {
throw new JRException(e);
}
if (datasource != null) {
// build column names list
List columnNamesList = null;
String columnNames = getStringParameterOrProperty(JRXlsxQueryExecuterFactory.XLSX_COLUMN_NAMES);
if(columnNames != null) {
columnNamesList = new ArrayList();
columnNamesList.add(columnNames);
} else {
String[] columnNamesArray = (String[]) getParameterValue(JRXlsxQueryExecuterFactory.XLSX_COLUMN_NAMES_ARRAY, true);
if(columnNamesArray != null) {
columnNamesList = Arrays.asList(columnNamesArray);
} else {
String propertiesPrefix = JRXlsxQueryExecuterFactory.XLSX_COLUMN_NAMES;
List properties = getPropertiesUtil().getAllProperties(dataset, propertiesPrefix);
if (properties != null && !properties.isEmpty()) {
columnNamesList = new ArrayList();
for(int i = 0; i < properties.size(); i++) {
PropertySuffix property = properties.get(i);
columnNamesList.add(property.getValue());
}
} else {
JRField[] fields = dataset.getFields();
if (fields != null && fields.length > 0)
{
columnNamesList = new ArrayList();
for (int i = 0; i < fields.length; i++)
{
columnNamesList.add(fields[i].getName());
}
}
}
}
}
List splitColumnNamesList = null;
if (columnNamesList != null && columnNamesList.size() > 0) {
splitColumnNamesList = new ArrayList();
for(int i = 0; i < columnNamesList.size(); i++) {
String names = columnNamesList.get(i);
for(String token: names.split(",")){
splitColumnNamesList.add(token.trim());
}
}
}
// build column indexes list
List columnIndexesList = null;
String columnIndexes = getStringParameterOrProperty(JRXlsxQueryExecuterFactory.XLSX_COLUMN_INDEXES);
if (columnIndexes != null) {
columnIndexesList = new ArrayList();
for (String colIndex: columnIndexes.split(",")){
columnIndexesList.add(Integer.valueOf(colIndex.trim()));
}
} else {
Integer[] columnIndexesArray = (Integer[]) getParameterValue(JRXlsxQueryExecuterFactory.XLSX_COLUMN_INDEXES_ARRAY, true);
if (columnIndexesArray != null) {
columnIndexesList = Arrays.asList(columnIndexesArray);
} else {
String propertiesPrefix = JRXlsxQueryExecuterFactory.XLSX_COLUMN_INDEXES;
List properties = getPropertiesUtil().getAllProperties(dataset, propertiesPrefix);
if (properties != null && !properties.isEmpty()) {
columnIndexesList = new ArrayList();
for(int i = 0; i < properties.size(); i++) {
String propertyValue = properties.get(i).getValue();
for (String colIndex: propertyValue.split(",")){
columnIndexesList.add(Integer.valueOf(colIndex.trim()));
}
}
}
}
}
// set column names or column indexes or both
if (splitColumnNamesList != null) {
if (columnIndexesList != null) {
int[] indexesArray = new int[columnIndexesList.size()];
for (int i=0; i