001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.search.BaseIndexer;
023    import com.liferay.portal.kernel.search.BooleanQuery;
024    import com.liferay.portal.kernel.search.Document;
025    import com.liferay.portal.kernel.search.Field;
026    import com.liferay.portal.kernel.search.SearchContext;
027    import com.liferay.portal.kernel.search.SearchEngineUtil;
028    import com.liferay.portal.kernel.search.Summary;
029    import com.liferay.portal.kernel.util.GetterUtil;
030    import com.liferay.portal.kernel.util.Validator;
031    import com.liferay.portal.model.UserGroup;
032    import com.liferay.portal.security.permission.PermissionChecker;
033    import com.liferay.portal.service.UserGroupLocalServiceUtil;
034    import com.liferay.portal.service.permission.UserGroupPermissionUtil;
035    import com.liferay.portal.service.persistence.UserGroupActionableDynamicQuery;
036    import com.liferay.portal.util.PortletKeys;
037    import com.liferay.portal.util.PropsValues;
038    
039    import java.util.ArrayList;
040    import java.util.Collection;
041    import java.util.HashMap;
042    import java.util.LinkedHashMap;
043    import java.util.Locale;
044    import java.util.Map;
045    
046    import javax.portlet.PortletURL;
047    
048    /**
049     * @author Hugo Huijser
050     */
051    public class UserGroupIndexer extends BaseIndexer {
052    
053            public static final String[] CLASS_NAMES = {UserGroup.class.getName()};
054    
055            public static final String PORTLET_ID = PortletKeys.USER_GROUPS_ADMIN;
056    
057            public UserGroupIndexer() {
058                    setFilterSearch(true);
059                    setIndexerEnabled(PropsValues.USER_GROUPS_INDEXER_ENABLED);
060                    setPermissionAware(true);
061                    setStagingAware(false);
062            }
063    
064            @Override
065            public String[] getClassNames() {
066                    return CLASS_NAMES;
067            }
068    
069            @Override
070            public String getPortletId() {
071                    return PORTLET_ID;
072            }
073    
074            @Override
075            public boolean hasPermission(
076                            PermissionChecker permissionChecker, String entryClassName,
077                            long entryClassPK, String actionId)
078                    throws Exception {
079    
080                    UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(
081                            entryClassPK);
082    
083                    return UserGroupPermissionUtil.contains(
084                            permissionChecker, userGroup.getUserGroupId(), actionId);
085            }
086    
087            @Override
088            public void postProcessSearchQuery(
089                            BooleanQuery searchQuery, SearchContext searchContext)
090                    throws Exception {
091    
092                    addSearchTerm(searchQuery, searchContext, "description", false);
093                    addSearchTerm(searchQuery, searchContext, "name", false);
094    
095                    LinkedHashMap<String, Object> params =
096                            (LinkedHashMap<String, Object>)searchContext.getAttribute("params");
097    
098                    if (params != null) {
099                            String expandoAttributes = (String)params.get("expandoAttributes");
100    
101                            if (Validator.isNotNull(expandoAttributes)) {
102                                    addSearchExpando(searchQuery, searchContext, expandoAttributes);
103                            }
104                    }
105            }
106    
107            @Override
108            protected void doDelete(Object obj) throws Exception {
109                    UserGroup userGroup = (UserGroup)obj;
110    
111                    deleteDocument(userGroup.getCompanyId(), userGroup.getUserGroupId());
112            }
113    
114            @Override
115            protected Document doGetDocument(Object obj) throws Exception {
116                    UserGroup userGroup = (UserGroup)obj;
117    
118                    Document document = getBaseModelDocument(PORTLET_ID, userGroup);
119    
120                    document.addKeyword(Field.COMPANY_ID, userGroup.getCompanyId());
121                    document.addText(Field.DESCRIPTION, userGroup.getDescription());
122                    document.addText(Field.NAME, userGroup.getName());
123                    document.addKeyword(Field.USER_GROUP_ID, userGroup.getUserGroupId());
124    
125                    return document;
126            }
127    
128            @Override
129            protected String doGetSortField(String orderByCol) {
130                    if (orderByCol.equals("description")) {
131                            return "description";
132                    }
133                    else if (orderByCol.equals("name")) {
134                            return "name";
135                    }
136                    else {
137                            return orderByCol;
138                    }
139            }
140    
141            @Override
142            protected Summary doGetSummary(
143                    Document document, Locale locale, String snippet,
144                    PortletURL portletURL) {
145    
146                    String title = document.get("name");
147    
148                    String content = null;
149    
150                    String userGroupId = document.get(Field.USER_GROUP_ID);
151    
152                    portletURL.setParameter(
153                            "struts_action", "/users_admin/edit_user_group");
154                    portletURL.setParameter("userGroupId", userGroupId);
155    
156                    return new Summary(title, content, portletURL);
157            }
158    
159            @Override
160            protected void doReindex(Object obj) throws Exception {
161                    if (obj instanceof Long) {
162                            long userGroupId = (Long)obj;
163    
164                            UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(
165                                    userGroupId);
166    
167                            doReindex(userGroup);
168                    }
169                    else if (obj instanceof long[]) {
170                            long[] userGroupIds = (long[])obj;
171    
172                            Map<Long, Collection<Document>> documentsMap =
173                                    new HashMap<Long, Collection<Document>>();
174    
175                            for (long userGroupId : userGroupIds) {
176                                    UserGroup userGroup = UserGroupLocalServiceUtil.fetchUserGroup(
177                                            userGroupId);
178    
179                                    if (userGroup == null) {
180                                            continue;
181                                    }
182    
183                                    Document document = getDocument(userGroup);
184    
185                                    long companyId = userGroup.getCompanyId();
186    
187                                    Collection<Document> documents = documentsMap.get(companyId);
188    
189                                    if (documents == null) {
190                                            documents = new ArrayList<Document>();
191    
192                                            documentsMap.put(companyId, documents);
193                                    }
194    
195                                    documents.add(document);
196                            }
197    
198                            for (Map.Entry<Long, Collection<Document>> entry :
199                                            documentsMap.entrySet()) {
200    
201                                    long companyId = entry.getKey();
202                                    Collection<Document> documents = entry.getValue();
203    
204                                    SearchEngineUtil.updateDocuments(
205                                            getSearchEngineId(), companyId, documents,
206                                            isCommitImmediately());
207                            }
208                    }
209                    else if (obj instanceof UserGroup) {
210                            UserGroup userGroup = (UserGroup)obj;
211    
212                            Document document = getDocument(userGroup);
213    
214                            SearchEngineUtil.updateDocument(
215                                    getSearchEngineId(), userGroup.getCompanyId(), document,
216                                    isCommitImmediately());
217                    }
218            }
219    
220            @Override
221            protected void doReindex(String className, long classPK) throws Exception {
222                    UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(classPK);
223    
224                    doReindex(userGroup);
225            }
226    
227            @Override
228            protected void doReindex(String[] ids) throws Exception {
229                    long companyId = GetterUtil.getLong(ids[0]);
230    
231                    reindexUserGroups(companyId);
232            }
233    
234            @Override
235            protected String getPortletId(SearchContext searchContext) {
236                    return PORTLET_ID;
237            }
238    
239            protected void reindexUserGroups(long companyId)
240                    throws PortalException, SystemException {
241    
242                    ActionableDynamicQuery actionableDynamicQuery =
243                            new UserGroupActionableDynamicQuery() {
244    
245                            @Override
246                            protected void performAction(Object object) {
247                                    UserGroup userGroup = (UserGroup)object;
248    
249                                    try {
250                                            Document document = getDocument(userGroup);
251    
252                                            addDocument(document);
253                                    }
254                                    catch (PortalException pe) {
255                                            if (_log.isWarnEnabled()) {
256                                                    _log.warn(
257                                                            "Unable to index user group " +
258                                                                    userGroup.getUserGroupId(),
259                                                            pe);
260                                            }
261                                    }
262                            }
263    
264                    };
265    
266                    actionableDynamicQuery.setCompanyId(companyId);
267                    actionableDynamicQuery.setSearchEngineId(getSearchEngineId());
268    
269                    actionableDynamicQuery.performActions();
270            }
271    
272            private static Log _log = LogFactoryUtil.getLog(UserGroupIndexer.class);
273    
274    }