package your.package.mvc.controller;
import
java.util.ArrayList;
import java.util.List;
import
javax.portlet.ActionRequest;
import
javax.portlet.ActionResponse;
import
javax.portlet.PortletRequest;
import
javax.portlet.RenderResponse;
import
org.apache.commons.logging.Log;
import
org.apache.commons.logging.LogFactory;
import
org.springframework.stereotype.Controller;
import
org.springframework.web.bind.annotation.ModelAttribute;
import
org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;
import
your.package.mvc.vo.SearchContentVO;
import
your.package.mvc.vo.SearchResult;
import
com.liferay.portal.kernel.search.BooleanQuery;
import
com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
import
com.liferay.portal.kernel.search.Document;
import
com.liferay.portal.kernel.search.DocumentImpl;
import
com.liferay.portal.kernel.search.Field;
import
com.liferay.portal.kernel.search.Hits;
import
com.liferay.portal.kernel.search.ParseException;
import
com.liferay.portal.kernel.search.SearchEngineUtil;
import
com.liferay.portal.kernel.search.SearchException;
/**
*
Controller to add search contents.
* @author nverma
*/
@Controller("addSearchContentController")
@RequestMapping(value = "VIEW")
public class AddSearchContentController
extends AbstractBaseController
{
/** The Constant LOG. */
private static final Log LOG = LogFactory
.getLog(AddSearchContentController.class);
/** The Constant MODEL_ATT. */
private static final String MODEL_ATT = "searchContentVO";
/**
*
Inits the command object.
*
*
@param portletRequest the portlet request
*
@return the search content vo
*/
@ModelAttribute(MODEL_ATT)
public SearchContentVO initCommandObject(final PortletRequest portletRequest) {
LOG.debug("Initializing command object 'SearchContentVO'...");
SearchContentVO searchContentVO = new SearchContentVO();
searchContentVO.setSearchContents(allItems());
return searchContentVO;
}
/**
*
Render add search content.
*
*
@param renderResponse the render response
*
@return the string
*/
@RenderMapping
public String renderAddSearchContent(final RenderResponse renderResponse) {
return "addSearchContent";
}
/**
*
Adds the search contents.
*
*
@param searchContentVO the search content vo
*
@param actionRequest the action request
*
@param actionResponse the action response
*/
@ActionMapping(params = "action=addSearchContent")
public void addSearchContents(@ModelAttribute SearchContentVO
searchContentVO,
final ActionRequest actionRequest,
ActionResponse actionResponse) {
// Adding Search Document
Document document = new DocumentImpl();
document.addKeyword("Id",
searchContentVO.getId());
document.addText(Field.TITLE, searchContentVO.getTitle());
document.addText("description",
searchContentVO.getDescription());
document.addText("AllDocuments", "OnlyMyItems");
try {
LOG.info("Adding search
content...");
SearchEngineUtil.addDocument(10132, document);
LOG.info("Added search
content successfully.");
} catch (SearchException e) {
LOG.error("Failed to add
search document." +e.getMessage());
}
searchContentVO.setSearchContents(allItems());
}
/**
*
Delete search contents.
*
*
@param searchContentVO the search content vo
*
@param actionRequest the action request
*
@param actionResponse the action response
*/
@ActionMapping(params = "action=deleteSearchContent")
public void deleteSearchContents(@ModelAttribute SearchContentVO
searchContentVO,
final ActionRequest actionRequest,
ActionResponse actionResponse) {
try {
LOG.info("Deleting
search content...["+searchContentVO.getId()+"]");
SearchEngineUtil.deleteDocument(10132, String.valueOf(searchContentVO.getId()));
LOG.info("Deleted search
content successfully.");
} catch (SearchException e) {
LOG.error("Failed to
deleted search document." +e.getMessage());
}
searchContentVO.setSearchContents(allItems());
}
/**
*
All items.
*
* @return the list
*/
public
List<SearchResult> allItems(){
List<SearchResult> searchResultLst = new ArrayList<SearchResult>();
Document[]
resultDocs = null;
Hits hits = null;
BooleanQuery
searchQuery = BooleanQueryFactoryUtil.create();
try {
searchQuery.addTerm("AllDocuments",
"OnlyMyItems");
hits =
SearchEngineUtil.search(10132, searchQuery, -1, -1);
resultDocs = hits.getDocs();
} catch (ParseException e) {
LOG.error("Failed to
parse while searching. " + e.getMessage());
e.printStackTrace();
} catch (SearchException e) {
LOG.error("Failed to
search. " + e.getMessage());
e.printStackTrace();
}
for (int i = 0; i < resultDocs.length;
i++) {
LOG.debug("Searched
Items: ");
SearchResult searchResult = new
SearchResult();
LOG.debug("Document
-> "
+ hits.doc(i).get(Field.UID));
String
idStr = hits.doc(i).get("Id");
if(idStr==null || idStr.isEmpty()){
continue;
}
Long id = Long.parseLong(idStr);
LOG.debug("Id -> " + id);
searchResult.setId(id);
String
title = hits.doc(i).get(Field.TITLE);
if(title==null || title.isEmpty()){
continue;
}
LOG.debug("Title
-> "
+ title);
searchResult.setTitle(title);
String
description = hits.doc(i).get("description");
LOG.debug("Description
-> "
+ description);
searchResult.setDescription(description);
//Add search result into list
searchResultLst.add(searchResult);
}
return searchResultLst;
}
}
|