Showing posts with label service builder. Show all posts
Showing posts with label service builder. Show all posts

Service Builder in Liferay


Service builder in Liferay provide a easy way to created services. We just need to provide the table structure in a xml file and all the CRUD methods generate automatically. For custom methods you may create Custom Sql  and Finder Methods. Today we create a basic services with just single table and see how to enter data in the table.

So lets start this step by step:-

Step 1:-Create Liferay Project
Inside eclipse IDE Click on File->New->Liferay plugin project and give the project name as service_demo and click finish.

Step 2:-Create MVC Portlet inside project
Right Click on your project ie service_demo and click on New than click on Liferay Portlet. Provide class and package name and select MVCPortlet and click finish.

Note:-You can check the snapshots in my  blog on Portlet Configuration Page in Liferay.

Step 3:-Create Service.xml
Right Click on your project ie service_demo and click on New than click on Liferay Service Builder. Provide the package path, namespaces and author



and Click Finish. This will create a service.xml inside WEB-INF.





Step 4:-Create Entity in Service.xml
Open Service.xml and Click overview and then click on entity.By using the "+" button you may enter as many entities as you want each entity represent one table in database.



Here we use Employee as name of the entity so all services is created with Employee as prefix. 

But we want the table name in database is employee_data .So Click on Employee on Left and fill Table as employee_data



So our table is created with name employee_data and our entity name is Employee.


Step 5:-Create columns in table
Click on columns on Left and by using the "+" button add columns



Here we check eid Primary checkbox so that eid become primary key for the table.

Now we want the primary key is auto incremented. So select eid from left, this will open property page for eid enter Id type to increment .



We have other options for Id type like sequence, identity, class but i am using Mysql so use increment.

Now all the columns in database will created with eid,name,phone ,age but i want that salary is created in database as EMP_SALARY so from Left click on salary and change Db name as:-



When you click on source you will see the service.xml as:-


We can directly create service.xml without using GUI. So you can directly copy paste service.xml and skip step 4 and 5.

Step 6:-Build Services
Click on overview in service.xml and click on Build service icon as:-



After build service is completed you can check all class are created in src folder.


Step 7:-Call the services 
Open your controller of the portlet ie java class and paste this:-



Explanation:-
Here in doView method we use CounterLocalServiceUtil to generate primary key ie eid because we take eid as autoincremented.

Step 8:-Check Output
Open your database you can see table as:-




Check data for that table




Project Structure








Hope this will Help....

You can Download Source code from  Service Builder in Liferay.

In the next blog Service Builder in Detail we will see each and every tag of service.xml in detail.

Related Post:-

Creating Portlet and Services using Maven in Liferay

Liferay Portlet And Services using  Maven 


Defining Maven in a single line is not possible .Maven do many things for us like resolving dependencies, create project structure, build  a project, creating war/jar etc. By using Maven you can also create portlet, theme, services without any need of sdk. Before starting this you have to install Maven and Ant in your machine.So it is highly recommended to read my previous blog  How to install Maven and How to install Ant.

Lets Start this step by step:-





Step 1:-Set up liferay-portal-maven
First download liferay-portal-maven from here according to your server version , in my case liferay-portal-maven-6.2.0-ce-rc5 and i am using liferay-portal-6.2.0-ce-rc5 server.

After downloading extract it to a folder and open it . Create a file build.username.properties inside this here username is your Computer name like in my case  build.Aditya.properties . Then paste this inside build.Aditya.properties:-

build.Aditya.properties

Then Open command prompt and go to that directory(H:\Liferay6.2 practice\liferay-portal-maven-6.2.0-ce-rc5) and run ant install. This will take sometime because it install many necessary file be sure you are connected to internet.All downloaded files goes to C:\Users\Aditya\.m2.

Note:- Step 1 is one time process next time you can directly create project.

Step 2:-Creating Project
Create a folder in which you want to create your project .Open command prompt go to your folder and run 
mvn archetype:generate

This will give you a huge list of archetype, filter it with liferay this will give you are liferay related archetype select liferay-servicebuilder-archetype ie number 10 and press enter.



After this it will ask for archetype version:-




select  version you can match the version from C:\Users\Aditya\.m2\repository\com\liferay\portal\portal-impl



