java - Add custom data source to Jaspersoft Studio -
i trying fill table passing custom data source it. have created simple report table on it. report self gets data ms sql database. have written java class similar class in example. no value in table. @ example there no scriptlet. have checked (string) this.getfieldvalue("kn_formelgg");
line of code. gets data field , can show on report. guess bean data source not filled. call fill table method in aftergroupinit
. how can use collection of data java in jasper? tried adding java bean in dataset , query dialog, did not me either. should add scriptlet subreport/table? main emphasis of problem having custom data source in scriptlet. went other problem through, still no answer. added $p{fielddatasource}.getdata()
check data, delivers null.
java class 1:
package testprojektiman.scriptlets; import java.util.arraylist; import java.util.hashmap; import java.util.list; import java.util.map; import net.sf.jasperreports.engine.jrdefaultscriptlet; import net.sf.jasperreports.engine.jrscriptletexception; import net.sf.jasperreports.engine.data.jrbeancollectiondatasource; public class filltable extends jrdefaultscriptlet { @override public void aftergroupinit(final string id) throws jrscriptletexception { filltable(); } public arraylist<string> splitggarray(final string knformelgg) { arraylist<string> fieldnames = new arraylist<>(); string[] array = (knformelgg.split(" ")); (string sub : array) { fieldnames.add(sub); } return fieldnames; } public map<string, object> filltable() throws jrscriptletexception { string knformelgg = null; knformelgg = (string) this.getfieldvalue("kn_formelgg"); list<tablecells> listtablecells = new arraylist<>(); tablecells tablecell; (string fn : splitggarray(knformelgg)) { tablecell = new tablecells(); tablecell.setfieldname(fn); listtablecells.add(tablecell); } jrbeancollectiondatasource tablecelljrbean = new jrbeancollectiondatasource(listtablecells); map<string, object> parameters = new hashmap<>(); parameters.put("fielddatasource", tablecelljrbean); return parameters; } }
java class 2
package testprojektiman.scriptlets; public class tablecells { private string fieldname; private string keyformel; private string mk; private string notation; private string item; //getters setters }
jrxml
<?xml version="1.0" encoding="utf-8"?> <jasperreport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="tableautofill" pagewidth="595" pageheight="842" columnwidth="555" leftmargin="20" rightmargin="20" topmargin="20" bottommargin="20" uuid="37fc3a9c-38e9-41be-9039-56249c5283d7"> <subdataset name="tabledatasource" uuid="5999e646-aeec-4b2b-b29b-68e897d56999"> <querystring> <![cdata[]]> </querystring> <field name="fieldname" class="java.lang.string"/> </subdataset> <scriptlet name="filltable" class="testprojektiman.scriptlets.filltable"/> <parameter name="fielddatasource" class="net.sf.jasperreports.engine.data.jrbeancollectiondatasource" isforprompting="false"/> <querystring> <![cdata[select * "kennzahlreferenz2015_qibericht", "images" lb_id = 62 , kn_offiziellgruppe = 3 , img_id = 1]]> </querystring> <field name="kn_id" class="java.lang.integer"/> <field name="kn_formelz" class="java.lang.string"/> <field name="kn_formelgg" class="java.lang.string"/> <group name="id"> <groupexpression><![cdata[$f{kn_id}]]></groupexpression> <groupheader> <band height="50"/> </groupheader> <groupfooter> <band height="50"/> </groupfooter> </group> <detail> <band height="191" splittype="stretch"> <componentelement> <reportelement x="50" y="18" width="260" height="120" uuid="942ab836-df83-4a2f-8215-845073ad163f"> <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.verticalrowlayout"/> <property name="com.jaspersoft.studio.table.style.table_header" value="table_th"/> <property name="com.jaspersoft.studio.table.style.column_header" value="table_ch"/> <property name="com.jaspersoft.studio.table.style.detail" value="table_td"/> </reportelement> <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemalocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" whennodatatype="allsectionsnodetail"> <datasetrun subdataset="tabledatasource" uuid="b554ada5-c388-4534-af8e-93571a417adb"> <parametersmapexpression><![cdata[$p{fielddatasource}]]></parametersmapexpression> <datasourceexpression><![cdata[$p{fielddatasource}]]></datasourceexpression> </datasetrun> <jr:column width="100" uuid="f57e4e8e-8a2e-405f-b9ab-7a704e0986fd"> <property name="com.jaspersoft.studio.components.table.model.column.name" value="column1"/> <jr:columnheader style="table_ch" height="30"> <statictext> <reportelement x="0" y="0" width="100" height="30" uuid="9a55dd73-71e3-4559-9a8b-17d98bf17753"/> <textelement> <font isbold="true"/> </textelement> <text><![cdata[fieldname]]></text> </statictext> </jr:columnheader> <jr:detailcell style="table_td" height="30"> <textfield> <reportelement x="0" y="0" width="100" height="30" uuid="c3e6ccfc-9f91-4d7a-800a-613f5dded928"/> <textfieldexpression><![cdata[$f{fieldname}]]></textfieldexpression> </textfield> </jr:detailcell> </jr:column> </jr:table> </componentelement> </band> </detail> </jasperreport>
data adapter
create data adapter file, example adapter.xml
, through user interface. contents might resemble:
<?xml version="1.0" encoding="utf-8" ?> <beandataadapter class="net.sf.jasperreports.data.bean.beandataadapterimpl"> <name>yourclass</name> <factoryclass>com.yourcompany.jasper.jrdatasourcefactory</factoryclass> <methodname>createcollection</methodname> <usefielddescription>false</usefielddescription> </beandataadapter>
data class
create class has createcollection
method, per data adapter definition above:
package com.compay.jasper; public class jrdatasourcefactory { /** * @return collection of data report. */ public static collection<yourclass> createcollection() { return arrays.aslist( new yourclass() ); } }
set report properties
ensure report has following property (links report custom data adapter):
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="adapter.xml"/>
set report fields
the report fields should able reference bean properties:
<field name="yourobject.property" class="java.lang.string"> <fielddescription><![cdata[yourobject.property]]></fielddescription> </field>
it's important fielddescription
element contain value reflects bean property (i.e., java code call retrieve value bean on instance of bean).
application
the jrdatasourcefactory
class stands alone -- it's used data adapter create collection of bean instances. static
method (createcollection
) work here , not, indeed cannot, use inheritance.
example
if possible, borrow field names report bean's attributes. code written in question makes difficult discern value kn_id
comes from.
the following example links data adapter field names in report.
bean class
a bean exposes properties:
package com.company.domain; public final class student extends entity { private string firstname; private string lastname; public string getfirstname() { return this.firstname; } public string getlastname() { return this.lastname; } }
data adapter
the data adapter looks like:
<?xml version="1.0" encoding="utf-8" ?> <beandataadapter class="net.sf.jasperreports.data.bean.beandataadapterimpl"> <name>student</name> <factoryclass>com.company.jasper.jrdatasourcefactory</factoryclass> <methodname>createcollection</methodname> <usefielddescription>false</usefielddescription> </beandataadapter>
data class
the data class creates instances of bean:
package com.compay.jasper; import com.company.domain.student; public class jrdatasourcefactory { public static collection<student> createcollection() { return arrays.aslist( new student() ); } }
report fields
the report fields reflect bean fields:
<field name="firstname" class="java.lang.string"> <fielddescription><![cdata[firstname]]></fielddescription> </field> <field name="lastname" class="java.lang.string"> <fielddescription><![cdata[lastname]]></fielddescription> </field>
a collection of "student" instances passed report. in example, collection contains single instance. in example, collection contain many instances. report library iterates on collection, different values of firstname
, lastname
made available.
the mechanics how student
instance data populated outside scope of answer. far reporting tool concerned, uses pre-populated instances of student
. if filltable
populates tablecells
, that's not concern reporting tool.
Comments
Post a Comment