GoncaloSete Posted October 2, 2009 Share Posted October 2, 2009 Hi!I've searched the forum for a subject like this, but I couldn't find one thread.I have a Master report and several subreports and I want them all to share the same scriptlet class.The first solution I came up with was: parameters.put(JRParameter.REPORT_SCRIPTLET, new MyScriptlet());but this only applies MyScriptlet to the master report.Every other subreport continue to have JRDefaultScriptlet instantiated.My challenge is to avoid passing the built-in parameter REPORT_SCRIPTLET to all subreports. In the future, I might have several more and I wouldn't like to worry about passing it all the time.Is there a way of defining that the default scriptlet is MyScriptlet? (instead of JRDefaultScriptlet) Thanks in advanceGonçalo Link to comment Share on other sites More sharing options...
GoncaloSete Posted October 6, 2009 Author Share Posted October 6, 2009 Hi againI found that there is this JRFillDataset class that loads a default scriptlet if none is defined in the report.Is there a way of doing this with "Extensions"? Thank youCode:/** * Creates the scriptlets. * * @return the scriptlets list * @throws JRException */ protected List createScriptlets(Map parameterValues) throws JRException { ScriptletFactoryContext context = new ScriptletFactoryContext(parameterValues); scriptlets = new ArrayList(); List factories = ExtensionsEnvironment.getExtensionsRegistry().getExtensions(ScriptletFactory.class); for (Iterator it = factories.iterator(); it.hasNext();) { ScriptletFactory factory = (ScriptletFactory)it.next(); List tmpScriptlets = factory.getScriplets(context); if (tmpScriptlets != null) { scriptlets.addAll(tmpScriptlets); } } if (scriptlets.size() == 0) { scriptlets.add(0, new JRDefaultScriptlet()); } return scriptlets; } Link to comment Share on other sites More sharing options...
GoncaloSete Posted October 6, 2009 Author Share Posted October 6, 2009 Well, I reverse engineered the jasperreports code and found out a (potentially dirty) solution.I will share with you, even if I feel like talking to myself. Your scriptlet class should extend and implement the classes below. In your jasperreports_extension.properties define the following line:net.sf.jasperreports.extension.registry.factory.scriptlet=com.alert.pfh.reports.business.MyScriplet Of course you should separate your Scriptlet class from the Factory class. That what I've done. Cheers Code: Code:private static final ExtensionsRegistry scriptletExtensionsRegistry = new ExtensionsRegistry(){ public List getExtensions(Class extensionType){ if(ScriptletFactory.class.equals(extensionType)){ return Collections.singletonList(new MyScriplet()); } return null; } }; public ExtensionsRegistry createRegistry(String registryId, JRPropertiesMap properties){ return scriptletExtensionsRegistry; }Post Edited by GoncaloSete at 10/06/2009 08:11 Link to comment Share on other sites More sharing options...
lucianc Posted October 8, 2009 Share Posted October 8, 2009 That's not a dirty solution, it's the standard JR extension mechanism. Not well documented at this moment, so it's basically for people with reverse engineering skills like you :)Regards,Lucian Link to comment Share on other sites More sharing options...
kashoory Posted January 9, 2015 Share Posted January 9, 2015 I really appreciate that you shared your nice methodical solution. Link to comment Share on other sites More sharing options...
arnaudsimon091 Posted June 26, 2020 Share Posted June 26, 2020 Thanks for your solution ! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now