Wednesday 21 March 2012

Using Service builder (very basic)- to create database and insert some values


1. Right click on your employee-portlet Project.
    Goto New->Liferay Service Builder


2. Give Package Path: and Namespace for your database.


3.Go to docroot-> WEB-INF->service.xml
<?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="generate.employee">
<author>priyanka.dhingra</author>
<namespace>EMPLOYEE</namespace>

<entity name="Employee" local-service="true" remote-service="false">


<!-- PK fields -->


<column name="empId" type="long" primary="true" />



<column name="fname" type="String" />

<column name="phone" type="long" />
<column name="lname" type="String" />
<column name="address" type="String" />
<column name="gender" type="int" />
<column name="email" type="String" />
<column name="dob" type="Date" />
</entity>
</service-builder>


4. Hope you have set ANT_HOME. Now, right click on the project->Goto Liferay->click Build Services

5. Refresh your project. You will see many packages in your src and a service folder plus a sql folder  wherein you can open tables.sql to see if there is a create table Query for your respective table.

6. Now, Go to your EmployeeAction.java class(in docroot/WEB-INF/src->generate.employee->EmployeeAction.java )

7. In the doView() method.Type:-
long id = CounterLocalServiceUtil.increment();
generate.employee.service.model Employee empObj = EmployeeLocalServiceUtil.createEmployee(id);
empObj.setFname("Sam");
bookObj.setLname("Barron");
//Set all the fields
EmployeeLocalServiceUtil.updateEmployee(empObj);

8. Deploy the portlet.
Check your database you will have your table with this record.

So, now you know what is service.xml and the package and classes it creates. GOOD GOING :)
Thanks.

2 comments:

  1. Thanks for helping out the community.

    But it can be improved a lot, like: you just explained how to create a simple service.xml and the classes it generates, but didn't explain what is service.xml as you say you do - "So, now you know what is service.xml"?

    Neither does this post explain any of the classes that are generated nor the tags which you have used.

    And by the way it is not at all a good practice to have update/insert code in your doView() method (apart from updating PortletPreferences), I understand this is a basic example but I feel good examples are those which also take care of the best practices right from start or atleast mentions that this is not a best practice.

    ReplyDelete
    Replies
    1. Thanks for listing out the points so that I can improve...I will try my best to take care of all the things, that you mentioned. :)

      Delete