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