Create Liferay Portlet URLs

No Comments »

You will often have to built a URL leading to a specific portlet without knowing on which page it was placed. In order to find the page (layout) containing your portlet and building a valid LiferayPortletURL you can use the following code:

ThemeDisplay themeDisplay = (ThemeDisplay)liferayPortletRequest.getAttribute(WebKeys.THEME_DISPLAY);
Layout targetLayout = themeDisplay.getLayout(); 
List allLayouts = LayoutLocalServiceUtil.getLayouts(-1, -1);
        
layoutloop:
for(Layout l:allLayouts){
   LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet)l.getLayoutType();
   List portletIdList = layoutTypePortlet.getPortletIds();
            
   for(String portletId: portletIdList ){
      if(portletId.equalsIgnoreCase(*your portletId*)){
          targetLayout = l;
          break layoutloop;
      }
   }
}
        
LiferayPortletURL targetUrl = 
PortletURLFactoryUtil.create(liferayPortletRequest, *your portletId*, targetLayout .getPlid(), PortletRequest.RENDER_PHASE);

Format file size

No Comments »

Liferay provides

com.liferay.portal.kernel.util.TextFormatter
, which can be used to format the size of a file given in bytes and thus make it human readable. The relevant method is given with two different signatures;
static String  formatStorageSize(double size, Locale locale)
           
static String  formatStorageSize(int size, Locale locale)