Wednesday, July 08, 2015

Creating Portlet URLs


Listed below are various ways to create portlet action and render phase URLs

Creating URL within/to the current portlet.

1. Using Taglib:

For Render URL

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

<portlet:renderURL var="myRenderURL1" windowState="<%=LiferayWindowState.NORMAL.toString()%>">
<portlet:param name="mvcPath" value="/html/view/myresponse.jsp"/>
</portlet:renderURL>

Similarly action URL looks like below
<portlet:actionURL var="myActionURL1" windowState="<%=LiferayWindowState.NORMAL.toString()%>">
<portlet:param name="cmd" value="update"/>
</portlet:actionURL>

2. Using Java code

For render URL
PortletURL myRenderURL2=renderResponse.createRenderURL();
myRenderURL2.setWindowState(LiferayWindowState.NORMAL);
myRenderURL2.setParameter("cmd", "view");
Similarly action URL looks like below
PortletURL myActionURL2=renderResponse.createActionURL();
myActionURL2.setWindowState(LiferayWindowState.NORMAL);
myActionURL2.setParameter("cmd", "update");

Creating URL outside the current portlet, that is URL to the other page or portlet.


PortletURL myRenderURL3= PortletURLFactoryUtil.create(request,  themeDisplay.getPortletDisplay().getId(), themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);

 myRenderURL3.setWindowState(LiferayWindowState.NORMAL);
 myRenderURL3.setPortletMode(LiferayPortletMode.VIEW);
 myRenderURL3.setParameter("jspPage", "/season/rainy.jsp");

Here themeDisplay.getPortletDisplay().getId() is the id of the current portlet to be viewed or it can be the id of the other portlet to be viewed and themeDisplay.getPlid() is the page layout Id of the current page and can point to the plid of some other page on which the portlet is deployed.


Tuesday, June 30, 2015

Invoke custom portlet service using Java Reflection


Requirement:
Create a custom service 'myPojo' in a custom portlet named 'my-portlet'.
Access the above service in a Liferay jsp via hook.
This can be achieved by placing the service jar in the tomcat-lib but in certain scenarios where the access and deployment restrictions are applied and deployment does not allow to manually deploy the service jar to tomcat-lib below approach will be helpful.

Portlet name : my-portlet
Service Entity : MyPojo
Method to invoke: getMyPojo
Pojo property to read : _myField (exact name can be verified from the service generated model implementation class MyPojoModelImpl)




ClassLoader classLoader = PortletBeanLocatorUtil.getBeanLocator("my-portlet").getClassLoader();
Class clazz = classLoader.loadClass("com.informatick.hub.service.MyPojoLocalServiceUtil");
java.lang.reflect.Method method = clazz.getMethod("getMyPojo", long.class);
Object retunedObject = method.invoke(clazz.newInstance(), classPK);
    
java.lang.reflect.Field ouaField;
Class beanClass = retunedObject.getClass().getSuperclass().getSuperclass();
        
 ouaField = beanClass.getDeclaredField("_myField");
 ouaField.setAccessible(true);
 String tempString = (String) ouaField.get(retunedObject);

 long oua = 0;
 oua = GetterUtil.getLong(tempString);

 System.out.println("Returned vvalue : "+oua) 



Wednesday, April 30, 2014

Liferay : Advance Web Content Structures and Templates

The post details about the functionality to have multiple views for a web content article.

Currently only one view can be rendered for a article, but in real word scenarios there are occasions when different view are needed for the same content on same or different pages in Liferay.
Below are the steps with detailed code to achieve the above feature:

Step 1:
Create a new structure

<?xml version="1.0"?>

<root>
 <dynamic-element name="Name" type="text" index-type="" repeatable="false">
  <meta-data>
   <entry name="displayAsTooltip"><![CDATA[true]]></entry>
   <entry name="required"><![CDATA[true]]></entry>
   <entry name="instructions"><![CDATA[]]></entry>
   <entry name="label"><![CDATA[Name]]></entry>
   <entry name="predefinedValue"><![CDATA[]]></entry>
  </meta-data>
 </dynamic-element>
 <dynamic-element name="Address" type="text" index-type="" repeatable="false">
  <meta-data>
   <entry name="displayAsTooltip"><![CDATA[false]]></entry>
   <entry name="required"><![CDATA[true]]></entry>
   <entry name="instructions"><![CDATA[]]></entry>
   <entry name="label"><![CDATA[Address]]></entry>
   <entry name="predefinedValue"><![CDATA[]]></entry>
  </meta-data>
 </dynamic-element>
