TranscriptSearchManager.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.jivesoftware.smackx.workgroup.agent;

  18. import org.jivesoftware.smackx.search.ReportedData;
  19. import org.jivesoftware.smackx.workgroup.packet.TranscriptSearch;
  20. import org.jivesoftware.smackx.xdata.Form;
  21. import org.jivesoftware.smack.SmackException.NoResponseException;
  22. import org.jivesoftware.smack.SmackException.NotConnectedException;
  23. import org.jivesoftware.smack.XMPPConnection;
  24. import org.jivesoftware.smack.XMPPException.XMPPErrorException;
  25. import org.jivesoftware.smack.packet.IQ;
  26. import org.jxmpp.jid.DomainBareJid;

  27. /**
  28.  * A TranscriptSearchManager helps to retrieve the form to use for searching transcripts
  29.  * {@link #getSearchForm(DomainBareJid)} or to submit a search form and return the results of
  30.  * the search {@link #submitSearch(DomainBareJid, Form)}.
  31.  *
  32.  * @author Gaston Dombiak
  33.  */
  34. public class TranscriptSearchManager {
  35.     private XMPPConnection connection;

  36.     public TranscriptSearchManager(XMPPConnection connection) {
  37.         this.connection = connection;
  38.     }

  39.     /**
  40.      * Returns the Form to use for searching transcripts. It is unlikely that the server
  41.      * will change the form (without a restart) so it is safe to keep the returned form
  42.      * for future submissions.
  43.      *
  44.      * @param serviceJID the address of the workgroup service.
  45.      * @return the Form to use for searching transcripts.
  46.      * @throws XMPPErrorException
  47.      * @throws NoResponseException
  48.      * @throws NotConnectedException
  49.      * @throws InterruptedException
  50.      */
  51.     public Form getSearchForm(DomainBareJid serviceJID) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException  {
  52.         TranscriptSearch search = new TranscriptSearch();
  53.         search.setType(IQ.Type.get);
  54.         search.setTo(serviceJID);

  55.         TranscriptSearch response = (TranscriptSearch) connection.createPacketCollectorAndSend(
  56.                         search).nextResultOrThrow();
  57.         return Form.getFormFrom(response);
  58.     }

  59.     /**
  60.      * Submits the completed form and returns the result of the transcript search. The result
  61.      * will include all the data returned from the server so be careful with the amount of
  62.      * data that the search may return.
  63.      *
  64.      * @param serviceJID    the address of the workgroup service.
  65.      * @param completedForm the filled out search form.
  66.      * @return the result of the transcript search.
  67.      * @throws XMPPErrorException
  68.      * @throws NoResponseException
  69.      * @throws NotConnectedException
  70.      * @throws InterruptedException
  71.      */
  72.     public ReportedData submitSearch(DomainBareJid serviceJID, Form completedForm) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  73.         TranscriptSearch search = new TranscriptSearch();
  74.         search.setType(IQ.Type.get);
  75.         search.setTo(serviceJID);
  76.         search.addExtension(completedForm.getDataFormToSend());

  77.         TranscriptSearch response = (TranscriptSearch) connection.createPacketCollectorAndSend(
  78.                         search).nextResultOrThrow();
  79.         return ReportedData.getReportedDataFrom(response);
  80.     }
  81. }