Saturday, September 23, 2017

Liferay Search Form with Search Container


We already know how to use Search Container in Liferay. Today we will create a search form that can search values from search container. For this we use <liferay-ui:search-form> tag. By using this we can apply basic as well as advance search.

Liferay use this in many places like when you go to web content section (Admin->Content)




Here you can search web content by name and when you click Gear icon you can search by id, name etc. You can also apply All(AND) and ANY(OR) operations.

I already created my Services using Service Builder. Here is my service.xml:-

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.2.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_2_0.dtd">
<service-builder package-path="com.liferay.sample">
<author>aditya.bhardwaj</author>
<namespace>SAMPLE</namespace>
<entity name="Employee" local-service="true" remote-service="false">
<column name="eId" primary="true" type="int"></column>
<column name="eName" type="String"></column>
<column name="eAddress" type="String"></column>
<column name="ePhone" type="String"></column>
</entity>
</service-builder>
view raw service.xml hosted with ❤ by GitHub


I fill some data in Database:-



Today we apply Searching on these records on the basis of eId and eName.





Project Structure




So lets start this Step by Step:-

Step 1:-Create init.jsp
First we create a init.jsp that contain all the imports:-

init.jsp
<%@page import="com.liferay.portal.kernel.util.ParamUtil"%>
<%@page import="com.liferay.sample.service.EmployeeLocalServiceUtil"%>
<%@page import="com.liferay.sample.model.Employee"%>
<%@page import="javax.portlet.PortletURL"%>
<%@page import="com.liferay.portal.kernel.dao.search.SearchContainer"%>
<%@page import="com.liferay.portal.kernel.util.ListUtil"%>
<%@page import="com.liferay.portal.kernel.dao.search.DisplayTerms"%>
<%@page import="java.util.List"%>
<%@page import="com.liferay.portal.util.PortalUtil"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui"%>
<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui"%>
<%@page import="com.search.EmployeeSearchContainer"%>
<%@page import="com.search.EmployeeDisplayTerms"%>
<%@page import="com.test.EmployeeHelper"%>
<portlet:defineObjects />
view raw init.jsp hosted with ❤ by GitHub


Now as you can see in init.jsp we use two java classes EmployeeSearchContainer and EmployeeDisplayTerms. Now we create these two classes.

Step 2:-Create EmployeeSearchContainer.java
Now we create EmployeeSearchContainer that extends SearchContainer in this class we can set properties like empty result message and default records per page(DELTA).

EmployeeSearchContainer.java
package com.search;
import java.util.ArrayList;
import java.util.List;
import javax.portlet.PortletRequest;
import javax.portlet.PortletURL;
import com.liferay.portal.kernel.dao.search.SearchContainer;
import com.liferay.sample.model.Employee;
public class EmployeeSearchContainer extends SearchContainer<Employee> {
public static final String EMPTY_RESULTS_MESSAGE = "No Record Found";
public static final int DEFAULT_DELTA = 5;
static List<String> headerNames = new ArrayList<String>();
static {
headerNames.add("eId");
headerNames.add("eName");
}
public EmployeeSearchContainer(PortletRequest portletRequest, PortletURL iteratorURL) {
super(portletRequest, new EmployeeDisplayTerms(portletRequest), new EmployeeDisplayTerms(portletRequest), DEFAULT_CUR_PARAM, DEFAULT_DELTA, iteratorURL,
headerNames, EMPTY_RESULTS_MESSAGE);
EmployeeDisplayTerms displayTerms = (EmployeeDisplayTerms) getDisplayTerms();
iteratorURL.setParameter("eId", ""+displayTerms.geteId());
iteratorURL.setParameter("eName", displayTerms.geteName());
}
}


Step 3:-Create EmployeeDisplayTerms.java
This class extends DisplayTerms .We want searching on the basis of eId and eName so we create getter and setter for both. This class also gives us important methods like isAdvancedSearch() ,  isAndOperator() etc.

EmployeeDisplayTerms.java
package com.search;
import javax.portlet.PortletRequest;
import com.liferay.portal.kernel.dao.search.DisplayTerms;
import com.liferay.portal.kernel.util.ParamUtil;
public class EmployeeDisplayTerms extends DisplayTerms {
private int eId;
private String eName;
public EmployeeDisplayTerms(PortletRequest portletRequest) {
super(portletRequest);
eId = ParamUtil.getInteger(portletRequest, "eId");
eName = ParamUtil.getString(portletRequest, "eName");
}
public int geteId() {
return eId;
}
public void seteId(int eId) {
this.eId = eId;
}
public String geteName() {
return eName;
}
public void seteName(String eName) {
this.eName = eName;
}
}
 

 

Step 4:-Create view.jsp
We already discuss about Search container in Liferay. Here we apply searching in this:-

view.jsp
<%@include file="init.jsp" %>
<%
PortletURL portletURL = renderResponse.createRenderURL();
String portletURLString = portletURL.toString();
EmployeeSearchContainer employeeSearchContainer = new EmployeeSearchContainer(renderRequest, portletURL);
EmployeeDisplayTerms displayTerms = (EmployeeDisplayTerms)employeeSearchContainer.getDisplayTerms();
%>
<aui:form action="<%= portletURLString %>" method="post" name="fm">
<liferay-ui:search-container searchContainer="<%= employeeSearchContainer %>" >
<liferay-ui:search-form page="/html/test/search.jsp" servletContext="<%= application %>" />
<liferay-ui:search-container-results results="<%= EmployeeHelper.getEmployee(displayTerms,employeeSearchContainer.getStart(), employeeSearchContainer.getEnd()) %>"
total="<%= EmployeeHelper.getTotalEmployeeCount(displayTerms,employeeSearchContainer.getStart(), employeeSearchContainer.getEnd()) %>"/>
<liferay-ui:search-container-row className="com.liferay.sample.model.Employee" modelVar="aEmployee" >
<liferay-ui:search-container-column-text property="EId" />
<liferay-ui:search-container-column-text property="EName" />
<liferay-ui:search-container-column-text property="EPhone" />
<liferay-ui:search-container-column-text property="EAddress" />
</liferay-ui:search-container-row>
<liferay-ui:search-iterator />
</liferay-ui:search-container>
</aui:form>
view raw view.jsp hosted with ❤ by GitHub


