001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.usersadmin.util;
016    
017    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.dao.orm.IndexableActionableDynamicQuery;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.search.BaseIndexer;
023    import com.liferay.portal.kernel.search.BooleanQuery;
024    import com.liferay.portal.kernel.search.Document;
025    import com.liferay.portal.kernel.search.Field;
026    import com.liferay.portal.kernel.search.IndexWriterHelperUtil;
027    import com.liferay.portal.kernel.search.SearchContext;
028    import com.liferay.portal.kernel.search.Summary;
029    import com.liferay.portal.kernel.search.filter.BooleanFilter;
030    import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
031    import com.liferay.portal.kernel.util.GetterUtil;
032    import com.liferay.portal.kernel.util.Validator;
033    import com.liferay.portal.kernel.workflow.WorkflowConstants;
034    import com.liferay.portal.model.Contact;
035    import com.liferay.portal.model.User;
036    import com.liferay.portal.service.ContactLocalServiceUtil;
037    import com.liferay.portal.service.UserLocalServiceUtil;
038    
039    import java.util.LinkedHashMap;
040    import java.util.Locale;
041    
042    import javax.portlet.PortletRequest;
043    import javax.portlet.PortletResponse;
044    
045    /**
046     * @author Raymond Aug??
047     * @author Zsigmond Rab
048     * @author Hugo Huijser
049     */
050    @OSGiBeanProperties
051    public class ContactIndexer extends BaseIndexer<Contact> {
052    
053            public static final String CLASS_NAME = Contact.class.getName();
054    
055            public ContactIndexer() {
056                    setStagingAware(false);
057            }
058    
059            @Override
060            public String getClassName() {
061                    return CLASS_NAME;
062            }
063    
064            @Override
065            public void postProcessSearchQuery(
066                            BooleanQuery searchQuery, BooleanFilter fullQueryBooleanFilter,
067                            SearchContext searchContext)
068                    throws Exception {
069    
070                    addSearchTerm(searchQuery, searchContext, "city", false);
071                    addSearchTerm(searchQuery, searchContext, "country", false);
072                    addSearchTerm(searchQuery, searchContext, "emailAddress", false);
073                    addSearchTerm(searchQuery, searchContext, "firstName", false);
074                    addSearchTerm(searchQuery, searchContext, "fullName", false);
075                    addSearchTerm(searchQuery, searchContext, "lastName", false);
076                    addSearchTerm(searchQuery, searchContext, "middleName", false);
077                    addSearchTerm(searchQuery, searchContext, "region", false);
078                    addSearchTerm(searchQuery, searchContext, "screenName", false);
079                    addSearchTerm(searchQuery, searchContext, "street", false);
080                    addSearchTerm(searchQuery, searchContext, "zip", false);
081    
082                    LinkedHashMap<String, Object> params =
083                            (LinkedHashMap<String, Object>)searchContext.getAttribute("params");
084    
085                    if (params != null) {
086                            String expandoAttributes = (String)params.get("expandoAttributes");
087    
088                            if (Validator.isNotNull(expandoAttributes)) {
089                                    addSearchExpando(searchQuery, searchContext, expandoAttributes);
090                            }
091                    }
092            }
093    
094            @Override
095            protected void doDelete(Contact contact) throws Exception {
096                    deleteDocument(contact.getCompanyId(), contact.getContactId());
097            }
098    
099            @Override
100            protected Document doGetDocument(Contact contact) throws Exception {
101                    if (contact.isUser()) {
102                            User user = UserLocalServiceUtil.fetchUserByContactId(
103                                    contact.getContactId());
104    
105                            if ((user == null) || user.isDefaultUser() ||
106                                    (user.getStatus() != WorkflowConstants.STATUS_APPROVED)) {
107    
108                                    return null;
109                            }
110                    }
111    
112                    Document document = getBaseModelDocument(CLASS_NAME, contact);
113    
114                    document.addKeyword(Field.COMPANY_ID, contact.getCompanyId());
115                    document.addDate(Field.MODIFIED_DATE, contact.getModifiedDate());
116                    document.addKeyword(Field.USER_ID, contact.getUserId());
117                    document.addKeyword(Field.USER_NAME, contact.getFullName());
118    
119                    document.addText("emailAddress", contact.getEmailAddress());
120                    document.addText("firstName", contact.getFirstName());
121                    document.addText("fullName", contact.getFullName());
122                    document.addText("jobTitle", contact.getJobTitle());
123                    document.addText("lastName", contact.getLastName());
124                    document.addText("middleName", contact.getMiddleName());
125    
126                    return document;
127            }
128    
129            @Override
130            protected String doGetSortField(String orderByCol) {
131                    if (orderByCol.equals("email-address")) {
132                            return "emailAddress";
133                    }
134                    else if (orderByCol.equals("first-name")) {
135                            return "firstName";
136                    }
137                    else if (orderByCol.equals("job-title")) {
138                            return "jobTitle";
139                    }
140                    else if (orderByCol.equals("last-name")) {
141                            return "lastName";
142                    }
143                    else {
144                            return orderByCol;
145                    }
146            }
147    
148            @Override
149            protected Summary doGetSummary(
150                    Document document, Locale locale, String snippet,
151                    PortletRequest portletRequest, PortletResponse portletResponse) {
152    
153                    return null;
154            }
155    
156            @Override
157            protected void doReindex(Contact contact) throws Exception {
158                    Document document = getDocument(contact);
159    
160                    IndexWriterHelperUtil.updateDocument(
161                            getSearchEngineId(), contact.getCompanyId(), document,
162                            isCommitImmediately());
163            }
164    
165            @Override
166            protected void doReindex(String className, long classPK) throws Exception {
167                    Contact contact = ContactLocalServiceUtil.getContact(classPK);
168    
169                    doReindex(contact);
170            }
171    
172            @Override
173            protected void doReindex(String[] ids) throws Exception {
174                    long companyId = GetterUtil.getLong(ids[0]);
175    
176                    reindexContacts(companyId);
177            }
178    
179            protected void reindexContacts(long companyId) throws PortalException {
180                    final IndexableActionableDynamicQuery indexableActionableDynamicQuery =
181                            ContactLocalServiceUtil.getIndexableActionableDynamicQuery();
182    
183                    indexableActionableDynamicQuery.setCompanyId(companyId);
184                    indexableActionableDynamicQuery.setPerformActionMethod(
185                            new ActionableDynamicQuery.PerformActionMethod<Contact>() {
186    
187                                    @Override
188                                    public void performAction(Contact contact) {
189                                            try {
190                                                    Document document = getDocument(contact);
191    
192                                                    indexableActionableDynamicQuery.addDocuments(document);
193                                            }
194                                            catch (PortalException pe) {
195                                                    if (_log.isWarnEnabled()) {
196                                                            _log.warn(
197                                                                    "Unable to index contact " +
198                                                                            contact.getContactId(),
199                                                                    pe);
200                                                    }
201                                            }
202                                    }
203    
204                            });
205                    indexableActionableDynamicQuery.setSearchEngineId(getSearchEngineId());
206    
207                    indexableActionableDynamicQuery.performActions();
208            }
209    
210            private static final Log _log = LogFactoryUtil.getLog(ContactIndexer.class);
211    
212    }