Adding a Page ,Change Layout, Adding a Portlet on a page Programmatically(via Code) in Liferay.

Normally we add a page, change layout adding a portlet on a particular page by using drag and drop or by clicking our mouse.Today we will add a page,Change Layout from 2 to 3 and than finally add a portlet on newly created page Programmatically.


Introduction
For doing this we basically focus on two classes:-

1)LayoutLocalServiceUtil

  • Because page means layout in liferay thats why we use LayoutLocalServiceUtil
  • For using method of LayoutLocalServiceUtil we need userID , groupID so we also need ThemeDisplay Object.

2)LayoutTypePortlet
Object of this Class is used to programmatically add or remove a portlets from a page.This object can be get in jsp by <liferay-theme:defineObjects />

Ok Lets discuss step by Step



Step 1 :- Create Liferay project and Generic Portlet in it.

Create a liferay project then create a generic portlet in it . Copy this code inside your view.jsp

view.jsp


Explanation:-
Here we create just 3 action URL that hit 3 different methods on click.


Step 2 :- Adding a Page.

On Clicking Adding Page addPage method is called which is responsible for adding a page in Liferay.

Explanation:-
Here we use addLayout method of LayoutLocalServiceUtil Class which take parameters as:-

a)UserId:- Current User Id .
b)GroupId:- Group id from which user is belong.
c)Private Layout:- The page added as a public(false) page or private(true) page.
d)name:- Name of the page appears in menu.
e)title:-That is show on tabs of browser.
f)description:- description about the page.
g)type:- type of page .ex- Panel ,embedded etc.
i)hidden:- page is shown in menu bar or not.
j)friendlyUrl:- the friendly Url for the page.

After Clicking the link just Reload the page will appear in menu bar.

Step 3 :-Change Page Layout 

By default the page is added with 2 column .If you want to change from 2 column layout to 3 column layout click on update page Layout link. 
          
Explanation:-

>Here first we get the Layout(ie page) Object for which we have to change the layout by providing friendlyUrl and other values.
>Then we get the LayoutTypePortlet Object and then use setLayoutTempelateId method.
>Other values rather than "3_columns" can be found (\ROOT\layouttpl\custom)

Step 4:- Adding Portlet on Page

Now we have a page with 3 Column layout.Now we add Loan Calculator portlet on the page. Just Click the link Add Loan Calculator this will call the addPortlet method.

Explanation:-
>Here first we again get the Layout(ie page) Object on which we have to add the portlet by providing friendlyUrl and other values.
>Then we use the method addPortletId which take 4 parameters as:-
a)UserId:- Current User Id .
b)PortletId:- id of the portlet which is to be added on page.
c)ColoumnId:-In which column you want to add your portlet. Ex- column-1 hence portlet added at LHS if column-1 then portlet added at centre and if column-3 portlet added at RHS.
d)Column Position:- Consider if there is already any other portlet in particular column.
                 0>=:-Our portlet is added at the top of existing portlet.
                -1 :-Our portlet is added at the bottom of existing portlet.

>Finally we update the layout

you can download source code from Here






Hope this will help....

Related Post:-






Search Container in Liferay 6.2


Liferay Provide a simple and easy way to show list of Objects in proper tabular format with pagination by using Search Container tag of liferay.

Our goal is to show a list of Student in which:-

1)Column show student name,last name,Class etc.
2)In one column we show address of Student that comes from                    another jsp.

3)In one column we provide a link .When user click on that link it                will go to another jsp that show complete detail of Student.
4)Also we provide pagination among the list.


Step 1:- Create Liferay Plugin Portlet and Services

First we create a liferay plugin project than create a portlet in this project. Then create sevice.xml and build services. For details of services you can read my Previous Blog.
Project Structure look like :-






Step 2:-Then Copy the code in view.jsp


Explaination of view.jsp:-

a)String currentURL = PortalUtil.getCurrentURL(request); 

By using getCurrentURL we can get the URL of Current Page this url is helpful when we get back from detail page to our current list page.

b)<liferay-ui:search-container delta="5" deltaConfigurable="true">

   Here the search container tag start which contain many attribute like:- 

  delta:-  This attribute is used to set by default how many records per page is shown.
  deltaConfigurable:- This attribute is used so that end user can change no of records per page.

C)<liferay-ui:search-container-results results=" " total=""/>

    This result tag has two main attribute like:-

 results:- This attribute contain records with start index and end index . The start index and end index can be get with the help of searchContainer Object.The object of searchContainer is  available as soon as we include the import statement:-

            <%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui"%>

Ex-  StudentLocalServiceUtil.getStudents(searchContainer.getStart(), searchContainer.getEnd()) %>"

total:- This attribute contain total number of records.

d)<liferay-ui:search-container-row className=" " modelVar="" keyProperty=""> 

This search container row tag contain attribute:-

className:-This contain the full path of your model Class like Student in this case.
modelVar:- This contain the current object that is available in the row .You can use this to get values and provide in Urls etc.
keyProperty:- Primary key of Model Object.

