Monday, 8 February 2016

Get portlet preference

Get portlet preference


In many cases we need to find portlet preferences.
Below is the code which can help to find portlet preferences.


PortletPreferences preferences = renderRequest.getPreferences();
String portletResource = ParamUtil.getString(request, "portletResource");
if (Validator.isNotNull(portletResource)) {
preferences = PortletPreferencesFactoryUtil.getPortletSetup(request, portletResource);
}



String OAuthConsumerKey = preferences.getValue("OAuthConsumerKey", StringPool.BLANK);
String OAuthConsumerSecret = preferences.getValue("OAuthConsumerSecret", StringPool.BLANK);
String OAuthAccessToken = preferences.getValue("OAuthAccessToken", StringPool.BLANK);

Portlet Preferences

Portlet Preferences


There are two kinds of portlet preferences available.
  • Static Preferences
  • RunTime Preferences
  •   
1. Static Preferences:

It is nothing but, the portlet prefrences which we set will be constant troughout the portlet.
Example:
Mention the following lines inside your portlet.xml file:
<portlet-preferences>
 <preference>
      <name>department</name>
      <value>liferay</value>
   </preference>


    <preference>        <name>company</name>
      <value>cignex</value>
   </preference>

</portlet-preferences>

Access the above preferences department and company in your jsp or java files.
PortletPreferences preferences = renderRequest.getPreferences();
String department = preferences.getValue("departmentt", "");
String company = preferences.getValue("company", "");


2. RunTime Preferences:

We can change the portlet preferences value at runtime and we can access the value throught the portlet. The preferences for a portlet are stored in "PortletPreferences" table.
Steps to follow:

  • Access the "department" value in jsp page from the portlet preferences table.
  • Provide another jsp page to modify the department value.
  • Update the department value in portlet preferences table.
Example:

Step 1: Mention the following code where you want to access department value. In our case we using view.jsp page.

<%
PortletPreferences preferences = renderRequest.getPreferences();
String portletResource = ParamUtil.getString(request, "portletResource");
if (Validator.isNotNull(portletResource)) {
   preferences = PortletPreferencesFactoryUtil.getPortletSetup(request, portletResource);

}
 String department = preferences.getValue("department", "liferay");
%>
Note: If the department value is null while fetching from portlet preferences table, by default liferay will be assigned to department value, because we mentioned liferay will be the default value for department.
Step 2: Mention the following code where you want to provide the UI for altering the department value. In our case we using modifyportletprefernces.jsp page.

<%
PortletPreferences preferences = renderRequest.getPreferences();
String portletResource = ParamUtil.getString(request, "portletResource");
if (Validator.isNotNull(portletResource)) {
  preferences = PortletPreferencesFactoryUtil.getPortletSetup(request, portletResource);

}
String department = preferences.getValue("department", "liferay");
%>
<form action="Mention your action" method="POST" name="<portlet:namespace />fm">
  <table>
    <tr>
      <td class="lfr-label">Page Title</td>
      <td><input name="<portlet:namespace />department" type="text" value="<%= department%>"/></td>
    </tr>
  </table>
  <input type="submit" value="<liferay-ui:message key="save" />" />
</form>

Step 3: Mention the following code in your action class to update the department value in the portlet preferences table.

String depratment= ParamUtil.getString(actionRequest, "department");
//Getting the PortletResource
String portletResource = ParamUtil.getString(actionRequest, "portletResource");

//Creating the Reference for Portletpreferences Table
PortletPreferences prefs = PortletPreferencesFactoryUtil.getPortletSetup(actionRequest, portletResource);

 //Setting The Value in the PortletPreferences table
prefs.setValue("department", department);
prefs.store();

Sharing service layer

We can share our custom service layer in liferay if we want to use it in other portlets,hooks,ext,etc.
Step 1:
Create portlet X.And generate Service Layer.
Note:Use table and name attribute in entity tag before generating service layer.
Step 2:
If you want to make portlet X service layer available for whole portal then you want to create separate portlet (for ex: portlet Y) and create generate service layer as you created in portlet X.
Note:Here you should use table and name attribute in entity tag.Or else It  may affect portlet X service layer.
Step 3: Now check whether you got .jar file in portlet Y WEB-INF/lib
Step 4:Copy that jar file and paste it inside your server  /tomcat-7.0.42/lib/ext.And restart your server.
Note:You can use ext instead of directly pasting it in server.
Create ext and paste .jar file inside YourEXT/docroot/WEB-INF/ext-lib/global.Then do direct deploy.
Step 5:Now you can see that API's will be available for all portlets,hooks and ext.

