/**
* 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.portlet.usersadmin.util;
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.model.Contact;
import com.liferay.portal.kernel.model.User;
import com.liferay.portal.kernel.search.BaseIndexer;
import com.liferay.portal.kernel.search.BooleanQuery;
import com.liferay.portal.kernel.search.Document;
import com.liferay.portal.kernel.search.Field;
import com.liferay.portal.kernel.search.IndexWriterHelperUtil;
import com.liferay.portal.kernel.search.SearchContext;
import com.liferay.portal.kernel.search.Summary;
import com.liferay.portal.kernel.search.filter.BooleanFilter;
import com.liferay.portal.kernel.service.ContactLocalServiceUtil;
import com.liferay.portal.kernel.service.UserLocalServiceUtil;
import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.workflow.WorkflowConstants;
import java.util.LinkedHashMap;
import java.util.Locale;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
/**
* @author Raymond Augé
* @author Zsigmond Rab
* @author Hugo Huijser
*/
@OSGiBeanProperties
public class ContactIndexer extends BaseIndexer<Contact> {
public static final String CLASS_NAME = Contact.class.getName();
public ContactIndexer() {
setStagingAware(false);
}
@Override
public String getClassName() {
return CLASS_NAME;
}
@Override
public void postProcessSearchQuery(
BooleanQuery searchQuery, BooleanFilter fullQueryBooleanFilter,
SearchContext searchContext)
throws Exception {
addSearchTerm(searchQuery, searchContext, "city", false);
addSearchTerm(searchQuery, searchContext, "country", false);
addSearchTerm(searchQuery, searchContext, "emailAddress", false);
addSearchTerm(searchQuery, searchContext, "firstName", false);
addSearchTerm(searchQuery, searchContext, "fullName", false);
addSearchTerm(searchQuery, searchContext, "lastName", false);
addSearchTerm(searchQuery, searchContext, "middleName", false);
addSearchTerm(searchQuery, searchContext, "region", false);
addSearchTerm(searchQuery, searchContext, "screenName", false);
addSearchTerm(searchQuery, searchContext, "street", false);
addSearchTerm(searchQuery, searchContext, "zip", false);
LinkedHashMap<String, Object> params =
(LinkedHashMap<String, Object>)searchContext.getAttribute("params");
if (params != null) {
String expandoAttributes = (String)params.get("expandoAttributes");
if (Validator.isNotNull(expandoAttributes)) {
addSearchExpando(searchQuery, searchContext, expandoAttributes);
}
}
}
@Override
protected void doDelete(Contact contact) throws Exception {
deleteDocument(contact.getCompanyId(), contact.getContactId());
}
@Override
protected Document doGetDocument(Contact contact) throws Exception {
if (contact.isUser()) {
User user = UserLocalServiceUtil.fetchUserByContactId(
contact.getContactId());
if ((user == null) || user.isDefaultUser() ||
(user.getStatus() != WorkflowConstants.STATUS_APPROVED)) {
return null;
}
}
Document document = getBaseModelDocument(CLASS_NAME, contact);
document.addKeyword(Field.COMPANY_ID, contact.getCompanyId());
document.addDate(Field.MODIFIED_DATE, contact.getModifiedDate());
document.addKeyword(Field.USER_ID, contact.getUserId());
document.addKeyword(Field.USER_NAME, contact.getFullName());
document.addText("emailAddress", contact.getEmailAddress());
document.addText("firstName", contact.getFirstName());
document.addText("fullName", contact.getFullName());
document.addText("jobTitle", contact.getJobTitle());
document.addText("lastName", contact.getLastName());
document.addText("middleName", contact.getMiddleName());
return document;
}
@Override
protected String doGetSortField(String orderByCol) {
if (orderByCol.equals("email-address")) {
return "emailAddress";
}
else if (orderByCol.equals("first-name")) {
return "firstName";
}
else if (orderByCol.equals("job-title")) {
return "jobTitle";
}
else if (orderByCol.equals("last-name")) {
return "lastName";
}
else {
return orderByCol;
}
}
@Override
protected Summary doGetSummary(
Document document, Locale locale, String snippet,
PortletRequest portletRequest, PortletResponse portletResponse) {
return null;
}
@Override
protected void doReindex(Contact contact) throws Exception {
Document document = getDocument(contact);
IndexWriterHelperUtil.updateDocument(
getSearchEngineId(), contact.getCompanyId(), document,
isCommitImmediately());
}
@Override
protected void doReindex(String className, long classPK) throws Exception {
Contact contact = ContactLocalServiceUtil.getContact(classPK);
doReindex(contact);
}
@Override
protected void doReindex(String[] ids) throws Exception {
long companyId = GetterUtil.getLong(ids[0]);
reindexContacts(companyId);
}
protected void reindexContacts(long companyId) throws PortalException {
final IndexableActionableDynamicQuery indexableActionableDynamicQuery =
ContactLocalServiceUtil.getIndexableActionableDynamicQuery();
indexableActionableDynamicQuery.setCompanyId(companyId);
indexableActionableDynamicQuery.setPerformActionMethod(
new ActionableDynamicQuery.PerformActionMethod<Contact>() {
@Override
public void performAction(Contact contact) {
try {
Document document = getDocument(contact);
indexableActionableDynamicQuery.addDocuments(document);
}
catch (PortalException pe) {
if (_log.isWarnEnabled()) {
_log.warn(
"Unable to index contact " +
contact.getContactId(),
pe);
}
}
}
});
indexableActionableDynamicQuery.setSearchEngineId(getSearchEngineId());
indexableActionableDynamicQuery.performActions();
}
private static final Log _log = LogFactoryUtil.getLog(ContactIndexer.class);
}