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