Export data to csv
Important: This module works only for Ontimize Boot version 3.9.0 or above. Actual release version:
Introduction
Ontimize provides a system to export the DAO data of a service and dump it directly to an *.csv file. This system uses a JSON template where all the necessary parameters are indicated to use in the body of the request to obtain the file.
Prerequisites
Note: You can follow this tutorial using your own application, although for this example we will use an application created using the archetype that can be found on this page and with a REST service.
There are 2 options to follow this tutorial, clone the repository with the initial state and follow the tutorial step by step, or download the final example and see which files are new and which have been updated.
Initial project
/$ git clone https://github.com/ontimize/ontimize-examples
/ontimize-examples$ cd ontimize-examples
/ontimize-examples$ git checkout boot-export-initial
Final example
/$ git clone https://github.com/ontimize/ontimize-examples
/ontimize-examples$ cd ontimize-examples
/ontimize-examples$ git checkout boot-export
Steps
Note: To simplify the code being written, three dots (…) may appear in some parts of the code. This indicates that there may be previous code before and after those dots.
Add dependencies
Two dependencies need to be added, one to the ws module containing the controllers (so that it can respond to the export request) and one to the boot module to be able to load the autoconfigurator in the application.yml file for export.
-
ontimize-examples
-
projectwiki-api
-
src
-
main
-
java
-
com
-
ontimize
-
projectwiki
-
api
-
core
-
service
- ICandidateService.java
- IUserService.java
-
service
-
core
-
api
-
projectwiki
-
ontimize
-
com
-
java
-
main
- .gitignore
- pom.xml
-
src
-
projectwiki-boot
-
src
-
main
-
java
-
com
-
ontimize
-
projectwiki
- ServerApplication.java
-
projectwiki
-
ontimize
-
com
-
resources
- application.yml
-
java
-
main
- .gitignore
- pom.xml
-
src
-
projectwiki-model
-
src
-
main
-
db
- templateDB.lck
- templateDB.properties
- templateDB.script
- templateDB.txt
-
java
-
com
-
ontimize
-
projectwiki
-
model
-
core
-
dao
- CandidateDao.java
- UserDao.java
- UserRoleDao.java
-
service
- CandidateService.java
- UserService.java
-
dao
-
core
-
model
-
projectwiki
-
ontimize
-
com
-
resources
-
dao
- CandidateDao.xml
- placeholders.properties
- RoleDao.xml
- RoleServerPermissionDao.xml
- ServerPermissionDao.xml
- UserDao.xml
- UserRoleDao.xml
-
dao
-
db
-
main
- .gitignore
- pom.xml
-
src
-
projectwiki-ws
-
src
-
main
-
java
-
com
-
ontimize
-
projectwiki
-
ws
-
core
-
rest
- CandidateRestController.java
- MainRestController.java
- TestRestController.java
- UserRestController.java
-
rest
-
core
-
ws
-
projectwiki
-
ontimize
-
com
-
java
-
main
- .gitignore
- pom.xml
-
src
- .gitignore
- pom.xml
- README.md
-
projectwiki-api
projectwiki-boot/pom.xml
projectwiki-ws/pom.xml
Add export url to application.yml
In the application.yml file, a configuration will be added to allow indicating the export URL and the extension used.
-
ontimize-examples
-
projectwiki-api
-
src
-
main
-
java
-
com
-
ontimize
-
projectwiki
-
api
-
core
-
service
- ICandidateService.java
- IUserService.java
-
service
-
core
-
api
-
projectwiki
-
ontimize
-
com
-
java
-
main
- .gitignore
- pom.xml
-
src
-
projectwiki-boot
-
src
-
main
-
java
-
com
-
ontimize
-
projectwiki
- ServerApplication.java
-
projectwiki
-
ontimize
-
com
-
resources
- application.yml
-
java
-
main
- .gitignore
- pom.xml
-
src
-
projectwiki-model
-
src
-
main
-
db
- templateDB.lck
- templateDB.properties
- templateDB.script
- templateDB.txt
-
java
-
com
-
ontimize
-
projectwiki
-
model
-
core
-
dao
- CandidateDao.java
- UserDao.java
- UserRoleDao.java
-
service
- CandidateService.java
- UserService.java
-
dao
-
core
-
model
-
projectwiki
-
ontimize
-
com
-
resources
-
dao
- CandidateDao.xml
- placeholders.properties
- RoleDao.xml
- RoleServerPermissionDao.xml
- ServerPermissionDao.xml
- UserDao.xml
- UserRoleDao.xml
-
dao
-
db
-
main
- .gitignore
- pom.xml
-
src
-
projectwiki-ws
-
src
-
main
-
java
-
com
-
ontimize
-
projectwiki
-
ws
-
core
-
rest
- CandidateRestController.java
- MainRestController.java
- TestRestController.java
- UserRestController.java
-
rest
-
core
-
ws
-
projectwiki
-
ontimize
-
com
-
java
-
main
- .gitignore
- pom.xml
-
src
- .gitignore
- pom.xml
- README.md
-
projectwiki-api
ontimize:export:
Attribute | Values | Meaning |
---|---|---|
url | String | Specifies the path to use the export system. |
enable | Boolean | Indicates when export is enabled. |
Example
...
ontimize:
...
export:
url: /export
enable: true
...
Check the export system
An application such as Postman will be used to execute the REST export request for our project. A POST request will be made to the previously configured url using *.json as the body of the request containing all the necessary information for the export.
- URL: http://localhost:33333/export/csv
- HTTP Method: POST
- Authorization: User: demo, Password: demouser
- Body: JSON
After click in Send button, click in Save Response ^ and save it to a file. Then, open the *.csv file in your editor.
Postman
CSV
Creating the JSON for exporting data
This is the list of values accepted by the JSON request to generate the export file.
Attribute | Values | Meaning | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
queryParam | JSON Object | A JSON object defining the columns and sqltypes elements.
|
||||||||||||||||
service | String | This is the bean name of the service you want to query. (The name that appears inside the @Service() annotation, e.g.: @Service(“SERVICE_NAME”) = SERVICE_NAME) | ||||||||||||||||
dao | String | Name of the query method of the service to be queried without the suffix Query or PaginationQuery, e.g.: customerQuery = customer | ||||||||||||||||
path | String | Name of the path to be queried | ||||||||||||||||
advQuery | Boolean | Determines whether the DAO query method is Query or PaginationQuery. | ||||||||||||||||
columns | JSON Object | Determines the order of the columns in the export | ||||||||||||||||
columnTitles | JSON Object | Translates the name of the column to be exported, replacing it with the value of the key. | ||||||||||||||||
columnTypes | JSON Object | Key-value pairs that indicate how the data will be treated within the cell, e.g.: String, Date, Integer, etc. These data types are those corresponding to the database data type, which can be found at this link | . |