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.usergroupsadmin.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.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.search.BaseIndexer;
022    import com.liferay.portal.kernel.search.BooleanQuery;
023    import com.liferay.portal.kernel.search.Document;
024    import com.liferay.portal.kernel.search.Field;
025    import com.liferay.portal.kernel.search.SearchContext;
026    import com.liferay.portal.kernel.search.SearchEngineUtil;
027    import com.liferay.portal.kernel.search.Summary;
028    import com.liferay.portal.kernel.search.filter.BooleanFilter;
029    import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
030    import com.liferay.portal.kernel.util.GetterUtil;
031    import com.liferay.portal.kernel.util.Validator;
032    import com.liferay.portal.model.UserGroup;
033    import com.liferay.portal.service.UserGroupLocalServiceUtil;
034    import com.liferay.portal.util.PropsValues;
035    
036    import java.util.ArrayList;
037    import java.util.Collection;
038    import java.util.HashMap;
039    import java.util.LinkedHashMap;
040    import java.util.Locale;
041    import java.util.Map;
042    
043    import javax.portlet.PortletRequest;
044    import javax.portlet.PortletResponse;
045    
046    /**
047     * @author Hugo Huijser
048     */
049    @OSGiBeanProperties
050    public class UserGroupIndexer extends BaseIndexer {
051    
052            public static final String CLASS_NAME = UserGroup.class.getName();
053    
054            public UserGroupIndexer() {
055                    setCommitImmediately(true);
056                    setDefaultSelectedFieldNames(
057                            Field.COMPANY_ID, Field.UID, Field.USER_GROUP_ID);
058                    setIndexerEnabled(PropsValues.USER_GROUPS_INDEXER_ENABLED);
059                    setPermissionAware(true);
060                    setStagingAware(false);
061            }
062    
063            @Override
064            public String getClassName() {
065                    return CLASS_NAME;
066            }
067    
068            @Override
069            public void postProcessSearchQuery(
070                            BooleanQuery searchQuery, BooleanFilter fullQueryBooleanFilter,
071                            SearchContext searchContext)
072                    throws Exception {
073    
074                    addSearchTerm(searchQuery, searchContext, "description", false);
075                    addSearchTerm(searchQuery, searchContext, "name", false);
076    
077                    LinkedHashMap<String, Object> params =
078                            (LinkedHashMap<String, Object>)searchContext.getAttribute("params");
079    
080                    if (params != null) {
081                            String expandoAttributes = (String)params.get("expandoAttributes");
082    
083                            if (Validator.isNotNull(expandoAttributes)) {
084                                    addSearchExpando(searchQuery, searchContext, expandoAttributes);
085                            }
086                    }
087            }
088    
089            @Override
090            protected void doDelete(Object obj) throws Exception {
091                    UserGroup userGroup = (UserGroup)obj;
092    
093                    deleteDocument(userGroup.getCompanyId(), userGroup.getUserGroupId());
094            }
095    
096            @Override
097            protected Document doGetDocument(Object obj) throws Exception {
098                    UserGroup userGroup = (UserGroup)obj;
099    
100                    Document document = getBaseModelDocument(CLASS_NAME, userGroup);
101    
102                    document.addKeyword(Field.COMPANY_ID, userGroup.getCompanyId());
103                    document.addText(Field.DESCRIPTION, userGroup.getDescription());
104                    document.addText(Field.NAME, userGroup.getName());
105                    document.addKeyword(Field.USER_GROUP_ID, userGroup.getUserGroupId());
106    
107                    return document;
108            }
109    
110            @Override
111            protected String doGetSortField(String orderByCol) {
112                    if (orderByCol.equals("description")) {
113                            return "description";
114                    }
115                    else if (orderByCol.equals("name")) {
116                            return "name";
117                    }
118                    else {
119                            return orderByCol;
120                    }
121            }
122    
123            @Override
124            protected Summary doGetSummary(
125                    Document document, Locale locale, String snippet,
126                    PortletRequest portletRequest, PortletResponse portletResponse) {
127    
128                    String title = document.get("name");
129    
130                    String content = null;
131    
132                    return new Summary(title, content);
133            }
134    
135            @Override
136            protected void doReindex(Object obj) throws Exception {
137                    if (obj instanceof Long) {
138                            long userGroupId = (Long)obj;
139    
140                            UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(
141                                    userGroupId);
142    
143                            doReindex(userGroup);
144                    }
145                    else if (obj instanceof long[]) {
146                            long[] userGroupIds = (long[])obj;
147    
148                            Map<Long, Collection<Document>> documentsMap = new HashMap<>();
149    
150                            for (long userGroupId : userGroupIds) {
151                                    UserGroup userGroup = UserGroupLocalServiceUtil.fetchUserGroup(
152                                            userGroupId);
153    
154                                    if (userGroup == null) {
155                                            continue;
156                                    }
157    
158                                    Document document = getDocument(userGroup);
159    
160                                    long companyId = userGroup.getCompanyId();
161    
162                                    Collection<Document> documents = documentsMap.get(companyId);
163    
164                                    if (documents == null) {
165                                            documents = new ArrayList<>();
166    
167                                            documentsMap.put(companyId, documents);
168                                    }
169    
170                                    documents.add(document);
171                            }
172    
173                            for (Map.Entry<Long, Collection<Document>> entry :
174                                            documentsMap.entrySet()) {
175    
176                                    long companyId = entry.getKey();
177                                    Collection<Document> documents = entry.getValue();
178    
179                                    SearchEngineUtil.updateDocuments(
180                                            getSearchEngineId(), companyId, documents,
181                                            isCommitImmediately());
182                            }
183                    }
184                    else if (obj instanceof UserGroup) {
185                            UserGroup userGroup = (UserGroup)obj;
186    
187                            Document document = getDocument(userGroup);
188    
189                            SearchEngineUtil.updateDocument(
190                                    getSearchEngineId(), userGroup.getCompanyId(), document,
191                                    isCommitImmediately());
192                    }
193            }
194    
195            @Override
196            protected void doReindex(String className, long classPK) throws Exception {
197                    UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(classPK);
198    
199                    doReindex(userGroup);
200            }
201    
202            @Override
203            protected void doReindex(String[] ids) throws Exception {
204                    long companyId = GetterUtil.getLong(ids[0]);
205    
206                    reindexUserGroups(companyId);
207            }
208    
209            protected void reindexUserGroups(long companyId) throws PortalException {
210                    final ActionableDynamicQuery actionableDynamicQuery =
211                            UserGroupLocalServiceUtil.getActionableDynamicQuery();
212    
213                    actionableDynamicQuery.setCompanyId(companyId);
214                    actionableDynamicQuery.setPerformActionMethod(
215                            new ActionableDynamicQuery.PerformActionMethod() {
216    
217                                    @Override
218                                    public void performAction(Object object) {
219                                            UserGroup userGroup = (UserGroup)object;
220    
221                                            try {
222                                                    Document document = getDocument(userGroup);
223    
224                                                    actionableDynamicQuery.addDocument(document);
225                                            }
226                                            catch (PortalException pe) {
227                                                    if (_log.isWarnEnabled()) {
228                                                            _log.warn(
229                                                                    "Unable to index user group " +
230                                                                            userGroup.getUserGroupId(),
231                                                                    pe);
232                                                    }
233                                            }
234                                    }
235    
236                            });
237                    actionableDynamicQuery.setSearchEngineId(getSearchEngineId());
238    
239                    actionableDynamicQuery.performActions();
240            }
241    
242            private static final Log _log = LogFactoryUtil.getLog(
243                    UserGroupIndexer.class);
244    
245    }