/*
* Light Portal
*
* Copyright (c) 2009, Light Portal, Inc or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Light Portal, Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*
*/
package org.light.portlets.search;
import static org.light.portal.util.Constants._PORTLET_JSP_PATH;
import static org.light.portal.util.Constants._SEARCH_LIST;
import java.util.LinkedList;
import java.util.List;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletRequest;
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.light.portal.Context;
import org.light.portal.core.PortalContextFactory;
import org.light.portal.model.User;
import org.light.portal.portlet.core.impl.LightGenericPortlet;
import org.light.portal.search.Searcher;
import org.light.portal.search.model.SearchCriteria;
import org.light.portal.search.model.SearchResult;
import org.light.portal.search.model.SearchResultItem;
import org.light.portal.util.LabelBean;
import org.light.portal.util.PropUtil;
import org.light.portal.util.StringUtil;
/**
*
* @author Jianmin Liu
**/
public class GlobalSearchPortlet extends LightGenericPortlet {
public void processAction (ActionRequest request, ActionResponse response)
throws PortletException, java.io.IOException
{
String action = request.getParameter("action");
if("find".equals(action)){
// String input = request.getParameter("input");
// String keyword = request.getParameter("keyword");
// String type = request.getParameter("type");
// String sort = request.getParameter("sort");
// String show = request.getParameter("show");
// if("0".equals(input)) keyword=null;
// SearchCriteria criteria = (SearchCriteria)request.getPortletSession().getAttribute("criteria",PortletSession.APPLICATION_SCOPE);
// if(criteria == null)
// criteria = new SearchCriteria();
// criteria.setKeyword(keyword);
// if(show != null) criteria.setSort(sort);
// if(!StringUtil.isEmpty(show)) try{criteria.setRowPerPage(Integer.parseInt(show));}catch(Exception e){}
// try{
// SearchResult result= null;
// if(type == null){
// if(request.getPortletSession().getAttribute("searchTypes",PortletSession.APPLICATION_SCOPE) == null){
// this.initSearch(request);
// }
// List<LabelBean> beans = (List<LabelBean>)request.getPortletSession().getAttribute("searchTypes",PortletSession.APPLICATION_SCOPE);
// for(LabelBean bean : beans){
// if(bean.isDefaulted()){
// type = bean.getId();
// break;
// }
// }
// if(type != null)
// result = this.getSearcher(request).search(Class.forName(type),criteria);
// else
// result = this.getSearcher(request).search(User.class,criteria);
// }
// else{
// if(!type.equals("all")){
// result = this.getSearcher(request).search(Class.forName(type),criteria);
// List<LabelBean> beans = (List<LabelBean>)request.getPortletSession().getAttribute("searchTypes",PortletSession.APPLICATION_SCOPE);
// for(LabelBean bean : beans){
// if(bean.getId().equals(type))
// bean.setDefaulted(true);
// else
// bean.setDefaulted(false);
// }
// }else{
// result = this.getSearcher(request).search(criteria);
// List<LabelBean> beans = (List<LabelBean>)request.getPortletSession().getAttribute("searchTypes",PortletSession.APPLICATION_SCOPE);
// for(LabelBean bean : beans){
// if(bean.getId().equals(type))
// bean.setDefaulted(true);
// else
// bean.setDefaulted(false);
// }
// }
// }
// request.getPortletSession().setAttribute("criteria",criteria,PortletSession.APPLICATION_SCOPE);
// request.getPortletSession().setAttribute("gsResult",result,PortletSession.APPLICATION_SCOPE);
// }catch(Exception e){
// }
}
}
private void initSearch(PortletRequest request){
if(request.getPortletSession().getAttribute("searchTypes",PortletSession.APPLICATION_SCOPE) == null){
List<LabelBean> types = new LinkedList<LabelBean>();
types.add(new LabelBean("all","All"));
String value=PropUtil.getString(_SEARCH_LIST);
String[] lists = value.split(";");
int i = 0;
for(String list : lists){
String[] unit = list.split(",");
if(unit.length >= 4){
if(i == 0)
types.add(new LabelBean(unit[0],PortalContextFactory.getPortalContext().getMessageByKey(unit[3]),true));
else
types.add(new LabelBean(unit[0],PortalContextFactory.getPortalContext().getMessageByKey(unit[3])));
}
i++;
}
request.getPortletSession().setAttribute("searchTypes",types,PortletSession.APPLICATION_SCOPE);
}
if(request.getPortletSession().getAttribute("searchSorts",PortletSession.APPLICATION_SCOPE) == null){
List<LabelBean> sorts = new LinkedList<LabelBean>();
String value=PropUtil.getString("search.criteria.sort");
String[] lists = value.split(";");
for(String list : lists){
String[] unit = list.split(":");
String sort = (unit.length > 1) ? unit[1] : "";
String desc = unit[0];
sorts.add(new LabelBean(sort,desc));
}
request.getPortletSession().setAttribute("searchSorts",sorts,PortletSession.APPLICATION_SCOPE);
}
if(request.getPortletSession().getAttribute("searchShows",PortletSession.APPLICATION_SCOPE) == null){
List<LabelBean> shows = new LinkedList<LabelBean>();
String value=PropUtil.getString("search.criteria.page.rows");
String[] units = value.split(",");
for(String unit : units){
shows.add(new LabelBean(unit,unit));
}
request.getPortletSession().setAttribute("searchShows",shows,PortletSession.APPLICATION_SCOPE);
}
}
protected void doView (RenderRequest request, RenderResponse response)
throws PortletException, java.io.IOException
{
initSearch(request);
String flowOff = request.getParameter("flowOff");
if(flowOff != null){
if("0".equals(flowOff))
request.getPortletSession().setAttribute("flowOff",true,PortletSession.APPLICATION_SCOPE);
else
request.getPortletSession().removeAttribute("flowOff",PortletSession.APPLICATION_SCOPE);
}
String viewType = request.getParameter("viewType");
if(viewType != null && "2".equals(viewType))
doViewFlow(request,response);
else if(viewType != null && "1".equals(viewType))
doViewSlide(request,response);
else
doViewNormal(request,response);
}
private void doViewFlow (RenderRequest request, RenderResponse response)
throws PortletException, java.io.IOException
{
SearchResult result = (SearchResult)request.getPortletSession().getAttribute("gsResult",PortletSession.APPLICATION_SCOPE);
if(result != null){
int number = 0;
int total = result.getTotal();
if(request.getParameter("page") != null){
try{
number = Integer.parseInt(request.getParameter("page"));
}catch(Exception e){}
}else if(request.getParameter("number") != null){
try{
number = Integer.parseInt(request.getParameter("number"));
}catch(Exception e){}
}
if(number >= total) number = total - 1;
if(number < 0) number = 0;
List<SearchResultItem> leftPage = new LinkedList<SearchResultItem>();
for(int i=number - 5;i<number;i++){
if(i < 0) continue;
if(i >= total) break;
leftPage.add(result.getItems().get(i));
}
List<SearchResultItem> rightPage = new LinkedList<SearchResultItem>();
for(int i=number + 1;i<=number + 5;i++){
if(i >= total) break;
rightPage.add(0,result.getItems().get(i));
}
request.setAttribute("leftPage", leftPage);
request.setAttribute("rightPage", rightPage);
request.setAttribute("pages", total);
if(number < total)
request.setAttribute("current", result.getItems().get(number));
request.setAttribute("page", number);
}
this.getPortletContext().getRequestDispatcher(_PORTLET_JSP_PATH+"/global/flowView.jsp").include(request,response);
}
private void doViewSlide (RenderRequest request, RenderResponse response)
throws PortletException, java.io.IOException
{
SearchResult result = (SearchResult)request.getPortletSession().getAttribute("gsResult",PortletSession.APPLICATION_SCOPE);
if(result != null){
int pageNumber = 5;
int pages = (result.getTotal() % pageNumber == 0) ? result.getTotal() / pageNumber : result.getTotal() / pageNumber + 1;
int page =0;
int number = 0;
if(request.getParameter("page") != null){
try{
page = Integer.parseInt(request.getParameter("page"));
number = page * pageNumber;
}catch(Exception e){}
}else if(request.getParameter("number") != null){
try{
number = Integer.parseInt(request.getParameter("number"));
page = number / pageNumber;
}catch(Exception e){}
}
List<SearchResultItem> currentPage = new LinkedList<SearchResultItem>();
for(int i=page * pageNumber;i<result.getTotal();i++){
if(i>=(page + 1)* pageNumber) break;
currentPage.add(result.getItems().get(i));
}
request.setAttribute("members", currentPage);
request.setAttribute("pages", pages);
request.setAttribute("page", page);
}
this.getPortletContext().getRequestDispatcher(_PORTLET_JSP_PATH+"/global/slideView.jsp").include(request,response);
}
private void doViewNormal (RenderRequest request, RenderResponse response)
throws PortletException, java.io.IOException
{
this.initSearch(request);
String input = request.getParameter("input");
String keyword = request.getParameter("keyword");
String type = request.getParameter("type");
String sort = request.getParameter("sort");
String show = request.getParameter("show");
if("0".equals(input)) keyword=null;
SearchCriteria criteria = (SearchCriteria)request.getPortletSession().getAttribute("criteria",PortletSession.APPLICATION_SCOPE);
if(criteria == null)
criteria = new SearchCriteria();
if(!StringUtil.isEmpty(show)) try{criteria.setRowPerPage(Integer.parseInt(show));}catch(Exception e){}
int page =1;
int number = 0;
boolean pageChange = false;
if(request.getParameter("page") != null){
try{
page = Integer.parseInt(request.getParameter("page"));
number = page * criteria.getRowPerPage();
pageChange = true;
}catch(Exception e){}
}else if(request.getParameter("number") != null){
try{
number = Integer.parseInt(request.getParameter("number"));
page = number / criteria.getRowPerPage();
pageChange = true;
}catch(Exception e){}
}
criteria.setPage(page);
if(!pageChange){
criteria.setKeyword(keyword);
if(sort != null) criteria.setSort(sort);
}
SearchResult result= null;
try{
if(type == null){
List<LabelBean> beans = (List<LabelBean>)request.getPortletSession().getAttribute("searchTypes",PortletSession.APPLICATION_SCOPE);
for(LabelBean bean : beans){
if(bean.isDefaulted()){
type = bean.getId();
break;
}
}
if(type != null)
result = this.getSearcher(request).search(Class.forName(type),criteria);
else
result = this.getSearcher(request).search(User.class,criteria);
}
else{
if(!type.equals("all")){
result = this.getSearcher(request).search(Class.forName(type),criteria);
List<LabelBean> beans = (List<LabelBean>)request.getPortletSession().getAttribute("searchTypes",PortletSession.APPLICATION_SCOPE);
for(LabelBean bean : beans){
if(bean.getId().equals(type))
bean.setDefaulted(true);
else
bean.setDefaulted(false);
}
}else{
result = this.getSearcher(request).search(User.class,criteria);
List<LabelBean> beans = (List<LabelBean>)request.getPortletSession().getAttribute("searchTypes",PortletSession.APPLICATION_SCOPE);
for(LabelBean bean : beans){
if(bean.getId().equals(type))
bean.setDefaulted(true);
else
bean.setDefaulted(false);
}
}
}
request.getPortletSession().setAttribute("criteria",criteria,PortletSession.APPLICATION_SCOPE);
request.setAttribute("gsResult",result);
request.getPortletSession().setAttribute("gsResult",result,PortletSession.APPLICATION_SCOPE);
}catch(Exception e){
}
int total = result.getTotal();
int pages = (result.getTotal() % criteria.getRowPerPage() == 0) ? result.getTotal() / criteria.getRowPerPage() : result.getTotal() / criteria.getRowPerPage() + 1;
int start = (page-1) * criteria.getRowPerPage();
int end = (page * criteria.getRowPerPage() < result.getTotal()) ? page * criteria.getRowPerPage() : result.getTotal();
request.setAttribute("items", result.getItems());
request.setAttribute("pages", pages);
request.setAttribute("page", page);
request.setAttribute("start",start);
request.setAttribute("end",end);
request.setAttribute("total",result.getTotal());
this.getPortletContext().getRequestDispatcher(_PORTLET_JSP_PATH+"/global/view.jsp").include(request,response);
}
protected Searcher getSearcher(PortletRequest request) {
return (Searcher)Context.getInstance().getService(this.getServletRequest(request), "searcher");
}
}
|