Developing Liferay Application With Service Builder

79
Developing Liferay application with Service Builder | 1 Developing Liferay application with Service Builder By Pankajsharma contact

description

Liferay Service Builder Cookbook

Transcript of Developing Liferay Application With Service Builder

  • Developing Liferay application with Service Builder | 1

    Developing Liferay

    application with

    Service Builder

    By Pankajsharma contact

  • Developing Liferay application with Service Builder | 2

    Table of Content:

    I. Create Spring MVC Portlet and Service Builder. II. Create Liferay-6.1.1 (Rest-Json) Web Services. III. Calling Portlet as a Web Service using SOAP API. IV. Custom-SQL Query in Liferay-6.1.1.ga2

  • Developing Liferay application with Service Builder | 3

    Create Spring MVC Portlet and Service Builder

    1. Create new Liferay Spring MVC Click on File Menu | New | Liferay Project.

    2. Given the Project Portlet name is SampleSpring-portlet this Portlet name specified into

    screenshot given below.

  • Developing Liferay application with Service Builder | 4

    3. This step click on next then Finish Button.

  • Developing Liferay application with Service Builder | 5

    4. Now we need to create Service.xml file, Right click on WEB-INF folder, New | Liferay

    Service Builder.

  • Developing Liferay application with Service Builder | 6

    5. Now we have created Service.xm file, In This file we have specified to various kind of

    fields. And we have to take primary Key :employeeId,Entity name: Employee, And now we have taken different kind of fields as:

    And we have specified ascending Order during the insert employee details into Database table,

    ascending order is display by employee First name Latter.

  • Developing Liferay application with Service Builder | 7

    And In this file specified Package: com.attuneww, auther name and namespace.

    Service.xml:

    PankajSharma

    AttuneWW

    PankajSharma

    AttuneWW

  • Developing Liferay application with Service Builder | 8

    6. Now we need to build service builder, Right Click on Project portlet |Liferay |Build

    Services. After then generate various types of services. We can see that all services

    hierarchy into snippet shot given below.

  • Developing Liferay application with Service Builder | 9

    7. Now create Controller Action Class is EmployeeController,This Class is directory

    :/SampleSpring-portlet/docroot/WEB-

    INF/src/com/attuneww/controller/EmployeeController.java.

    8. Now create to sample-spring-mvc-portlet.xml file,this file directory is :SampleSpring-

    portlet/docroot/WEB-INF/sample-spring-mvc-portlet.xml and we have to specified of

    Action Controller Action Class: EmployeeController.

  • Developing Liferay application with Service Builder | 10

    sample-spring-mvc-portlet.xml:

  • Developing Liferay application with Service Builder | 11

    9. Create to portlet.xml file and directory : SampleSpring-portlet/docroot/WEB-

    INF/portlet.xml, In this file we need to replace Portlet- class tag by

    org.springframework.web.portlet.DispatcherPortlet

    From it is by default generate tag.

  • Developing Liferay application with Service Builder | 12

    portlet.xml :

  • Developing Liferay application with Service Builder | 13

    SampleSpring

    SampleSpring

    org.springframework.web.portlet.DispatcherPortlet

    name>contextConfigLocation

    /WEB-INF/sample-spring-mvc-portlet.xml

    view-jsp

    /view.jsp

    0

    text/html

    SampleSpring

    SampleSpring

    SampleSpring

    administrator

    guest

    power-user

  • Developing Liferay application with Service Builder | 14

    10. Now we have need to Create web.xml file,it is directory SampleSpring-

    user

  • Developing Liferay application with Service Builder | 15

    portlet/docroot/WEB-INF/web.xml ,and we need to add some extra tags are given

    below:

    web.xml:

    SampleSpring-portlet

    view-servlet

    org.springframework.web.servlet.ViewRendererServlet

    1

    view-servlet

    /WEB-INF/servlet/view

    http://java.sun.com/portlet_2_0

    /WEB-INF/tld/liferay-portlet.tld

    view-servlet

    org.springframework.web.servlet.ViewRendererServlet

    1

    view-servlet

    /WEB-INF/servlet/view

  • Developing Liferay application with Service Builder | 16

    11. Create of liferay-plugin-package.properties file , we use to add of different types of jars

    by this file. This file directory is: SampleSpring-portlet/docroot/WEB-INF/liferay-

    plugin-package.properties.These all jars are copy into this file.

    liferay-plugin-package.properties:

    http://liferay.com/tld/aui

    /WEB-INF/tld/aui.tld

  • Developing Liferay application with Service Builder | 17

    12. Now this file by default generate after Build Service, If you have got it some exception to

    related version build.number=8, then during deploy Liferay Portlet, while you can replace

    build number as like 8.

    13. Create of jsp page and it is given name: view.jsp ,it is directory : SampleSpring-

    portlet/docroot/WEB-INF/jsp/view.jsp.

    view.jsp:

    name=SampleSpring

    module-group-id=liferay

    module-incremental-version=1

    tags=

    short-description=

    change-log=

    page-url=http://www.liferay.com

    author=Liferay, Inc.

    licenses=LGPL

    liferay-versions=6.1.1

    portal-dependency-jars=\

    commons-beanutils.jar,\

    commons-collections.jar,\

    commons-fileupload.jar,\

    commons-io.jar,\

    commons-lang.jar,\

    jstl-api.jar,\

    jstl-impl.jar,\

    spring-aop.jar,\

    spring-asm.jar,\

    spring-beans.jar,\

    spring-context-support.jar,\

    spring-context.jar,\

    spring-core.jar,\

    spring-expression.jar,\

    spring-jdbc.jar,\

    spring-transaction.jar,\

    spring-web-portlet.jar,\

    spring-web-servlet.jar,\

    spring-web.jar

  • Developing Liferay application with Service Builder | 18

    Please Enter Employee Details

  • Developing Liferay application with Service Builder | 19

    8

    10

  • Developing Liferay application with Service Builder | 20

    14. In this code, we have need to get all Employee List, then we have taken if condition to

    check resourcePrimKey not equal null, then you can get all parameters with in help of

    resourcePrimKey.

    15. In this code we have specified various kinds of fields with in Input validation tag.

    Please Enter Employee Details

  • Developing Liferay application with Service Builder | 21

    16. Now in Jsp we have form when we submit form it will match Actionurl parameter in

    Controller class action parameters value actionOne, Mapping with action value. Then call

    @ActionMapping(params = "action=actionOne") actionOneMethod and call

    actionOneMethod(ActionRequest request, ActionResponse response) method.

    8

    10

  • Developing Liferay application with Service Builder | 22

    package com.attuneww.controller;

    import javax.portlet.ActionRequest;

    import javax.portlet.ActionResponse;

    import javax.portlet.RenderRequest;

    import javax.portlet.RenderResponse;

    import org.springframework.stereotype.Controller;

    import org.springframework.ui.Model;

    import org.springframework.web.bind.annotation.RequestMapping;

    import org.springframework.web.portlet.bind.annotation.ActionMapping;

    import org.springframework.web.portlet.bind.annotation.RenderMapping;

    import com.liferay.counter.service.CounterLocalServiceUtil;

    import com.liferay.portal.kernel.exception.PortalException;

    import com.liferay.portal.kernel.exception.SystemException;

    import com.liferay.portal.kernel.log.Log;

    import com.liferay.portal.kernel.log.LogFactoryUtil;

    import com.attuneww.controller.EmployeeController;

    import com.attuneww.model.Employee;

    import com.attuneww.model.impl.EmployeeImpl;

    import com.attuneww.service.EmployeeLocalServiceUtil;

    @Controller(value = "EmployeeController")

    @RequestMapping("VIEW")

    public class EmployeeController {

    private static Log log = LogFactoryUtil.getLog(EmployeeController.class);

    @RenderMapping

    public String handleRenderRequest(RenderRequest request,RenderResponse

    response,Model model){

    return "view";

    }@ActionMapping(params = "action=actionOne")

    public void actionOneMethod(ActionRequest request, ActionResponse response)

    throws SystemException, NumberFormatException, PortalException {

  • Developing Liferay application with Service Builder | 23

    So it will check whether this is coming for Add, Update or Delete and accordingly it will do add

    or Update or Delete Operation

    Then we have actionkey.jsp file which is as follows.

    String employeeId= request.getParameter("employeeId");

    String del=request.getParameter("resourcePrimKeyDelete");

    System.out.println("Test for delete update>>>"+del+"update"+employeeId);

    String firstName=request.getParameter("firstName");

    String lastName=request.getParameter("lastName");

    String email=request.getParameter("email");

    String mobileNo=request.getParameter("mobileNo");

    if(del==null){

    Employee emp=new EmployeeImpl();

    emp.setFirstName(firstName);

    emp.setLastName(lastName);

    emp.setEMail(email);

    emp.setMobileNo(mobileNo);

    if(employeeId.equals("")){

    emp.setEmployeeId(CounterLocalServiceUtil.increment());

    EmployeeLocalServiceUtil.addEmployee(emp);

    }else{

    emp.setEmployeeId(Long.valueOf(employeeId));

    EmployeeLocalServiceUtil.updateEmployee(emp);

    }}

    if(del!=null){

    EmployeeLocalServiceUtil.deleteEmployee(Long.valueOf(del));

    }}}

  • Developing Liferay application with Service Builder | 24

  • Developing Liferay application with Service Builder | 25

    17. This is Action Key files which will have Edit and Delete Action.

    18. When we click on Edit it will add resourcePrimKey as parameter and redirecting to

    view.jsp so in view when we get this it will go for update.

    if(request.getParameter("resourcePrimKey")!=null){

    update=request.getParameter("resourcePrimKey");

    employ=EmployeeLocalServiceUtil.getEmployee(Long.valueOf(request.getPa

    rameter("resourcePrimKey")));

    firstName=employ.getFirstName();

    lastName=employ.getLastName();

    email=employ.getEMail();

    mobileNo=employ.getMobileNo();

  • Developing Liferay application with Service Builder | 26

    19. For Delete again match with actionOne, Mapping with action value. Then call

    @ActionMapping(params = "action=actionOne") method.

    20. This way Portlet will work now.

    21. Now we have to import liferay Project Portlet with Liferay Portal. We can see crud

    operations as add, update and delete with in browser. And we have following snippet shot

    given below.

  • Developing Liferay application with Service Builder | 27

    Liferay-6.1.1 (REST-JSON) Web Services

    1. Create New Liferay Project JSONWebServices. Right Click on new |Liferay Project and

    Given name of Rest-json Portlet is: Employee-RestWebServices-portlet.

    2. Now click on next Select Liferay MVC then Finish.

  • Developing Liferay application with Service Builder | 28

    3. Now Right Click on WEB-INF folder Select New Liferey Service Builder, it is directory of

    service builder file: Employee-RestWebServices-portlet/docroot/WEB-INF/service.xml.

    4. Now we can see to appear a popup window and Insert Details and Click On Finish.

    5. In this file to create table column values were auto generated by Build Service within help

    eclipse Editor. Now write own table columns name replace/rewrite Table field in File.

    6. We have to specified entity name :EmployRestfull.and create an primary key is

    employeeId with id(long) and remote service:true.

    7. Now create an Employee attributes various kinds of fields given below.

    8. Now write finder Tag for Search Content. A field which you want to Search.

  • Developing Liferay application with Service Builder | 29

    Service.xml:

    pankaj

    JSONWebservices

  • Developing Liferay application with Service Builder | 30

    9. Now, right click on Service.xml file and Liferay -> Build Services.

    10. It will generate portlet service packages under docroot/WEB-INF/src folder.

  • Developing Liferay application with Service Builder | 31

    11. Now, right click on build.xml file |Liferay|SDK and run the build-service task.

    12. Now Implement services Edit in com.attuneww.service.impl.EmployRestfullServiceImpl.

    Here we are getting EmployRestfull Object using EmployRestfull Id (employeeId). This

    method returns EmployRestfull Object. In Liferay any Object automatically sterilizes and

    produces as JSON data.

    package com.attuneww.service.impl;

    package com.attuneww.service.impl;

    import com.attuneww.model.EmployRestfull;

    import com.attuneww.service.EmployRestfullLocalServiceUtil;

    import com.attuneww.service.base.EmployRestfullServiceBaseImpl;

    import com.liferay.portal.kernel.exception.PortalException;

    import com.liferay.portal.kernel.exception.SystemException;

    public class EmployRestfullServiceImpl extends

    EmployRestfullServiceBaseImpl {

    public EmployRestfull getEmployRestfull(long employeeId) throws

    PortalException,SystemException

    { return EmployRestfullLocalServiceUtil.getEmployRestfull(employeeId);

    }}

  • Developing Liferay application with Service Builder | 32

    13. Add the following and Entries to portlets web.xml file.

    Secure JSON Web Service Servlet Filter

    com.liferay.portal.kernel.servlet.PortalClassLoaderFilter

    filter-class

    com.liferay.portal.servlet.filters.secure.SecureFilter

    basic_auth

    true

    portal_property_prefix

    jsonws.servlet.

    Secure JSON Web Service Servlet Filter

    /api/jsonws/*

    JSON Web Service Servlet

  • Developing Liferay application with Service Builder | 33

    14. Now, again run the build-service task and make sure that build successful.

    15. Once this completed, start the server if not started previously.

    16. Then Run Employee-RestWebServices-portlet|compile and Employee-RestWebServices-

    portlet|deploy.

    17. Now make sure that build successful, and now open Liferay console at

    http://localhost:8080 in your browser.

    18. Login as [email protected] password is test.

    19. Now click on Add-|more-|sample , then type portlet name you can see to your portlet

    project inside sample folder.

    class>com.liferay.portal.kernel.servlet.PortalClassLoaderServlet

    servlet-class

    com.liferay.portal.jsonwebservice.JSONWebServiceServlet

    0

    JSON Web Service Servlet

    /api/jsonws/*

  • Developing Liferay application with Service Builder | 34

    20. Make sure that plugins appear, this may confirm that plugins deployed properly.

    21. Now open http://localhost:8080/api/jsonws this URL in browser. It will show JSON web

    services API page.

  • Developing Liferay application with Service Builder | 35

    22. Now to access your json web services enter the following url

    http://localhost:8080//api/jsonws

    portlet-context-name: portlet project name.

    http://localhost:8080/Employee-RestWebServices-portlet/api/jsonws

    After then we can see that is generate get method name is getEmployRestfull left corner side

    portlet.

  • Developing Liferay application with Service Builder | 36

    Note: service-method-name is generated from the services method name by converting its camel case to lower case and using dashes (-) to separate words. We can see that into given

    above snippet as get-employ-restfull.

    23. Now click on This Method as get-employ-restfull, we got it new form.

  • Developing Liferay application with Service Builder | 37

    24. It will show One text field Id, now enter value in Box (I previously added One value direct

    in database employeeId=1, name=pankaj, [email protected], mobileNo=7895461231,comapnayname=attuneww.)

  • Developing Liferay application with Service Builder | 38

    25. When we enter 1 in employeeid in textbox it will show result to json format in result tab.

  • Developing Liferay application with Service Builder | 39

    26. When we click on URL Example tab it will show URL to access this result. And any client

    can access result directly using this link.

    http://localhost:8080/Employee-RestWebServices-portlet/api/secure/jsonws/employrestfull/get-employ-restfull/employee-id/1?p_auth=M9j0rD6G

  • Developing Liferay application with Service Builder | 40

    27. Now, write this URL in Browser. You will get Query result in Page. And we can see that

    employee result in json format.

    Calling Portlet (Liferay-6.1.1ga2) as a Web Service using SOAP API

    1. Create New Liferay Project In eclipse SOAPWebServices.Right Click select |New|Liferay

    Project then we see that a one popup window and enter following details as portlet

    Project name.

  • Developing Liferay application with Service Builder | 41

    2. Click on Next and Select Liferay MVC.

  • Developing Liferay application with Service Builder | 42

    3. Click Finish and Project will appear in Project Explorer.

    4. Now Right Click on WEB-INF Folder and

    5. Select |New |Liferey Service Builder.A popup will show and Insert Details and Click on

    Finish.

  • Developing Liferay application with Service Builder | 43

    6. Service.xml file will be seen under WEB-INF folder. And It's directory this file

    SOAPWebServices-portlet/docroot/WEB-INF/service.xml. Now open file in Editor.

    7. Table column values were auto generated by eclipse. Now write own table column name

    replace/rewrite Table field in File.

    8. Create an Employee entity with id (long) and name (String),email(String),

    mobileno(String),companyname(String)) attributes.

    9. Now write finder Tag for Search Content. A field which you want to Search.

  • Developing Liferay application with Service Builder | 44

    Service.xml:

    attune

    attune

  • Developing Liferay application with Service Builder | 45

    10. Now, Right click on Service.xml file and New| Liferay |Build Services.

    11. It will generate portlet service packages under docroot/WEB-INF/src folder.

  • Developing Liferay application with Service Builder | 46

    12. Now, right click on build.xml file and run the New|Liferay|SDK| build-service task. And

    again need to WSDD,New|Liferay|Build WSDD.it's mandatory to build WSDD.

    13. Now Implement services Edit in com.attune.service.impl.EmployeeLocalServiceImpl Add

    implementation for CURD and finder method.

  • Developing Liferay application with Service Builder | 47

    EmployeeLocalServiceImpl.java

    14. Now, again run the build-service task and make sure that build successful.

    15. Similarly Edit com.attune.service.impl.EmployeeServiceImpl.

    package com.attune.service.impl;

    import java.util.List;

    import com.attune.NoSuchEmployeeException;

    import com.attune.model.Employee;

    import com.attune.service.base.EmployeeLocalServiceBaseImpl;

    import com.attune.service.persistence.EmployeeUtil;

    import com.liferay.counter.service.CounterLocalServiceUtil;

    import com.liferay.portal.kernel.exception.SystemException;

    public class EmployeeLocalServiceImpl extends EmployeeLocalServiceBaseImpl {

    public Employee create(Employee model) throws SystemException{

    long empid = CounterLocalServiceUtil.increment(Employee.class.getName());

    model.setEmpid(empid);

    Employee newModel = EmployeeUtil.create(model.getEmpid());

    newModel.setName(model.getName());

    newModel.setCompanyname(model.getCompanyname());

    newModel.setEMail(model.getCompanyname());

    newModel.setMobileNo(model.getMobileNo());

    EmployeeUtil.update(newModel,true);

    //EmployeeUtil.update(newModel);

    return newModel;

    }

    public Employee update(Employee model) throws NoSuchEmployeeException,

    SystemException

    {

    Employee newModel = EmployeeUtil.findByPrimaryKey(model.getEmpid());

    newModel.setName(model.getName());

    newModel.setCompanyname(model.getCompanyname());

    newModel.setEMail(model.getCompanyname());

    newModel.setMobileNo(model.getMobileNo());

    EmployeeUtil.update(newModel,true);

    return newModel;}

    public List findByEmplyeeName(String name) throws SystemException

    { return EmployeeUtil.findByEmplyeeName(name);

    }public Employee remove(long empid) throws NoSuchEmployeeException,

    SystemException

    {return EmployeeUtil.remove(empid);

    }}

  • Developing Liferay application with Service Builder | 48

    EmployeeServiceImpl.java

    16. Now, again run the build-service task and make sure that build successful.

    17. Once this completed, start the server if not started previously.

    18. Then Run SOAPWebServices|compile and SOAPWebServices|deploy.

    package com.attune.service.impl;

    import java.util.List;

    import com.attune.NoSuchEmployeeException;

    import com.attune.model.Employee;

    import com.attune.service.EmployeeLocalServiceUtil;

    import com.attune.service.base.EmployeeServiceBaseImpl;

    import com.liferay.portal.kernel.exception.SystemException;

    public class EmployeeServiceImpl extends EmployeeServiceBaseImpl {

    public Employee create(Employee model) throws SystemException{

    return EmployeeLocalServiceUtil.create(model);

    }public Employee update(Employee model) throws NoSuchEmployeeException,

    SystemException

    {return EmployeeLocalServiceUtil.update(model);

    }public List findByEmplyeeName(String name) throws SystemException

    {return EmployeeLocalServiceUtil.findByEmplyeeName(name);}

    public Employee remove(long empid) throws NoSuchEmployeeException,

    SystemException

    {return EmployeeLocalServiceUtil.remove(empid);

    }}

  • Developing Liferay application with Service Builder | 49

    19. Now make sure that build successful, and now open Liferay console at

    http://localhost:8080 in your browser.

    20. Login as [email protected] password is test.

    21. Now click on Add->more|sample and we can see that your portlet under sample all portlet

    hierarchy, you already have added into sample by click on add.

  • Developing Liferay application with Service Builder | 50

    22. Make sure that plugins appear, this may confirm that plugins deployed properly.

    23. Now, open http://127.0.0.1:8080/WebServices-

  • Developing Liferay application with Service Builder | 51

    portlet/api/axis/Plugin_SOAPWS_EmployeeService?wsdl in browser. In this url

    WebServices-portlate is Portlate name, attune is Name Space which we were declare

    while we creating Service.xml, EmployeeServices is entityname of service.xml.

    24. Make sure that this url is open up in browser, because this completes plugin service

    creation. Then we can see xmlns:wsdl file.

    Create Web Service Client

    1. Now we need to test our webservice. For that we need to have another tomcat runtime as

    our Liferay tomcat 6 runtime eclipse plugin doesn't support building webservices. Also we

    need to make sure that the tomcat runtime and liferay run on different ports. I used the

    ports 8006, AJp-port: 8010 and http:8081 of my tomcat runtime.

    2. Create One dynamic web project.And make sure that you select normal tomcat instead of

    liferay tomcat while configuring the project. Right click select|New|other|Web|Next.We

  • Developing Liferay application with Service Builder | 52

    can see that and enter Dynamic Web project name.

  • Developing Liferay application with Service Builder | 53

  • Developing Liferay application with Service Builder | 54

    3. Now click on New Runtime. And configure Apache Webserver. Apache tomcat Server

    configuration before create Dynamic web project.

  • Developing Liferay application with Service Builder | 55

    4. Now we need to create us Service Client, Right click select|New|other|Web Services|Web

    Service Client|Next.

  • Developing Liferay application with Service Builder | 56

    5. After Next click, we can see that new popup window, we need to copy of Service URL

    as:http://localhost:8080/SOAPWebServices-

    portlet/api/axis/Plugin_SOAPWS_EmployeeService?wsdl and Make sure that scroll should

    be dragged to the top. You will find scroll on extreme left of the window, we have need to

    set server configuration, then click on server runtime:Tomcatv7.0Server,You will find

    scroll on extreme left of the window, we can see that all things given following snippet

    shot below and Click Finish.

  • Developing Liferay application with Service Builder | 57

    6. Click Next on this and next screen. Then click on "Start server" to start the tomcat

    runtime. Then click Finish.

  • Developing Liferay application with Service Builder | 58

  • Developing Liferay application with Service Builder | 59

  • Developing Liferay application with Service Builder | 60

    7. Eclipse will then deploy the client and open up a screen to test your webservice.

  • Developing Liferay application with Service Builder | 61

    8. We will test create method first. We will provide the

    primaryKey=0,name=Test,[email protected],mobileno=999999999,companyname=Tes

    t and id=1 and click invoke.

  • Developing Liferay application with Service Builder | 62

    9. In the results section we can see that the employee entity has been created with the

    id=1.

  • Developing Liferay application with Service Builder | 63

    10. Now invoke findByEmplyeeName and provide name=pankaj. The result will show the reference of the entity object returned by the webservice. (It will return the object not

    the exact value.

    11. We want add employee record into database, then we have used to created employee

    method by from servicesImpl class.

  • Developing Liferay application with Service Builder | 64

    12. We can see that which you have to already added employee record into your database

    table given following snippet shot below.

  • Developing Liferay application with Service Builder | 65

    Custom Query in Liferay 6.1.1 The following will explain how to work with custom-sql in liferay 6.1.1

    1. Create new Liferay MVC Click on File Menu | news |Liferay Project.

    2. Given the Project Portlet name is LiferayCustomQuery this Portlet name specified into

    screenshot given below.

  • Developing Liferay application with Service Builder | 66

    3. This step click on next then Finish button.

    4. N

    o

    w

    w

    e

    n

    e

    e

    d

    t

    o

    c

    r

    e

    a

    t

    e

    Service.xml file, Right click on WEB-INF folder,New | Liferay |Service Builder.

  • Developing Liferay application with Service Builder | 67

    5. In a service.xml (Under docroot/WEB-INF) add the following lines.

  • Developing Liferay application with Service Builder | 68

    pankaj

    LiferayCustom

  • Developing Liferay application with Service Builder | 69

    6. Build the service (you can use ant build-service or directly from IDE)

    7. We can that all service class hierarchy into Package Explorer.

    8. Create custom-sql folder under src folder

    9. Create default.xml file under custom-sql and add the following lines of code.

  • Developing Liferay application with Service Builder | 70

    10. Create book.xml file under the same custom-sql folder mentioned in step 4 and add the

    following lines of code.

    11. Create the file BooksFinderImpl.java under persistence (persistence will get after the first

    time build service) and add the following lines of code.

    (LiferayCustom_Books.title like ?)]]>

  • Developing Liferay application with Service Builder | 71

    package com.attune.service.persistence;

    import java.util.List;

    import com.attune.model.impl.BooksImpl;

    import com.liferay.portal.kernel.dao.orm.QueryPos;

    import com.liferay.portal.kernel.dao.orm.SQLQuery;

    import com.liferay.portal.kernel.dao.orm.Session;

    import com.liferay.portal.kernel.exception.SystemException;

    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

    import com.liferay.util.dao.orm.CustomSQLUtil;

    public class BooksFinderImpl extends BasePersistenceImpl implements BooksFinder {

    public List findBooks(String name) throws SystemException {

    Session session = null;

    try {

    session = openSession();

    String sql = CustomSQLUtil.get(FIND_BOOKS); SQLQuery query = session.createSQLQuery(sql);

    query.addEntity("Book", BooksImpl.class);

    QueryPos qPos = QueryPos.getInstance(query); qPos.add(name);

    return (List)query.list();

    }catch (Exception e) {

    }

    return null;

    }

    public static String FIND_BOOKS = "findBooks";

    }

  • Developing Liferay application with Service Builder | 72

    12. Add the following lines of code under BooksLocalServiceImpl.

    package com.attune.service.impl;

    import java.rmi.RemoteException;

    import java.util.List;

    import com.attune.model.Books;

    import com.attune.service.base.BooksLocalServiceBaseImpl;

    import com.attune.service.persistence.BooksFinderUtil;

    import com.liferay.portal.kernel.exception.PortalException;

    import com.liferay.portal.kernel.exception.SystemException;

    public class BooksLocalServiceImpl extends BooksLocalServiceBaseImpl {

    public List findBook(String name) throws PortalException,

    SystemException, RemoteException {

    return BooksFinderUtil.findBooks("%" + name + "%"); }

    }

  • Developing Liferay application with Service Builder | 73

    13. Again run "ant build-service" again passing the service.xml file as parameter. This will

    update the corresponding interface with the new method defined.

    14. Now go ahead and call BookLocalServiceImpl method from your jsp or java normally how

    you call other methods. For testing purpose add the following lines of code in Portlet

    under doView() method.

    15. Now we need to search form to create as View.jsp ,which is perform to operate to search

    container to use to find out value of book records.

    Here You can Search Of Products Record

  • Developing Liferay application with Service Builder | 74

  • Developing Liferay application with Service Builder | 75

    16. Now it will need to create Book Action classs ,this class we have to specified search

    method redirect on view jsp page then mapping with action URL,name variable value.

    we are enter bok name into search .

  • Developing Liferay application with Service Builder | 76

    package com.attune.action;

    import javax.portlet.ActionRequest;

    import javax.portlet.ActionResponse;

    import com.liferay.util.bridges.mvc.MVCPortlet;

    public class BookAction extends MVCPortlet {

    public void search(ActionRequest req,ActionResponse res) throws Exception

    {

    String str = req.getParameter("booknameLike");

    res.setRenderParameter("bookname",str );

    //Redirect search container to display result

    res.setRenderParameter("jspPage","/view.jsp");

    }

    }

    package com.attune.action;

    import javax.portlet.ActionRequest;

    import javax.portlet.ActionResponse;

    import com.liferay.util.bridges.mvc.MVCPortlet;

    public class BookAction extends MVCPortlet {

    public void search(ActionRequest req,ActionResponse res) throws Exception

    {

    String str = req.getParameter("booknameLike");

    res.setRenderParameter("bookname",str );

    //Redirect search container to display result

    res.setRenderParameter("jspPage","/view.jsp");

    }

    }

  • Developing Liferay application with Service Builder | 77

    17. In this step, we alreday have to insert some values manualy into database.

  • Developing Liferay application with Service Builder | 78

    18. No

    w

    ru

    n

    de

    plo

    y

    life

    ray

    pr

    otl

    et

    pr

    oje

    ct

    sel

    ect

    |N

    ew|

    Lferay|SDK|deploy,then we will add our liferay portlet project ,we are enter bok name into

    search text box,then we can see that book result display to given following snippet below.

  • Developing Liferay application with Service Builder | 79