Sunday 22 June 2014

Insert data into DB using Service Builder

       
Create a New Liferay project
Right click on the project àNew àLiferay Service Builder  àafter selecting this Enter package path(Ex:com.karthik),Namespace(Ex:B) and author(Ex:karthik)àInclude sample entity in new file(Enable this by default it is enable keep as it is)àFinish

service.xml
change your code with below code

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.1.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_1_0.dtd">
<service-builder package-path="com.karthik">
     <author>karthik</author>
     <namespace>B</namespace>

     <entity name="Employee" local-service="true" remote-service="false">

           <!-- PK fields -->

           <column name="empId" type="long" primary="true" />
           <column name="fname" type="String" />
           <column name="lname" type="String" />
           <column name="address" type="String" />
           <column name="email" type="String" />
     </entity>
</service-builder>


Select Overview(left corner) and click build services



After this create a portlet and jsp’s to insert the data and display output in browser

view.jsp

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui"%>

<portlet:defineObjects />
<portlet:actionURL var="employeeURL" name="insert"></portlet:actionURL>
<aui:form action="<%=employeeURL%>" method="post" name="insert" id="insert">
    <table>
        <tr>
            <td>First Name:</td>
            <td><input type="text" name="fname" /></td>
        </tr>
        <tr>
            <td>Last Name:</td>
            <td><input type="text" name=lname " /></td>
        </tr>
        <tr>
            <td>Address:</td>
            <td><input type="text" name="address" /></td>
        </tr>
        <tr>
            <td>Email:</td>
            <td><input type="text" name="email" /></td>
        </tr>
        <tr>
            <td colspan=2><aui:button type="submit" value="Submit"></aui:button></td>
        </tr>
    </table>
</aui:form>

EmpPortlet.java

package com.karthik;

import java.io.IOException;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;

import com.karthik.model.Employee;
import com.karthik.service.EmployeeLocalServiceUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;

/**
 * Portlet implementation class EmpPortlet
 */
public class EmpPortlet extends MVCPortlet
{
     public void insert(ActionRequest actionRequest,
                ActionResponse actionResponse) throws IOException, PortletException
           {
            try
                {
                 String fname = ParamUtil.getString(actionRequest,"fname");
                 String lname = ParamUtil.getString(actionRequest,"lname");
                 String address = ParamUtil.getString(actionRequest,"address");
                 String email = ParamUtil.getString(actionRequest,"email");
                 Employee employee=EmployeeLocalServiceUtil.insertEmployee(fname,lname,address,email);
                 long empId=employee.getEmpId();
                 
                 actionRequest.setAttribute("empId", empId);
                 actionRequest.setAttribute("fname", fname);
                 actionRequest.setAttribute("lname", lname);
                 actionRequest.setAttribute("address",address);
                 actionRequest.setAttribute("email",email);
          
                 actionResponse.setRenderParameter("jspPage",
                           "/jsp/emp/success.jsp");
               }
            catch (Exception e)
            {
                e.printStackTrace();
           }
     }
}

Change the below code in
com.karthik.service.implàEmployeeLocalServiceImpl.java

package com.karthik.service.impl;

import com.karthik.model.Employee;
import com.karthik.service.base.EmployeeLocalServiceBaseImpl;
import com.liferay.portal.kernel.exception.SystemException;

public class EmployeeLocalServiceImpl extends EmployeeLocalServiceBaseImpl
{
     public Employee insertEmployee(String fname, String lname,
                String address,String email)
     {
           try
                {
                     long empId = counterLocalService.increment(Employee.class
                           .getName());
                     Employee employee = employeePersistence.create(empId);
                     employee.setFname(fname);
                     employee.setLname(lname);
                     employee.setAddress(address);       
                     employee.setEmail(email);
                     return employeePersistence.update(employee,false);
                }
           catch (SystemException e) 
                {
                     e.printStackTrace();
                }
           return null;
     }
}

success.jsp


<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<portlet:defineObjects />
<%=renderRequest.getAttribute("fname") %></br>
<%=renderRequest.getAttribute("lname") %></br>
<%=renderRequest.getAttribute("address") %></br>
<%=renderRequest.getAttribute("email") %>

Note:If you get any compile errors build once again after adding the code in portlet and LocalServiceImpl






16 comments:

  1. hi i am getting an error of protlet not available when i click on submit
    can u help me out ?

    ReplyDelete
    Replies
    1. Hi saiyam,
      The above code is working fine. Build the services again and try. If it is not working post the error here(what you are getting in console).

      Delete
  2. The success.jsp is not working. it only shows "Your request completed successfully" after i click the add button.

    ReplyDelete
    Replies
    1. Hi,
      The above code is working fine.Please check your jsp folder name and path in the following code
      actionResponse.setRenderParameter("jspPage", "/jsp/emp/success.jsp");
      or change the above code with your jsp names.

      Delete
  3. hello, where did u put the method "insertEmployee" actually??? in the EmployeeLocalServiceUtil or in the EmployeeLocalServiceImpl?? how come in the "EmpPortlet.java" file when u called the class EmployeeLocalServiceUtil it can detect the method that was created in the EmployeeLocalServiceImpl??

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  4. hello, where did u put the method "insertEmployee" actually??? in the EmployeeLocalServiceUtil or in the EmployeeLocalServiceImpl?? how come in the "EmpPortlet.java" file when u called the class EmployeeLocalServiceUtil it can detect the method that was created in the EmployeeLocalServiceImpl??

    ReplyDelete
    Replies
    1. Hi Nazib,
      You are correct. It will communicate with EmployeeLocalServiceImpl.

      Delete
  5. if I want to click the button three: insert,update,delete form properties and attributes of the button will look like?

    ReplyDelete
    Replies
    1. The above code is for insert into database if you want to edit or delete you have to add some code

      Delete
  6. hi,

    are you sure all code above is working?
    1. where you define class Employee.java?
    2. How you connect Liferay Service Builder with Portlet JSP?
    3. where you put the database properties?
    4. where is sample of EmployeeLocalServiceBaseImpl and counterLocalService?

    arghhhhh....please guide us...

    ReplyDelete