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 */
017
018package org.jivesoftware.smackx.workgroup.agent;
019
020import org.jivesoftware.smack.SmackException.NoResponseException;
021import org.jivesoftware.smack.SmackException.NotConnectedException;
022import org.jivesoftware.smack.XMPPConnection;
023import org.jivesoftware.smack.XMPPException.XMPPErrorException;
024import org.jivesoftware.smack.packet.IQ;
025
026import org.jivesoftware.smackx.search.ReportedData;
027import org.jivesoftware.smackx.workgroup.packet.TranscriptSearch;
028import org.jivesoftware.smackx.xdata.Form;
029
030import org.jxmpp.jid.DomainBareJid;
031
032/**
033 * A TranscriptSearchManager helps to retrieve the form to use for searching transcripts
034 * {@link #getSearchForm(DomainBareJid)} or to submit a search form and return the results of
035 * the search {@link #submitSearch(DomainBareJid, Form)}.
036 *
037 * @author Gaston Dombiak
038 */
039public class TranscriptSearchManager {
040    private final XMPPConnection connection;
041
042    public TranscriptSearchManager(XMPPConnection connection) {
043        this.connection = connection;
044    }
045
046    /**
047     * Returns the Form to use for searching transcripts. It is unlikely that the server
048     * will change the form (without a restart) so it is safe to keep the returned form
049     * for future submissions.
050     *
051     * @param serviceJID the address of the workgroup service.
052     * @return the Form to use for searching transcripts.
053     * @throws XMPPErrorException
054     * @throws NoResponseException
055     * @throws NotConnectedException
056     * @throws InterruptedException
057     */
058    public Form getSearchForm(DomainBareJid serviceJID) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException  {
059        TranscriptSearch search = new TranscriptSearch();
060        search.setType(IQ.Type.get);
061        search.setTo(serviceJID);
062
063        TranscriptSearch response = connection.createStanzaCollectorAndSend(
064                        search).nextResultOrThrow();
065        return Form.getFormFrom(response);
066    }
067
068    /**
069     * Submits the completed form and returns the result of the transcript search. The result
070     * will include all the data returned from the server so be careful with the amount of
071     * data that the search may return.
072     *
073     * @param serviceJID    the address of the workgroup service.
074     * @param completedForm the filled out search form.
075     * @return the result of the transcript search.
076     * @throws XMPPErrorException
077     * @throws NoResponseException
078     * @throws NotConnectedException
079     * @throws InterruptedException
080     */
081    public ReportedData submitSearch(DomainBareJid serviceJID, Form completedForm) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
082        TranscriptSearch search = new TranscriptSearch();
083        search.setType(IQ.Type.get);
084        search.setTo(serviceJID);
085        search.addExtension(completedForm.getDataFormToSend());
086
087        TranscriptSearch response = connection.createStanzaCollectorAndSend(
088                        search).nextResultOrThrow();
089        return ReportedData.getReportedDataFrom(response);
090    }
091}
092
093