In the previous tutorial on Liferay Search Form with Search Container we see how we can search data from a search container. We override DisplayTerm and Search Container classes. Today we will create the same thing but in a simple way. Before reading this blog it is highly recommended to read my previous blog on Liferay Search Form with Search Container.
I already created my Services using Service Builder. Here is my service.xml:-
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
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
Step 2:-Create view.jsp
Explanation:-
Here we use a simple search container which is enclosed in a aui form.For results we call methods on 3 conditions:-
1)For Advance Search
2)For Basic Search
3)For Dafault data we load all the employees.
Iterator URL is very important for pagination . If you are not using this then after search it will always redirect to first page.
Here we include a search.jsp which contain our advance search fields.
search.jsp
Step 3:-Create Helper Class
Now Deploy the portlet and check the output
Output:-
1)Normal Search
2)Advance Search
You can download the source code from here.
Hope this will Help....
Related Post:-
Basic web content in Liferay
Categorization in web content
Structure and Template in Web Content
Fetch Web Content Programmatically
Get Journal Article Using SAX Parser
Form Handling in Spring Portlet
Embedding a Web Content in a Portlet
Fetch Latest Version Journal Article or Web Content in Liferay
init.jsp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@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.test.EmployeeHelper"%> | |
<%@page import="com.liferay.portal.kernel.util.Validator"%> | |
<%@page import="java.util.Collections"%> | |
<portlet:defineObjects /> | |
<% | |
int eId = ParamUtil.getInteger(request, "eId"); | |
String eName = ParamUtil.getString(request, "eName"); | |
%> |
Step 2:-Create view.jsp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@include file="init.jsp" %> | |
<liferay-portlet:renderURL varImpl="iteratorURL"> | |
<portlet:param name="mvcPath" value="/html/test/view.jsp" /> | |
<portlet:param name="eId" value="<%=String.valueOf(eId) %>"/> | |
<portlet:param name="eName" value="<%=eName %>"/> | |
</liferay-portlet:renderURL> | |
<liferay-portlet:renderURL varImpl="employeeSearchURL"> | |
<portlet:param name="mvcPath" value="/html/test/view.jsp" /> | |
</liferay-portlet:renderURL> | |
<aui:form action="<%= employeeSearchURL %>" method="post" name="fm"> | |
<liferay-portlet:renderURLParams varImpl="employeeSearchURL" /> | |
<liferay-ui:search-container delta="5" displayTerms="<%= new DisplayTerms(renderRequest) %>" iteratorURL="<%= iteratorURL %>" | |
emptyResultsMessage="No Records Found"> | |
<liferay-ui:search-form page="/html/test/search.jsp" servletContext="<%= application %>" /> | |
<liferay-ui:search-container-results > | |
<% | |
DisplayTerms displayTerms =searchContainer.getDisplayTerms(); | |
String keywords = displayTerms.getKeywords(); | |
List<Employee> employeeList = Collections.EMPTY_LIST; | |
if (displayTerms.isAdvancedSearch()) {//Advance Search | |
employeeList = EmployeeHelper.getEmployee(eId,eName,displayTerms.isAndOperator()); | |
searchContainer.setTotal(employeeList.size()); | |
searchContainer.setResults(ListUtil.subList(employeeList,searchContainer.getStart(),searchContainer.getEnd())); | |
} else if(!Validator.isBlank(keywords)){//Basic Search | |
employeeList = EmployeeHelper.getEmployeeByKeyWord(keywords); | |
searchContainer.setTotal(employeeList.size()); | |
searchContainer.setResults(ListUtil.subList(employeeList,searchContainer.getStart(),searchContainer.getEnd())); | |
} | |
else{//No Search | |
employeeList = EmployeeLocalServiceUtil.getEmployees(0, EmployeeLocalServiceUtil.getEmployeesCount()); | |
searchContainer.setTotal(employeeList.size()); | |
searchContainer.setResults(ListUtil.subList(employeeList,searchContainer.getStart(),searchContainer.getEnd())); | |
} | |
%> | |
</liferay-ui:search-container-results> | |
<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> |
Explanation:-
Here we use a simple search container which is enclosed in a aui form.For results we call methods on 3 conditions:-
1)For Advance Search
2)For Basic Search
3)For Dafault data we load all the employees.
Iterator URL is very important for pagination . If you are not using this then after search it will always redirect to first page.
Here we include a search.jsp which contain our advance search fields.
search.jsp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@include file="init.jsp" %> | |
<% | |
SearchContainer searchContainer = (SearchContainer)request.getAttribute("liferay-ui:search:searchContainer"); | |
DisplayTerms displayTerms = searchContainer.getDisplayTerms(); | |
%> | |
<liferay-ui:search-toggle buttonLabel="Employee Search" displayTerms="<%= displayTerms %>" id="toggle_id_employee_search"> | |
<aui:input label="Employee ID" name="eId" value='<%= eId==0 ? " ":eId %>'/> <!-- If Id = 0 then set blank in field --> | |
<aui:input label="Employee Name" name="eName" value='<%= eName %>'/> | |
</liferay-ui:search-toggle> |
Step 3:-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
EmployeeHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.HtmlUtil; | |
import com.liferay.portal.kernel.util.StringPool; | |
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; | |
public class EmployeeHelper { | |
public static List<Employee> getEmployee(int eId, String eName, boolean isAndOperator) throws SystemException { | |
ClassLoader classLoader = (ClassLoader) PortletBeanLocatorUtil.locate(ClpSerializer.getServletContextName(), "portletClassLoader"); | |
DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Employee.class, "emp", classLoader); | |
Junction junction = null; | |
List<Employee> employeeList = Collections.EMPTY_LIST; | |
if (isAndOperator) { | |
junction = RestrictionsFactoryUtil.conjunction(); | |
} else { | |
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(StringPool.PERCENT + HtmlUtil.escape(eName) + StringPool.PERCENT)); | |
} | |
dynamicQuery.add(junction); | |
try { | |
employeeList = EmployeeLocalServiceUtil.dynamicQuery(dynamicQuery); | |
} catch (final SystemException e) { | |
} | |
return employeeList; | |
} | |
public static List<Employee> getEmployeeByKeyWord(String keywords) throws SystemException { | |
List<Employee> employeeList = Collections.EMPTY_LIST; | |
final ClassLoader classLoader = (ClassLoader) PortletBeanLocatorUtil.locate(ClpSerializer.getServletContextName(), "portletClassLoader"); | |
final Junction junction = RestrictionsFactoryUtil.disjunction(); | |
DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Employee.class, "emp", classLoader); | |
if (Validator.isDigit(keywords)) { | |
junction.add(PropertyFactoryUtil.forName("emp.eId").eq(Integer.valueOf(keywords))); | |
} else { | |
junction.add(PropertyFactoryUtil.forName("emp.eName").like(StringPool.PERCENT + HtmlUtil.escape(keywords) + StringPool.PERCENT)); | |
} | |
dynamicQuery.add(junction); | |
try { | |
employeeList = EmployeeLocalServiceUtil.dynamicQuery(dynamicQuery); | |
} catch (final SystemException e) { | |
} | |
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.
Related Post:-
Basic web content in Liferay
Categorization in web content
Structure and Template in Web Content
Fetch Web Content Programmatically
Get Journal Article Using SAX Parser
Form Handling in Spring Portlet
Embedding a Web Content in a Portlet
Fetch Latest Version Journal Article or Web Content in Liferay
No comments:
Post a Comment