Pages

Sunday 11 November 2012

Send an email from liferay portal

Create a Scheduler in Liferay

If you want to send an email from your liferay portal then its bit easy because liferay provide an Utility class called MailEngine.So we don't have to play with direct JavaMail API.

In this article, we’ll use Gmail as SMTP

Below are the steps to send an email in Liferay.
  •  Configure your SMTP provider, outgoing port, user id and password.
    • Login as super user in your liferay portal
    •   Go to Control Panel
    •  Click on Server Administration from server section
    • Click on Mail Tab
    • Fill out the SMTP fields here,
      1. Set Outgoing SMTP Server (E.g. smtp.gmail.com)
      2. Set Outgoing Port (E.g. 465)
      3. Check the box of Use a Secure Network Connection if you want.
      4. Set User Name and Password.
      5. Press “Save” Button 
      6. You are done with SMTP configuration of outgoing mail sever.

  • In your MVCPortlet or related class add the below peace of code to send an email.


MailMessage mailMessage = new MailMessage();
                   mailMessage.setHTMLFormat(true);
                   mailMessage.setBody("set body here");
                   mailMessage.setFrom(new InternetAddress("fromAddress","fromName"));
                   mailMessage.setSubject("set mail subject here");
                   mailMessage.setTo(new InternetAddress("set receiver email id here"));
                   mailEngine.send(mailMessage);
  • If you want to add attachment to your email or you want to add Cc you can use below send method of MailEngine class,

        send(InternetAddress from, InternetAddress[] to, InternetAddress[] cc, InternetAddress[] bcc, String subject, String body, boolean html format, InternetAddress[] replyTo, String messageId, String inReplyTo, File[] attachments);

I hope this tutorial will help you.. :)

Friday 9 November 2012

Generate .xsd Schema file from .xml file

Create a Scheduler in Liferay
An easy way to generate your .xsd file from .xml is with trang command line tool.

Below are the steps to generate .xsd file from .xml file.

  1.     Download tarng.jar file and save it at your local drive.  
  2.       Generate .xsd file using below command
  • java -jar <<your local machine path of trang.jar>>trang.jar addToCart.xml addToCart.xsd

E.g suppose your .xml file is as below then It will generate .xsd

addToCart.xml

<?xml version="1.0" encoding="UTF-8"?>
<addToCart> 
         <cartItem>   
             <cart_item_id><![CDATA[156]]></cart_item_id> 
             <cart_id><![CDATA[9]]></cart_id>
             <nid><![CDATA[167]]></nid>   
             <qty><![CDATA[1]]></qty>   
             <title><![CDATA[Born To Die (Album)]]></title>
             <price><![CDATA[99]]></price>
             <discountedPrice><![CDATA[89.1]]></discountedPrice>
         </cartItem>  
 </addToCart>

addToCart.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="addToCart">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="cartItem"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="cartItem">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="cart_item_id"/>
        <xs:element ref="cart_id"/>
        <xs:element ref="nid"/>
        <xs:element ref="qty"/>
        <xs:element ref="title"/>
        <xs:element ref="price"/>
        <xs:element ref="discountedPrice"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="cart_item_id" type="xs:integer"/>
  <xs:element name="cart_id" type="xs:integer"/>
  <xs:element name="nid" type="xs:integer"/>
  <xs:element name="qty" type="xs:integer"/>
  <xs:element name="title" type="xs:string"/>
  <xs:element name="price" type="xs:integer"/>
  <xs:element name="discountedPrice" type="xs:decimal"/>
</xs:schema>

 



Tuesday 6 November 2012

Liferay remove docbar for general user

Liferay remove docbar for general user If you want to remove dockbar for general users form liferay page.
Add the below code in your portal_normal.vm file.


#if ( $is_signed_in )
#set ($rService = $serviceLocator.findService(“com.liferay.portal.service.RoleService”))
#set ($usrRoles = $rService.getUserRoles( $user_id ))
#set ($hasPermission = false)
#foreach( $usrRole in $usrRoles )
#if ( $usrRole.getName() == “Administrator” || $usrRole.getName() == “Content-Admin” || $usrRole.getName() == “Content-Editor”)
#set ($hasPermission = true)
#end
#end
#end
#if($hasPermission == true)
#dockbar()
#end

Sunday 28 October 2012

Create a Scheduler in Liferay

Create a Scheduler in Liferay
In this small blog we will give an understanding of how can we write a Scheduler to run a cron job for performing a background job on the portal. 

Step 1 : Create a scheduler class in your portlet

import com.liferay.portal.kernel.messaging.Message;
import com.liferay.portal.kernel.messaging.MessageListener;
import com.liferay.portal.kernel.messaging.MessageListenerException;

public class NewsLetterScheduler implements MessageListener {

  public void receive(Message arg0) throws MessageListenerException {
       System.out.println("newsletter received!!!");
        /* Add your business logic here*/
  }
}

Step 2 : Specify the path in the liferay-portlet.xml.

The class name specified in the liferay-portlet.xml must match with scheduler class.

<portlet>
<portlet-name>newsletter-portlet</portlet-name>
<icon>/icon.png</icon>
<scheduler-entry>
    <scheduler-description>
       scheduler will run on every 15 minutes
    </scheduler-description>
    <scheduler-event-listener-class>
      com.sa.newsletter.trigger.NewsLetterScheduler</scheduler-event-listener-class>
 
   <trigger>
       <simple>
          <simple-trigger-value>15</simple-trigger-value>
          <time-unit>minute</time-unit>
       </simple>
  
        <!--<cron>
            <cron-trigger-value>0 0 0 * * ?</cron-trigger-value>
        </cron>
-->

  </trigger>
</scheduler-entry>
</portlet>

