001    /**
002     * Copyright (c) 2000-2013 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.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.search.BaseIndexer;
021    import com.liferay.portal.kernel.search.BooleanQuery;
022    import com.liferay.portal.kernel.search.Document;
023    import com.liferay.portal.kernel.search.Field;
024    import com.liferay.portal.kernel.search.SearchContext;
025    import com.liferay.portal.kernel.search.SearchEngineUtil;
026    import com.liferay.portal.kernel.search.Summary;
027    import com.liferay.portal.kernel.util.GetterUtil;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.kernel.workflow.WorkflowConstants;
030    import com.liferay.portal.model.Contact;
031    import com.liferay.portal.model.User;
032    import com.liferay.portal.service.ContactLocalServiceUtil;
033    import com.liferay.portal.service.UserLocalServiceUtil;
034    import com.liferay.portal.service.persistence.ContactActionableDynamicQuery;
035    import com.liferay.portal.util.PortletKeys;
036    
037    import java.util.ArrayList;
038    import java.util.Collection;
039    import java.util.LinkedHashMap;
040    import java.util.Locale;
041    
042    import javax.portlet.PortletURL;
043    
044    /**
045     * @author Raymond Aug??
046     * @author Zsigmond Rab
047     * @author Hugo Huijser
048     */
049    public class ContactIndexer extends BaseIndexer {
050    
051            public static final String[] CLASS_NAMES = {Contact.class.getName()};
052    
053            public static final String PORTLET_ID = PortletKeys.USERS_ADMIN;
054    
055            public ContactIndexer() {
056                    setStagingAware(false);
057            }
058    
059            @Override
060            public String[] getClassNames() {
061                    return CLASS_NAMES;
062            }
063    
064            @Override
065            public String getPortletId() {
066                    return PORTLET_ID;
067            }
068    
069            @Override
070            public void postProcessSearchQuery(
071                            BooleanQuery searchQuery, SearchContext searchContext)
072                    throws Exception {
073    
074                    addSearchTerm(searchQuery, searchContext, "city", false);
075                    addSearchTerm(searchQuery, searchContext, "country", false);
076                    addSearchTerm(searchQuery, searchContext, "emailAddress", false);
077                    addSearchTerm(searchQuery, searchContext, "firstName", false);
078                    addSearchTerm(searchQuery, searchContext, "fullName", false);
079                    addSearchTerm(searchQuery, searchContext, "lastName", false);
080                    addSearchTerm(searchQuery, searchContext, "middleName", false);
081                    addSearchTerm(searchQuery, searchContext, "region", false);
082                    addSearchTerm(searchQuery, searchContext, "screenName", false);
083                    addSearchTerm(searchQuery, searchContext, "street", false);
084                    addSearchTerm(searchQuery, searchContext, "zip", false);
085    
086                    LinkedHashMap<String, Object> params =
087                            (LinkedHashMap<String, Object>)searchContext.getAttribute("params");
088    
089                    if (params != null) {
090                            String expandoAttributes = (String)params.get("expandoAttributes");
091    
092                            if (Validator.isNotNull(expandoAttributes)) {
093                                    addSearchExpando(searchQuery, searchContext, expandoAttributes);
094                            }
095                    }
096            }
097    
098            @Override
099            protected void doDelete(Object obj) throws Exception {
100                    Contact contact = (Contact)obj;
101    
102                    deleteDocument(contact.getCompanyId(), contact.getContactId());
103            }
104    
105            @Override
106            protected Document doGetDocument(Object obj) throws Exception {
107                    Contact contact = (Contact)obj;
108    
109                    if (contact.isUser()) {
110                            User user = UserLocalServiceUtil.getUserByContactId(
111                                    contact.getContactId());
112    
113                            if (user.isDefaultUser() ||
114                                    (user.getStatus() != WorkflowConstants.STATUS_APPROVED)) {
115    
116                                    return null;
117                            }
118                    }
119    
120                    Document document = getBaseModelDocument(PORTLET_ID, contact);
121    
122                    document.addKeyword(Field.COMPANY_ID, contact.getCompanyId());
123                    document.addDate(Field.MODIFIED_DATE, contact.getModifiedDate());
124                    document.addKeyword(Field.USER_ID, contact.getUserId());
125                    document.addKeyword(Field.USER_NAME, contact.getFullName());
126    
127                    document.addText("emailAddress", contact.getEmailAddress());
128                    document.addText("firstName", contact.getFirstName());
129                    document.addText("fullName", contact.getFullName());
130                    document.addText("jobTitle", contact.getJobTitle());
131                    document.addText("lastName", contact.getLastName());
132                    document.addText("middleName", contact.getMiddleName());
133    
134                    return document;
135            }
136    
137            @Override
138            protected String doGetSortField(String orderByCol) {
139                    if (orderByCol.equals("email-address")) {
140                            return "emailAddress";
141                    }
142                    else if (orderByCol.equals("first-name")) {
143                            return "firstName";
144                    }
145                    else if (orderByCol.equals("job-title")) {
146                            return "jobTitle";
147                    }
148                    else if (orderByCol.equals("last-name")) {
149                            return "lastName";
150                    }
151                    else {
152                            return orderByCol;
153                    }
154            }
155    
156            @Override
157            protected Summary doGetSummary(
158                    Document document, Locale locale, String snippet,
159                    PortletURL portletURL) {
160    
161                    return null;
162            }
163    
164            @Override
165            protected void doReindex(Object obj) throws Exception {
166                    Contact contact = (Contact)obj;
167    
168                    Document document = getDocument(contact);
169    
170                    if (document != null) {
171                            SearchEngineUtil.updateDocument(
172                                    getSearchEngineId(), contact.getCompanyId(), document);
173                    }
174            }
175    
176            @Override
177            protected void doReindex(String className, long classPK) throws Exception {
178                    Contact contact = ContactLocalServiceUtil.getContact(classPK);
179    
180                    doReindex(contact);
181            }
182    
183            @Override
184            protected void doReindex(String[] ids) throws Exception {
185                    long companyId = GetterUtil.getLong(ids[0]);
186    
187                    reindexContacts(companyId);
188            }
189    
190            @Override
191            protected String getPortletId(SearchContext searchContext) {
192                    return PORTLET_ID;
193            }
194    
195            protected void reindexContacts(long companyId)
196                    throws PortalException, SystemException {
197    
198                    final Collection<Document> documents = new ArrayList<Document>();
199    
200                    ActionableDynamicQuery actionableDynamicQuery =
201                            new ContactActionableDynamicQuery() {
202    
203                            @Override
204                            protected void performAction(Object object) throws PortalException {
205                                    Contact contact = (Contact)object;
206    
207                                    Document document = getDocument(contact);
208    
209                                    if (document != null) {
210                                            documents.add(document);
211                                    }
212                            }
213    
214                    };
215    
216                    actionableDynamicQuery.setCompanyId(companyId);
217    
218                    actionableDynamicQuery.performActions();
219    
220                    SearchEngineUtil.updateDocuments(
221                            getSearchEngineId(), companyId, documents);
222            }
223    
224    }