</root>



Step 2:
Create a new template


<p>
 <span style="font-size: 12px; ">
  <em><strong>Name: </strong>${Name.getData()}</em>
 </span>
</p>
<p>
 <span style="font-size: 12px; ">
  <em><strong>Address:</strong>${Address.getData()}</em>
 </span>
</p>




Step 3:
Create a web content based on structure and template created in step 1 and step 2. Populate some data in the web content.

Now our focus will be to create a new view for the content created in above in step 3

Step 4:
Thus create a new structure which will be dummy structure and fields here will not be relevant
<?xml version="1.0"?>

<root>
 <dynamic-element name="Dummy" type="text" index-type="" repeatable="false">
  <meta-data>
   <entry name="displayAsTooltip"><![CDATA[true]]></entry>
   <entry name="required"><![CDATA[true]]></entry>
   <entry name="instructions"><![CDATA[]]></entry>
   <entry name="label"><![CDATA[Dummy]]></entry>
   <entry name="predefinedValue"><![CDATA[]]></entry>
  </meta-data>
 </dynamic-element>

</root>





Step 5:
Create a new template, based upon structure creates in step 4, and will be considered as second view of the content created in step 3:

#set ($articleId = "10813")

#set ($renderUrl  = $request.render-url)
#set ($namespace = $request.portlet-namespace)
#set ($groupId = $getterUtil.getLong($groupId))

##
## Getting services
##
#set ($journalArticleService = 
 $serviceLocator.findService('com.liferay.portlet.journal.service.JournalArticleLocalService'))


#set ($article = $journalArticleService.getLatestArticle($groupId,$articleId))

<h3>Details</h3>
<br />
<table class="taglib-search-iterator">
 <tr class="results-header">
  <th class="col-1">Name</th>
  <th class="col-1">Address</th>
 </tr>
  
       #set ($document = $saxReaderUtil.read($article.content))
       #set ($root = $document.getRootElement())
       
       #set ($nameD = 
         $root.selectSingleNode(
           "dynamic-element[@name='Name']/dynamic-content"))
           
       #set ($addressD = 
         $root.selectSingleNode(
           "dynamic-element[@name='Address']/dynamic-content"))


   
      <tr class="results-row first">
       <td class="align-left col-1 valign-left">
       ${nameD.text}
       </td>

            <td class="align-left col-2 valign-middle">
           ${addressD.text}
       </td>

      </tr>
                   
</table>



Here $articleId is the article id of the content created in step 3.

Step 6:
Create a web content based on structure and template created in step 4 and step 5. The purpose of this content will be to display the data from the content created in step 3.


Liferay: Selecting multiple categories in Search portlet and Category Navigation portlet

Below is the original post which helps you to modify search and category navigation portlet and as a result user will be able to select multiple categories.

For more details go to:
http://liferaythought.wordpress.com/2013/04/30/selecting-multiple-categories/

Friday, April 18, 2014

RSA Encryption/Decryption - Part 2

My previous post on RSA details about basics of RSA and how to generate public and private key in java.


Encryption in Java



         public static byte[] encrypt(String text, PublicKey key) {
  byte[] cipherText = null;
  try {
   // get an RSA cipher object and print the provider
   final Cipher cipher = Cipher.getInstance(ALGORITHM);
   // encrypt the plain text using the public key
   cipher.init(Cipher.ENCRYPT_MODE, key);
   cipherText = cipher.doFinal(text.getBytes());

  } catch (Exception e) {
   e.printStackTrace();
  }
  return cipherText;
 }




Decryption in Java



         public static String decrypt(byte[] text, PrivateKey key) {
  byte[] decryptedText = null;
  try {
   // get an RSA cipher object and print the provider
   final Cipher cipher = Cipher.getInstance(ALGORITHM);

   // decrypt the text using the private key
   cipher.init(Cipher.DECRYPT_MODE, key);
   decryptedText = cipher.doFinal(text);

  } catch (Exception ex) {
   ex.printStackTrace();
  }

  return new String(decryptedText);
 }