RE: Uploading csv and donwloading excel file in liferay 7

Jiten R. Vaghela, modified 7 Years ago. New Member Posts: 20 Join Date: 8/22/16 Recent Posts
Hello everyone.

In my project I have one requirement. In which I have to upload bulk data using .csv file and validate it based on business logic.

If the data is not proper as per business logic, I have to create excel file with all csv records along with comment on recored where the validation get failed and donwload it on client side.

But if there is no error in csv data as per business logic then I have to add it in database and no file required to be donwload.

So my problem is.....

What should I use.

Whether I'll go with action command or do I use serveResource command?

Here, I must say that if data is valid than no file need to be donwloaded. if validation fail then file must be downloaded with some comment.........


I'm not sure what to do......

Somebody please help
thumbnail
David H Nebinger, modified 7 Years ago. Liferay Legend Posts: 14933 Join Date: 9/2/06 Recent Posts
Unlike the servlet world, these requests must be broken up into the separate portlet requests.

A file upload is an action request.

A response showing an error is a render request.

A file download is a resource request.

See them as the individual parts they must be, then it should be clearer how to proceed...

The upload receives the CSV and does the processing, perhaps keeping the file or the data or perhaps it goes all the way and builds the xls, but it can't return anything. It can, however, pass around request attributes so it can share the fact that the error has occurred and perhaps relevant keys for download.

The render request is handled by a JSP page (in Liferay MVC but could be any render technology you are using) which shows that an error has occurred and the link to download the xls, created as a resource URL and tied to the interface element.

The resource handler is used to return the xls. Perhaps this was already pre-built by the upload, but me, I'd actually delay and build the XLS on demand in the serve resource method. You never really know if someone will click the link to pull the file or not. The bigger question is whether you need to persist the XLS (so you only build one for many downloads or you build on demand so there is no storage requirement but if 100 people click the link you're recreating the file 100 times).

Either way, everything has been broken down to the point where it is clear how to proceed with the portlet implementation.

It was just a question of separating into the proper portlet request flow.
Jiten R. Vaghela, modified 7 Years ago. New Member Posts: 20 Join Date: 8/22/16 Recent Posts
Hello Devid,

Thanks for your valuable response.

in my scenario, I have below screen:

  

Here, I'm uploading CSV just for bulk data upload. I don't want to save csv at all.


Once I'm Getting data at server side from csv. I don't need the CSV furthermore.

After data unwrapped from csv. I have a mechanism to validate them.

During the validation process If data is not valid then only the entire csv data transferred to .xls(Excel) file and the same file will download
to the user who uploaded it.

The purpose of this downloading is to show the faulty record to the user who uploaded the csv.

Else data insert to the database table.

I don't need csv and xls at all for future use. Also, No one will allow to downloading same xls file or csv file again.


Process :

Step1: upload csv file.

Step2: Validate data

          if data is valid
                 Insert data in table
          else 
              -->>> create xls from csv data
              -->>> donwload xls to user who uploaded csv.
finish

How could I handle this ?
thumbnail
Fernando Fernandez, modified 7 Years ago. Expert Posts: 401 Join Date: 8/22/07 Recent Posts
Hi Jiten, 

Not sure what you're asking, but I usually process CSVs with opencsv and generate excel files with apache poi.  

If your csv is small enought to process imediatly, I'd suggest your upload action should generate HTML to inform the user of the result, showing a download link in case of data error.

HTH

​​​​​​​Fernando
Jiten R. Vaghela, modified 7 Years ago. New Member Posts: 20 Join Date: 8/22/16 Recent Posts
Hi Fernando Fernandez,

Thanks for your reply.