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.search.Document;
018    import com.liferay.portal.kernel.search.Query;
019    import com.liferay.portal.kernel.search.Sort;
020    import com.liferay.portal.kernel.util.StringBundler;
021    
022    import java.io.Serializable;
023    
024    import java.util.Arrays;
025    import java.util.Collection;
026    
027    /**
028     * @author Bruno Farache
029     */
030    public class SearchRequest implements Serializable {
031    
032            public static SearchRequest addDocument(long companyId, Document document) {
033                    SearchRequest searchRequest = new SearchRequest(
034                            SearchEngineCommand.ADD_DOCUMENT);
035    
036                    searchRequest.setCompanyId(companyId);
037                    searchRequest.setDocument(document);
038    
039                    return searchRequest;
040            }
041    
042            public static SearchRequest addDocuments(
043                    long companyId, Collection<Document> documents) {
044    
045                    SearchRequest searchRequest = new SearchRequest(
046                            SearchEngineCommand.ADD_DOCUMENTS);
047    
048                    searchRequest.setCompanyId(companyId);
049                    searchRequest.setDocuments(documents);
050    
051                    return searchRequest;
052            }
053    
054            public static SearchRequest deleteDocument(long companyId, String uid) {
055                    SearchRequest searchRequest = new SearchRequest(
056                            SearchEngineCommand.DELETE_DOCUMENT);
057    
058                    searchRequest.setCompanyId(companyId);
059                    searchRequest.setId(uid);
060    
061                    return searchRequest;
062            }
063    
064            public static SearchRequest deleteDocuments(
065                    long companyId, Collection<String> uids) {
066    
067                    SearchRequest searchRequest = new SearchRequest(
068                            SearchEngineCommand.DELETE_DOCUMENTS);
069    
070                    searchRequest.setCompanyId(companyId);
071                    searchRequest.setIds(uids);
072    
073                    return searchRequest;
074            }
075    
076            public static SearchRequest deletePortletDocuments(
077                    long companyId, String portletId) {
078    
079                    SearchRequest searchRequest = new SearchRequest(
080                            SearchEngineCommand.DELETE_PORTLET_DOCUMENTS);
081    
082                    searchRequest.setCompanyId(companyId);
083                    searchRequest.setId(portletId);
084    
085                    return searchRequest;
086            }
087    
088            public static SearchRequest search(
089                    long companyId, Query query, Sort[] sorts, int start, int end) {
090    
091                    SearchRequest searchRequest = new SearchRequest(
092                            SearchEngineCommand.SEARCH);
093    
094                    searchRequest.setCompanyId(companyId);
095                    searchRequest.setEnd(end);
096                    searchRequest.setQuery(query);
097                    searchRequest.setSorts(sorts);
098                    searchRequest.setStart(start);
099    
100                    return searchRequest;
101            }
102    
103            public static SearchRequest updateDocument(
104                    long companyId, Document document) {
105    
106                    SearchRequest searchRequest = new SearchRequest(
107                            SearchEngineCommand.UPDATE_DOCUMENT);
108    
109                    searchRequest.setCompanyId(companyId);
110                    searchRequest.setDocument(document);
111    
112                    return searchRequest;
113            }
114    
115            public static SearchRequest updateDocuments(
116                    long companyId, Collection<Document> documents) {
117    
118                    SearchRequest searchRequest = new SearchRequest(
119                            SearchEngineCommand.UPDATE_DOCUMENTS);
120    
121                    searchRequest.setCompanyId(companyId);
122                    searchRequest.setDocuments(documents);
123    
124                    return searchRequest;
125            }
126    
127            private SearchRequest(SearchEngineCommand searchEngineCommand) {
128                    _searchEngineCommand = searchEngineCommand;
129            }
130    
131            public long getCompanyId() {
132                    return _companyId;
133            }
134    
135            public Document getDocument() {
136                    return _document;
137            }
138    
139            public Collection<Document> getDocuments() {
140                    return _documents;
141            }
142    
143            public int getEnd() {
144                    return _end;
145            }
146    
147            public String getId() {
148                    return _id;
149            }
150    
151            public Collection<String> getIds() {
152                    return _ids;
153            }
154    
155            public Query getQuery() {
156                    return _query;
157            }
158    
159            public SearchEngineCommand getSearchEngineCommand() {
160                    return _searchEngineCommand;
161            }
162    
163            public Sort[] getSorts() {
164                    return _sorts;
165            }
166    
167            public int getStart() {
168                    return _start;
169            }
170    
171            public void setCompanyId(long companyId) {
172                    _companyId = companyId;
173            }
174    
175            public void setDocument(Document document) {
176                    _document = document;
177            }
178    
179            public void setDocuments(Collection<Document> documents) {
180                    _documents = documents;
181            }
182    
183            public void setEnd(int end) {
184                    _end = end;
185            }
186    
187            public void setId(String id) {
188                    _id = id;
189            }
190    
191            public void setIds(Collection<String> ids) {
192                    _ids = ids;
193            }
194    
195            public void setQuery(Query query) {
196                    _query = query;
197            }
198    
199            public void setSorts(Sort[] sorts) {
200                    _sorts = sorts;
201            }
202    
203            public void setStart(int start) {
204                    _start = start;
205            }
206    
207            public String toString() {
208                    StringBundler sb = new StringBundler(17);
209    
210                    sb.append("{companyId=");
211                    sb.append(_companyId);
212                    sb.append(", document=");
213                    sb.append(_document);
214                    sb.append(", documents=");
215                    sb.append(_documents);
216                    sb.append(", end=");
217                    sb.append(_end);
218                    sb.append(", id=");
219                    sb.append(_id);
220                    sb.append(", ids=");
221                    sb.append(_ids);
222                    sb.append(", query=");
223                    sb.append(_query);
224                    sb.append(", searchEngineCommand=");
225                    sb.append(_searchEngineCommand);
226                    sb.append(", sorts=");
227                    sb.append(Arrays.toString(_sorts));
228                    sb.append(", start=");
229                    sb.append(_start);
230                    sb.append("}");
231    
232                    return sb.toString();
233            }
234    
235            private long _companyId;
236            private Document _document;
237            private Collection<Document> _documents;
238            private int _end;
239            private String _id;
240            private Collection<String> _ids;
241            private Query _query;
242            private SearchEngineCommand _searchEngineCommand;
243            private Sort[] _sorts;
244            private int _start;
245    
246    }