com.liferay.mail.util.MessageIndexer.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.mail.util.MessageIndexer.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.mail.util;

import com.liferay.mail.model.Account;
import com.liferay.mail.model.Folder;
import com.liferay.mail.model.Message;
import com.liferay.mail.service.MessageLocalServiceUtil;
import com.liferay.mail.service.persistence.MessageActionableDynamicQuery;
import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.search.BaseIndexer;
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.DocumentImpl;
import com.liferay.portal.kernel.search.Field;
import com.liferay.portal.kernel.search.Hits;
import com.liferay.portal.kernel.search.SearchContext;
import com.liferay.portal.kernel.search.SearchEngineUtil;
import com.liferay.portal.kernel.search.SortFactoryUtil;
import com.liferay.portal.kernel.search.Summary;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.HtmlUtil;
import com.liferay.portal.util.PortletKeys;
import com.liferay.portlet.expando.model.ExpandoBridge;
import com.liferay.portlet.expando.util.ExpandoBridgeIndexerUtil;

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

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

/**
 * @author Scott Lee
 */
public class MessageIndexer extends BaseIndexer {

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

    public static final String PORTLET_ID = PortletKeys.MAIL;

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

    @Override
    public String getPortletId() {
        return PORTLET_ID;
    }

    @Override
    protected void doDelete(Object obj) throws Exception {
        SearchContext searchContext = new SearchContext();

        searchContext.setSearchEngineId(getSearchEngineId());

        if (obj instanceof Account) {
            Account account = (Account) obj;

            searchContext.setCompanyId(account.getCompanyId());
            searchContext.setEnd(QueryUtil.ALL_POS);
            searchContext.setSorts(SortFactoryUtil.getDefaultSorts());
            searchContext.setStart(QueryUtil.ALL_POS);

            BooleanQuery booleanQuery = BooleanQueryFactoryUtil.create(searchContext);

            booleanQuery.addRequiredTerm(Field.PORTLET_ID, PORTLET_ID);

            booleanQuery.addRequiredTerm("accountId", account.getAccountId());

            Hits hits = SearchEngineUtil.search(searchContext, booleanQuery);

            List<String> uids = new ArrayList<String>(hits.getLength());

            for (int i = 0; i < hits.getLength(); i++) {
                Document document = hits.doc(i);

                uids.add(document.get(Field.UID));
            }

            SearchEngineUtil.deleteDocuments(getSearchEngineId(), account.getCompanyId(), uids,
                    isCommitImmediately());
        } else if (obj instanceof Folder) {
            Folder folder = (Folder) obj;

            searchContext.setCompanyId(folder.getCompanyId());
            searchContext.setEnd(QueryUtil.ALL_POS);
            searchContext.setSorts(SortFactoryUtil.getDefaultSorts());
            searchContext.setStart(QueryUtil.ALL_POS);

            BooleanQuery booleanQuery = BooleanQueryFactoryUtil.create(searchContext);

            booleanQuery.addRequiredTerm(Field.PORTLET_ID, PORTLET_ID);

            booleanQuery.addRequiredTerm("folderId", folder.getFolderId());

            Hits hits = SearchEngineUtil.search(searchContext, booleanQuery);

            List<String> uids = new ArrayList<String>(hits.getLength());

            for (int i = 0; i < hits.getLength(); i++) {
                Document document = hits.doc(i);

                uids.add(document.get(Field.UID));
            }

            SearchEngineUtil.deleteDocuments(getSearchEngineId(), folder.getCompanyId(), uids,
                    isCommitImmediately());
        } else if (obj instanceof Message) {
            Message message = (Message) obj;

            Document document = new DocumentImpl();

            document.addUID(PORTLET_ID, message.getMessageId());

            SearchEngineUtil.deleteDocument(getSearchEngineId(), message.getCompanyId(), document.get(Field.UID),
                    isCommitImmediately());
        }
    }

    @Override
    protected Document doGetDocument(Object obj) throws Exception {
        Message message = (Message) obj;

        Document document = getBaseModelDocument(PORTLET_ID, message);

        ExpandoBridge expandoBridge = message.getExpandoBridge();

        document.addText(Field.CONTENT, HtmlUtil.extractText(message.getBody()));
        document.addKeyword(Field.FOLDER_ID, message.getFolderId());
        document.addText(Field.TITLE, message.getSubject());

        document.addKeyword("accountId", message.getAccountId());
        document.addKeyword("remoteMessageId", message.getRemoteMessageId());

        ExpandoBridgeIndexerUtil.addAttributes(document, expandoBridge);

        return document;
    }

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

        return null;
    }

    @Override
    protected void doReindex(Object obj) throws Exception {
        Message message = (Message) obj;

        Document document = getDocument(message);

        SearchEngineUtil.updateDocument(getSearchEngineId(), message.getCompanyId(), document,
                isCommitImmediately());
    }

    @Override
    protected void doReindex(String className, long classPK) throws Exception {
        Message message = MessageLocalServiceUtil.getMessage(classPK);

        doReindex(message);
    }

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

        reindexMessages(companyId);
    }

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

    protected void reindexMessages(long companyId) throws PortalException {
        ActionableDynamicQuery actionableDynamicQuery = new MessageActionableDynamicQuery() {

            @Override
            protected void performAction(Object object) throws PortalException {
                Message message = (Message) object;

                Document document = getDocument(message);

                addDocument(document);
            }

        };

        actionableDynamicQuery.setCompanyId(companyId);
        actionableDynamicQuery.setSearchEngineId(getSearchEngineId());

        actionableDynamicQuery.performActions();
    }

}