001/**
002 *
003 * Copyright 2003-2007 Jive Software.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.search;
018
019import org.jivesoftware.smack.SmackException.NoResponseException;
020import org.jivesoftware.smack.SmackException.NotConnectedException;
021import org.jivesoftware.smack.XMPPConnection;
022import org.jivesoftware.smack.XMPPException.XMPPErrorException;
023import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
024import org.jivesoftware.smackx.xdata.Form;
025
026import java.util.List;
027
028/**
029 * The UserSearchManager is a facade built upon Jabber Search Services (XEP-055) to allow for searching
030 * repositories on a Jabber Server. This implementation allows for transparency of implementation of
031 * searching (DataForms or No DataForms), but allows the user to simply use the DataForm model for both
032 * types of support.
033 * <pre>
034 * XMPPConnection con = new XMPPTCPConnection("jabber.org");
035 * con.login("john", "doe");
036 * UserSearchManager search = new UserSearchManager(con, "users.jabber.org");
037 * Form searchForm = search.getSearchForm();
038 * Form answerForm = searchForm.createAnswerForm();
039 * answerForm.setAnswer("last", "DeMoro");
040 * ReportedData data = search.getSearchResults(answerForm);
041 * // Use Returned Data
042 * </pre>
043 *
044 * @author Derek DeMoro
045 */
046public class UserSearchManager {
047
048    private XMPPConnection con;
049    private UserSearch userSearch;
050
051    /**
052     * Creates a new UserSearchManager.
053     *
054     * @param con the XMPPConnection to use.
055     */
056    public UserSearchManager(XMPPConnection con) {
057        this.con = con;
058        userSearch = new UserSearch();
059    }
060
061    /**
062     * Returns the form to fill out to perform a search.
063     *
064     * @param searchService the search service to query.
065     * @return the form to fill out to perform a search.
066     * @throws XMPPErrorException 
067     * @throws NoResponseException 
068     * @throws NotConnectedException 
069     */
070    public Form getSearchForm(String searchService) throws NoResponseException, XMPPErrorException, NotConnectedException  {
071        return userSearch.getSearchForm(con, searchService);
072    }
073
074    /**
075     * Submits a search form to the server and returns the resulting information
076     * in the form of <code>ReportedData</code>
077     *
078     * @param searchForm    the <code>Form</code> to submit for searching.
079     * @param searchService the name of the search service to use.
080     * @return the ReportedData returned by the server.
081     * @throws XMPPErrorException 
082     * @throws NoResponseException 
083     * @throws NotConnectedException 
084     */
085    public ReportedData getSearchResults(Form searchForm, String searchService) throws NoResponseException, XMPPErrorException, NotConnectedException  {
086        return userSearch.sendSearchForm(con, searchForm, searchService);
087    }
088
089
090    /**
091     * Returns a collection of search services found on the server.
092     *
093     * @return a Collection of search services found on the server.
094     * @throws XMPPErrorException 
095     * @throws NoResponseException 
096     * @throws NotConnectedException 
097     */
098    public List<String> getSearchServices() throws NoResponseException, XMPPErrorException, NotConnectedException  {
099        ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(con);
100        return discoManager.findServices(UserSearch.NAMESPACE, false, false);
101    }
102}