Sunday 13 September 2015

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);

No comments:

Post a Comment