Monday, December 9, 2013

Sharing Liferay Service Layer between two plugin portlet contexts


Objective:

Using one plug-in portlet services in other plug-in portlet.

In liferay we develop portlets in plug-in portlet environment. Here some time we may develop group of portlets in single plug-in context or sometimes we develop in multiple plug-in contexts.

When we get requirement like we want use one plug-in context liferay services in other plug-in context, we need to follow the some sequence of steps when we share service layer between two different liferay plug-in portlet contexts.

Download Sharing service layer Example  portlets from following location

You can find source and war file


Note: 

This portlet developed in Liferay6.1GA2 EE you can change source based on your liferay version, please go through my previous articles to know more information about this.

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.

First deploy the Child-portlet make sure deployment should be successful.

Second deploy Parent-portlet.

Drag and Drop Parent portlet which in sample category then you can see the child table row data.

Note:

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


Frits we need to identify required dependent plug-in context

We need to specify the required plug-in context name in liferay-plugin-package.properties file as follows



required-deploymentcontexts=
PluginPortletContextName1,PluginPortletContextName2

Example:

required-deployment-contexts=Child-portlet




Note: if multiple contexts then speared by comma

First deploy the liferay plug-in portlet context which we mention in liferay-plugin-package.properties as a property required-deployment-contexts.

Next deploy the second portlet

Example:

Assume we have two plug-in contexts Parent-portlet and Child-portlet

In Parent-portlet I have one entity called Parent and in Child-portlet I have entity called Child.

Now I create service builder for both plug-in portlets and run the service builder. Here both are two different plug-in contexts

Now we will get two service jar files in each plug-in portlet WEB-INF/lib directory (Parent-portlet.war/WEB-INF/lib/Parent-portlet-service.jar and Childt-portlet.war/WEB-INF/lib/Child-portlet-service.jar).

Now we want use Child-portlet services in Parent-portlet for this need to specify the Child-portlet context information in Parent-portlet. This information we need provided by liferay-plugin-package.properties of Parent-portlet as follows


required-deployment-contexts=Child-portlet


Once we mention above property then Child-portlet services will be available in Paretn-portlet means we can use ChildLocalServieUtil, ChildSeriveceUtil and ChildUtil classes in Parent-portlet(As we know our entity in child portlet is child)

Now simple get child information in parent portlet JSP page as follows


<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@page import="com.meer.db.service.ChildLocalServiceUtil"%>
<portlet:defineObjects />
<h1>This is parent portlet</h1>
<%=ChildLocalServiceUtil.getChild(1)%>


Now deploy the Child-portlet first

Next and finally we have to deploy the Parent-portlet.

Note:

In the development in Liferay IDE/Eclipse when we use child services it will show compile time errors but don’t worry you can deploy the portlet.

If you really don’t want see such compile time errors you can add services jar file in portlet build path.

In above example you can add Child-portlet-service.jar file in Parent-portlet build path. Simple right click on Parent-portlet in eclipse and select build path option add Child-portlet-service.jar before do this you need to run service builder for Child-portlet then only Child-portlet-service.jar file available.

Observations:

When we want share service layer between two plug-in contexts the services jar file should be available in other portlet plug-in context class path i.e. WEB-INF/lib.

In above example we are sharing Child-portlet services in Parent-portlet so we need to make available Child-portlet-service.jar file in Parent-portlet/WEB-INF/lib so that we can use Child-portlet service classes.

To do above task we need to mention the child context information in parent portlet as I mention above property.

After deployment of two portlet if you see the Parent-portlet/WEB-INF/lib (in server deployment directory) you can find the Child-portlet-service.jar file. Because of this only we able access the child portlet services.

The following is Parent-portlet lib after deployment in the server



Screens:

Child-portlet with service.xml file



Parent-portlet and we mentioned child deployment context.



The following is Parent-portlet which is accessing Child-portlet services.




Author
Meera Prince
www.liferaysavvy.com







Friday, December 6, 2013

Access Liferay Database Services in Themes/Velocity Templates

Access Liferay Database Services in Themes/Velocity Templates
Objective:

Access liferay database services in themes. Means access Locals Service and Service classes in liferay themes.

Generally some time we need to show some dynamic data in theme, the data may be stored in any table in liferay database .When we get such requirement we need to access database from vm files.
In theme level liferay have given service locater feature to access database services.

Here we are talking about theme means we are access liferay services in portal_normal.vm file or other VM (velocity) files which is in liferay themes.