in my case it is 6.2.0-RC5  ie select17 enter 17 press enter. After this you have to enter:-

  • groupId:- package name in which all your classes is generate.
  • artifactId:-Name of project by which war is created.
  • version:-leave blank
  • package:-leave blank

 press y and enter.



Now go to that folder your project is now created which contain 3 things:-
1)student-management-portlet
2)student-management-portlet-service
3)pom.xml

Step 3:-Provide entries of Deployment in pom.xml
Open pom.xml and provide entries of server after version tag now your pom.xml become:-

pom.xml

Line 11 to 19 <properties> tag provide the information of server and deploy directories.
After this open command prompt and  go to that folder where is pom.xml and run command:-
mvn clean install

Step 4:-Create service in Project
Now open this project in eclipse as :-
import->Maven->Existing Maven project 
And give the path till parent folder and click finish this will open 3 project in eclipse as:-

1)student-management:- Parent folder we run our project from here
2)student-management-portlet:-This is our main project all service impl classes are here .
3)student-management-portlet-service:- This contain only service classes but not impl classes.

Open service.xml from student-management-portlet inside web-inf and paste your services .For detail explaination of service.xml you can refer my blog Service builder in Detail

service.xml

Open command prompt go to folder where pom.xml of parent is and run command:-
mvn clean liferay:build-service
This command will generate all the service .Refresh both projects.



Step 5:-Create Portlet in Project
Now open student-management-portlet create a package inside src/main/java and inside that package create your controller class and paste this content:-

StudentController.java

Then create a folder html/student inside webapps and create your view.jsp .

view.jsp

then provide corresponding entries in of portlet class and init-param in  portlet.xml .

portlet.xml


After this provide entry in liferay-display.xml.

liferay-display.xml

Step 6:-Deploy your project
Open command prompt go to folder where your parent pom.xml and run command:-
mvn clean compile package liferay:deploy

This will deploy your project on server you can see your war file in target folder(..\student-management-portlet\target).

Thats it open your browser and hit localhost:8080 and just put your portlet on page and see tomcat console your service is call sucessfully.

You can download source code from Creatin portlet/Services with maven





Hope this will help....

 Related Post:-

Liferay Service Builder in Detail

How to install Maven

How to install Ant








Custom Sql With Two Table

Custom sql/sql query with two table 


As discussed in my previous article Custom Sql in Liferay , custom sql is used to write native sql query in liferay. Today we will discuss how to write custom query that belong to two or more table like join query. Before Reading this blog it is highly recommended to read my previous blog on  Service Builder in Detail and custom sql in liferay .

Lets Start this step by step:-

Step 1:-Create service.xml
You can create service as mention in my previous article Service Builder in Detail .

service.xml

And Build Service





Step 2:- Fill data in tables
After inserting data tables in data base look like this:-




For our example we like to fetch all the student details that belong to India ie our query is:-




Step 3:- Create our method
Open either any one from:-
a)StudentLocalServiceImpl  b)StudentAddressLocalServiceImpl 
Because we have to fetch the data from both the tables so pick any one .

StudentLocalServiceImpl 
Explanation:- 

1)List<Object[]> getAllDetailByCountry(String name)
Here we create a method that take String argument and whose return type is List<Object[]> because output result is neither of Employee type and not of Address type it is a combination of both.

2)Session session = studentPersistence.openSession()
Here we create a Session object with studentPersistence

3)SQLQuery query = session.createSQLQuery(" ")
Here we create our query this is not the standard way normally we take query from xml file like i described in Custom sql in Liferay but this approach is sometimes very handy when we have to save time.

4)QueryPos pos = QueryPos.getInstance(query)
Here we pass the query and fill the value of ? with pos like
Ex- pos.add(countryName);
and than finally return the list.

Again Build Service

So that this can be available in Util class.

Step 4:- Call method from util Class
After building services this method is available in StudentLocalServiceUtil use it like:-



You can download source code from Custom Sql with two table





Hope this will help....

 Related Post:-

Liferay Service Builder in Detail

Custom Sql in Liferay

Many To Many Relationship mapping in Liferay Services









Many To Many Relationship in Liferay Services

