Hi,
In order to export all data out my JSpresso application into, by preference, an XML file, what is the best approach? Should I use JasperReports?
Thanks for your insights,
Marc
Hi,
In order to export all data out my JSpresso application into, by preference, an XML file, what is the best approach? Should I use JasperReports?
Thanks for your insights,
Marc
Hi Marc,
It should be pretty straightforward without using JasperReports (keep it for actual reporting). Jspresso comes with a built-in sava action that allows you to export the data to whatever format you need.
All you have to do is implement a callback that will be passed an output stream as parameter and write to it.
Here we go :
public class MyXmlExportCallback implements IFileSaveCallback {
public void cancel(IActionHandler actionHandler,
Map<String, Object> context) {
//Do whatever you want when the file download is canceled.
}
public void fileChosen(OutputStream out,
IActionHandler actionHandler,
Map<String, Object> context) {
// Use the action context to explore the model
// and write data to the output stream. In your case
// I would use an outputstream writer to take care of the XML text
// encoding. As of now, you have to flush and close here.
// This might not be necessary in the future.
}
}
<bean
parent="saveFileAction">
<property
name="fileFilter">
<map>
<entry
key="xml"> <!-- xml will be translated using the resource bundles -->
<list>
<value>.xml</value>
</list>
</entry>
</map>
</property>
<property
name="defaultFileName"
value="export.xml" />
<property
name="fileSaveCallback">
<bean
class="my.package.MyXmlExportCallback" />
</property>
</bean>
The action will prompt the user for choosing a file to save to (same as saving a binary property value). Feel free to change the icon, description, and so on.
HTH,
Vincent
Hi,
I have no immediate clue on how to serialize or explore "context" or on how to retrieve the models behind.
This is probably also due to my limited knowledge on Java.
Shouldn't all the objects be marked as serializable? Then I get an XML output "for free" just by writing it via teh good type of outputstream?
Any indication welcome,
Marc
Hi Marc,
Could you elaborate a little more on your needs ? Do you really want to export ALL the domain model in XML ? Or only the model (and its hierarchy) backing the view the export action is assigned to ?
Unfortunately, there is no base abstract class to inherit from when implementing an IFileSaveCallback. But you can still get everything out of the action context with little more code. Have a look to AbstractAction.getSelectedModel(...) for instance. You will see how the context is explored to retrieve the model backing the view. Once you have the model that is the root of the model hierarchy you want to export, you can just follow its relationships, dumping your custom xml by writing to the outputstream.
Marking the domain objects as Serializable is not an option for various reasons :
Best,
Vincent
Unfortunately, there is no base abstract class to inherit from when implementing an IFileSaveCallback. But you can still get everything out of the action context with little more code. Have a look to AbstractAction.getSelectedModel(...) for instance. You will see how the context is explored to retrieve the model backing the view. Once you have the model that is the root of the model hierarchy you want to export, you can just follow its relationships, dumping your custom xml by writing to the outputstream.
I've just created an multi-purpose abstract base class you can inherit and that provides helper methods to explore the context :
org.jspresso.framework.application.action.AbstractActionContextAware
So make your file save callback inherit from it and you will benefit the action context helper methods :
public class MyXmlExportCallback extends AbstractActionContextAware implements IFileSaveCallback
You can now call for instance getSelectedModel, getSelectedModels, ... from your implementation.
HTH,
Vincent
Vincent,
Thanks for the refactoring and for moving the model selection methods in AbstractActionContextAware: That makes it quite easier.
But I encounter a strange error:
I could sucessfully write application data using Ajax (wings), but when I execute the same program using flex, I get the very short error message "Null". The Tomcat log file says:
java.lang.NullPointerException at org.jspresso.framework.application.action.AbstractActionContextAware.getTranslationProvider(AbstractActionContextAware.java:84)
??
(Just say if you need my programs)
Marc
Sure... My bad.
The new SNAPSHOT is uploading fixing the NPE.
Best,
Vincent
Hello,
I implemented a class MyXMLExportCallback that worked fine in teh previous version of JSPresso. Now however I get the following compile error:
MyXMLExportCallback.java:[37,7] com.visual.modeler.MyXMLExportCallback is not abstract and does not over
ride abstract method getFileName(java.util.Map<java.lang.String,java.lang.Object>) in org.jspresso.framework.application.frontend.file.IFileSaveCallback
I have no clue what I should put in a new method getFileName.
Thanks,
Marc
Oups, I forgot to notify about this one.
We needed to be able to dynamically compute the file name that is suggested in the save dialog box opened by the file save action. This is what this method is made for. It gives the file save callback a chance to compute the proposed file name based on the current action context instead of having it statically configured.
However, if you want everything as before, just implement this new method and return null.
HTH,
Vincent