com.liferay.portlet.bookmarks.util.BookmarksIndexer.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.portlet.bookmarks.util.BookmarksIndexer.java

Source

/**
 * Copyright (c) 2000-2012 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.portlet.bookmarks.util;

import com.liferay.portal.kernel.search.BaseIndexer;
import com.liferay.portal.kernel.search.BooleanClauseOccur;
import com.liferay.portal.kernel.search.BooleanQuery;
import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
import com.liferay.portal.kernel.search.Document;
import com.liferay.portal.kernel.search.Field;
import com.liferay.portal.kernel.search.Indexer;
import com.liferay.portal.kernel.search.SearchContext;
import com.liferay.portal.kernel.search.SearchEngineUtil;
import com.liferay.portal.kernel.search.Summary;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.model.Group;
import com.liferay.portal.service.GroupLocalServiceUtil;
import com.liferay.portal.util.PortletKeys;
import com.liferay.portlet.bookmarks.model.BookmarksEntry;
import com.liferay.portlet.bookmarks.model.BookmarksFolder;
import com.liferay.portlet.bookmarks.model.BookmarksFolderConstants;
import com.liferay.portlet.bookmarks.service.BookmarksEntryLocalServiceUtil;
import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
import com.liferay.portlet.bookmarks.service.BookmarksFolderServiceUtil;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;

import javax.portlet.PortletURL;

/**
 * @author Brian Wing Shun Chan
 * @author Bruno Farache
 * @author Raymond Aug
 */
public class BookmarksIndexer extends BaseIndexer {

    public static final String[] CLASS_NAMES = { BookmarksEntry.class.getName() };

    public static final String PORTLET_ID = PortletKeys.BOOKMARKS;

    public String[] getClassNames() {
        return CLASS_NAMES;
    }

    public String getPortletId() {
        return PORTLET_ID;
    }

    @Override
    public boolean isPermissionAware() {
        return _PERMISSION_AWARE;
    }

    @Override
    public void postProcessContextQuery(BooleanQuery contextQuery, SearchContext searchContext) throws Exception {

        long[] folderIds = searchContext.getFolderIds();

        if ((folderIds != null) && (folderIds.length > 0)) {
            if (folderIds[0] == BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {

                return;
            }

            BooleanQuery folderIdsQuery = BooleanQueryFactoryUtil.create(searchContext);

            for (long folderId : folderIds) {
                try {
                    BookmarksFolderServiceUtil.getFolder(folderId);
                } catch (Exception e) {
                    continue;
                }

                folderIdsQuery.addTerm(Field.FOLDER_ID, folderId);
            }

            contextQuery.add(folderIdsQuery, BooleanClauseOccur.MUST);
        }
    }

    @Override
    protected void doDelete(Object obj) throws Exception {
        BookmarksEntry entry = (BookmarksEntry) obj;

        deleteDocument(entry.getCompanyId(), entry.getEntryId());
    }

    @Override
    protected Document doGetDocument(Object obj) throws Exception {
        BookmarksEntry entry = (BookmarksEntry) obj;

        Document document = getBaseModelDocument(PORTLET_ID, entry);

        document.addText(Field.DESCRIPTION, entry.getDescription());
        document.addKeyword(Field.FOLDER_ID, entry.getFolderId());
        document.addText(Field.TITLE, entry.getName());
        document.addText(Field.URL, entry.getUrl());

        return document;
    }

    @Override
    protected Summary doGetSummary(Document document, Locale locale, String snippet, PortletURL portletURL) {

        String title = document.get(Field.TITLE);

        String url = document.get(Field.URL);

        String entryId = document.get(Field.ENTRY_CLASS_PK);

        portletURL.setParameter("struts_action", "/bookmarks/view_entry");
        portletURL.setParameter("entryId", entryId);

        return new Summary(title, url, portletURL);
    }

    @Override
    protected void doReindex(Object obj) throws Exception {
        BookmarksEntry entry = (BookmarksEntry) obj;

        Document document = getDocument(entry);

        SearchEngineUtil.updateDocument(entry.getCompanyId(), document);
    }

    @Override
    protected void doReindex(String className, long classPK) throws Exception {
        BookmarksEntry entry = BookmarksEntryLocalServiceUtil.getEntry(classPK);

        doReindex(entry);
    }

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

        reindexFolders(companyId);
        reindexRoot(companyId);
    }

