/**
* Copyright (c) 2000-present 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.mail.reader.internal.search;
import com.liferay.expando.kernel.model.ExpandoBridge;
import com.liferay.expando.kernel.util.ExpandoBridgeIndexerUtil;
import com.liferay.mail.reader.model.Message;
import com.liferay.mail.reader.service.MessageLocalService;
import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
import com.liferay.portal.kernel.dao.orm.IndexableActionableDynamicQuery;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
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.IndexWriterHelper;
import com.liferay.portal.kernel.search.Indexer;
import com.liferay.portal.kernel.search.Summary;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.HtmlUtil;
import java.util.Locale;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
/**
* @author Scott Lee
* @author Peter Fellwock
*/
@Component(immediate = true, service = Indexer.class)
public class MessageIndexer extends BaseIndexer<Message> {
public static final String CLASS_NAME = Message.class.getName();
@Override
public String getClassName() {
return CLASS_NAME;
}
@Override
protected void doDelete(Message message) throws Exception {
deleteDocument(message.getCompanyId(), message.getMessageId());
}
@Override
protected Document doGetDocument(Message message) throws Exception {
Document document = getBaseModelDocument(CLASS_NAME, message);
ExpandoBridge expandoBridge = message.getExpandoBridge();
document.addText(
Field.CONTENT, HtmlUtil.extractText(message.getBody()));
document.addKeyword(Field.FOLDER_ID, message.getFolderId());
document.addText(Field.TITLE, message.getSubject());
document.addKeyword("accountId", message.getAccountId());
document.addKeyword("remoteMessageId", message.getRemoteMessageId());
ExpandoBridgeIndexerUtil.addAttributes(document, expandoBridge);
return document;
}
@Override
protected Summary doGetSummary(
Document doc, Locale locale, String snippet,
PortletRequest portletRequest, PortletResponse portletResponse) {
return null;
}
@Override
protected void doReindex(Message message) throws Exception {
Document document = getDocument(message);
indexWriterHelper.updateDocument(
getSearchEngineId(), message.getCompanyId(), document,
isCommitImmediately());
}
@Override
protected void doReindex(String className, long classPK) throws Exception {
Message message = messageLocalService.getMessage(classPK);
doReindex(message);
}
@Override
protected void doReindex(String[] ids) throws Exception {
long companyId = GetterUtil.getLong(ids[0]);
reindexMessages(companyId);
}
protected void reindexMessages(long companyId) throws PortalException {
final IndexableActionableDynamicQuery indexableActionableDynamicQuery =
messageLocalService.getIndexableActionableDynamicQuery();
indexableActionableDynamicQuery.setCompanyId(companyId);
indexableActionableDynamicQuery.setPerformActionMethod(
new ActionableDynamicQuery.PerformActionMethod<Message>() {
@Override
public void performAction(Message message)
throws PortalException {
try {
Document document = getDocument(message);
indexableActionableDynamicQuery.addDocuments(document);
}
catch (PortalException pe) {
if (_log.isWarnEnabled()) {
_log.warn(
"Unable to index message " +
message.getMessageId(),
pe);
}
}
}
});
indexableActionableDynamicQuery.setSearchEngineId(getSearchEngineId());
indexableActionableDynamicQuery.performActions();
}
@Reference
protected IndexWriterHelper indexWriterHelper;
@Reference
protected MessageLocalService messageLocalService;
private static final Log _log = LogFactoryUtil.getLog(MessageIndexer.class);
}