Thursday 30 April 2015

clean undeployed portlet's preferences and permissions from database tables

Problem Description:
An undeloyed portlets from Liferay portal webapp context only removes the portlet of visibility in the “Add Application” so that permission users can’t add that portlet to a page. But portlets instance information was not removed from layout and its preferences (user and layout level) and permissions. Still there is a mapping existed in tables from liferay schema.
Proposed Solution: As Liferay provides cool feature of hooks which users can customize Liferay Portal source. We can use this hook to identify the undeployed portltes and clean the unwanted data.
Steps to reproduce the problem
Step: 1 - Download Liferay Instance
Step: 2 - Download photolibraryportlet (this only for example, you can deploy and underplay any custom portlet)
Step: 3 - Create 2 Liferay Pages and add calendar portlet,
Step: 4 - Configure Portlet Preferences
Step-5 - Setup Portlet Permissions

Page1


 
Page2

Page/Layout Level Porlet Related Data
SELECT * FROM portletpreferences WHERE portletId LIKE '%photolibraryportlet%';


SELECT * FROM resourcepermission WHERE NAME LIKE '%photolibraryportlet%';


Step-6: Undeploy the photolibraryportlet portlet
  1. Remove the removelibraryportlet webapp context from portal
cd %LIFE_HOME%\tomcat-7.0.42\webapps\market-photolibrary-portlet
rm –rf market-photolibrary-portlet
Step-7: Access Page1 and Page2 where you have render the portlet through admin
Admin can’t add the portlet from UI as it’s undeployed – working as expected



Validation: Even though portlet was undeployed still its information existed in liferay tables
SELECT * FROM portletpreferences WHERE portletId LIKE '%photolibraryportlet%';
SELECT * FROM resourcepermission WHERE NAME LIKE '%photolibraryportlet%';
Summary of API for this hook plugin:
  1. Get Group Layouts(public and private)
List<Layout> layouts= LayoutLocalServiceUtil.getLayouts(groupId, privateLayout)
  1. Iterate Layouts and identify the undeployed portlets
List<Portlet> allPortlets = layoutTypePortlet.getPortlets()
if(pagePortlet.isUndeployedPortlet())
  1. Remove Porlet from Layout and update Layout
layoutTypePortlet.removePortletId(userId, pagePortlet.getPortletId())

Steps to clean undeployed portlets entries from database tables.
  • Step2: click ‘server administration’ option

  • Step3: click ‘Execute’ button to clean Database tables entries of undeployed portlets. 

     
  • Step4: After successful execution we will get success message.

  • On the log file, you can find
    • 1. total number sites are available in the current instance
    • 2. total number of applied layouts are 4 for the respective site
    • 3. total number of added portlets for the respective page
    • 4. cleaned undeployed xxx-portlet portlet entries from database table

Final validations :
  • DB Validation (No Data related to page level)
    • SELECT * FROM portletpreferences WHERE portletId LIKE '%photolibraryportlet%';
    • SELECT * FROM resourcepermission WHERE NAME LIKE '%photolibraryportlet %';