Wednesday, 13 January 2016

How to create Friendly URL for Liferay portlet

  • Q-What is friendly URL?
  • ans: Friendly URL is a technique to make short portlet URL.
  • To make friendly URL, we need to define router xml for that portlet.
  • In this router xml, we can define various parameters (implicit , explicit etc).
  • We can define multiple friendly URL for same portlet.
 Step1--first we need to create friendly-url-router.xml  inside src.
example:-
<?xml version="1.0"?>
<!DOCTYPE routes PUBLIC "-//Liferay//DTD Friendly URL Routes 6.2.0//EN"
"http://www.liferay.com/dtd/liferay-friendly-url-routes_6_2_0.dtd">

<routes>
    <route>
        <pattern>/{jspPageName}</pattern>
        <generated-parameter name="jspPage">/html/controller/{jspPageName}.jsp</generated-parameter>
        <implicit-parameter name="p_p_lifecycle">0</implicit-parameter>
        <implicit-parameter name="p_p_state">normal</implicit-parameter>
        <implicit-parameter name="p_p_mode">view</implicit-parameter>
    </route>
</routes>

Step2- we need to add three line code in liferay-portlet.xml

<icon>/icon.png</icon><friendly-url-mapper-class>com.liferay.portal.kernel.portlet.DefaultFriendlyURLMapper</friendly-url-mapper-class><friendly-url-mapping>ekhub</friendly-url-mapping><friendly-url-routes>com/test/ccavenue-friendly-url-routes.xml</friendly-url-routes><instanceable>false</instanceable>

Tuesday, 12 January 2016

Diffrence b/n Liferay MVC vs Spring MVC

  • Liferay MVC and Spring MVC are vastly used portlet framework to create portlets in Liferay.
  • Liferay MVC is a lightweight framework which can be used to create portlet if it contains less complex render / resource logic and few action methods.
  • On other hand Spring MVC is full fledged portlet framework. It provides many functionality.
  • Liferay MVC can have just one render method. However liferay provides mechanism to render different jsps at runtime. render URL is used to call render method.
  • Spring MVC portlet provides multiple render methods. There will be one default render method which is called by default. To call other render methods, you need to pass render method mapping key to action parameter while creating render URL.
  • You can use Liferay MVC for simple portlet creation. Spring MVC Portlet framework can be used where you have complex render  / resource logic and you need spring integration in your portlet.
  • Liferay MVC vs Spring MVC is always debatable topic. You should choose the appropriate framework while created portlet based on you requirement
  • You may refer this link to create portlet in Liferay.
  • Action Methods
    • Both Liferay MVC and Spring MVC frameworks supports multiple action methods.
  • Resource Method
    • In Liferay MVC, you can define just one resource method.
    • In Spring MVC, you can define multiple resource method.

Following are the steps to implement Custom SQL

Following are the steps to implement Custom SQL
  1. Create Simple Liferay MVC Portlet
  2. Define entity in service.xml
  3. Run the Service Builder
  4. Create SQL queries configuration XML file (Custom SQL XML file)
  5. Write Native SQL in Custom SQL xml file
  6. Create Finder Implementation class
  7. Run the Service Builder
  8. Implement Required Custom SQL Method in Finder Implementation Java Class
  9. Run the Service Builder
  10. Write Custom Service Method in respective Local Service Implementation Class
  11. Run the Service Builder
  12. Use Respective Local Service Util class to call service method

How to increase String column size in a ServiceBuilder generated service?

Step1:Now the issue is that Strings here are mapped to a 75 VARCHAR in the database by default, but you can change that in the portlet-model-hints.xml file by specifying a max-length hint for the given field entry. Here's an example how to increase it to 500 for instance:
1
2
3
4
5
6
<model name="my.package.model.DummyEntity">
 <field name="id" type="long"></field>
 <field name="description" type="String">
  <hint name="max-length">500</hint>
 </field>
</model>