Java tutorial
/** * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved. * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library 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. */ package com.liferay.experts.questions.util; import com.liferay.experts.model.Answer; import com.liferay.experts.model.Question; import com.liferay.experts.service.AnswerLocalServiceUtil; import com.liferay.experts.service.QuestionLocalServiceUtil; import com.liferay.experts.service.persistence.QuestionActionableDynamicQuery; import com.liferay.experts.util.PortletKeys; import com.liferay.experts.util.StatusConstants; import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery; import com.liferay.portal.kernel.exception.PortalException; import com.liferay.portal.kernel.exception.SystemException; import com.liferay.portal.kernel.search.BaseIndexer; import com.liferay.portal.kernel.search.Document; import com.liferay.portal.kernel.search.Field; import com.liferay.portal.kernel.search.SearchContext; import com.liferay.portal.kernel.search.SearchEngineUtil; import com.liferay.portal.kernel.search.SearchException; import com.liferay.portal.kernel.search.Summary; import com.liferay.portal.kernel.util.GetterUtil; import com.liferay.portal.kernel.util.StringUtil; import com.liferay.portal.kernel.util.Validator; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Locale; import javax.portlet.PortletURL; /** * @author Ryan Park */ public class QuestionsIndexer extends BaseIndexer { public static final String[] CLASS_NAMES = { Question.class.getName() }; public static final String PORTLET_ID = PortletKeys.QUESTIONS; public String[] getClassNames() { return CLASS_NAMES; } public String getPortletId() { return PORTLET_ID; } @Override protected void doDelete(Object obj) throws Exception { Question question = (Question) obj; deleteDocument(question.getCompanyId(), question.getQuestionId()); } @Override protected Document doGetDocument(Object obj) throws Exception { Question question = (Question) obj; Document document = getBaseModelDocument(PORTLET_ID, question); document.addText(Field.CONTENT, question.getContent()); document.addText(Field.TITLE, question.getTitle()); document.addText("answers", getAnswers(question.getQuestionId())); return document; } @Override protected Summary doGetSummary(Document document, Locale locale, String snippet, PortletURL portletURL) { String title = document.get(Field.TITLE); String content = snippet; if (Validator.isNull(snippet)) { content = StringUtil.shorten(document.get(Field.CONTENT), 200); } String questionId = document.get(Field.ENTRY_CLASS_PK); portletURL.setParameter("mvcPath", "/question/view_question.jsp"); portletURL.setParameter("questionId", questionId); return new Summary(title, content, portletURL); } @Override protected void doReindex(Object obj) throws Exception { Question question = (Question) obj; reindex(question); } @Override protected void doReindex(String className, long classPK) throws Exception { Question question = QuestionLocalServiceUtil.getQuestion(classPK); reindex(question); } @Override protected void doReindex(String[] ids) throws Exception { long companyId = GetterUtil.getLong(ids[0]); reindex(companyId); } @Override protected String getPortletId(SearchContext searchContext) { return PORTLET_ID; } private String[] getAnswers(long questionId) throws PortalException, SystemException { List<Answer> answers = AnswerLocalServiceUtil.getAnswers(questionId, StatusConstants.APPROVED); String[] answerContents = new String[answers.size()]; for (int i = 0; i < answers.size(); i++) { Answer answer = answers.get(i); answerContents[i] = answer.getContent(); } return answerContents; } private void reindex(long companyId) throws Exception { final Collection<Document> documents = new ArrayList<Document>(); ActionableDynamicQuery actionableDynamicQuery = new QuestionActionableDynamicQuery() { @Override protected void performAction(Object object) throws PortalException { Question question = (Question) object; Document document = getDocument(question); documents.add(document); } }; actionableDynamicQuery.setCompanyId(companyId); actionableDynamicQuery.performActions(); SearchEngineUtil.updateDocuments(companyId, documents); } private void reindex(Question question) throws SearchException { Document document = getDocument(question); SearchEngineUtil.updateDocument(question.getCompanyId(), document); } }