Today we will discuss Many To Many relationship in Liferay


As discuss earlier Liferay Service Builder create tables with service.xml. But is there any way to provide Many To Many relationship between entities. Yes there is today we will discuss  Many To Many relationship in liferay. Before Reading this blog it is highly recommended to read my previous blog on  Service Builder in Detail.

Lets Start this step by step:-

Step 1:-Create service.xml
You can create service as mention in my previous article Service Builder in Detail .

service.xml

Just focus on line no 11 and 17:-


Explanation:-
Here we create a third entity(table) Employee_department Which contain the primary key of both tables.





Step 2:-Check point
Now build the service.xml and open tables.sql inside sql folder.  

tables.sql

As you can see 3 tables are created two for entities and one for mapping.

Step 3:-Calling Services
Just create a doView() method and add data to the tables here we consider a scenario where one employee belong to two department.


Thats it just see the tables in database:-



You can Download source code from Many To Many mapping in Liferay





Hope this will help....

Related Post:-

Liferay Service Builder in Detail

Finder Method for Service Builder in Liferay

Custom Sql in Liferay

Custom Sql with two table in Liferay

Liferay Service Builder in Detail

How to install Maven

Creating Portlet/Services using Maven









Custom Sql in Liferay

Today we will discuss Custom Sql/Sql query in Liferay


Liferay Service Builder create basic CRUD method but there are some scenarios when we have to write SQL query. For writing native SQL query we use the concept of Custom Query in Liferay. Before Reading this blog it is highly recommended to read my previous blog on  Service Builder in Detail.


Lets Start this step by step:-

Step 1:-Create service.xml
You can create service as mention in my previous article Service Builder in Detail .

service.xml




Step 2:-Create xml files for sql Query

Now create a folder custom-sql inside src and create default.xml in it.

default.xml(/WEB-INF/src/custom-sql/default.xml)

here we can write our sql query but for good maintanability we create seperate file student-custom-sql.xml and include in default.xml.

student-custom-sql.xml

  • Here we use sql id this is unique for each query.This id is used to fetch the query in method.
  • No need to write semicolon(;).
  • You can create one xml file for one entity and then include in default.xml.

Step 3:-Create xxxFinderImpl Class
Now create xxxFinderImpl class inside persistence in our case create StudentFinderImpl that extends BasePersistenceImpl and implements StudentFinder.

StudentFinderImpl.java

Initally it shows error because there is no StudentFinder interface.

Run Service builder

After this error is gone and StudentFinder interface is created which is blank.


StudentFinder.java
One more class StudentFinderUtil is also created .

StudentFinderUtil.java

Step 4:-Create method in xxxFinderImpl Class
Create method in this class and provide implementation.This is the main task where we fire query and get the result.

StudentFinderImpl.java

Explanation:-

1)CustomSQLUtil.get("studentBetweenQuery");
Here we fetch the sql query from xml file with the help of sql id. For different queries different id and method in this class.

2)queryObject.addEntity("Student", StudentImpl.class);
Here we add our entity to queryObject. If sql query belong to multiple tables than we have to add all the entities to query object.

3)return (List<Student>) queryObject.list();
Here we return List<Student> because our query return all records belong to Student table but if our query contain two or more table like in case of join then we return List<Object[]>.

Step 5:-Expose method to LocalServiceUtil Class
We can't directly call method from StudentFinderImpl or StudentFinderUtil . All method are call from xxxLocalServiceUtil
Class so first we create method in xxxLocalServiceImpl 

StudentLocalServiceImpl.java

Here this show error because StudentFinderUtil has not contain getStudentBetweenStudentId(start, end).

Run Service builder

Now error is gone coz this will create method in StudentFinder interface and StudentFinderUtil  Class also.Now StudentFinder and StudentFinderUtil become:-

StudentFinder.java

StudentFinderUtil.java

Now Both Contain  getStudentBetweenStudentId(int start, int end).

Note:-
Normally we first create method in interface then provide implementation in Impl class but in Liferay we provide implementation in xxxLocalServiceImpl first and Run service builder which create method in Inteface.

Step 6:-Call method from LocalServiceUtil Class
Now from your doView() or from jsp call your method as:-


