Share Services Between Plugin Portlets In Liferay

Sharing services between plugin portlets is very easy in Liferay. Usually people place services jar manually when they want to access any other portlet's services.

Here is the automated where liferay will take of the dependent service jar. You just need to name the dependent portlet's name.

Let say for your library management portlet you want to access services of student portlet. In liferay-plugin-package.properties file you need to enter below entry.
File liferay-plugin-package.properties you can locate inside WEB-INF folder.

required-deployment-contexts=\
    student-portlet

When you deploy library-portlet you will see below logs. As you can see in the logs before deploying library-portlet it will check for student-portlet.
It will build student-portlet first and then copy service jar from student-portlet into library-portlet.

18 Apr 2014 05:23:35:866 INFO  [HotDeployEvent:109] Plugin library-portlet requires student-portlet
18 Apr 2014 05:23:36:111 INFO  [HotDeployImpl:178] Deploying library-portlet from queue
18 Apr 2014 05:23:36:112 INFO  [PluginPackageUtil:1033] Reading plugin package for library-portlet
18 Apr 2014 05:23:36:928 INFO  [PortletHotDeployListener:294] Registering portlets for library-portlet
18 Apr 2014 05:23:37:079 INFO  [PortletHotDeployListener:454] 1 portlet for library-portlet is available for use

There are other usage properties available inside liferay-plugin-package.properties file
(portal-dependency-jars and portal-dependency-tlds). 
By using this properties you can use jars and tlds available Liferay portal.



JSON Serialization in Liferay

Many times we have requirement to convert Liferay service response or any list into JSON object. Most of the time we are doing this manually.

There are other ways as well by which we can convert List into JSON string.
Service builder has support for JSON  serialization,too. The json-enabled attribute of entity tag specifies whether or not the entity should be annotated for JSONserialization.
By default, if the remote-service value is true, then thejson-enabled value is true.
Column tag has the same attribute, json-enabled.
It specifies whether or not the column should be annotated for JSON serialization. By default, if the json-enabled value in the entity element is true, then the json-enabled value in the column element is true.

json enabled is true for enttity TestJson so all the column tags will have by default json-enabled as true.
In case if you want to exclude any property to not to appear when you serialize then you can set json-enabled as false. For email column json-enabled is set to false.

build-service and check TestJSONModelImpl class. For the field json-enabled is true you will see @JSON annotation. For excluded fields you will see @JSON(include = false) .

Now you can easily convert your list returned using Liferay services into JSON format.

JSONSerializer jsonSerializer = JSONFactoryUtil.createJSONSerializer();
String json = jsonSerializer.serialize(list);


You can also exclude fields you don't need in JSON.




Same way you can use serialize OOB data as well like user.

 List<User> userList = UserLocalServiceUtil.getUsers(-1, -1);
 String json = JSONFactoryUtil.looseSerialize(userList);

 By default collections are excluded from serialization . You can include them using below way.
 String json  = JSONFactoryUtil.looseSerialize(userList,"organizations")

Sort Solr Search Result

Below snippet will help you to sort solr search results.

Few things i would like to highlight about solr result sorting.
You can not sort tokenized field i mean if you have defined your solr field with the type text or textToTight.
You can sort the field which have type string . You can create dynamic fields easily in solr.
Add below entry in schema.xml and solr will automatically create field with the type string.

<dynamicField name="*_sortable" type="string" indexed="true" multiValued="false" stored="true" />

If you use any field with suffix _sortable then solr will automatically create field if it is not exist.

Sort Search Container Result

To sort search container result follow below steps.

1) Create search container in your JSP.


2) Create search container in your controller and set result in search container object.


3) Get sorted results using dynamic query.

Connect Liferay with SAP using SAP JCO Connector

This post will elaborate you on how to connect your Liferay portal with SAP Database.
Many times we want need data from SAP database either using web service call or using any third party connector.
In this post we are going to use JCO connector to connect Liferay portal with SAP Database.


Prerequisite

1) First download SAP JCO Connector for your operating system from http://service.sap.com/connectors.
If you are using windows then make sure you download correct version of sapjco dll and jar.
For windows you need to place sapjco dll inside Windows\System32 directory

2) Next thing is configure sapjco3.jar inside Lifeary portal.
Place this jar inside [LIFERAY_TOMCAT]/lib/ext.


Liferay SAP Integration


Following diagram will give you some idea on integrating Liferay with SAP. [Image 1 Diagram]

SAP Java Connector (SAP JCo) is a middle ware component that supports communication with the SAP Server in both directions: inbound calls (Java calls ABAP) and outbound calls (ABAP calls Java).

In this post we will only try to understand inbound calls on how java calls ABAP (SAP)

There are basically 3 steps required to connect with this SAP System.

  1. Defining Destination
    • Define and prepare property file with all connection details.
    • Connection details includes
      1. Host Name
      2. System Number
      3. Client Details
      4. User Name
      5. Password
  1. Establishing Connection
    • In the 1 st step we created property file with all connection details.
    • In this step we are going to use property file to establish connection with SAP System.
  1. Executing Function
    • The RFCs enable you to call and execute predefined functions in a remote system.
    • The RFCs manage the communication process, parameter transfer, and error handling. 

Example

  • I have created sample portlet in LR 6.1 GA2 to connect liferay with SAP database.
  • Portlet will display success message when connection is successfully done.
  • Download and play with this portlet. Here is the download link.
  • Following are some portlet screens that will help you to understand this sample portlet.



  • Click on Test SAP Connection button to connect with SAP System.
  • Once Connection is successfully done you will see following screen.



  • Define all connection related properties inside portlet.properties file of this sample portlet.