In liferay themes all VM (velocity) files available in templates directory. One of the  core velocity file is portal_normal.vm.

Liferay already providing many default velocity variables in theme level such as
$theme_display, $portlet_display, $layout, $company and $user.

These variables we can see in init.vm file we can use these variables anywhere in theme vm files when we include vm file in our working velocity file.

Liferay already include this in portal_notmal.vm file so that we can use any variable which is available in init.vm

Information about velocity

Velocity is a Java-based template engine. It permits anyone to use a simple yet powerful template language to reference objects defined in Java code.

Liferay uses velocity templates to aggregate dynamic content of  web page.

Here all variable which starts with $ symbol

The following is link to know more about velocity


Declare variable and assign value

#set( $a = "Velocity" )

Print variable  

<html>
<body>
#set( $foo = "Velocity" )
Hello $foo World!
</body>
<html>

In liferay we can see many declaration of variables in init.vm file for example as follows

#set ($user_name = $user.getFullName())

To print user Name

Hi I am $user_name

Note: 

Whenever it find the $ it will interpret as velocity variable by velocity engine while rendering.
This is enough for us to understand about velocity.

Now accessing liferay data base services in themes

  1. Make it available of service jar file in liferay global class path
  2. Use velocity service locater to find data base service in theme


Make it available of service jar file in liferay global class path

Foremost thing is your service jar file should be available in liferay portal global class path
Means liferay portal service (portal-service.jar) available in liferay portal global class path. By default this jar file available in global lib directory of portal.

For Tomcat:

The following is liferay portal global class path

tomcat-7.0.27\lib\ext

 If any jar file which is available in tomcat-7.0.27\lib\ext path then it can be available to liferay portal and plug-in portlets too.

By default liferay portal service jar file available in global lib so we can access all liferay portal services such as UserLocalService,GrouopLocalService and LayoutLolcaServices.

Note:

 There are many services available in liferay portal level all clasess you can find in portal-service.jar the following packages com.liferay.portal.service.

For JBoss:

The following is liferay portal global class path

jboss-7.1.1\modules\com\liferay\portal\main

When we work with Liferay JBoss Server this is something different from tomcat server.
For JBoss the service jar file should available in following location then it will be treated as global jar so that all liferay portlets can access these classes.

jboss-7.1.1\modules\com\liferay\portal\main

Once we place the jar in the location we need to entry the jar file name in module.xml file. The module.xml also in same location i.e. jboss7.1.1\modules\com\liferay\portal\main

Example for entries like follows

<resource-root path="portal-service.jar" />

module.xml file

<module xmlns="urn:jboss:module:1.0" name="com.liferay.portal">
            <resources>
                        <resource-root path="hsql.jar" />
                        <resource-root path="jtds.jar" />
                        <resource-root path="portal-service.jar" />
                        <resource-root path="portlet.jar" />
                        <resource-root path="postgresql.jar" />
                        <resource-root path="mysql.jar" />
                        <resource-root path="ojdbc6.jar" />       
            </resources>
            <dependencies>
                        <module name="javax.api" />
                        <module name="javax.mail.api" />
                        <module name="javax.servlet.api" />
                        <module name="javax.servlet.jsp.api" />
                        <module name="javax.transaction.api" />
            </dependencies>
</module>

Note:

Liferay by default make the portal services in global class path level in JBoss .
So we need not do anything and we can use directly all portal service which in portal-service.jar file.

Use velocity service locater to find data base service in theme

We can access any portal service in them i.e. in portal_noraml.vm file or other vm files in liferay. Because liferay already provided porta-service.jar file in global class path , so we can access in liferay themes.

In Liferay theme all vm files included in portal_noraml.vm file this is core vm file for liferay to aggregate all web page content.

If you use any vm file then it should be included in portal_noram.vm file then only it will be rendered.

To access services in them liferay provided service locater in velocity by using this we can get the services in themes.

We have default velocity variable $serviceloacater we can use this variable to get the service.

The following is example code


#set ($userLocalService= $serviceLocator.findService("com.liferay.portal.service.UserLocalService"))


In above code we get the user Service and make sure we have to use full qualified path of java class means class name including package name.

Example like this

com.liferay.portal.service.UserLocalService this is nothing but com.liferay.portal.service.UserLocalService.java class in portal-service.jar file.

Once we get the service object we can apply any method which available in service class.

Example code as follows


#set ($userLocalService= $serviceLocator.findService("com.liferay.portal.service.UserLocalService"))
#set ($user = $userLocalService.getUserById($request.get("theme-display").get("user-id")))
#set ($emailAddress = $user.getEmailAddress())