Explanation:-
In this we use liferay-ui:search-form tag that include search.jsp which contain two fields eId and eName:-

search.jsp
<%@include file="init.jsp" %>
<%
EmployeeSearchContainer searchContainer = (EmployeeSearchContainer)request.getAttribute("liferay-ui:search:searchContainer");
EmployeeDisplayTerms displayTerms = (EmployeeDisplayTerms)searchContainer.getDisplayTerms();
%>
<liferay-ui:search-toggle buttonLabel="Employee Search" displayTerms="<%= displayTerms %>" id="toggle_id_employee_search">
<aui:input label="EmployeeID" name="eId" value="<%=displayTerms.geteId() %>"/>
<aui:input label="EmployeeName" name="eName" value="<%=displayTerms.geteName() %>"/>
</liferay-ui:search-toggle>
view raw search.jsp hosted with ❤ by GitHub



Step 5:-Create Helper Class
In view.jsp we use EmployeeHelper class to load the data.In EmployeeHelper we use dynamic query  and Conjunction , Disjunction on the basis of various scenarios like For Normal Search we use Keywords ,for Advance Search ALL operation we use Conjunction , for Advance Search ANY operation we use Disjunction etc.

EmployeeHelper.java
package com.test;
import java.util.Collections;
import java.util.List;
import com.liferay.portal.kernel.bean.PortletBeanLocatorUtil;
import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
import com.liferay.portal.kernel.dao.orm.Junction;
import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ListUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.sample.model.Employee;
import com.liferay.sample.service.ClpSerializer;
import com.liferay.sample.service.EmployeeLocalServiceUtil;
import com.search.EmployeeDisplayTerms;
public class EmployeeHelper {
public static List<Employee>getEmployee(EmployeeDisplayTerms displayTerms,int start,int end) throws SystemException{
List<Employee> employeeList = getEmployeeData(displayTerms.isAdvancedSearch(), displayTerms.isAndOperator(), displayTerms.geteId(), displayTerms.geteName(), displayTerms.getKeywords());
return ListUtil.subList(employeeList, start , end) ;
}
public static int getTotalEmployeeCount(EmployeeDisplayTerms displayTerms,int start,int end) throws SystemException{
return getEmployeeData(displayTerms.isAdvancedSearch(), displayTerms.isAndOperator(), displayTerms.geteId(), displayTerms.geteName(), displayTerms.getKeywords()).size();
}
public static List<Employee>getEmployeeData(boolean isAdvancedSearch,boolean isAndOperator,int eid,String eName,String keywords) throws SystemException {
List<Employee> employeeList = Collections.EMPTY_LIST;
if((Validator.isBlank(keywords)) && (!isAdvancedSearch)){// No search
employeeList = EmployeeLocalServiceUtil.getEmployees(0, EmployeeLocalServiceUtil.getEmployeesCount());
}
else{
ClassLoader classLoader = (ClassLoader)PortletBeanLocatorUtil.locate(ClpSerializer.getServletContextName(),"portletClassLoader");
DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Employee.class, "emp", classLoader);
Junction junction = null;
if(isAdvancedSearch){// Advance Search
if(isAndOperator){
System.out.println("All means And");
junction = RestrictionsFactoryUtil.conjunction();
}else{
System.out.println("Any means OR");
junction = RestrictionsFactoryUtil.disjunction();
}
if(Validator.isDigit(eid+"") || eid>0){
junction.add(PropertyFactoryUtil.forName("emp.eId").eq(Integer.valueOf(eid)));
}
if(!Validator.isBlank(eName)){
junction.add(PropertyFactoryUtil.forName("emp.eName").like("%"+eName+"%"));
}
}else{// Normal Search
junction = RestrictionsFactoryUtil.disjunction();
if(Validator.isDigit(keywords)){
junction.add(PropertyFactoryUtil.forName("emp.eId").eq(Integer.valueOf(keywords)));
}
junction.add(PropertyFactoryUtil.forName("emp.eName").like("%"+keywords+"%"));
}
dynamicQuery.add(junction);
employeeList = EmployeeLocalServiceUtil.dynamicQuery(dynamicQuery);
}
return employeeList;
}
}


Now Deploy the portlet and check the output




Output:-

1)Normal Search



2)Advance Search



You can download the source code from here.

In the next blog we create Search Form With Search Container but in a simple way.

Hope this will Help....

Related Post:-

1 comment:

  1. CASINO GIVES ITEM DATE OF CONCEPT
    A Las 구리 출장안마 Vegas, Nevada judge 영천 출장안마 has ordered 진주 출장샵 casino operators to give away a $5M in monetary gifts to a group 의왕 출장마사지 of high rollers and punters in 영천 출장마사지 the US.

    ReplyDelete

Total Pageviews

1042677

Number Of Unique Visitor

Free counters!