Tuesday, December 3, 2013

Open Liferay Portlet in AUI Dialog/PopUp


Objective:

Open liferay  portlet in AUI Dialog/ PopUp.

We can open any portlet in AUI dialog in liferay portal. This is very helpful in many real time scenarios

Download OpenPortletAuiDialog portlet from following location

You can find source and war file


Note: 

Portlet developed in Liferay 6.1GA2 EE version

If you want deploy in CE version you just do changes in liferay-plugin-package.properties

Liferay 6.1 EE version

name=OpenPortletAuiDialg
module-group-id=liferay-ee
module-incremental-version=1
tags=
short-description=
change-log=
page-url=http://www.liferay.com
author=Liferay, Inc.
licenses=EE
liferay-versions=6.1.20


Liferay 6.1 CE version

name = OpenPortletAuiDialg
module-group-id=liferay
module-incremental-version=1
tags=
short-description=
change-log=
page-url=http://www.liferay.com
author=Liferay, Inc.
licenses=LGPL
liferay-versions=6.1.1

Procedure for deploy portlet:

You can use war file and directly place in your portal deploy folder and test or you can also use source to deploy portlet.

Once portlet is deployed successfully you can see the portletX and PortletY in sample category.

Add PortletX to any page and then click on button you can see PortletY in AUI Dialog and you can perform some action in PorletY.

Note:

Before use this portlet read entire article and then test the portlet

When we open portlet in dialog we need consider the following points
  • Add the tag  <add-default-resource/> in liferay-portlet.xml file
  • Add portlet id to portlet.add.default.resource.check.whitelist
  • Make sure window sate should be pop_up/exclusive
  • AUI Dialog should be Iframe Type


Add the tag <add-default-resource/> in liferay-portlet.xml file

We need to add this xml tag in liferay-portlet.xml file. This tag is used to adding the  portlet to page dynamically.

If we not add this tag liferay-portlet.xml then portlet won’t be load into the dialog.

Note:

Carefully add tag in appropriate position because in liferay portlet xml file have some sequence to each tag. This is we can add after tag
<css-class-wrapper>portlety-portlet</css-class-wrapper>.

Add portlet id to.add.default.resource.check.whitelist property in portal-ext.properties file.

We need to mention our portlet id in portlet.add.default.resource.check.whitelist other wise portlet not allowed to load in dialog or pop up.

Already this list has some default portlets we need to add our portlet id as comma separate.

By default liferay have some security checks so we need specify above property so that portlet can be loaded into pop up or dialogs.

Example:

portlet.add.default.resource.check.whitelist=3,56_INSTANCE_0000,
58,82,86,87,88,103,113,145,164,166,170,177,
userpreferences_WAR_StockCurrencyWeatherNewsportlet

Here our custom portlet id is:
userpreferences_WAR_StockCurrencyWeatherNewsportlet

Note:

This scenario happened when we want to embed portlet in theme then we need to use above property in portal-ext.properties file

Make sure widow sate should be pop_up/execlusive

When we going to open portlet in pop ups or AUI dialog then portlet window state should be pop_up or exclusive

These two sates are meant for this specific purposes.

AUI Dialog should be Iframe Type

When we open portlet in Dialog we need to perform some action after port let is loaded that is why needed to choose Iframe AUI dialog.

We can also use normal AUI dialog but all actions in portlet should be performed by Ajax call.

The following is example code for open Portlet in AUI dialog /Pop up


