Pages

Monday, August 31, 2009

Get Current Language Id - Liferay

to get current selected language we can use the following code

String languageId = LanguageUtil.getLanguageId( renderRequest );

Which will resides in portal-kernal.jar file.

Open popup window in liferay

I have a situation - where i need to display the playlists of videos in a portlet, whenever the user clicks on the playlist it should open the popup, in that we need to run the video.

For this i used the Add application window as it is.

after login we will use add application option for adding our own portlets, yes That POP UP only.

To use this we need to use liferay theme objects

Step 1- <%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
-lt- liferay-theme:defineObjects / -gt-

Step 2 - script type="text/javascript"
src="<%=themeDisplay.getPathJavaScript()%>/jquery/ui.dialog.js"

Step 3 -jQuery.noConflict();
jQuery(document).ready(
function()
{
// jQuery("#ayudaCertificados").dialog({ width: 450, height: 400, modal: true,
autoOpen: true});
}
);

Step 4 - Write the following javascript functions in script tag

buttonClicked(title,content) {
// alert(" om sai ram 1");
var playerText="content";
title = ""+title+"";
callAjax(playerText);
jQuery("#myDiv").dialog({
width: 830,
height:470,
modal: true,
autoOpen: true,
closeOnEscape:true,
title:title,
resizable:false
});
// alert(" om sai ram 2");
}
function callAjax(content){
document.getElementById("myDiv").innerHTML = content;
}


Step 5 - Now difine one empty div tag in your jsp file with ID name "myDiv"( you can change
it).
eg: -lt- div id="myDiv"-gt- -lt- /div -gt-

Step 6 - Now the important code which shows pop up window.
write an anchor tag and call the above defined function ( buttonClicked(title,content) )
on onclick function like...

a href="#" onclick="buttonClicked(title,content) "

supply the necessary parameters title and content as per your requirement.

change default language in custom portlets

our own portlets defaultly doesnt changes the data as per selected language even the database contains the related language stuff. To Get that respective language data we need to use

JournalContentUtil.getContent(groupId, articleId, viewMode, languageId, themeDisplay);

It will returns the Content in String format.
To work with this we need to import base file

<%@ page import="com.liferay.portlet.journalcontent.util.JournalContentUtil" %>

It will be available in portal-service.jar file.

Packages used by Liferay

liferay uses N no of packages as per the different situations, where they are existed, how they are importing in to the liferay, how the liferay portlets are getting the portlet objects, liferay theme objects....etc is a big question for every liferay starters.

Answer for this question is Liferay defines all the necessary package classes in one jsp file called init.jsp which is available at

--liferay tomcat home-- /webapps/ROOT/html/common

So please go through the to know how many important classes are needed for liferay and think about how can we utilize them in our real time applications?

Get Current Portlet ID

Way 1- liferay Theme objects will give us the current portlet Id. (To know about liferay theme
objects please go through my previous post)

portletDisplay.getId();


Way 2- we can directly get the current portlet id using webkeys. To get WebKeys reference we
need to import

com.liferay.portal.kernel.util.WebKeys which is available in portal-kernal.jar file.

request.getAttribute(WebKeys.PORTLET_ID)

Liferay theme objects

To get Liferay Theme object please follow the below code.


--><%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
-lt-portlet:defineObjects /-gt-

To get the portlet related objects.

<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
-lt- liferay-theme:defineObjects -gt-

To get the theme related Objects.
Example: To get selected theme folder path.
script type="text/javascript"
src="-lt-%=themeDisplay.getPathJavaScript ()%-gt-/jquery/ui.dialog.js"

--> The objects that are injected into the pageContext by the
-lt- liferay-theme:defineObjects /-gt- tag are:

* themeDisplay - com.liferay.portal.theme.ThemeDisplay
* company - com.liferay.portal.model.Company
* account - com.liferay.portal.model.Account (deprecated)
* user - com.liferay.portal.model.User
* realUser - com.liferay.portal.model.User
* contact - com.liferay.portal.model.Contact
* ?layout - com.liferay.portal.model.Layout
* ?layouts - List
* plid - java.lang.Long
* ?layoutTypePortlet - com.liferay.portal.model.LayoutTypePortlet
* portletGroupId - java.lang.Long
* permissionChecker - com.liferay.portal.security.permission.PermissionChecker
* locale - java.util.Locale
* timeZone - java.util.TimeZone
* theme - com.liferay.portal.model.Theme
* colorScheme - com.liferay.portal.model.ColorScheme
* portletDisplay - com.liferay.portal.theme.PortletDisplay



Note: -lt- with lessthan symbol
-gt- with greaterthan symbol

Liferay-Connecting to the Database in the way of liferay

We can directly use the Liferay Database connection while writing our own portlets. By following the below procedure.

step 1- import com.liferay.portal.kernel.util.InfrastructureUtil;
which will available under portlet-kernal jar file.

Step 2- import javax.sql.DataSource;
liferay uses DataSource mechanism for maintaining the database connections.

step 3- Connection connection = null;
DataSource dataSource = null;

create above variable for getting and storing datasource and connection to the database.

step 4- dataSource = InfrastructureUtil.getDataSource();
connection = dataSource.getConnection();

This is it here we are having the liferay datasource and from that datasource object we
will get the connection object.

Note: In general way it will raises sql exceptions so dont forget to handle the exceptions. ;)