Now we can simple print email address like this in portal_noram.vm file as follows
<h1>$emailAddress</h1>

Note:

This just example snippet I showed and we already have direct $user object in theme we can use directly that to get user information.

Examples for Services in portal


#set ($userLocalService= $serviceLocator.findService("com.liferay.portal.service.UserLocalService"))

#set ($groupLocalService= $serviceLocator.findService("com.liferay.portal.service.GroupLocalService"))

#set ($layoutLocalService= $serviceLocator.findService("com.liferay.portal.service.LayoutLocalService"))

#set ($roleLocalService= $serviceLocator.findService("com.liferay.portal.service.RoleLocalService"))



Like this we can access any portal services in portal_normal.vm file

Accessing plug-in portlet services in themes

We already know to access portal services but some time we may get requirement to access plug-in portlet services that is our custom services.

The following are the steps
  1. Create plug-in portlet using Liferay IDE with eclipse.
  2. Write entities in service.xml file
  3. Run service builder
  4. Once service builder successfully run then you will get service jar file in plug-in portlet WEB-INF/lib directory.
  5. Copy the plug-in portlet service jar file and place this into liferay global lib directory based on your liferay server (JBoss/Tomcat)
  6. Remove service jar file from plug-in portlet WEB-INF/lib directory
  7. Now deploy portlet and  portlet should be successfully deployed
  8. Now use service locater to get the plug-in service in theme or portal_normal.vm


 Example:

Portlet Name: Student-portlet


Table Name: Student

service.xml file as follows

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.1.0//EN"
 "http://www.liferay.com/dtd/liferay-service-builder_6_1_0.dtd">
<service-builder package-path="com.meera.db">
            <author>E5410</author>
            <namespace>meera</namespace>
            <entity name="Student" local-service="true" remote-service="true">
                        <column name="studentId" type="long" primary="true" />
                        <column name="studentName" type="String" />
                        <column name="studentAddress" type="String" />
            </entity>
</service-builder>

Now run service builder once run service builder successfully then you can get Student-portlet-service.jar in WEB-INF/lib



Now we have to copy Student-portlet-service.jar file to liferay global lib

If Tomcat


tomcat-7.0.27\lib\ext


If JBoss

jboss-7.1.1\modules\com\liferay\portal\main

Register jar file name in module.xml as follows

<resource-root path=" Student-portlet-service.jar" />


Remove Student-portlet-service.jar file form portlet WEB-INF/lib directory

Now deploy then portlet and portlet should be successfully deployed.

Now use following code in portal_normal.vm of your currently working theme


#set ($studentLocalService= $serviceLocator.findService("com.liferay.portal.service.UserLocalService"))
#set ($student = $studentLocalService.getStudent(1)
#set ($studentName= $student.getStudentName())
<div>Hi I am student  my name is <h1>($studentName</h1></div>


Note:

When we use plugin portlet services in then we have to apply above steps to each change/modification  in plug-in portlet services .so make sure you have to follow the steps properly for each change.

Important points

  • Liferay provide many velocity variables to use in themes.
  • Service locater is one velocity variable in liferay from that we can get any services from liferay.
  • To access service in liferay all services should be in Liferay Global class path.

Reference Links:



Author
Meera Prince

Liferay Tables Meta Data

Objective:

Get the Liferay tables meta data information

We can get tables Meta Data from JDBC.

Liferay providing DataAccess.java class from that we can open Connection to liferay default Database. Means we can connect to lportal data base.

Once we get the connection we pass simple select statement to statement object from that we can get Meta Data of table.

The following is example code to get Meta data of User_ tables in Liferay


java.sql.Connection con=null;
java.sql.Statement st=null;

try{
con=com.liferay.portal.kernel.dao.jdbc.DataAccess.getConnection();
st = con.createStatement();
java.sql.ResultSet rs = st.executeQuery("SELECT * FROM user_");
java.sql.ResultSetMetaData rsMetaData = rs.getMetaData();
int numberOfColumns = rsMetaData.getColumnCount();
System.out.println("resultSet MetaData column Count=" + numberOfColumns);
for (int i = 1; i <= numberOfColumns; i++) {
System.out.println("column MetaData ");
System.out.println("column number " + i);
System.out.println(rsMetaData.getColumnName(i));
}
}catch(Exception e){

}finally{
st.close();
con.close();
}



Note:
We need very much take care about connection closing.

Author
Meera Prince

Recent Posts

Recent Posts Widget

Popular Posts