e)<liferay-ui:search-container-column-text property="" name="" href=""  value=""/>

This search container column-text property tag is used to set the value in column.For each column their is one tag.This contain attribute:-

property:- This must be the same as fields mention in Model Class whose getter and setters are available.If this match with instance variables of Model Class(Student) then it get value automatically from current object
name:- Header name of your column.
href:- If you want any action is perform on Clicking of this field then pass Url  here.
Value:- Any specific value you want to show  on the basis of value of database or any hardcoded value.Otherwise by property tag it will automatically take from current object.
Example:-

<liferay-ui:search-container-column-text  name="Gender" value='<%=(aStudent.getGender()==1)?"Male":"Female"%>'/>

Here value is by default is O or 1 but we use Current Object and get Gender and on the basis of result we change the value to Male or Female.

f)<liferay-ui:search-container-column-jsp name="" align="left" path=""/>

If you want to show a particular jsp in some column then you can use this tag by providing complete path of your jsp.

g)<liferay-ui:search-iterator />

This is iterator this tag must come after row tag this iterate the list and create rows.

Step 3:-Then Create a jsp file inside html and paste the content 

display_student.jsp

Note:- The back to all link use the URL that is captured by PortalUtil.getCurrentURL(request) in view.jsp and send with renderUrl  .So that we can go back to view.jsp again.

Step 4:-Then Create a jsp file inside html and paste the content 

address.jsp

Note:- This Complete jsp is directly put in column(search-container-column-jsp tag)

Step 5:-Then Copy the code in your Controller class

SearchContainer.java


This code is self explanatory.

Output:-




On Click on Student Name or Detail Control goes to another jsp where we show whole data related to student.


You can Download source code from Here




Hope this will help ...


Related Post:-






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

Liferay Jsp Hook for override login jsp

Today we write a Hook for overriding default Login Page. Here we remove the openID, Create Account and Forgot Password links



Step 1:- Create a Liferay Plugin project and select Hook


Create a liferay plugin project give project name as LoginProject and select hook then finish.






Step 2:- Create a Login hook in the Project

Right click on the project> New >Liferay Hook Configuration>Select Custom Jsp>Next




Click  Add from Liferay
then search for login.jsp  >Ok>Finish




Step 3:- Now Override login.jsp

You can Override the jsp as per your need, we can see that the links come from navigation.jsp which is included in login.jsp so we just comment the included part.

Code Snipplet From login.jsp

..........
..........


<aui:button-row>

<aui:button type="submit" value="sign-in" />

</aui:button-row>

</aui:form>



<!-- <liferay-util:include page="/html/portlet/login/navigation.jsp" /> -->



.........
.........

Step 4:- Deploy the Hook and see the output

And Build your build.xml and try to login you see a login page with no links for openID, Create Account and Forgot Password 





Note:-

In this way you can override default login page provide by liferay when you deploy your hook it create a  ".portal.jsp" file in 
(..\tomcat-7.0.42\webapps\ROOT\html\portlet\login)  .

So we have 2 file :-
1)login.jsp
2)login.portal.jsp

when you deploy your hook  "login.portal.jsp" is in use when you undelpoy your hook "login.portal.jsp" is deleted and again login.jsp is in use.

Liferay Service Hook to override Liferay Existing Service

Today we override User Service method  - getUserCount() 

Here we create a class that extend the particular wrapper class and just provide the entries of our custom class in liferay-hook.xml so that liferay use our services instead of Original.

Step 1:-Create a liferay Plug in project and Select Plugin type Hook








Step 2:-Create a Hook in the Project

Right Click on Project -->New-->Liferay Hook Configuration--->Select Services


Click Next--->Click Add-->Select Service Type(UserLocalService)


For Impl Class

Click New-->Give Package Name and your Impl Class Name

Then Click Finish

This step automatically Create the entry in  liferay-hook.xml. 

<hook>
<service>
<service-type>com.liferay.portal.service.UserLocalService</service-type>
<service-impl>com.ipg.MyUserLocalService</service-impl>
</service>
</hook>






Step 3:-Override the getUserCount()

Open MyUserLocalService and override getUserCount()


package com.ipg;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.service.UserLocalServiceWrapper;
import com.liferay.portal.service.UserLocalService;

public class MyUserLocalService extends UserLocalServiceWrapper {
public MyUserLocalService(UserLocalService userLocalService) {
super(userLocalService);
}

@Override
public int getUsersCount() throws SystemException {
System.out.println("This text is from getUsersCount ");
return super.getUsersCount();
}
}

Step 4:-Deploy the Hook

Deploy the hook and call getUsersCount() from your portlet :-


public void doView(RenderRequest renderRequest,
RenderResponse renderResponse) throws IOException, PortletException {
try {
UserLocalServiceUtil.getUsersCount();

} catch (SystemException e) {
e.printStackTrace();
}
super.doView(renderRequest, renderResponse);
}

Output:-This text is from getUsersCount 

Thats it every time you call getUserCount() it will call your method rather than original one.