Finder Method – Service Builder in Liferay 7

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

20-1

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.

20-2

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

20-3

5. Now build the services again, this method will be added CustomerLocalServiceUtil.

20-4

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

20-5

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...

20-6

 

Happy Coding… 🙂

Rupal Chatterjee
Sr. Associate Projects, Cognizant Technology Solutions
https://www.cognizant.com
Connect me on Google, Facebook

 

 

Explaining service.xml – Service Builder in Liferay 7

In our last blog, we have discussed on a sample implementation on Liferay service builder(i.e. Service Builder in Liferay 7). In this particular blog we will discuss only about service.xml file that we created in my previous blog.

<?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>
</entity>
</service-builder>

Lets move line-by-line and discuss the details,

1. service-builder package-path=”com.rc.blog.service”

Liferay generates all classes in the provided package-path.

2. <author>rupal.chatterjee</author>

This is not a mandatory tag. This tag helps Liferay to generate proper Javadoc.

19-1

3. <namespace>rcblog</namespace>

This is a mandatory tag. This helps to create tables with a specific naming convention.We have another attribute, auto-namespace-tables=”true/false” in “service-builder” tag.

For example,If we have auto-namespace-tables=true set, then liferay will create the tables as “<namespace>_<entity name>”.

But, if we mention the “table” attribute in “entity” tag, then Liferay will ignore the namespace.

i.e. <entity name=”Customer” local-service=”true” table=”TBL_CUSTOMER”>

In this case, Liferay will create the table as “TBL_CUSTOMER” irrespective of the “auto-namespace-tables” property.

19-2

4. <entity name=”Customer” local-service=”true” table=”TBL_CUSTOMER”>

This is a mandatory tag.

i. name=”Customer”

This is a mandatory attribute. Liferay uses the entity name to Generate all java class. For Example – Customer, CustomerLocalServiceUtils etc.

In some case it is also used as the table name as described in the previous point.

ii. local-service=”true”

If we set the values as true, Liferay allows us to use the services as web-services.

iii. table=”TBL_CUSTOMER”

As described in point no. 3, this is not a mandatory tag. Liferay uses the table tag to create the table in database using that name.

5. <column name=”CustomerId” type=”long” primary=”true” id-type=”increment”></column>

This tag defines each column of the table.

i. name=”CustomerId”

This is the name of the column in the database.

ii. type=”long”

This is the datatype of the coumn.

iii. primary=”true”

This specify that the given column is a Primary key.

iv. id-type=”increment”

This specify that the given column is an auto-generated type.

19-3

 

Happy Coding… 🙂
Rupal Chatterjee
Sr. Associate Projects, Cognizant Technology Solutions
https://www.cognizant.com
Connect me on Google, Facebook

 

Service Builder in Liferay 7

Liferay provides a mechanism to simplify DB connectivity which is called Service Builder. As the name suggest, Liferay helps the developer by creating all CRUD (CREATE-READ-UPDATE-DELETE) functions and classes. The developer just need to provide the table structure in a xml file and rest is taken care by Liferay.

In this example we will go through each phase step by step,

1. We will use the same liferay project that we created while demonstrating the logger example. i.e. Using Logger in Liferay 7

18-1

2. Right click on the “RC-Blog-Logger-Example-portlet”, then click on “New à Liferay Service Builder”.

18-2

3. Fill the details and click “Finish”. This will create a service.xml file inside WEB-INF folder.

18-3

18-4

4. Open the service.xml file in “Overview” mode. Remove the auto generated entity “Foo” and add a new Entity “Customer”.

18-5

5. Then click on “Customer” entity and provide the table name “TBL_CUSTOMER”.

18-6

6. Now click on “Columns” and define the required columns.

18-7

7. Now as a normal practice we want the primary key to be auto-generated. Select “CustomerId” and change the “Id Type” to “increment”.

18-8

8. Now we are done with the table structure creation. The service.xml now looks like this,

18-9

9. Now open the “Service.xml” file in “Overview” mode and click on “Build Services”.

18-10

10. On successful service build, all service related class will be generated inside “src”.

18-11

11. Add the below code snippet in “doView” method,

_log.info("START... Process Customer...");
try {
long cid = CounterLocalServiceUtil.increment(Customer.class.getName());
Customer customer = new CustomerImpl();
customer.setCustomerId(cid);
customer.setName("Rupal Chatterjee");
customer.setPhone("111-11111");
customer.setAge(28);
customer.setEmail("a@b.com");
CustomerLocalServiceUtil.addCustomer(customer);
} catch (SystemException e) {
_log.error(e);
}
_log.info("DONE... Process Customer...");

18-12

12. Build and Deploy the code.

13. Once the portlet is executed, go ahead and check the liferay database. A new table TBL_CUSTOMER should be added along with the sample data we created.

18-1318-14

Happy Coding… 🙂
Rupal Chatterjee
Associate Projects, Cognizant Technology Solutions
https://www.cognizant.com
Connect me on Google, Facebook