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.Locale;
040    import java.util.Map;
041    
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                    setIndexerEnabled(PropsValues.USER_GROUPS_INDEXER_ENABLED);
055                    setPermissionAware(true);
056                    setStagingAware(false);
057            }
058    
059            @Override
060            public String[] getClassNames() {
061                    return CLASS_NAMES;
062            }
063    
064            @Override
065            public String getPortletId() {
066                    return PORTLET_ID;
067            }
068    
069            @Override
070            public void postProcessSearchQuery(
071                            BooleanQuery searchQuery, 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(PORTLET_ID, 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                    PortletURL portletURL) {
127    
128                    String title = document.get("name");
129    
130                    String content = null;
131    
132                    String userGroupId = document.get(Field.USER_GROUP_ID);
133    
134                    portletURL.setParameter(
135                            "struts_action", "/users_admin/edit_user_group");
136                    portletURL.setParameter("userGroupId", userGroupId);
137    
138                    return new Summary(title, content, portletURL);
139            }
140    
141            @Override
142            protected void doReindex(Object obj) throws Exception {
143                    if (obj instanceof Long) {
144                            long userGroupId = (Long)obj;
145    
146                            UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(
147                                    userGroupId);
148    
149                            doReindex(userGroup);
150                    }
151                    else if (obj instanceof long[]) {
152                            long[] userGroupIds = (long[])obj;
153    
154                            Map<Long, Collection<Document>> documentsMap =
155                                    new HashMap<Long, Collection<Document>>();
156    
157                            for (long userGroupId : userGroupIds) {
158                                    UserGroup userGroup = UserGroupLocalServiceUtil.fetchUserGroup(
159                                            userGroupId);
160    
161                                    if (userGroup == null) {
162                                            continue;
163                                    }
164    
165                                    Document document = getDocument(userGroup);
166    
167                                    long companyId = userGroup.getCompanyId();
168    
169                                    Collection<Document> documents = documentsMap.get(companyId);
170    
171                                    if (documents == null) {
172                                            documents = new ArrayList<Document>();
173    
174                                            documentsMap.put(companyId, documents);
175                                    }
176    
177                                    documents.add(document);
178                            }
179    
180                            for (Map.Entry<Long, Collection<Document>> entry :
181                                            documentsMap.entrySet()) {
182    
183                                    long companyId = entry.getKey();
184                                    Collection<Document> documents = entry.getValue();
185    
186                                    SearchEngineUtil.updateDocuments(
187                                            getSearchEngineId(), companyId, documents,
188                                            isCommitImmediately());
189                            }
190                    }
191                    else if (obj instanceof UserGroup) {
192                            UserGroup userGroup = (UserGroup)obj;
193    
194                            Document document = getDocument(userGroup);
195    
196                            SearchEngineUtil.updateDocument(
197                                    getSearchEngineId(), userGroup.getCompanyId(), document,
198                                    isCommitImmediately());
199                    }
200            }
201    
202            @Override
203            protected void doReindex(String className, long classPK) throws Exception {
204                    UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(classPK);
205    
206                    doReindex(userGroup);
207            }
208    
209            @Override
210            protected void doReindex(String[] ids) throws Exception {
211                    long companyId = GetterUtil.getLong(ids[0]);
212    
213                    reindexUserGroups(companyId);
214            }
215    
216            @Override
217            protected String getPortletId(SearchContext searchContext) {
218                    return PORTLET_ID;
219            }
220    
221            protected void reindexUserGroups(long companyId)
222                    throws PortalException, SystemException {
223    
224                    ActionableDynamicQuery actionableDynamicQuery =
225                            new UserGroupActionableDynamicQuery() {
226    
227                            @Override
228                            protected void performAction(Object object) throws PortalException {
229                                    UserGroup userGroup = (UserGroup)object;
230    
231                                    Document document = getDocument(userGroup);
232    
233                                    addDocument(document);
234                            }
235    
236                    };
237    
238                    actionableDynamicQuery.setCompanyId(companyId);
239                    actionableDynamicQuery.setSearchEngineId(getSearchEngineId());
240    
241                    actionableDynamicQuery.performActions();
242            }
243    
244    }