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