Thursday 28 November 2013

JobCron/Scheduler

Job cron or scheduler is a way or method by which we can execute a specific class with precise interval of time continuously. This concept is similar to the interrupt of microcontroller.

Procedure to achieve to JobCron :

Step1) Configure liferay-portlet.xml file by adding following code.
This can be achieve by using two different way

Method1:
<scheduler-entry>
    <scheduler-description>description</scheduler-description>
          <scheduler-event-listener-class>com.test.cron.CronJob</scheduler-event-listener-class>
             <trigger>
        <cron>
                <cron-trigger-value>0 0/1 * 1/1 * ? *</cron-trigger-value>               
                  </cron>
             </trigger>
</scheduler-entry>

com.test.cron.CronJob is complete path of class CronJob. So we can say that CronJob is a class created by the programmer which is present in package com.test.cron.
Value present in <cron-trigger-value>0 0/1 * 1/1 * ? *</cron-trigger-value> ,  means   “0 0/1 * 1/1 * ? * we get from http://www.cronmaker.com/  this site. Here we have to set the time and that site will gives this peculiar type of value. Our class executes continuously  after the given duration of time.



Method2:   

<scheduler-entry>
    <scheduler-description>description</scheduler-description>
          <scheduler-event-listener-class>com.test.cron.CronJob</scheduler-event-listener-class>
             <trigger>
        <simple>
                   <simple-trigger-value>1</simple-trigger-value>
                   <time-unit>minute</time-unit>
               </simple>
             </trigger>
</scheduler-entry>

    In this method we don’t have to worry about any site we just put the value inside the tag <simple-trigger-value>for number<simple-trigger-value/> and  <time-unit>unit time e.g. second, minute or hour</time-unit>  and our JobCron class will execute after each provided intervall of time.


CronJob.java

package com.test.cron;

import com.liferay.portal.kernel.messaging.Message;
import com.liferay.portal.kernel.messaging.MessageListener;
import com.liferay.portal.kernel.messaging.MessageListenerException;
import com.liferay.util.bridges.mvc.MVCPortlet;

public class CronJob extends MVCPortlet implements MessageListener
{
  
   @Override
   public void receive(Message arg0) throws MessageListenerException
   {
       // Inside this method whatever code we write will execute continuously with provided specific interval of time
       System.out.println("This output is printed because of job cron or scheduler");

   }
}

   receive method will call automatically means in this class receive method is entry point of execution. So even if we want to write other method inside this class we have to call those from receive method. Here receive method behaves as main function in Language C.

Wednesday 27 November 2013

Creation of pdf by using liferay portal

In this application I am creating a pdf by using data from table namely studentdetails
Description of table
table name: studentdetails
column name:
FatherName, Emailid, firstlanguage, lastschoolname, mothername, nationality, religion, secondlanguage, sex, stream, studentid, date of birth, sname
Please add
itext-1.1.4.jar
 file in your lib directory manually"

My action class

