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