You can download source code from Custom Sql in Liferay





Hope this will help....

Related Post:-

Liferay Service Builder in Detail

Custom Sql with two table in Liferay

Finder Method for Service Builder in Liferay

Many To Many Relationship mapping in Liferay Services

Liferay Service Builder in Detail

How to install Maven

Creating Portlet/Services using Maven







Finder method for Service Builder in Liferay

Today we will Discuss Finder method for Service Builder in Liferay


In my Previous article Service Builder in Detail we see that liferay created basic CRUD method automatically but what if we want to add our custom method in Liferay services . There are many ways but creating Finder method is the simplest one.


Lets Start this step by step:-

Step 1:-Create service.xml
You can create service as mention in my previous article Service Builder in Detail .Today we add finder method in my Services:-

Service.xml


Just Focus on these lines:-

Explanation:-

1) <finder name="Mobile" return-type="Collection">
a) name="Mobile"
This is the name by which method is Created. Our method is Created by name 
findByMobile(). findBy is automatically prepended by liferay.

Note=> The name must be Capital otherwise method is created as findBymobile() 
which is not follow java camel case convention.

b) return-type="Collection"
return type of our method Collection represent List<T> in this case List<Student>.


2)<finder-column name="mobileno"></finder-column>
Column-name represent on which column you want to apply conditions.

After doing this just build your service and refresh your project.





Step 2:-Check Point

At this point liferay create our method in two places:-

1)StudentPersistence (Interface)
(under web-inf/services/com/aditya/services/persistence)

This interface contain many overloaded method of same name like:-
Their are other method also like removeByMobile, countByMobile etc.

2)StudentPersistenceImpl (Class)
(under web-inf/src/com/aditya/services/persistence)
This contain the implementation of StudentPersistence interface.

Step 3:-Expose method to Util Classes

Till now our method is not available to LocalServiceUtil Class.We have to expose it.Open StudentLocalServiceImpl Class. This class is just blank provide implementation of your method here so that it can be available by Util Class.


As you see in implementation we just call the method from interface thats it.
Now again build services and that it .Your method is now available to StudentLocalServiceUtil Class.

You can download source code from Finder method for service builder in lifeary





Hope this will help....

Related Post:-

Liferay Service Builder in Detail

Custom Sql in Liferay

Custom Sql with two table in Liferay

Many To Many Relationship mapping in Liferay Services

Liferay Service Builder in Detail

How to install Maven

Creating Portlet/Services using Maven








Liferay Service Builder in Detail

Today we will see concept of Service Builder in liferay


Before reading this blog it is highly recommended to read my blog on Service Builder in Liferay .Liferay Service Builder based on hibernate and Spring DI .Service builder create tables automatically as in Hibernate.Service builder generates Services for the entities in two location. These location use the package path describe in service.xml :-

1)docroot/WEB-INF/services/package name
2)docroot/WEB-INF/src/package name

Service Builder divide the source in 2 layers:
1)An interface Layer
2)An implementation Layer

You ’ll never change anything in the interface layer manually.The implementation layer is generated in your src folder and is initially skeleton code that allows you to implement the functionality you need.

Service builder also create src folder inside META-INF folder .This folder contains all the Spring configuration files and Hibernate configuration.

Sample service.xml

<service-builder package-path="com.student.services" auto-namespace-tables="true">
    <author>monu</author>
    <namespace>Aditya</namespace>
    <entity name="Student" local-service="true" cache-enabled="false" table="student">
        <column primary="true" name="studentId" type="int" id-type="increment"></column>
        <column name="name" type="String"></column>
        <column name="lastname" type="String"></column>
        <column name="std" type="String"></column>
        <column name="address" type="String"></column>
        <column name="phone" type="String"></column>
        <column name="gender" type="int"></column>
    </entity>
</service-builder>





We will discuss each and every tag in detail

1)<service-builder package-path="com.student.services" auto-namespace-tables="true">

a)service-builder package-path

This is the path of package in which all the java file is generated

b)auto-namespace-tables

By default it is false if this is true then table is created in database with a prefix of namespace.
(But if there is no table tag we see it later) 

2) <author>monu</author>

This tag is not mandatory.