<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<%@ taglib uri="http://liferay.com/tld/security" prefix="liferay-security" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@ taglib uri="http://liferay.com/tld/util" prefix="liferay-util" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@page import="com.liferay.portal.kernel.portlet.LiferayWindowState"%>
<portlet:defineObjects />
<liferay-theme:defineObjects />
<div>
<h1>Iframe Dialog Please click on button  and see Portlet Y is open In AUI Dialog....
</h1><br/>
<aui:button value="Click Me I will Open PortletY" id="openPortletY" name="openPortletY"></aui:button>
</div>
<aui:script>
AUI().use('aui-base','aui-dialog','aui-io','liferay-portlet-url','aui-dialog-iframe',function(A) {
A.one('#<portlet:namespace/>openPortletY').on('click', function(event){
    var url =Liferay.PortletURL.createRenderURL();   
    url.setPortletId("PortletY_WAR_OpenPortletAuiDialgportlet_INSTANCE_S4Rs"); 
    url.setWindowState('pop_up');  
    var dialog = new A.Dialog({
    title: 'I Opened PortletY',
    centered: true,
    modal: true,
    width: 500,
    height: 400,
    }).plug(A.Plugin.DialogIframe, {
    uri:url,
    iframeCssClass: 'dialog-iframe',
    }).render();

});
});
</aui:script>


For detailed information about AUI Dialogs Please go through following linke



In the Example I have developed 2 portlets PortletX and PortletY

I am going to open PortletY in AUI Dialog from PortletX

I will explain two scenarios here

  1. Drag and Drop the PortletX in any Portal Page
  2. Embed Portlet in Theme


Drag and Drop the PortletX in any Portal Page

When we add port let in page then clicks on button then portletY is opened in AUI Dialog for this we need to add following xml tag in liferay-portlet.xml

That is <add-default-resource>true</add-default-resource> in liferay-portlet.xml

Embed Portlet in Theme:

When we going to embed portlet in Theme then we need add xml tag and portet Id should be in portlet.add.default.resource.check.whitelist  property list

Example:

We want to open PortletY in AUI Dialog and portletX embed in theme then we need to do following steps

add following in lifera-portlet.xml

<portlet>
                        <portlet-name>PortletY</portlet-name>
                        <icon>/icon.png</icon>
                        <instanceable>false</instanceable>
                        <header-portlet-css>/css/main.css</header-portlet-css>
                        <footer-portlet-javascript>/js/main.js
                        </footer-portlet-javascript>
                        <css-class-wrapper>portlety-portlet</css-class-wrapper>
                        <add-default-resource>true</add-default-resource>
</portlet>

Add portlet id in portal-ext.properties file with following property list


portlet.add.default.resource.check.whitelist=3,56_INSTANCE_0000,
58,82,86,87,88,103,113,145,164,166,170,177,
PortletY_WAR_OpenPortletAuiDialgportlet


The following is code for Embed portlet in theme

When we want embed portlet in side theme we need to add following code in portal_noram.vm file of theme which is templates directory of theme


<div>
#set ($VOID = $velocityPortletPreferences.setValue("portlet-setup-show-borders", "false"))
#set ($VOID = $theme.runtime("PortletY_WAR_OpenPortletAuiDialgportlet","",
$velocityPortletPreferences.toString())) 
</div>


When we embed portlet theme then consider following tag too. This also need to add in liferay-portlet.xml file of the port let going to open in AUI dialog.

<system>true</system>

Example:

<portlet>
                        <portlet-name>PortletY</portlet-name>
                        <icon>/icon.png</icon>
                        <instanceable>false</instanceable>
                        <header-portlet-css>/css/main.css</header-portlet-css>
                        <footer-portlet-javascript>/js/main.js
                        </footer-portlet-javascript>
                        <css-class-wrapper>portlety-portlet</css-class-wrapper>
                        <add-default-resource>true</add-default-resource>
                       <system>true</system>
</portlet>

Note:

When we work with any action like render or action in side PortletY should be window state pop_up/execlusive.


Generate Portlet Id

We have two types of portlet

  1. Single Instance Portlets
  2. Multi Insttance Portlets


Single Instance Portlets

Single instance portlet can be added only one to each liferay page. From the following tag we can decide portlet is single instance.

Add the following tag in liferay-portlet.xml file and values should be false.

<instanceable>false</instanceable>

