001    /**
002     * Copyright (c) 2000-2010 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.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    }