@Override public List<JRDesignField> getFields(DataAdapterService con, JasperReportsConfiguration jConfig, JRDataset jDataset) throws JRException, UnsupportedOperationException { Throwable t = null; ArrayList<JRDesignField> fields = new ArrayList<JRDesignField>(); try { String fileName = getDataAdapter().getFileName(); Document doc = JRXmlUtils.parse(new URL(fileName), XMLUtils.isNamespaceAware(getDataAdapter(), jConfig.getJasperDesign())); fields.addAll(getFieldsFromDocument(doc, jConfig, jDataset)); } catch (IOException e) { t = e; } if (t != null) { UIUtils.showError(t); } return fields; }
@Override public List<JRDesignField> getFields(DataAdapterService con, JasperReportsConfiguration jConfig, JRDataset jDataset) throws JRException, UnsupportedOperationException { Map<String, Object> parameters = new HashMap<String, Object>(); parameters.put("REPORT_PARAMETERS_MAP", new HashMap<String, Object>()); try { //Here there is the password of the connection con.contributeParameters(parameters); } catch (JRException e) { e.printStackTrace(); } ParameterUtil.setParameters(jConfig, jDataset, parameters); parameters.put(JRParameter.REPORT_MAX_COUNT, 2); Map<String,? extends JRValueParameter> tmpMap = ParameterUtil.convertMap(parameters, jDataset); JRXmlaQueryExecuter qe = new JRXmlaQueryExecuter(jConfig, jDataset, tmpMap); JROlapResult result = qe.getResult(); return OlapFieldsProviderSupport.getFieldsFromResult(result); }
/** * Given a JSON selection query, extracts from the result set * the list of possible fields that can be used in a report. * * @param query the JSON query text * @return a list of fields */ public List<JRDesignField> extractFields(String query){ JsonQueryHelper jsonQueryHelper=new JsonQueryHelper(mapper); try{ JsonNode jsonData = jsonQueryHelper.getJsonData(jsonRoot, query); if(jsonData!=null){ if(jsonData.isArray()){ return getFieldsFromArrayNode((ArrayNode)jsonData); } else if(jsonData.isObject()){ return getFieldsFromObjectNode((ObjectNode)jsonData); } } }catch (JRException e){ // Do not care about error in node selection } return new ArrayList<JRDesignField>(); }
/** * Returns the list of fields provided by an XML document and the related * query. * * @param doc * the W3C XML document * @param jConfig * the JasperReports configuration instance * @param jDataset * the current dataset * @return the list of fields * @throws JRException */ protected List<JRDesignField> getFieldsFromDocument(Document doc, JasperReportsConfiguration jConfig, JRDataset jDataset) throws JRException { JRXPathExecuterFactory xPathExecuterFactory = JRXPathExecuterUtils.getXPathExecuterFactory(jConfig); JRXPathExecuter xPathExecuter = xPathExecuterFactory.getXPathExecuter(); NodeList nodes = xPathExecuter.selectNodeList(doc, jDataset.getQuery().getText()); LinkedHashMap<String, JRDesignField> fieldsMap = new LinkedHashMap<String, JRDesignField>(); for (int nIdx = 0; nIdx < nodes.getLength(); nIdx++) { Node currNode = nodes.item(nIdx); findDirectChildrenAttributes(currNode,fieldsMap,""); if(currNode.getNodeType() == Node.ELEMENT_NODE) { NodeList childNodes = currNode.getChildNodes(); findChildFields(childNodes, fieldsMap,""); } } return new ArrayList<JRDesignField>(fieldsMap.values()); }
private void findChildFields(NodeList nodes, LinkedHashMap<String, JRDesignField> fieldsMap,String prefix) { if(nodes!=null) { List<String> childrenNames = new ArrayList<String>(); // temp list to avoid duplicates at the same level for (int i = 0; i < nodes.getLength(); i++) { Node item = nodes.item(i); String nodeName = item.getNodeName(); if((item.getNodeType() == Node.ELEMENT_NODE || item.getNodeType() == Node.ATTRIBUTE_NODE) && !childrenNames.contains(nodeName)) { if(recursiveFind) { findDirectChildrenAttributes(item,fieldsMap,prefix+nodeName+"/"); } addNewField(nodeName, fieldsMap, item, prefix); if(recursiveFind && item.hasChildNodes()){ findChildFields(item.getChildNodes(), fieldsMap,prefix+nodeName+"/"); } } } } }
@Override public List<JRDesignField> getFields(DataAdapterService con, JasperReportsConfiguration jConfig, JRDataset jDataset) throws JRException, UnsupportedOperationException { Map<String, Object> parameters = new HashMap<String, Object>(); con.contributeParameters(parameters); ParameterUtil.setParameters(jConfig, jDataset, parameters); parameters.put(JRParameter.REPORT_MAX_COUNT, 0); // JasperReports query executer instances require // REPORT_PARAMETERS_MAP parameter to be defined and not null Map<String, JRValueParameter> tmpMap = ParameterUtil.convertMap(parameters, jDataset); tmpMap.put(JRParameter.REPORT_PARAMETERS_MAP, new SimpleValueParameter(new HashMap<String, JRValueParameter>())); JRMondrianQueryExecuter qe = new JRMondrianQueryExecuter(jConfig, jDataset, tmpMap); qe.createDatasource(); JRMondrianResult result = new JRMondrianResult(qe.getResult()); return OlapFieldsProviderSupport.getFieldsFromResult(result); }
@Override public List<JRDesignField> readFields() throws Exception { if (getDataAdapterDescriptor() != null && getDataAdapterDescriptor() instanceof IFieldsProvider) { try { return ((IFieldsProvider) getDataAdapterDescriptor()).getFields( DataAdapterServiceUtil.getInstance(getJasperReportsConfiguration()).getService( getDataAdapterDescriptor().getDataAdapter()), getJasperReportsConfiguration(), getDataset()); } catch (JRException ex) { // Cleanup of the error. JRException are a very low meaningful exception when working // with data, what the user is interested into is the underline error (i.e. an SQL error). // That's why we rise the real cause, if any instead of rising the highlevel exception... if (ex.getCause() != null && ex.getCause() instanceof Exception) { throw (Exception) ex.getCause(); } throw ex; } } return new ArrayList<JRDesignField>(); }
/** * Creates a valid name for a JRField element. * <p> * It searches the existing fields in order to check for field name validity. If no other field is found with the same * name, then the <code>namePrefix</code> itself is used. * * @param fields * list of already existing fields * @param namePrefix * name prefix for new field name * @return a valid name for the JRField */ public static String getNameForField(List<JRDesignField> fields, String namePrefix) { boolean match = false; String tmp = namePrefix; for (int i = 0; i < 100000; i++) { if (i != 0) tmp = ModelUtils.getNameFormat(namePrefix, i); for (JRField f : fields) { match = f.getName().equals(tmp); if (match) break; } if (!match) break; } return tmp; }
public void setPropertyValue(Object id, Object value) { JRDesignField jrField = (JRDesignField) getValue(); if (id.equals(JRDesignParameter.PROPERTY_NAME)) { if (!value.equals("")) { jrField.setName((String) value); } } else if (id.equals(JRDesignParameter.PROPERTY_VALUE_CLASS_NAME)){ jrField.setValueClassName((String) value); } else if (id.equals(JRDesignParameter.PROPERTY_DESCRIPTION)){ jrField.setDescription((String) value); } else if (id.equals(PROPERTY_MAP)) { JRPropertiesMap v = (JRPropertiesMap) value; String[] names = jrField.getPropertiesMap().getPropertyNames(); for (int i = 0; i < names.length; i++) { jrField.getPropertiesMap().removeProperty(names[i]); } names = v.getPropertyNames(); for (int i = 0; i < names.length; i++) jrField.getPropertiesMap().setProperty(names[i], v.getProperty(names[i])); this.getPropertyChangeSupport().firePropertyChange(PROPERTY_MAP, false, true); } }
public void storeSettings(Object settings) { getWizardPanel().getWizard().putProperty("table_type", jRadioButton1.isSelected() ? 0 : 1); getWizardPanel().getWizard().putProperty("dataset", jComboBoxDataset.getSelectedItem()); if (jComboBoxDataset.getSelectedItem() != null) { List<JRDesignField> list = new ArrayList<JRDesignField>(); List fieldsList = ((JRDesignDataset)jComboBoxDataset.getSelectedItem()).getFieldsList(); for (int i=0; i<fieldsList.size(); ++i) { list.add((JRDesignField)fieldsList.get(i)); } getWizardPanel().getWizard().putProperty("discoveredFields", list ); getWizardPanel().getWizard().putProperty("discoveredFieldsNeedRefresh","true"); } getWizardPanel().getWizard().putProperty("columns", jSpinner1.getValue()); }
public void propertyChange(PropertyChangeEvent evt) { com.jaspersoft.ireport.designer.IReportManager.getInstance().notifyReportChange(); if (evt.getPropertyName() == null) return; if (evt.getPropertyName().equals( JRDesignField.PROPERTY_NAME )) { super.setName(getField().getName()); this.setDisplayName(getField().getName()); // update sorting of childrens... if (this.getParentNode() != null && this.getParentNode() instanceof FieldsNode) { SwingUtilities.invokeLater(new Runnable() { public void run() { ((FieldsNode)(FieldNode.this.getParentNode())).updateSorting(); } }); } } // Update the sheet this.firePropertyChange(evt.getPropertyName(), evt.getOldValue(), evt.getNewValue() ); }
@SuppressWarnings("unchecked") public void recalculateKeys() { List l = (List)lock(); l.clear(); l.addAll(dataset.getFieldsList()); if (getNode() != null && getNode() instanceof FieldsNode) { if (((FieldsNode)getNode()).isSort()) { // Order elements by name... Object[] fields = dataset.getFieldsList().toArray(); Arrays.sort(fields, new Comparator() { public int compare(Object o1, Object o2) { return ((JRDesignField)o1).getName().compareToIgnoreCase(((JRDesignField)o2).getName()); } }); l.clear(); l.addAll(Arrays.asList(fields)); } } update(); }
@Override public JRDesignElement createReportElement(JasperDesign jasperDesign) { JRDesignTextField element = (JRDesignTextField)super.createReportElement( jasperDesign ); JRDesignField field = (JRDesignField)getPaletteItem().getData(); ((JRDesignExpression)element.getExpression()).setText("$F{"+ field.getName() + "}"); setMatchingClassExpression( ((JRDesignExpression)element.getExpression()), field.getValueClassName(), true ); return element; }
private boolean fieldAlreadyExists(JRDesignField field) { boolean found = false; for (int j=0; j<getJTableFields().getRowCount(); ++j) { Object ff = getJTableFields().getValueAt(j, 0); if ( ff instanceof JRDesignField ) { if ( ((JRDesignField)ff).getName().equals(field.getName())) { found = true; break; } } } return found; }
@SuppressWarnings("unchecked") public Vector getFields(Object obj) { Vector fields = new Vector(); java.beans.PropertyDescriptor[] pd = org.apache.commons.beanutils.PropertyUtils.getPropertyDescriptors(obj.getClass()); for (int nd =0; nd < pd.length; ++nd) { String fieldName = pd[nd].getName(); if (pd[nd].getPropertyType() != null && pd[nd].getReadMethod() != null) { String returnType = pd[nd].getPropertyType().getName(); JRDesignField field = new JRDesignField(); field.setName(fieldName); field.setValueClassName(Misc.getJRFieldType(returnType)); //field.setDescription(""); //Field returned by " +methods[i].getName() + " (real type: "+ returnType +")"); fields.addElement(field); } } return fields; }
public JRDesignField createField(Object obj, int pos) { String fieldName = "COLUMN_" + (pos+1); if (pos < 0) { fieldName = obj.getClass().getName(); if (fieldName.indexOf(".") > 0) { fieldName = fieldName.substring(fieldName.indexOf(".")+1); } if (fieldName.length() == 0) fieldName = "COLUMN_1"; } JRDesignField field = new JRDesignField(); field.setName(fieldName); field.setValueClassName(obj.getClass().getName()); //field.setDescription(""); return field; }
public List<JRDesignField> readFields(String query) throws Exception { List<JRDesignField> fields = new ArrayList<JRDesignField>(); Vector names = getColumnNames(); for (int nd =0; nd < names.size(); ++nd) { String fieldName = ""+names.elementAt(nd); JRDesignField field = new JRDesignField(); field.setName(fieldName); field.setValueClassName("java.lang.String"); //field.setDescription(null); //Field returned by " +methods[i].getName() + " (real type: "+ returnType +")"); fields.add(field); } return fields; }
public List<JRDesignField> readFields(String query) throws Exception { SQLFieldsProvider provider = new SQLFieldsProvider(); List<JRDesignField> result = new ArrayList<JRDesignField>(); JRDesignDataset dataset = new JRDesignDataset(true); JRDesignQuery dquery = new JRDesignQuery(); dquery.setLanguage("SQL"); dquery.setText(query); dataset.setQuery(dquery); JRField[] fields = provider.getFields(this, dataset, new HashMap()); for (int i=0; i<fields.length; ++i) { result.add((JRDesignField)fields[i]); } return result; }
public List<JRDesignField> readFields(String query) throws Exception { List<JRDesignField> fields = new ArrayList<JRDesignField>(); List<String> names = getColumnNames(); for (int nd =0; nd < names.size(); ++nd) { String fieldName = ""+names.get(nd); JRDesignField field = new JRDesignField(); field.setName(fieldName); field.setValueClassName("java.lang.String"); //field.setDescription(""); //Field returned by " +methods[i].getName() + " (real type: "+ returnType +")"); fields.add(field); } return fields; }
public void updateLists() { ((DefaultListModel)jList1.getModel()).clear(); ((DefaultListModel)jList2.getModel()).clear(); List<JRDesignField> discoveredFields = (List<JRDesignField>) panel.getWizard().getProperty("discoveredFields"); if (discoveredFields != null) { for (JRDesignField field : discoveredFields) { ((DefaultListModel)jList1.getModel()).addElement(field); } } if (jList1.getModel().getSize() > 0) { jList1.setSelectedIndex(0); } updateButtons(); }
@Override public List<JRDesignField> readFields(String query) throws Exception { MongoDbFieldsProvider provider = new MongoDbFieldsProvider(); List<JRDesignField> result = new ArrayList<JRDesignField>(); JRDesignDataset dataset = new JRDesignDataset(true); JRDesignQuery designQuery = new JRDesignQuery(); designQuery.setLanguage(MongoDbDataSource.QUERY_LANGUAGE); designQuery.setText(query); dataset.setQuery(designQuery); Map<String, String> parameters = new HashMap<String, String>(); JRField[] fields = provider.getFields(this, dataset, parameters); for (int i = 0; i < fields.length; ++i) { result.add((JRDesignField) fields[i]); } return result; }
@Test public void testDatasourceWithNoFilters() throws JRException { JRDataSource dataSource = new ResourceQueryCommand().executeCommand(getCommand()); assertNotNull(dataSource); assertTrue(dataSource.next()); JRDesignField pathField = new JRDesignField(); pathField.setName("path"); String pathVal = (String) dataSource.getFieldValue(pathField); assertNotNull(pathVal); assertTrue(String.format("Path does not match: %s", pathVal), pathVal.matches(".*src/test/resources/share/rrd/snmp/10/nsVpnMonitor/tun_id_1")); JRDesignField filterField = new JRDesignField(); filterField.setName("icmp"); String val = (String) dataSource.getFieldValue(filterField); assertNull(val); }
@Test public void testDatasourceWithFilters() throws JRException { JRDataSource dataSource = new ResourceQueryCommand().executeCommand(getCommandWithFilter()); assertNotNull(dataSource); assertTrue(dataSource.next()); JRDesignField pathField = new JRDesignField(); pathField.setName("path"); assertNotNull("", dataSource.getFieldValue(pathField)); JRDesignField filterField = new JRDesignField(); filterField.setName("nsVpnMonBytesIn"); String dsFieldValue = (String) dataSource.getFieldValue(filterField); assertNotNull(dsFieldValue); assertTrue(dsFieldValue.matches(".*src/test/resources/share/rrd/snmp/10/nsVpnMonitor/tun_id_1/nsVpnMonBytesIn.jrb")); }
static JRDesignField[] createFields(ODLTableDefinition table, boolean includeArtificialSubreportField) { int nc = table.getColumnCount(); JRDesignField[] ret = new JRDesignField[nc + (includeArtificialSubreportField ? 1 : 0)]; for (int i = 0; i < nc; i++) { ret[i] = new JRDesignField(); ret[i].setName(table.getColumnName(i)); ret[i].setValueClass(ColumnValueProcessor.getJavaClass(table.getColumnType(i))); } if (includeArtificialSubreportField) { ret[nc] = new JRDesignField(); ret[nc].setName(ReportConstants.SUBREPORT_DATASTORE_FIELDNAME); ret[nc].setValueClass(net.sf.jasperreports.engine.JRRewindableDataSource.class); } return ret; }
@Test public void testDatasourceWithNoFilters() throws JRException { JRDataSource dataSource = new ResourceQueryCommand().executeCommand(getCommand()); assertNotNull(dataSource); assertTrue(dataSource.next()); JRDesignField pathField = new JRDesignField(); pathField.setName("path"); String pathVal = (String) dataSource.getFieldValue(pathField); assertNotNull("", pathVal); assertTrue(pathVal.matches(".*src/test/resources/share/rrd/snmp/10/nsVpnMonitor/tun_id_1")); JRDesignField filterField = new JRDesignField(); filterField.setName("icmp"); String val = (String) dataSource.getFieldValue(filterField); assertNull(val); }
private void addFieldValue(GridColumnVO columnVO) throws JRException { JRDesignBand bDetalle = (JRDesignBand) jasperDesign.getDetailSection().getBands()[0]; JRDesignField f = new JRDesignField(); f.setName(columnVO.getDbName()); f.setValueClass(columnVO.getFieldClass()); jasperDesign.addField(f); JRDesignExpressionChunk chunk = new JRDesignExpressionChunk(); chunk.setText(columnVO.getDbName()); chunk.setType(JRDesignExpressionChunk.TYPE_FIELD); JRDesignExpression expression = new JRDesignExpression(); expression.addChunk(chunk); JRDesignTextField textField = new JRDesignTextField(); textField.setWidth(columnVO.getWidth()); textField.setHeight(bDetalle.getHeight()); textField.setX(px); textField.setExpression(expression); textField.setBlankWhenNull(true); textField.setFontName(gridReportVO.getDetailBandStyle().getFontName()); textField.setFontSize(new Float(gridReportVO.getDetailBandStyle().getFontSize())); textField.setForecolor(gridReportVO.getDetailBandStyle().getForeColor()); textField.setBold(gridReportVO.getDetailBandStyle().isBold()); textField.setItalic(gridReportVO.getDetailBandStyle().isItalic()); textField.setUnderline(gridReportVO.getDetailBandStyle().isUnderline()); textField.setStretchWithOverflow(true); textField.setStretchType(StretchTypeEnum.RELATIVE_TO_TALLEST_OBJECT); bDetalle.addElement(textField); }
/** * @see net.sf.jasperreports.engine.JRDataSourceProvider#getFields(net.sf.jasperreports.engine.JasperReport) */ @Override public JRField[] getFields(JasperReport report) throws JRException { BeanInfo beanInfo = null; try { beanInfo = Introspector.getBeanInfo(beanClass); } catch (IntrospectionException e) { throw new JRException(e); } PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors(); if(descriptors != null) { ArrayList<JRField> fields = new ArrayList<JRField>(descriptors.length); for (int i = 0; i < descriptors.length; i++) { PropertyDescriptor descriptor = descriptors[i]; if (!(descriptor instanceof IndexedPropertyDescriptor) && descriptor.getReadMethod() != null) { JRDesignField field = new JRDesignField(); field.setValueClassName(normalizeClass(descriptor.getPropertyType()).getCanonicalName()); field.setName(descriptor.getName()); fields.add(field); } } return fields.toArray(new JRField[fields.size()]); } return new JRField[0]; }
@Override public Object createObject(Attributes atts) { JRDesignField field = new JRDesignField(); field.setName(atts.getValue(JRXmlConstants.ATTRIBUTE_name)); if (atts.getValue(JRXmlConstants.ATTRIBUTE_class) != null) { field.setValueClassName(atts.getValue(JRXmlConstants.ATTRIBUTE_class)); } return field; }
public List<JRDesignField> getFields( JasperReportsContext context, JRDataset dataset, Map<String, Object> parameters, ESSearch connection) throws JRException { logger.debug("Providing fields a query."); String query = "{ query: { match_all: {} } }"; if ( dataset.getQuery() != null ) { query = dataset.getQuery().getText(); } logger.debug("Passing query to connection: " + query); connection.setSearch(query); Map<String, Class<?>> queryFields = connection.getFields(query); List<JRDesignField> fields = new ArrayList<JRDesignField>(); for (String fieldName: queryFields.keySet() ) { JRDesignField field = new JRDesignField(); field.setName(fieldName); field.setValueClass(queryFields.get(fieldName)); field.setDescription(fieldName); fields.add(field); } return fields; }
public List<JRDesignField> getFields(DataAdapterService con, JasperReportsConfiguration jConfig, JRDataset reportDataset) throws JRException, UnsupportedOperationException { Map<String, Object> parameters = new HashMap<String, Object>(); parameters.put("REPORT_PARAMETERS_MAP", new HashMap<String, Object>()); con.contributeParameters(parameters); ParameterUtil.setParameters(jConfig, reportDataset, parameters); parameters.put(JRParameter.REPORT_MAX_COUNT, 2); ExcelDataSource ds = null; ExcelDataAdapter da = (ExcelDataAdapter) ((AbstractDataAdapterService) con).getDataAdapter(); if (da.isQueryExecuterMode()) { ExcelQueryExecuter qe = (ExcelQueryExecuter) new ExcelQueryExecuterFactory().createQueryExecuter(jConfig, reportDataset, ParameterUtil.convertMap(parameters, reportDataset)); ds = (ExcelDataSource) qe.createDatasource(); } else { ds = (ExcelDataSource) parameters.get(JRParameter.REPORT_DATA_SOURCE); } if (ds != null) { ds.setUseFirstRowAsHeader(da.isUseFirstRowAsHeader()); ds.next(); Map<String, Integer> map = ds.getColumnNames(); List<JRDesignField> columns = new ArrayList<JRDesignField>(map.keySet().size()); for (String key : map.keySet()) { JRDesignField field = new JRDesignField(); field.setName(StringUtils.xmlEncode(key, null)); field.setValueClass(String.class); columns.add(field); } return columns; } return null; }
/** * This method will populate the data model with the Excel columns This also * checks the button "Skip the first line " and enables the delete button * * @throws Exception */ private void getExcelColumns() throws Exception { if (textFileName.getText().length() > 0) { DataAdapterDescriptor da = getDataAdapter(); if (jConfig == null) jConfig = JasperReportsConfiguration.getDefaultJRConfig(); DataAdapterService das = DataAdapterServiceUtil.getInstance(jConfig).getService(da.getDataAdapter()); ((AbstractDataAdapterService) das).getDataAdapter(); jConfig.setJasperDesign(new JasperDesign()); // The get fields method call once a next on the data adapter to get the // first line and from that is read the // fields name. But is useFirstRowAsHeader flag is set to false than the // next call will skip the first line // that is the only one read to get the fields, so it will return an empty // set of column names. For this // reason this flag must be force to true if the data adapter is used to // get the column names XlsDataAdapter xlsAdapter = (XlsDataAdapter) da.getDataAdapter(); boolean useRowHeader = xlsAdapter.isUseFirstRowAsHeader(); xlsAdapter.setUseFirstRowAsHeader(true); List<JRDesignField> fields = ((IFieldsProvider) da).getFields(das, jConfig, new JRDesignDataset(jConfig, false)); xlsAdapter.setUseFirstRowAsHeader(useRowHeader); rows.clear(); int columnIndex = 0; for (JRDesignField f : fields) { rows.add(new String[] { f.getName(), String.valueOf(columnIndex++) }); } tableViewer.setInput(rows); tableViewer.refresh(); setTableSelection(-1); btnDelete.setEnabled(true); } }
/** * This method will populate the data model with the Excel columns This also * checks the button "Skip the first line " and enables the delete button * * @throws Exception */ private void getExcelColumns() throws Exception { if (textFileName.getText().length() > 0) { DataAdapterDescriptor da = getDataAdapter(); if (jConfig == null) jConfig = JasperReportsConfiguration.getDefaultJRConfig(); DataAdapterService das = DataAdapterServiceUtil.getInstance(jConfig).getService(da.getDataAdapter()); jConfig.setJasperDesign(new JasperDesign()); //The get fields method call once a next on the data adapter to get the first line and from that is read the //fields name. But is useFirstRowAsHeader flag is set to false than the next call will skip the first line //that is the only one read to get the fields, so it will return an empty set of column names. For this //reason this flag must be force to true if the data adapter is used to get the column names XlsxDataAdapter xlsAdapter = (XlsxDataAdapter)da.getDataAdapter(); boolean useRowHeader = xlsAdapter.isUseFirstRowAsHeader(); xlsAdapter.setUseFirstRowAsHeader(true); List<JRDesignField> fields = ((IFieldsProvider) da).getFields(das, jConfig, new JRDesignDataset(jConfig, false)); xlsAdapter.setUseFirstRowAsHeader(useRowHeader); rows.clear(); int columnIndex = 0; for (JRDesignField f : fields) { rows.add(new String[] { f.getName(), String.valueOf(columnIndex++) }); } tableViewer.setInput(rows); tableViewer.refresh(); setTableSelection(-1); btnDelete.setEnabled(true); } }
/** * This method will populate the data model with the Excel columns This also * checks the button "Skip the first line " and enables the delete button * * @throws Exception */ private void getExcelColumns() throws Exception { if (textFileName.getText().length() > 0) { DataAdapterDescriptor da = getDataAdapter(); if (jConfig == null) jConfig = JasperReportsConfiguration.getDefaultJRConfig(); DataAdapterService das = DataAdapterServiceUtil.getInstance(jConfig).getService(da.getDataAdapter()); ((AbstractDataAdapterService) das).getDataAdapter(); jConfig.setJasperDesign(new JasperDesign()); //The get fields method call once a next on the data adapter to get the first line and from that is read the //fields name. But is useFirstRowAsHeader flag is set to false than the next call will skip the first line //that is the only one read to get the fields, so it will return an empty set of column names. For this //reason this flag must be force to true if the data adapter is used to get the column names XlsDataAdapter xlsAdapter = (XlsDataAdapter)da.getDataAdapter(); boolean useRowHeader = xlsAdapter.isUseFirstRowAsHeader(); xlsAdapter.setUseFirstRowAsHeader(true); List<JRDesignField> fields = ((IFieldsProvider) da).getFields(das, jConfig, new JRDesignDataset(jConfig, false)); xlsAdapter.setUseFirstRowAsHeader(useRowHeader); rows.clear(); int columnIndex = 0; for (JRDesignField f : fields) { rows.add(new String[] { f.getName(), String.valueOf(columnIndex++) }); } tableViewer.setInput(rows); tableViewer.refresh(); setTableSelection(-1); btnDelete.setEnabled(true); } }
public List<JRDesignField> getFields(DataAdapterService con, JasperReportsConfiguration jConfig, JRDataset reportDataset) throws JRException, UnsupportedOperationException { Map<String, Object> parameters = new HashMap<String, Object>(); parameters.put("REPORT_PARAMETERS_MAP", new HashMap<String, Object>()); con.contributeParameters(parameters); ParameterUtil.setParameters(jConfig, reportDataset, parameters); parameters.put(JRParameter.REPORT_MAX_COUNT, 2); JRXlsDataSource ds = null; XlsDataAdapter da = (XlsDataAdapter) ((AbstractDataAdapterService) con).getDataAdapter(); if (da.isQueryExecuterMode()) { JRXlsQueryExecuter qe = (JRXlsQueryExecuter) new JRXlsQueryExecuterFactory().createQueryExecuter(jConfig, reportDataset, ParameterUtil.convertMap(parameters, reportDataset)); ds = (JRXlsDataSource) qe.createDatasource(); } else { ds = (JRXlsDataSource) parameters.get(JRParameter.REPORT_DATA_SOURCE); } if (ds != null) { ds.setUseFirstRowAsHeader(da.isUseFirstRowAsHeader()); ds.next(); Map<String, Integer> map = ds.getColumnNames(); List<JRDesignField> columns = new ArrayList<JRDesignField>(map.keySet().size()); for (String key : map.keySet()) { JRDesignField field = new JRDesignField(); field.setName(StringUtils.xmlEncode(key, null)); field.setValueClass(String.class); columns.add(field); } return columns; } return null; }
private List<JRDesignField> getFieldsFromObjectNode(ObjectNode node){ List<JRDesignField> fields = new ArrayList<JRDesignField>(); Iterator<String> fieldNames = node.fieldNames(); while(fieldNames.hasNext()){ String name = fieldNames.next(); JRDesignField f=new JRDesignField(); f.setName(ModelUtils.getNameForField(fields, name)); f.setDescription(name); f.setValueClass(String.class); fields.add(f); } return fields; }
private List<JRDesignField> getFieldsFromArrayNode(ArrayNode node){ // Assumption: consider the first element as template JsonNode firstEl = node.get(0); if(firstEl instanceof ObjectNode){ return getFieldsFromObjectNode((ObjectNode)firstEl); } else if (firstEl instanceof ArrayNode){ return getFieldsFromArrayNode((ArrayNode)firstEl); } return new ArrayList<JRDesignField>(); }