When we add the tag then we can see only one portlet for each page.

Make sure tag position of tag in liferay-portlet.xml file

Example:

<portlet>
                        <portlet-name>PortletY</portlet-name>
                        <icon>/icon.png</icon>
                        <instanceable>false</instanceable>
                        <header-portlet-css>/css/main.css</header-portlet-css>
                        <footer-portlet-javascript>
                                    /js/main.js
                        </footer-portlet-javascript>
                        <css-class-wrapper>portlety-portlet</css-class-wrapper>
                        <add-default-resource>true</add-default-resource>
</portlet>

Generating Portlet ID to single instance portlet

The Portlet Id pattern is following


PortletName_WAR_PluginPortletCotextName


Portlet Name:

You can find the portler Name in portlet.xml file as the tag 
<portlet-name>PortletX</portlet-name>

_WAR_

 This constant string for any plug-in portlet

Plug-in Portlet Context Name:

This is context of your plug-in portlet after deployment.

This name you can find in build.xml file of your portlet

The following is Example:

 build.xml file as follows

<?xml version="1.0"?>
<!DOCTYPE project>
<project name="OpenPortletAuiDialg-portlet" basedir="." default="deploy">
            <import file="../build-common-portlet.xml" />
</project>

Example of portlet Id for single instance portlet:


PortlrtY_WAR_ OpenPortletAuiDialg-portlet



Multi Instance Portlets

When portlet is multi instance then we can more portlets to one page.

We need to add following tag in liferay-portlet.xml file and values should be true

<instanceable>true</instanceable>

When we add the tag then we can add same portlet for multiple in the page.

Make sure tag position in liferay-portlet.xml file

Example:

<portlet>
                        <portlet-name>PortletY</portlet-name>
                        <icon>/icon.png</icon>
                        <instanceable>true</instanceable>
                        <header-portlet-css>/css/main.css</header-portlet-css>
                        <footer-portlet-javascript>
                                    /js/main.js
                        </footer-portlet-javascript>
                        <css-class-wrapper>portlety-portlet</css-class-wrapper>
                        <add-default-resource>true</add-default-resource>
</portlet>

Generating Portlet Id to single instance portlet

The Portlet Id pattern like


PortletName_WAR_PluginPortletCotextName_
INSTANCE_4CharectesAlphaNumeriString


Portlet Name

You can find the portlet Name in portlet.xml file as the tag
<portlet-name>PortletX</portlet-name>

_WAR_

This constant string for any plug-in portlet

Plug-in Portlet Context Name:

This is context of your plugin portlet after deployment.
This name you can find in build.xml file of your portlet.

_INSTANCE_

 This is another constant string for multiple instance portlets.

4 or more Characters Alpha Numeric String(your choice):

 Any string which consist of alphabets and numeric character

Example as follows

vD3e

Example Portlet Id for Multiple Instance Portlet:


PortletX_WAR_OpenPortletAuiDialgportlet_INSTANCE_vD3B


Note:

To make unique id for portlet  they have used  such pattern.

Important Points:

  • To open portlet in AUI dialog we need to add tag <add-default-resource/> in liferay-portlet.xml and value should be true
  • If we embed portlet in theme and we use AUI dialog to open portlet then we need to add portlet id to list of portlet.add.default.resource.check.whitelist as comma separate with existed values and which should be overridden by using portal-ext.properties file
  • We have two kinds of portlets single instance and multi instance and portlet id generated with different pattern for each type.
  • When we open portlet in AUI Dialog then portlet window state should be pop_up/exclusive for each URL it may be render URL or action URL.


Portlet Screen




Reference Links









 Author: Meera Prince

Sunday, December 1, 2013

Liferay Document Viewer Plugin Portlet


Objective: 

Create Plug-in portlet  to view documents

Liferay already have documents preview in documents and media portlet by using that we can view documents

Document preview concept is uses Apache PDF Box by default from this we can see the PDF document.

