Monday, 29 July 2013

SERVICE BUILDER

SERVICE BUILDER

The Service Builder is a tool built by Liferay to automate the creation of interfaces and classes that are used by a given portal or portlet. This includes code for EJBs, Spring, Persistence, and Model.

{(Meaning of ‘Persistence’):
Any software layer that makes it easier for a program to persist its state is generically called a persistence layer. Most persistence layers will not achieve persistence directly but will use an underlying database management system.
}
The input to the Service Builder is an XML file, typically /ext/ext-ejb/service.xml. For a complete description of the service.xml syntax refer to the well documented DTD at http://www.liferay.com/dtd/liferay-service-builder_6_0_0.dtd

The service-builder element is the root of the deployment descriptor for a Service Builder descriptor that is used to generate services available to portlets. The Service Builder saves the developer time by generating Spring utilities, SOAP utilities, and Hibernate persistence classes to ease the development of services.

----
{
Finder methods are created automatically but these will return all the results. FilterFindBy method can also be generated under certain conditions and they will return the results filtered by permissions. The conditions are:
    the entity has a simple primitive pk
    the entity has permission checked registered in resource-actions/....xml
    the entity has user id an group id fields
    the finder method has group id in it
}


·     Liferay’s Service Builder is a code generator. It generates the code automatically for our own services.
·     Service Builder is a model-driven code generation tool built by Liferay to automate the creation of interfaces and classes for database persistence and a service layer. Service Builder will generate most of the common code needed to implement find, create, update, and delete operations on the database, allowing you to focus on the higher level aspects of service design.
·     The service layer generated by Service Builder, has an implementation class that is responsible to handle retrieving and storing data classes and adding the



necessary business logic around them. This layer can optionally be composed of two layers, the local service and the remote service. The local service contains the business logic and accesses the persistence layer. It can be invoked by client code running in the same Java Virtual Machine. The remote service usually ads a code to check security and is meant to be accessible from anywhere over the Internet or your local network. Service Builder automatically generates the code necessary to allow access to the remote services using SOAP, JSON and Java RMI.
·     Using an XML Descriptor, it generates,
o  SQL for creating tables
o  Java Beans
o  Hibernate Configuration
o  Spring Configuration
o  Axis Web Service
o  JSON Javascript Interface
·     Steps for creating Service Builder
1.  Prepare service.xml
2.  Run Build-service option in ANT script
3.  Open <xxx>impl and implement methods
(we do not write any signatures or modify the interface. Everything has to be done in the Implementation class)
4.  Run build-service one more time
5.  Write the business logic processAction/processEvent/serveResource/doView/doEdit
(Ex: studentLocalServiceUtil.<implemented methodnames>)

All the classes are generated by Service Builder and should not be modified, with the exception of <xxxx>LocalServiceImpl.java

Note: userID, groupID & companyID are mandatory for creating Event/Table in Service Builder.
Create Service Builder via ‘Eclipse Indigo’

1.  Create Project and generate a ‘portlet’.
2.  Right click on the project and click on new>Liferay Service Builder
(Give the package name and a ‘name space’. By default service.xml will be created)
Note: With the name space, a table will be created in the respective schema(for say entity name is ‘Product’ and name space you have given is ‘s’, the respective table in the Database will look like s_Product)
3.  Create Entity #
Inside service.xml, add:




<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.0.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_0_0.dtd">
<service-builder package-path="com.test.service.action">
    <author>Administrator</author>
    <namespace>s</namespace>

    <entity name="Product" local-service="true" remote-service="true">

        <!-- PK fields -->
        <column name="productId" type="long" primary="true" />

        <!-- Audit fields -->
        <column name="productName" type="String" />
        <column name="productQty" type="String" />

        <!-- Finder methods -->

        <finder name="pName" return-type="Collection">
            <finder-column name="productName" />
        </finder>
        <finder name="pQty" return-type="Collection">
            <finder-column name="productName" />
            <finder-column name="productQty" />
        </finder>
    </entity>
</service-builder>

Run ant build-service from the library-portlet folder. This will generate the entity and utility classes (once you refresh).

4.  Customize the generated code by opening ProductLocalServiceImpl in com.test.service.impl and add:

package com.test.service.action.service.impl;

import java.util.List;
import com.liferay.portal.kernel.exception.SystemException;
import com.test.service.action.model.Product;





import com.test.service.action.service.base.ProductLocalServiceBaseImpl;

 * <p>
 * Never reference this interface directly. Always use {@link com.test.service.action.service.ProductLocalServiceUtil} to access the product local service.
 * </p>
 *
 * <p>
 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 * </p>
 *
 * @author Administrator
 * @see com.test.service.action.service.base.ProductLocalServiceBaseImpl
 * @see com.test.service.action.service.ProductLocalServiceUtil
 */
public class ProductLocalServiceImpl extends ProductLocalServiceBaseImpl {
public List<Product> getProductName(String pName) throws SystemException{
List<Product> productList = productPersistence.findBypName(pName);
return productList;
}
}
Run ant build-service again.

5.  We have now generated the persistence layer. Let's look at the GUI now. Create class in docroot and the following code.

package com.check;

import java.io.IOException;
import java.util.List;

import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;




import com.liferay.portal.kernel.exception.SystemException;
import com.test.service.action.model.Product;
import com.test.service.action.service.ProductLocalServiceUtil;

public class ServiceBuilderTest extends GenericPortlet {

    @Override
    protected void doView(RenderRequest request, RenderResponse response)
            throws PortletException, IOException {
        System.out.println("DovIEW invoked");
        try {
            List<Product> productList = ProductLocalServiceUtil.getProductName("SOAP");
            System.out.println("Product List size"+productList.size());
        } catch (SystemException e) {
            System.out.println("Catch");
            e.printStackTrace();
        }
        
        PortletRequestDispatcher dispatcher = getPortletContext().getRequestDispatcher(getPortletConfig().getInitParameter("view-jsp"));
        dispatcher.include(request, response);

    }

}

6.  Deploy in ‘ANT’ and run the code.
7.  From WEB-INF>sql>tables.sql, copy the query and paste it in MySQL-Query Browser and execute.
8.  Now start the server.


No comments:

Post a Comment