Export entities to csv

Hi.

I´d like to export the whole data of an entity to a csv file. After searching for a while, it seems to me that this feature is not available yet in Cuba. I found this example, which works fine:

https://github.com/andreysubbotin/sample-sales/commit/6fb88bd1b124719eed41a4940eb37fbcd2a0044a

But it only exports the data that is being displayed on the GUI rather than exporting the whole table data (of an entity). Like, when the user only wants to the 20 records on the GUI, my ‘export’ function should export every record of the entity nevertheless.

I thought of writing a service that uses the entity manager, but this approach has the disadvantage that I would have to implement a SQL-query for every database supported. Generally, it would be better if I could leave this to Cuba.

Any advice on how to proceed?

Thank you very much.

Best regards,
Markus

Under the Administration menu item in your app, there is an item called “Entity Inspector”. From here you can choose the Entity you want to view. This populates a table with all instances of that Entity. You can then hit the “Excel” button. This also limits the exported data to the items currently loaded into the table, but from the filter you can choose to show 5000 rows at a time which should be ample in most applications.

Hi Weston,
thank you for your reply.

Unfortunately, I have to enable this funtionality directly on the entity’s browse view. Thereby, the user does not have to click on another menu entry and select the entity subsequently.

At the moment, I think I have to use an EntityManager and query the tables directly.

Other suggestions are welcome :slight_smile:

Best regards,
Markus

Hi,
I didn’t understand your words about SQL:

I thought of writing a service that uses the entity manager, but this approach has the disadvantage that I would have to implement a SQL-query for every database supported.

You don’t have to write SQL, you’ll have to write JPQL once ant it will work for all databases. In the project you mentioned there is a method that exports a collection of entities. Its single argument is that collection. As I understand your question is how to get a full collection of entities of specific type? You can use the DataManager for that. The following code should work:

dataManager.load(entityClass).list();

Hi Max,

thank you very much, your code works like a charm. Somehow, I must have missed this when reading about DataManager :thinking:

In general you have to provide a jpql query to the data manager, e.g. “select e from demo$MyEntity e where …”, but if you don’t do it then the default query “select e from <ENTITY_NAME> e” is used.