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