Today we will discuss how we can display files which are stored in document and media portlet of Liferay.We also create download link so that user can download the files which are stored in document and media . Here i am using Liferay 7.3.
So lets start this step by step :-
For this i created a folder with name Testing and upload 2 files in it.
Step 1:- Create a simple MVC Portlet
Create a simple Liferay module Project and provide the portlet name as DocumentAndMediaPortlet and paste this content:-
DocumentAndMediaPortlet.java
package com.aditya.portlet; | |
import com.aditya.constants.DocumentAndMediaPortletKeys; | |
import com.liferay.document.library.kernel.service.DLAppService; | |
import com.liferay.portal.kernel.exception.PortalException; | |
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet; | |
import com.liferay.portal.kernel.repository.model.FileEntry; | |
import com.liferay.portal.kernel.repository.model.Folder; | |
import com.liferay.portal.kernel.theme.ThemeDisplay; | |
import com.liferay.portal.kernel.util.WebKeys; | |
import java.io.IOException; | |
import java.util.Collections; | |
import java.util.List; | |
import javax.portlet.Portlet; | |
import javax.portlet.PortletException; | |
import javax.portlet.RenderRequest; | |
import javax.portlet.RenderResponse; | |
import org.osgi.service.component.annotations.Component; | |
import org.osgi.service.component.annotations.Reference; | |
/** | |
* @author adit2 | |
*/ | |
@Component( | |
immediate = true, | |
property = { | |
"com.liferay.portlet.display-category=category.Liferay Is Easy", | |
"com.liferay.portlet.header-portlet-css=/css/main.css", | |
"com.liferay.portlet.instanceable=true", | |
"javax.portlet.display-name=DocumentAndMedia", | |
"javax.portlet.init-param.template-path=/", | |
"javax.portlet.init-param.view-template=/view.jsp", | |
"javax.portlet.name=" + DocumentAndMediaPortletKeys.DOCUMENTANDMEDIA, | |
"javax.portlet.resource-bundle=content.Language", | |
"javax.portlet.security-role-ref=power-user,user" | |
}, | |
service = Portlet.class | |
) | |
public class DocumentAndMediaPortlet extends MVCPortlet { | |
@Reference | |
DLAppService dlAppService; | |
@Override | |
public void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException { | |
ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest.getAttribute(WebKeys.THEME_DISPLAY); | |
long groupId = themeDisplay.getSiteGroupId(); | |
List<FileEntry> files = Collections.emptyList(); | |
try { | |
Folder folder = dlAppService.getFolder(groupId,0,"Testing"); | |
files = dlAppService.getFileEntries(groupId, folder.getFolderId()); | |
} catch (PortalException e) { | |
e.printStackTrace(); | |
} | |
renderRequest.setAttribute("fileList", files); | |
super.doView(renderRequest, renderResponse); | |
} | |
} |
Explanation :
Here i am using DLAppService which provide as all the required methods. We get the List of files are present in Testing folder and put in request.
Step 2:- Change view.jsp
Now change the view.jsp so that we can display all the files using search container.
view.jsp
<%@page import="com.aditya.portlet.DocumentAndMediaUtil"%> | |
<%@page import="java.text.SimpleDateFormat"%> | |
<%@page import="com.liferay.portal.kernel.repository.model.FileEntry"%> | |
<%@page import="java.util.List"%> | |
<%@page import="com.liferay.portal.kernel.util.ListUtil"%> | |
<%@ include file="/init.jsp" %> | |
<% | |
List<FileEntry> files = (List<FileEntry>)request.getAttribute("fileList"); | |
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); | |
%> | |
<liferay-portlet:renderURL varImpl="iteratorURL"/> | |
<liferay-ui:search-container total="<%= files.size() %>" delta="10" emptyResultsMessage=" Please add some documents in Testing folder" iteratorURL="<%=iteratorURL %>"> | |
<liferay-ui:search-container-results results="<%=ListUtil.subList(files, searchContainer.getStart(),searchContainer.getEnd())%>" /> | |
<liferay-ui:search-container-row className="com.liferay.portal.kernel.repository.model.FileEntry" modelVar="file" keyProperty="fileEntryId" > | |
<liferay-ui:search-container-column-text name="Title" value="${file.title}"/> | |
<liferay-ui:search-container-column-text name="Type" value="${file.extension}"/> | |
<c:set var="file" value="${file}"/> | |
<% | |
FileEntry fileEntry = (FileEntry) pageContext.getAttribute("file"); | |
String formattedSize = DocumentAndMediaUtil.getFormattedFileSize(fileEntry.getSize()); | |
String downloadURL = DocumentAndMediaUtil.getDownloadURL(themeDisplay, fileEntry); | |
%> | |
<liferay-ui:search-container-column-text name="size" value="<%= formattedSize %>"/> | |
<liferay-ui:search-container-column-text name="Last Updated By" value="<%= sdf.format(file.getModifiedDate()) %>"/> | |
<liferay-ui:search-container-column-text name="Download" value="Click Here" href="<%=downloadURL %>"/> | |
</liferay-ui:search-container-row> | |
<liferay-ui:search-iterator /> | |
</liferay-ui:search-container> |
Explanation:
By default size is in digits like 123, 6787 so we need to format it so that it should formatted in MB or KB.
For this we use LanguageUtil class . I created a separate utility class that format the size and give us URL for download.
Step 3:- Create Utility Class
Now create a utility class DocumentAndMediaUtil and paste this:-
DocumentAndMediaUtil.java
package com.aditya.portlet; | |
import com.liferay.document.library.util.DLURLHelperUtil; | |
import com.liferay.portal.kernel.exception.PortalException; | |
import com.liferay.portal.kernel.language.LanguageUtil; | |
import com.liferay.portal.kernel.repository.model.FileEntry; | |
import com.liferay.portal.kernel.theme.ThemeDisplay; | |
import java.util.Date; | |
import java.util.Locale; | |
public class DocumentAndMediaUtil { | |
public static String getFormattedFileSize(long fileSize) { | |
return LanguageUtil.formatStorageSize(fileSize, Locale.getDefault()); | |
} | |
public static String getDownloadURL(ThemeDisplay themeDisplay,FileEntry fileEntry) { | |
String downloadURL =""; | |
try { | |
downloadURL = DLURLHelperUtil.getDownloadURL(fileEntry, fileEntry.getLatestFileVersion(), themeDisplay, String.valueOf(new Date().getTime())); | |
} catch (PortalException e) { | |
e.printStackTrace(); | |
} | |
return downloadURL; | |
} | |
} |
Step 4:- Check Output
Deploy your portlet and add to page.
You can download the source code from here.
No comments:
Post a Comment