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