    @Override
    protected String getPortletId(SearchContext searchContext) {
        return PORTLET_ID;
    }

    protected void reindexEntries(long companyId, long groupId, long folderId, int entryStart, int entryEnd)
            throws Exception {

        List<BookmarksEntry> entries = BookmarksEntryLocalServiceUtil.getEntries(groupId, folderId, entryStart,
                entryEnd);

        if (entries.isEmpty()) {
            return;
        }

        Collection<Document> documents = new ArrayList<Document>();

        for (BookmarksEntry entry : entries) {
            Document document = getDocument(entry);

            documents.add(document);
        }

        SearchEngineUtil.updateDocuments(companyId, documents);
    }

    protected void reindexFolders(long companyId) throws Exception {
        int folderCount = BookmarksFolderLocalServiceUtil.getCompanyFoldersCount(companyId);

        int folderPages = folderCount / Indexer.DEFAULT_INTERVAL;

        for (int i = 0; i <= folderPages; i++) {
            int folderStart = (i * Indexer.DEFAULT_INTERVAL);
            int folderEnd = folderStart + Indexer.DEFAULT_INTERVAL;

            reindexFolders(companyId, folderStart, folderEnd);
        }
    }

    protected void reindexFolders(long companyId, int folderStart, int folderEnd) throws Exception {

        List<BookmarksFolder> folders = BookmarksFolderLocalServiceUtil.getCompanyFolders(companyId, folderStart,
                folderEnd);

        for (BookmarksFolder folder : folders) {
            long groupId = folder.getGroupId();
            long folderId = folder.getFolderId();

            int entryCount = BookmarksEntryLocalServiceUtil.getEntriesCount(groupId, folderId);

            int entryPages = entryCount / Indexer.DEFAULT_INTERVAL;

            for (int i = 0; i <= entryPages; i++) {
                int entryStart = (i * Indexer.DEFAULT_INTERVAL);
                int entryEnd = entryStart + Indexer.DEFAULT_INTERVAL;

                reindexEntries(companyId, groupId, folderId, entryStart, entryEnd);
            }
        }
    }

    protected void reindexRoot(long companyId) throws Exception {
        int groupCount = GroupLocalServiceUtil.getCompanyGroupsCount(companyId);

        int groupPages = groupCount / Indexer.DEFAULT_INTERVAL;

        for (int i = 0; i <= groupPages; i++) {
            int groupStart = (i * Indexer.DEFAULT_INTERVAL);
            int groupEnd = groupStart + Indexer.DEFAULT_INTERVAL;

            reindexRoot(companyId, groupStart, groupEnd);
        }
    }

    protected void reindexRoot(long companyId, int groupStart, int groupEnd) throws Exception {

        List<Group> groups = GroupLocalServiceUtil.getCompanyGroups(companyId, groupStart, groupEnd);

        for (Group group : groups) {
            long groupId = group.getGroupId();
            long folderId = BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID;

            int entryCount = BookmarksEntryLocalServiceUtil.getEntriesCount(groupId, folderId);

            int entryPages = entryCount / Indexer.DEFAULT_INTERVAL;

            for (int i = 0; i <= entryPages; i++) {
                int entryStart = (i * Indexer.DEFAULT_INTERVAL);
                int entryEnd = entryStart + Indexer.DEFAULT_INTERVAL;

                reindexEntries(companyId, groupId, folderId, entryStart, entryEnd);
            }
        }
    }

    private static final boolean _PERMISSION_AWARE = true;

}