com.liferay.user.groups.admin.web.internal.search.UserGroupIndexer.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.user.groups.admin.web.internal.search.UserGroupIndexer.java

Source

/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.user.groups.admin.web.internal.search;

import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
import com.liferay.portal.kernel.dao.orm.IndexableActionableDynamicQuery;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.model.UserGroup;
import com.liferay.portal.kernel.search.BaseIndexer;
import com.liferay.portal.kernel.search.BooleanQuery;
import com.liferay.portal.kernel.search.Document;
import com.liferay.portal.kernel.search.Field;
import com.liferay.portal.kernel.search.IndexWriterHelper;
import com.liferay.portal.kernel.search.Indexer;
import com.liferay.portal.kernel.search.SearchContext;
import com.liferay.portal.kernel.search.Summary;
import com.liferay.portal.kernel.search.filter.BooleanFilter;
import com.liferay.portal.kernel.security.permission.PermissionChecker;
import com.liferay.portal.kernel.service.UserGroupLocalService;
import com.liferay.portal.kernel.service.permission.UserGroupPermission;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.user.groups.admin.constants.UserGroupsAdminPortletKeys;

import java.util.LinkedHashMap;
import java.util.Locale;

import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
 * @author Hugo Huijser
 */
@Component(immediate = true, property = {
        "javax.portlet.name=" + UserGroupsAdminPortletKeys.USER_GROUPS_ADMIN }, service = Indexer.class)
public class UserGroupIndexer extends BaseIndexer<UserGroup> {

    public static final String CLASS_NAME = UserGroup.class.getName();

    public UserGroupIndexer() {
        setDefaultSelectedFieldNames(Field.COMPANY_ID, Field.UID, Field.USER_GROUP_ID);
        setFilterSearch(true);
        setPermissionAware(true);
        setStagingAware(false);
    }

    @Override
    public String getClassName() {
        return CLASS_NAME;
    }

    @Override
    public boolean hasPermission(PermissionChecker permissionChecker, String entryClassName, long entryClassPK,
            String actionId) throws Exception {

        UserGroup userGroup = _userGroupLocalService.getUserGroup(entryClassPK);

        return _userGroupPermission.contains(permissionChecker, userGroup.getUserGroupId(), actionId);
    }

    @Override
    public void postProcessSearchQuery(BooleanQuery searchQuery, BooleanFilter fullQueryBooleanFilter,
            SearchContext searchContext) throws Exception {

        addSearchTerm(searchQuery, searchContext, "description", false);
        addSearchTerm(searchQuery, searchContext, "name", false);

        LinkedHashMap<String, Object> params = (LinkedHashMap<String, Object>) searchContext.getAttribute("params");

        if (params != null) {
            String expandoAttributes = (String) params.get("expandoAttributes");

            if (Validator.isNotNull(expandoAttributes)) {
                addSearchExpando(searchQuery, searchContext, expandoAttributes);
            }
        }
    }

    @Override
    protected void doDelete(UserGroup userGroup) throws Exception {
        deleteDocument(userGroup.getCompanyId(), userGroup.getUserGroupId());
    }

    @Override
    protected Document doGetDocument(UserGroup userGroup) throws Exception {
        Document document = getBaseModelDocument(CLASS_NAME, userGroup);

        document.addKeyword(Field.COMPANY_ID, userGroup.getCompanyId());
        document.addText(Field.DESCRIPTION, userGroup.getDescription());
        document.addText(Field.NAME, userGroup.getName());
        document.addKeyword(Field.USER_GROUP_ID, userGroup.getUserGroupId());

        return document;
    }

    @Override
    protected Summary doGetSummary(Document document, Locale locale, String snippet, PortletRequest portletRequest,
            PortletResponse portletResponse) {

        String title = document.get("name");

        String content = null;

        return new Summary(title, content);
    }

    @Override
    protected void doReindex(String className, long classPK) throws Exception {
        UserGroup userGroup = _userGroupLocalService.getUserGroup(classPK);

        doReindex(userGroup);
    }

    @Override
    protected void doReindex(String[] ids) throws Exception {
        long companyId = GetterUtil.getLong(ids[0]);

        reindexUserGroups(companyId);
    }

    @Override
    protected void doReindex(UserGroup userGroup) throws Exception {
        Document document = getDocument(userGroup);

        _indexWriterHelper.updateDocument(getSearchEngineId(), userGroup.getCompanyId(), document,
                isCommitImmediately());
    }

    protected void reindexUserGroups(long companyId) throws PortalException {
        final IndexableActionableDynamicQuery indexableActionableDynamicQuery = _userGroupLocalService
                .getIndexableActionableDynamicQuery();

        indexableActionableDynamicQuery.setCompanyId(companyId);
        indexableActionableDynamicQuery
                .setPerformActionMethod(new ActionableDynamicQuery.PerformActionMethod<UserGroup>() {

                    @Override
                    public void performAction(UserGroup userGroup) {
                        try {
                            Document document = getDocument(userGroup);

                            indexableActionableDynamicQuery.addDocuments(document);
                        } catch (PortalException pe) {
                            if (_log.isWarnEnabled()) {
                                _log.warn("Unable to index user group " + userGroup.getUserGroupId(), pe);
                            }
                        }
                    }

                });
        indexableActionableDynamicQuery.setSearchEngineId(getSearchEngineId());

        indexableActionableDynamicQuery.performActions();
    }

    private static final Log _log = LogFactoryUtil.getLog(UserGroupIndexer.class);

    @Reference
    private IndexWriterHelper _indexWriterHelper;

    @Reference
    private UserGroupLocalService _userGroupLocalService;

    @Reference
    private UserGroupPermission _userGroupPermission;

}