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