For this we need not to do any configuration so that it will automatically view the PDF documents.

For better quality views life ray have provide future to configure Image Magick tool so that we can view documents with very good quality.

If we not configure Image Magick tool for life ray then it will show following message in the console  when we try to preview documents in liferay.


Liferay is not configured to use ImageMagick for generating Document Library previews and will default to PDFBox. For better quality previews, install ImageMagick and enable


If we use Image Magic tool then document is converted into images. Means each page is converted into one image.

This can be handle by liferay libraries the main class is PDFProcessorImpl.java it will take care the all the things

Download LiferayDocViewr  portlet from following location

You can find source and war file


Note: 

Portlet developed in Liferay 6.1GA2 EE version

If you want deploy in CE version you just do changes in liferay-plugin-package.properties
Liferay 6.1 EE version

name=LiferayDocViewr
module-group-id=liferay-ee
module-incremental-version=1
tags=
short-description=
change-log=
page-url=http://www.liferay.com
author=Liferay, Inc.
licenses=EE
liferay-versions=6.1.20


Liferay 6.1 CE version

name = LiferayDocViewr
module-group-id=liferay
module-incremental-version=1
tags=
short-description=
change-log=
page-url=http://www.liferay.com
author=Liferay, Inc.
licenses=LGPL
liferay-versions=6.1.1

Procedure for deploy portlet:

You can use war file and directly place in your portal deploy folder and test or you can also use source to deploy portlet.

Once portlet is deployed successfully you can see the portlet in sample category name as
Liferay Doc Viewer.

Login as Admin and add some sample documents using Document Media Portlet in liferay

Note:

Before use this portlet read entire article and then test the portlet

Now our target id create plug-in portlet for to view documents

  1. Configure Image Magic tool
  2. Configure Open Office and Start Open Office Service.
  3. Develop portlet to view documents


Configure Image Magic tool

Download Image Magick Tool from Following Location


Install image magic in your system, follow the installation steps and choose your image magick installation directory

Generally it is in program Files, this path we will use in liferay Image magick configuration


Now login as Admin in liferay and go control panel

Go to portal server section and click on Server Administration menu form the tab choose External Service from that enable image magick tool and configure Image Magick installation path


The following is screen for Image Magick configuration in liferay.




Now Document Viewer uses Images Magick service to generate images from document then we can see view good quality

If any quality problem then go through following URL from liferay wiki

Configure Open Office and Start Open Office Service

From document viewer can support by default PDF view. If we want see MS office documents and open office documents we need to configure open office and we need to start open office service.

The concept something like first documents will be converted into PDF and then PDF documents will be converted into images based on pages the document contains.each page converted into one image.

Go through following link configure open office and start open office service


Develop portlet to view documents

Liferay have already provides document preview in Document Library Portlet.

They have created AUI JavaScript to make view template.

This AUI script will take preview URL and Number of pages count as options.
The page preview URL something like Follows


The following is Example URL




Each time when the request have  /document URL pattern the request will be handled by WebServerServlet.java  servlet this will start PDFProcessor and do further actions like convert PDF to images or documents to PDF and then images.

The following is important java classes

PDFProcessorUtil.java and PDFProcessorImpl.java

Internally it uses many implementation classes from portal

Note:

If document is more then it will take time to generate images

If document is ms office or open office first it should convert into PDF and then images so It will take time to complete this process.

