Thursday 10 April 2014

Understanding and using the Liferay Document Library - Part 2

In Part 1 of this blog post we saw some basic concepts around how document library can be used, also the database side of it like the tables being used and also the file structure.

In this post I will show you how to upload a document using Liferay's  document library API.

To get started create a MVC Portlet.


I have created a simple portlet using Liferay plugin SDK. I am going to make some additions to that. If you want to understand how to create a Portlet you can either refer to Liferay developer guide, here is the link. Or you can refer to one of my previous blog posts.

The API call to upload document is -

PortletFileRepositoryUtil.addPortletFileEntries(
oa.getGroupId(), oa.getUserId(), OfficeAccount.class.getName(), oa.getAccountId(), "OfficeAccount",

15635, inputStreamOVPs);

I will show you how to do this in a Portlet.

Lets call this portlet as "manage-document-portlet".

Our intention is to present a simple interface to the user from where the user can upload a document.

Something like this


Next let's create a simple database table where we will keep some meta information related to the document. It helps since the document library knows which class the document refers to.

I used Liferay service builder to generate persistence layer. I like this tool since its convenient and takes care of all the tedious stuff.

I called the primary entity "OfficeAccount". And a screenshot of the service xml which I used to generate classes and table is provided below


Now lets create the JSP to look like the UI I had shown previously. Its a pretty simple JSP.


Once the user selects a document and clicks on submit then the action class is invoked. The controller class looks like



As you can see its a pretty simple controller. I have created a "OfficeAccount" object and populated some values in it.

The main part is the call to service layer

OfficeAccountLocalServiceUtil.addOfficeDocs(oaObj, inputStreamOVPs);

The inputStreamOVPs parameter comes from the following logic:

UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(request);
List> inputStreamOVPs =new ArrayList>(2);
String fileName = uploadRequest.getFileName("documentFile");
InputStream inputStream = uploadRequest.getFileAsStream("documentFile");
ObjectValuePair inputStreamOVP = new ObjectValuePair(fileName, inputStream);
inputStreamOVPs.add(inputStreamOVP);

Next lets go to the service implementation class and look at the changes


We make a simple Liferay API call to upload the document

PortletFileRepositoryUtil.addPortletFileEntries(
oa.getGroupId(), oa.getUserId(), OfficeAccount.class.getName(), oa.getAccountId(), "OfficeAccount",

15635, inputStreamOVPs);

Please note - I have hard coded the folder id here. The one we created in Part 1. This is not a good practice though.

That's it. This will associate your "OfficeAccount" entry with the document library table.

To verify you can check the DLFileEntry table.

Look out for my next post in which I will show how to add logic to display the documents we have uploaded.

No comments:

Post a Comment