In our last two blogs, we have discussed on a sample implementation on Liferay service builder(i.e. Service Builder in Liferay 7) and details service.xml file (i.e. Explain service.xml – Service Builder in Liferay 7)
In this blog, we will discuss on Finder method feature provided by Liferay service builder. Finder method is a feature of Liferay service that enables us to create fetch/retrieval methods according to our requirement.
1. We will use the same liferay project that we created while demonstrating the Service Builder example. i.e. Service Builder in Liferay 7
2. In the existing service.xml, we will create a Finder method by email.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 7.0.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_7_0_0.dtd"> <service-builder package-path="com.rc.blog.service"> <author>rupal.chatterjee</author> <namespace>rcblog</namespace> <entity name="Customer" local-service="true" table="TBL_CUSTOMER"> <column name="CustomerId" type="long" primary="true" id-type="increment"></column> <column name="Name" type="String"></column> <column name="Age" type="int"></column> <column name="Phone" type="String"></column> <column name="Email" type="String"></column> <finder name="EmailId" return-type="Collection"> <finder-column name="Email"></finder-column> </finder> </entity> </service-builder>
3. On successful service build, this will generate a method findByEmailId() in com.rc.blog.service.service.persistence.CustomerUtil class.
4. To use this method we have expose the finder method to our LocalServiceUtil class. Open “CustomerLocalServiceImpl” class and add the below code in it.
public List<Customer> findByEmailId(java.lang.String Email) { return getCustomerPersistence().findByEmailId(Email); }
5. Now build the services again, this method will be added CustomerLocalServiceUtil.
6. Add the below code in doView to get the list of all Customers with emailId a@b.com.
_log.info("START... Find Customer..."); List<Customer> customers = CustomerLocalServiceUtil.findByEmailId("a@b.com"); for(Customer customer : customers){ _log.info("Customer found : " + customer.toString()); } _log.info("DONE... Find Customer...");
7. Now let’s build and deploy the portlet.
OUTPUT –
[BlogLoggerPortlet:37] START... Process Customer... [BlogLoggerPortlet:50] DONE... Process Customer... [BlogLoggerPortlet:52] START... Find Customer... [BlogLoggerPortlet:55] Customer found : {CustomerId=1, Name=Rupal Chatterjee, Age=28, Phone=111-11111, Email=a@b.com} [BlogLoggerPortlet:57] DONE... Find Customer...
Happy Coding… 🙂
Rupal Chatterjee
Sr. Associate Projects, Cognizant Technology Solutions
https://www.cognizant.com
Connect me on Google, Facebook