Service Builder also generates Javadoc, and the name you place in the Author tags winds up in the Javadoc as the author of the code.

You can skip this tag but if you mention all Java classes have author name as mention like in this case monu.

Ex -
/
 * @author monu
 * @see StudentModel
 * @see com.student.services.model.impl.StudentImpl
 * @see com.student.services.model.impl.StudentModelImpl
 * @generated
 */
public interface Student extends StudentModel, PersistedModel {

}

3) <namespace>Aditya</namespace>

This tag is mandatory .

By default, tables you define with Service Builder go in the Liferay database. To set them off from the rest of Liferay’s tables, you can prefix them with a namespace. This table, when created, will be called Aditya_Student in the database.

If auto-namespace-tables is true than this name is appended before table name.The entry of this namespace is also mention in database with a table name servicecomponent that contain various columns like build number ,build date.


Ex-



Note :-

a)When ever you want to change the structure of table like number of columns and changes not reflect then delete entry of namespace from this table and build service again.

b)Namespace must be some valid name .Ex- Aditya9 this number 9 will create a problem in build service.

4) <entity name="Student" local-service="true" cache-enabled="false" table="student">

a)entity name="Student"

This name is compulsory but need not to be same as table name.

All Service classes are made by this name like entityLocalServiceUtil etc.
Ex- StudentLocalServiceUtil

b)cache-enabled=""

By using cache-enabled  true data base data is cached.

c)table="student"

This tag is not Compulsory

If you skip this tag then table in database is created with same name as entity name. Otherwise table name in database is same as table tag

d)local-service="true"

By using local-service true we can use our services as web services like SOAP

5) <column primary="true" name="studentId" type="int" id-type="increment"></column>

a)primary="true"

Means this field is primary key for database.

b)name="studentId"

Column is created with studentId in database

c)type="int"

Field is of int type. We have many options like int,long,String etc.

d)id-type="increment"

This field is of autoincrement type. Other options are sequence,identity etc.


Some Cases:-

1)<service-builder package-path="com.student.services" auto-namespace-tables="true">
<namespace>Aditya</namespace>
    <entity name="Student" local-service="true" cache-enabled="false" table="StudentData">
        <column primary="true" name="id" type="int"></column>
        <column name="name" type="String"></column>
    </entity>
</service-builder>

Table name = StudentData

Reason:-Because we provide table tag so namespace has no significance here.

2)<service-builder package-path="com.student.services" auto-namespace-tables="true">
<namespace>Aditya</namespace>
    <entity name="Student" local-service="true" cache-enabled="false">
        <column primary="true" name="id" type="int"></column>
        <column name="name" type="String"></column>
    </entity>
</service-builder>

Table name = aditya_student (namespace_entityname)

Reason:-Because auto-namespace-table="true"

3)<service-builder package-path="com.student.services" auto-namespace-tables="false">
<namespace>Aditya</namespace>
    <entity name="Student" local-service="true" cache-enabled="false">
        <column primary="true" name="id" type="int"></column>
        <column name="name" type="String"></column>
    </entity>
</service-builder>

Table name = Student

Reason:-Because  auto-namespace-table="false" and no table tag so table is created with name same as entity name.

Complsory Fields

1)<namespace>Aditya</namespace> because entry in service component
2)entity name all java files created with this

Optional Fields

1)<author></author> 
2)table name="optional"

Classes Created by Service Builder

1)com.student.services.model.impl

a)StudentBaseImpl
b)StudentCacheModel
c)StudentImpl
d)StudentModelImpl

2)com.student.services.service.base

a)StudentLocalServiceBaseImpl
b)StudentLocalServiceClpInvoker
c)StudentServiceBaseImpl
d)StudentServiceClpInvoker

3)com.student.services.service.http

a)StudentServiceSoap

4)com.student.services.service.impl

a)StudentLocalServiceImpl
b)StudentServiceImpl

5)com.student.services.service.persistence

a)StudentPersistenceImpl





Thats it Hope this Help Someone

 Related Post:-

Service Builder in Liferay

Finder method for Service builder in liferay

Custom Sql/Query in Liferay

Custom Sql/Query with two table in Liferay

Many To Many Relationship mapping in Liferay Services