package com.test.pdf;
  3
  4import java.io.IOException;
  5
  6import javax.portlet.PortletException;
  7import javax.portlet.PortletSession;
  8import javax.portlet.ResourceRequest;
  9import javax.portlet.ResourceResponse;
 10
 11import pdf.ashraf.userdata.model.studentDetails;
 12import pdf.ashraf.userdata.service.studentDetailsLocalServiceUtil;
 13
 14
 15import com.lowagie.text.Anchor;
 16import com.lowagie.text.DocumentException;
 17import com.lowagie.text.Font;
 18import com.lowagie.text.FontFactory;
 19import com.lowagie.text.PageSize;
 20import com.lowagie.text.Paragraph;
 21import com.lowagie.text.Phrase;
 22import com.lowagie.text.Rectangle;
 23import com.lowagie.text.pdf.CMYKColor;
 24import com.lowagie.text.pdf.PdfPCell;
 25import com.lowagie.text.pdf.PdfPTable;
 26import com.lowagie.text.pdf.PdfWriter;
 27import java.io.ByteArrayOutputStream;
 28import java.io.File;
 29import java.io.FileOutputStream;
 30import java.io.FileWriter;
 31import java.io.IOException;
 32import java.io.OutputStream;
 33import java.io.PrintWriter;
 34import java.io.StringReader;
 35import java.util.ArrayList;
 36import java.util.Iterator;
 37import java.util.List;
 38
 39import com.liferay.portal.kernel.exception.PortalException;
 40import com.liferay.portal.kernel.exception.SystemException;
 41import com.liferay.portal.kernel.servlet.HttpHeaders;
 42import com.liferay.portal.kernel.util.Base64;
 43import com.liferay.portal.kernel.util.StringPool;
 44import com.liferay.util.bridges.mvc.MVCPortlet;
 45
 46
 47public class PdfAsfrafPortlet extends MVCPortlet
 48{
 49    public void serveResource(ResourceRequest request, ResourceResponse response)
 50        throws PortletException, IOException
 51            {
 52               
 53                try
 54                {
 55                    createPDF(request,response);
 56                }
 57                catch (SystemException e)
 58                {                   
 59                    e.printStackTrace();
 60                }
 61            }
 62   
 63public void createPDF(ResourceRequest resourceRequest, ResourceResponse resourceResponse)
 64    throws IOException, PortletException, SystemException
 65        {
 66        try
 67          {
 68           resourceRequest.setCharacterEncoding(StringPool.UTF8);                
 69           com.lowagie.text.Document document = new com.lowagie.text.Document();    //blank pdf created
 70           ByteArrayOutputStream baos = new ByteArrayOutputStream();
 71           PdfWriter.getInstance(document, baos);                        //predefine class PdfWriter calls static method getInstance
 72           document.open();                                                //open pdf in write method
 73           PdfPTable table= new PdfPTable(13);                            //creating a pdf table having single column hence PdfPTable table= new PdfPTable(1);
 74           table.setWidthPercentage(100);                                //setting width size                                            //to design cell
 75           table.addCell("FatherName");        //adding value in row. here number of rows is depends on the number of      table.addcell();
 76           table.addCell("Emailid");
 77           table.addCell("firstlanguage");
 78           table.addCell("lastschoolname");
 79           table.addCell("mothername");                                           //adding value in row
 80           table.addCell("nationality");
 81           table.addCell("religion");
 82           table.addCell("secondlanguage");
 83           table.addCell("gender");
 84           table.addCell("stream");
 85           table.addCell("studentid");
 86           table.addCell("date of birth");
 87           table.addCell("sname");
 88          
 89            List<studentDetails> tbl=null;
 90            tbl=studentDetailsLocalServiceUtil.findWholeTbl();       //to fetch whole table
 91            if(tbl!=null)
 92            {
 93            System.out.println("tbl size:  "+tbl.size());
 94            Iterator<studentDetails> it=tbl.iterator();
 95            while(it.hasNext())
 96            {
 97                studentDetails obj=it.next();
 98               
 99                table.addCell(obj.getFatherName());
100                table.addCell(obj.getEmailId());
101                table.addCell(obj.getFirstLanguage());
102                table.addCell(obj.getLastSchoolName());
103                table.addCell(obj.getMotherName());
104                table.addCell(obj.getNationality());
105                table.addCell(obj.getReligion());
106                table.addCell(obj.getSecondLanguage());
107                table.addCell(obj.getSex());
108                table.addCell(obj.getStream());
109                long l=obj.getStudentId();
110                String sl=String.valueOf(l);
111                table.addCell(sl);
112                String sd=String.valueOf(obj.getDateOfBirth());
113                table.addCell(sd);
114                table.addCell(obj.getSName());
115               
116               
117            }//while
118            }//if
119          
120           document.add(table);                                            //adding table to created pdf document
121           document.close();                                            //we have to first close the document
122           String fileName="attachment;filename=ashraf.pdf";            //filename
123           resourceResponse.setContentType("application/pdf");            //setting the content type either application or pdf(Portable Document Format)
124           resourceResponse.addProperty(HttpHeaders.CONTENT_DISPOSITION, fileName);    //
125           OutputStream out = resourceResponse.getPortletOutputStream();
126           byte[] downloadBytes = Base64.decode((String) resourceRequest.getAttribute("fileToDownloadBase64"));
127           out.write(downloadBytes);
128           baos.writeTo(out);
129           out.flush();
130           out.close();
131          }
132          catch (Exception e)
133          {
134           e.printStackTrace();
135          }
136         }//createPdf
137}//end of class

and my view.jsp 

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
 3
 4<portlet:defineObjects />
 5
 6This is the <b>Pdf Asfraf Portlet</b> portlet in View mode.
 7<portlet:resourceURL var="submitFormDetailsResourceURL" id="appform" escapeXml="false"/>
 8<br><br><br>
 9<form action="<%=submitFormDetailsResourceURL.toString() %>" method="post">
10<h2>This project working fine</h2>
11<p>Here I am retreiving data from table asif_studentDetails and then just publish that data in pdf format
12<input type="submit" value="Download" name="submitbutton">

Thanks and regards
asif aftab

Wednesday 13 November 2013

Liferay server is running but liferay home page is not found, Liferay page not found even tomcat server is running

This is a problem which I also faced when I was developed the servlet project manually that even each and every thing is working fine but whenever I tried to access the locathost page, an error is display either page is not found or fail to establish connection etc.
     Same problem I face here in liferay so lots of persons saying that just delete your server and then again configure it, but some time it is working and some time not, and I realize that most of the time this is not suitable solution. So the best solution is
      Create a new workspace folder and configure your liferay server and sdk plugins to that new workspace folder. In new workspace we have to just configure the server and you will easily find procedure that how to configure liferay server. No need to again installing liferay sdk plugins you just import your liferay sdk from old workspace directory.
And finally your liferay will work fine.
The way by which we can use liferay plugin sdk from any place is:
window->liferay->installed plugin sdks -> add -> browse plugin sdk folder -> ok.

Where window is the extreme second right tab in eclipse IDE

Thanks and regards 
Md Asif F Alam