Tuesday, 11 October 2011

Create Custom Attribute in Liferay

Hi here I'm sharing the way to create new custom Attribute in User tab of control Panel by coding in Liferay 5.2.

Code : 
            ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
            User userObj = UserLocalServiceUtil.getUser(themeDisplay.getUserId());
            ExpandoBridge expandoBridge                  =ExpandoBridgeFactoryUtil.getExpandoBridge(User.class.getName());

            if (!expandoBridge.hasAttribute("testCustomAttributeName"))
            {
                expandoBridge.addAttribute("testCustomAttributeName");
            }
 
It will check whether customAttribute name as testCustomAttributeName is already exist or not if it is not there then add in Custom Attibute.

If you want to add default value to that Custom Attribute than add following line at end of code.

            userObj.getExpandoBridge().setAttribute("testCustomAttributeName", "AARAV");

It will set testCustomAttributeName's value as AARAV for that user only .

Friday, 5 August 2011

Generate Report using iReport in Liferay

Step :1 )Create .jrxml file.

For creating PDF or EXCEL file , you need iReport that generates .jrxml file . 

I'm using iReport-3.7.5 for generating .jrxml file.


I had create two parameter name as "name" and "address" by clicking on Parameters in Report Inspector
     window (in left handside).                                
     After creating parameter just drag and drop where you want to put in report. You can also create static         Text("Name::" and "Address::") as I show above snapshot.

Step : 2 )  Create method to export in .xls file.

For below code you need to import jars in portlet's lib folder like iText-2.1.5.jar , iText-2.1.7.jar , jasperreports-3.7.5.jar , jasperreports-applet-3.7.5.jar , jasperreports-fonts-3.7.5.jar , jasperreports-javaflow-3.7.5.jar.

After creating report save it. Here i'm saving as reportDemo.jrxml under report folder of my docroot directory.

    private void makeExcelReport(ActionRequest actionRequest, ActionResponse actionResponse, User user)
            throws JRException, IOException
    {

               JasperReport report; 
               Map paramMap = new HashMap();
               report = JasperCompileManager.compileReport(getPortletContext().getRealPath("/reports/reportDemo.jrxml"));
               
        String nameFromRequest = user.getUserName();
        String addressFromRequest = userUserAddress();
        paramMap.put("name", nameFromRequest);
        paramMap.put("address", addressFromRequest );

        java.util.List list = new ArrayList();
        list.add(paramMap);
        String userFormFile = actionRequest.getPortletSession().getPortletContext().getRealPath("") + "/" +                                              
                                                                                    "UserForm_" + ".xls";
                                           // This is where we store .xls file, It is in temp folder where deployment is done.
        File fileName = new File(userFormFile);
        if (fileName.exists())
        {
            fileName.delete();
        }

        JRBeanCollectionDataSource dataSourceWebContent = new JRBeanCollectionDataSource(list);
        JasperPrint print = JasperFillManager.fillReport(report, paramMap, dataSourceWebContent);
        ByteArrayOutputStream outputByteArray = new ByteArrayOutputStream();
        OutputStream outputfile = new FileOutputStream(new File(userFormFile));

        JRXlsExporter exporter = new JRXlsExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);  // Optional
        exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, userFormFile);  // Optional
        exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, outputByteArray); // Optional
        exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); //
        exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
                               // Optional
        exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);                                                                                                              // Optional
        exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);                                                                                                // Optional
        exporter.exportReport();
        outputfile.write(outputByteArray.toByteArray());


Monday, 20 June 2011

use portal-src/portal-impl class from plugins.

Hello,
        We cannot call portal-impl class of portal-src from plugins . If you use class directly it is deploy successfully but at run-time it gives error that class not found. So here is solution for calling portal-impl class from plugins.

Steps:
1) first make class in portal-ext in any package that you want
 
    Here for example, I'm creating class name as intranetCommon.java in package com.liferay.portal

2) Copy Method (and/or modify method if you want) from portal-src's class that you want to use in plugin
     environment.

    i.e. I want to use PortalLDAPUtil.java's public static long getLdapServerId(long companyId, String
    screenName) method.
         Create method (any name you want) but having return type as Object of that primitive type.
    getLdapServerId(long companyId, String screenName) that is long . So return type of our method is Long.
      
            public static Long getServerId(Long companyId, String screenName) throws Exception  //parameters
                                                                                                           //also in Object of that primitives
   {
return PortalLDAPUtil.getLdapServerId(companyId, screenName); // here you can put entire        
                                                                                                                               // code of
                                                                                                                               //getLdapServerId
                                                                                                                               //and modify if you want.
   }


3) In plugin environment , Write PortalClassInvoker.invoke method.

i.e.     long  serverId=(long) PortalClassInvoker.invoke(
                     "com.liferay.portal.intranetCommon", "getServerId",  new Object[]{companyId, screenName}
                ,false); // call ext environment method using invoke method.

               1st parameter : class path along with className
               2nd parameter : method name
               3rd parameter : object arguments (here we have 2 parameter- companyId, screenName)
               4th parameter : newInstance (true/false)

               Thank You.