Wednesday, 1 August 2012

How to implement Simple Service Builder


Requirement : I want to insert Employee information into DB and fetch all the information and displaying into JSP.Here I am creating project as "Employee-Portlet".
Step 1 : Design your jsp with Emp Name, Emp Designation like
                <portlet:actionURL var="empActionURL" />
                <form action="<%=empActionURL.toString()%>" method="post">
                             Emp Name :<input type="text" name="empName" />
                             Emp Designation :<input type="text" name="empDesignation" />
                             <input type="submit" name="submit" value="Submit" />
                  </form>
Step 2 : create Service.xml under plugins/portlets/Employee-Portlet/docroot/WEB-INF  and provide entity information like
<!-- XML starts with pre-processor -->
<?xml version="1.0"?>                 
<!--docType declaration-->
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 5.2.0//EN" "[http://www.liferay.com/dtd/liferay-service-builder_5_2_0.dtd]">
<!-- Starts with ROOT Element-->
<service-builder package-path="com.liferay.book">
<!--namespace is mandatory, author is optional, with that same name space it will generate db query.-->
                <author>Satish Babu</author>
                <namespace>Book</namespace>
<!-- Entity where we can provide our model information with respective of Java and Table inforamtion with respective of DB-->   
<!-- table attribute is not mandatory. If you provide name attribute with the same name it will generate table also -->
<!-- local-service="true" means it will generate all the Services, Implementations, Model, Wrapper and Persistence classes -->
<!-- Which will be execute on top on our local JVM-->
<!-- remote-service="true" means it will generate all the services, implementations, model, wrapper, persistence and SOAP class-->
<!-- it will produce WSDL for you and with the help of Eclipse we can generate client stubs also by providing the WSDL-->
<!-- By default remote-service="true", if you dont want make the value as "false".-->
                <entity name="Book" local-service="true" remote-service="false">
                                <!-- column attribute will generate attribute for model class and create column for table-->
                                <!-- PK fields -->
                                <column name="bookId" type="long" primary="true" />
                                <!-- Other fields -->
                                <column name="name" type="String" />
                                <column name="designation" type="String" />
                </entity>
</service-builder>
Run ant build-service from the employee-portlet folder. This will generate the entity and utility classes.
Refresh the project once you execute build-service .
Customize the generated code by opening EmployeeLocalServiceImpl in com.liferay.book.service.impl and add:
public class EmployeeLocalServiceImpl extends EmployeeLocalServiceBaseImpl {
    public Employee addEmployee(String empName, String empDesignation) throws SystemException {
        Employee emp = EmployeeUtil.create(CounterLocalServiceUtil.increment());
        emp.setName(empName);
        emp.setDesignation(empDesination);
        return EmployeeUtil.update(emp, true);
    }
    public java.util.List<Employee> getAllEmployees() throws SystemException {
        return EmployeeUtil.findAll();
    }
Run ant build-service again. Now your custom method implementation will visible in service layer.
Ex : EmployeeLocalServiceUtil.addEmployee(name,manager);
Implementing Crud operation from your portlet action class (EmployeePortlet).
override the doView() like this :
public void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException {
        try {
           
                String empName = renderRequest.getParameter("empName");
                String empDesignation = renderRequest.getParameter("empDesignation");
                EmployeeLocalServiceUtil.addEmployee(empName,empDesignation);
        } catch(com.liferay.portal.SystemException se) {
                                // intentionally empty
        }
                PortletRequestDispatcher dispatcher = getPortletContext.getRequestDisplatcher(viewJSP);
                dispatcher.include(renderRequest,renderResponse);
Test the application
===============
Run ant deploy.
Once the portlet is registered, add it to your page and look in the tomcat output, you will see that each time you refresh the page it adds a book to your database.
Obviously this is just to get your feet wet, but it should give you a nice head start!
NOTE: If you get a BeanLocator exception, modify your web.xml to look like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
    <listener>
                                <listener-class>com.liferay.portal.kernel.spring.context.PortletContextLoaderListener</listener-class>
                </listener>
                <taglib>
                                <taglib-uri>http://java.sun.com/jstl/core_rt</taglib-uri>
                                <taglib-location>/WEB-INF/tld/c-rt.tld</taglib-location>
                </taglib>
</web-app>
and run ant deploy again.

4 comments:

  1. Hi babu,

    Nice job but whene i try it,i don't EmployeePortlet. I use service builder liferay 6.1.1 . any ideas why?

    ReplyDelete
  2. sorry i don't find EmployeePortlet. i have five package whene i build service.xml :1) model.impl 2)service.base 3) service.http 4)service.impl 5)service.persistence

    ReplyDelete
  3. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in Oracle Access Manager
    , kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor led training on in Oracle Access Manager We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us:
    Name : Arunkumar U
    Email : arun@maxmunus.com
    Skype id: training_maxmunus
    Contact No.-+91-9738507310
    Company Website –http://www.maxmunus.com


    ReplyDelete