001    /**
002     * Copyright (c) 2000-2010 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.portal.kernel.search.messaging;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.messaging.Message;
020    import com.liferay.portal.kernel.search.Document;
021    import com.liferay.portal.kernel.search.IndexWriter;
022    
023    import java.util.Collection;
024    
025    /**
026     * @author Bruno Farache
027     */
028    public class SearchWriterMessageListener
029            extends BaseSearchEngineMessageListener {
030    
031            public void receive(Message message) {
032                    try {
033                            doReceive(message);
034                    }
035                    catch (Exception e) {
036                            _log.error("Unable to process message " + message, e);
037                    }
038            }
039    
040            protected void doReceive(Message message) throws Exception {
041                    Object payload = message.getPayload();
042    
043                    if (!(payload instanceof SearchRequest)) {
044                            return;
045                    }
046    
047                    SearchRequest searchRequest = (SearchRequest)payload;
048    
049                    SearchEngineCommand searchEngineCommand =
050                            searchRequest.getSearchEngineCommand();
051    
052                    long companyId = searchRequest.getCompanyId();
053                    Document document = searchRequest.getDocument();
054                    Collection<Document> documents = searchRequest.getDocuments();
055                    String id = searchRequest.getId();
056                    Collection<String> ids = searchRequest.getIds();
057    
058                    IndexWriter indexWriter = searchEngine.getWriter();
059    
060                    if (searchEngineCommand.equals(SearchEngineCommand.ADD_DOCUMENT)) {
061                            indexWriter.addDocument(companyId, document);
062                    }
063                    else if (searchEngineCommand.equals(
064                                            SearchEngineCommand.ADD_DOCUMENTS)) {
065    
066                            indexWriter.addDocuments(companyId, documents);
067                    }
068                    else if (searchEngineCommand.equals(
069                                            SearchEngineCommand.DELETE_DOCUMENT)) {
070    
071                            indexWriter.deleteDocument(companyId, id);
072                    }
073                    else if (searchEngineCommand.equals(
074                                            SearchEngineCommand.DELETE_DOCUMENTS)) {
075    
076                            indexWriter.deleteDocuments(companyId, ids);
077                    }
078                    else if (searchEngineCommand.equals(
079                                            SearchEngineCommand.DELETE_PORTLET_DOCUMENTS)) {
080    
081                            indexWriter.deletePortletDocuments(companyId, id);
082                    }
083                    else if (searchEngineCommand.equals(
084                                            SearchEngineCommand.UPDATE_DOCUMENT)) {
085    
086                            indexWriter.updateDocument(companyId, document);
087                    }
088                    else if (searchEngineCommand.equals(
089                                            SearchEngineCommand.UPDATE_DOCUMENTS)) {
090    
091                            indexWriter.updateDocuments(companyId, documents);
092                    }
093            }
094    
095            private static Log _log = LogFactoryUtil.getLog(
096                    SearchWriterMessageListener.class);
097    
098    }