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;
025import org.jxmpp.jid.DomainBareJid;
026
027import java.util.List;
028
029/**
030 * The UserSearchManager is a facade built upon Jabber Search Services (XEP-055) to allow for searching
031 * repositories on a Jabber Server. This implementation allows for transparency of implementation of
032 * searching (DataForms or No DataForms), but allows the user to simply use the DataForm model for both
033 * types of support.
034 * <pre>
035 * XMPPConnection con = new XMPPTCPConnection("jabber.org");
036 * con.login("john", "doe");
037 * UserSearchManager search = new UserSearchManager(con, "users.jabber.org");
038 * Form searchForm = search.getSearchForm();
039 * Form answerForm = searchForm.createAnswerForm();
040 * answerForm.setAnswer("last", "DeMoro");
041 * ReportedData data = search.getSearchResults(answerForm);
042 * // Use Returned Data
043 * </pre>
044 *
045 * @author Derek DeMoro
046 */
047public class UserSearchManager {
048
049    private XMPPConnection con;
050    private UserSearch userSearch;
051
052    /**
053     * Creates a new UserSearchManager.
054     *
055     * @param con the XMPPConnection to use.
056     */
057    public UserSearchManager(XMPPConnection con) {
058        this.con = con;
059        userSearch = new UserSearch();
060    }
061
062    /**
063     * Returns the form to fill out to perform a search.
064     *
065     * @param searchService the search service to query.
066     * @return the form to fill out to perform a search.
067     * @throws XMPPErrorException 
068     * @throws NoResponseException 
069     * @throws NotConnectedException 
070     * @throws InterruptedException 
071     */
072    public Form getSearchForm(DomainBareJid searchService) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException  {
073        return userSearch.getSearchForm(con, searchService);
074    }
075
076    /**
077     * Submits a search form to the server and returns the resulting information
078     * in the form of <code>ReportedData</code>.
079     *
080     * @param searchForm    the <code>Form</code> to submit for searching.
081     * @param searchService the name of the search service to use.
082     * @return the ReportedData returned by the server.
083     * @throws XMPPErrorException 
084     * @throws NoResponseException 
085     * @throws NotConnectedException 
086     * @throws InterruptedException 
087     */
088    public ReportedData getSearchResults(Form searchForm, DomainBareJid searchService) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException  {
089        return userSearch.sendSearchForm(con, searchForm, searchService);
090    }
091
092
093    /**
094     * Returns a collection of search services found on the server.
095     *
096     * @return a Collection of search services found on the server.
097     * @throws XMPPErrorException 
098     * @throws NoResponseException 
099     * @throws NotConnectedException 
100     * @throws InterruptedException 
101     */
102    public List<DomainBareJid> getSearchServices() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException  {
103        ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(con);
104        return discoManager.findServices(UserSearch.NAMESPACE, false, false);
105    }
106}