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)