This (<cron-trigger-value>0 0 0 * * ?</cron-trigger-value>) indicates that the scheduler will run every night at 00 hrs.

<cron>
    <cron-trigger-value>0 0 0 * * ?</cron-trigger-value>
</cron>



I Hope this small blog helps you a lot!!!

Sunday 14 October 2012

Add a portlet to the Liferay Control Panel

Add a portlet to the Liferay Control Panel
If you want your portlet to be available in the liferay control panel, just add the following line in your liferay-portlet.xml:


<control-panel-entry-category>content</control-panel-entry-category>
<control-panel-entry-weight>15</control-panel-entry-weight>



  • control-panel-entry-category: The category where your plugin portlet will appear.
    • There are total 4 values you can use as per your requirements for category element.
      1. my
      2. content
      3. portal
      4. server
  • control-panel-entry-category: Determine the relative position in your relevant category portlet.The higher the number, the lower in the list your portlet will appear within that category. 
  • control-panel-entry-class: This is optional and is used to define rights (Who can see the portlet in the control panel). 


Make sure your portlet is not instanciable if you want your portlet in control panel.

add below line to your liferay-portlet.xml (for make portlet not instanciable).

<instanciable>false</instanciable>

I hope it helps you guys.... :)


Liferay Configuration in windows with Tomcat

Liferay Configuration in windows with Tomcat

Solution Analysts


In this brief tutorial, we will discuss how to setup Liferay with Apache tomcat Web Server.


Requirements before getting started #

  • JAVA (JDK & JRE)
  • Eclipse IDE
  • Liferay Tomcat bundle (Community Edition or Enterprise Edition)
  • Liferay Plug-in SDK
  • Apache Ant (with ecj.jar if not having)


Step -1: Download & Installation

1) To setup machine for Liferay development first of all download JDK from below link and install.

2) Download eclipse from below link.

3) Download Liferay plug-in SDK and Tomcat bundle from below link.

4) Download Apache ant version 1.8.1 from below link.

That’s all about downloading and installation bundles for development.


Step -2: Setting UP Environment Variables

Set JAVA_HOME:



Set LIFERAY_HOME:



Set ANT_HOME:




Step - 3:  Eclipse Configuration for Liferay Development

 First open eclipse and follow the instruction below,
1) Go to Help menu --> Eclipse Marketplace
2) Find here “Liferay” and just install Liferay IDE.



Install Liferay IDE and restart eclipse.


Step - 4:  Liferay Plug-ins SDK Setup

1) Open eclipse with Liferay IDE installed.
2) Open Preference page for Liferay (Go to window ->preferences -> Liferay -> Installed plugin SDK).



3) Here press the add button.
4) Browse to the location of your Plug-ins SDK installation.
5) Select OK and you should see your SDK in the list of installed SDKs.
6) Here, at the bottom of dialog screen, you will have option to update build. <username>.properties file. Select here prompt.

Congratulations! Your system is ready with development environment for Liferay.


Note: multiple SDKs can be added to the preferences but you will need to select at least one SDK to be the default.

Step – 5: Liferay Portal: Apache Tomcat Runtime/Server Setup #

          1) From eclipse go to Window àPreferences à Server àRuntime environments.


        
        2) Now, Click on Add to add a new Liferay runtime and find Liferay V6.1 tomcat under the     Liferay, Inc. and press next button.
           
            

      3) Now, set the Liferay tomcat directory here, also set the runtime JRE (Liferay v6.1 CE(tomcat 7) JRE) and press next button.


          
          4) Set here Liferay tomcat bundle zip file. And press Finish.




Step – 6: Set build.username.properties file

Set here tomcat directory path if not configured properly.

app.server.type=tomcat
          app.server.dir=${project.dir}/../bundles/tomcat-7.0.23
          app.server.deploy.dir=${app.server.dir}/webapps
          app.server.lib.global.dir=${app.server.dir}/lib/ext
          app.server.portal.dir=${app.server.dir}/webapps/ROOT

Step – 7: Create new Liferay Plug-in Project

1)   Now, a SDK and portal server have been configured. You can create a new Liferay plug-in project.

Go to file àNew à Liferay project.

2)     In the Liferay Plug-in project page, First add the project name and display name. after that select the SDK and Liferay runtime and then select the plug-in  type (Portal is default) and you can create a new plug-in project by clicking finish.





3)     If it worked fine than you should see a new plug-in project in the package explorer, so you are to begin Plug-in development.



Step – 8: Deploy Portlet

1)   Right click on portlet plug-in project àselect LiferayàSDKàdeploy


2) If deployment is successful than you got the message in console"BUILD SUCCESSFUL"


Step – 8: Start Apache tomcat server

1)    Run startup.bat from your Liferay tomcat bundle bin directory.
     
    wait till server start up.


    


3)    if server start successful, browser should open to the portal home at http://localhost:8080






Saturday 13 October 2012

How to fetch Liferay's entity using DynamicQuery or Finder class

How to fetch Liferay's entity using DynamicQuery or Finder class
How to fetch Liferay's entity using DynamicQuery?


I was using the DynamicQuery but getting the error "Unable" to find model class.

was trying to find the issue and I got the solution for the same.

I am sharing the solution with you Guys,

To allow your DynamicQuery to access the implementation class:

Below are the three different scenario to access the implementation class.

1) If your implementation class is in your portlet.

Ans: You don't need to use special classloader. It's will work out of the box. 

2) If the implementation class is the part of the portal .

Ans: Use the below line:
       
Classloader loader = PortalClassLoaderUtil.getClassLoader();

3) If the implementation class is in another portlet.

Ans: Use the below line:
        
ClassLoader classLoader = (ClassLoader)PortletBeanLocatorUtil.locate(ClpSerializer.SERVLET_CONTEXT_NAME,"portletClassLoader");

 Hope it helps! :)