The following is code for document viewer template
<%@page import="com.liferay.portlet.documentlibrary.util.DLUtil"%>
<%@page import="com.liferay.portlet.documentlibrary.util.PDFProcessorUtil"%>
<%@page import="com.liferay.portal.kernel.repository.model.FileVersion"%>
<%@page import="com.liferay.portal.kernel.repository.model.FileEntry"%>
<%@page import="com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil"%>
<%@page import="com.liferay.portlet.documentlibrary.service.DLAppHelperLocalServiceUtil"%>
<%@ include file="init.jsp"%>
<%
long fileEntryId=ParamUtil.getLong(renderRequest,"fileEntryId");
FileEntry fileEntry=DLAppLocalServiceUtil.getFileEntry(fileEntryId);
FileVersion fileVersion=fileEntry.getFileVersion();
boolean hasPDFImages = PDFProcessorUtil.hasImages(fileVersion);
int previewFileCount = 0;
String previewFileURL = null;
String[] previewFileURLs = null;
String videoThumbnailURL = null;
String previewQueryString = null;
if (hasPDFImages) {
            previewFileCount = PDFProcessorUtil.getPreviewFileCount(fileVersion);
            previewQueryString = "&previewFileIndex=";
            previewFileURL = DLUtil.getPreviewURL(fileEntry, fileVersion, themeDisplay, previewQueryString);
            %>
            <div class="lfr-preview-file" id="<portlet:namespace />previewFile">
            <div class="lfr-preview-file-content"
                        id="<portlet:namespace />previewFileContent">
                        <div class="lfr-preview-file-image-current-column">
                                    <div class="lfr-preview-file-image-container">
                                                <img class="lfr-preview-file-image-current"
                                                            id="<portlet:namespace />previewFileImage"
                                                            src="<%= previewFileURL + "1" %>" />
                                    </div>
                                    <span class="lfr-preview-file-actions aui-helper-hidden"
                                                id="<portlet:namespace />previewFileActions"> <span
                                                class="lfr-preview-file-toolbar"
                                                id="<portlet:namespace />previewToolbar"></span> <span
                                                class="lfr-preview-file-info"> <span
                                                            class="lfr-preview-file-index"
                                                            id="<portlet:namespace />previewFileIndex">1</span> of <span
                                                            class="lfr-preview-file-count"><%= previewFileCount %></span>
                                    </span>
                                    </span>
                        </div>

                        <div class="lfr-preview-file-images"
                                    id="<portlet:namespace />previewImagesContent">
                                    <div class="lfr-preview-file-images-content"></div>
                        </div>
            </div>
</div>
<aui:script use="aui-base,liferay-preview">
new Liferay.Preview(
                                    {
            actionContent: '#<portlet:namespace />previewFileActions',
            baseImageURL: '<%= previewFileURL %>',
            boundingBox: '#<portlet:namespace />previewFile',
    contentBox: '#<portlet:namespace />previewFileContent',
            currentPreviewImage: '#<portlet:namespace />previewFileImage',
            imageListContent: '#<portlet:namespace />previewImagesContent',
            maxIndex: <%= previewFileCount %>,
    previewFileIndexNode: '#<portlet:namespace />previewFileIndex',
            toolbar: '#<portlet:namespace />previewToolbar'
            }).render();
</aui:script>
<%}else{%>
<h2>Unable to view document</h2>
<%}%>

Liferay.Preview needs options baseImageURL and maxIndex means total page count.

Note:

We can also use Jquery Page Flip to make better view


Tip to generate images

<%
for(int i=1;i<= previewFileCount;i++){%>

<img   id="<portlet:namespace />previewFileImage"
src="<%= previewFileURL + "1" %>"
/>

<%}%>

Important Points

  • Liferay have provide document view by default by using PDF box
  • To get better quality view we need to configure Image Magick tool to liferay .
  • To view only PDF documents then we need not to configure open office.
  • To View documents we need to configure open office and we need to start open office service
  • In document view each page converted into one image by PDF processor.
  • If documents are Open office or MS office then first it will be converted into PDF and then it will be converted into images based on number of pages document contains
  • Liferay provide AUI java script template to view documents we can also use JQuery Page Flip to make better view.
  • The following are portlet screen shots

Screen 1:



Screen 2:



Reference Links


https://www.liferay.com/web/alexander.chow/blog/-/blogs/7341017


Author
Meera Prince

Recent Posts

Recent Posts Widget

Popular Posts