• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * $Revision$
3  * $Date$
4  *
5  * Copyright 2003-2007 Jive Software.
6  *
7  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 package org.jivesoftware.smackx.workgroup.agent;
21 
22 import org.jivesoftware.smackx.workgroup.packet.TranscriptSearch;
23 import org.jivesoftware.smack.PacketCollector;
24 import org.jivesoftware.smack.SmackConfiguration;
25 import org.jivesoftware.smack.Connection;
26 import org.jivesoftware.smack.XMPPException;
27 import org.jivesoftware.smack.filter.PacketIDFilter;
28 import org.jivesoftware.smack.packet.IQ;
29 import org.jivesoftware.smackx.Form;
30 import org.jivesoftware.smackx.ReportedData;
31 
32 /**
33  * A TranscriptSearchManager helps to retrieve the form to use for searching transcripts
34  * {@link #getSearchForm(String)} or to submit a search form and return the results of
35  * the search {@link #submitSearch(String, Form)}.
36  *
37  * @author Gaston Dombiak
38  */
39 public class TranscriptSearchManager {
40     private Connection connection;
41 
TranscriptSearchManager(Connection connection)42     public TranscriptSearchManager(Connection connection) {
43         this.connection = connection;
44     }
45 
46     /**
47      * Returns the Form to use for searching transcripts. It is unlikely that the server
48      * will change the form (without a restart) so it is safe to keep the returned form
49      * for future submissions.
50      *
51      * @param serviceJID the address of the workgroup service.
52      * @return the Form to use for searching transcripts.
53      * @throws XMPPException if an error occurs while sending the request to the server.
54      */
getSearchForm(String serviceJID)55     public Form getSearchForm(String serviceJID) throws XMPPException {
56         TranscriptSearch search = new TranscriptSearch();
57         search.setType(IQ.Type.GET);
58         search.setTo(serviceJID);
59 
60         PacketCollector collector = connection.createPacketCollector(
61                 new PacketIDFilter(search.getPacketID()));
62         connection.sendPacket(search);
63 
64         TranscriptSearch response = (TranscriptSearch) collector.nextResult(
65                 SmackConfiguration.getPacketReplyTimeout());
66 
67         // Cancel the collector.
68         collector.cancel();
69         if (response == null) {
70             throw new XMPPException("No response from server on status set.");
71         }
72         if (response.getError() != null) {
73             throw new XMPPException(response.getError());
74         }
75         return Form.getFormFrom(response);
76     }
77 
78     /**
79      * Submits the completed form and returns the result of the transcript search. The result
80      * will include all the data returned from the server so be careful with the amount of
81      * data that the search may return.
82      *
83      * @param serviceJID    the address of the workgroup service.
84      * @param completedForm the filled out search form.
85      * @return the result of the transcript search.
86      * @throws XMPPException if an error occurs while submiting the search to the server.
87      */
submitSearch(String serviceJID, Form completedForm)88     public ReportedData submitSearch(String serviceJID, Form completedForm) throws XMPPException {
89         TranscriptSearch search = new TranscriptSearch();
90         search.setType(IQ.Type.GET);
91         search.setTo(serviceJID);
92         search.addExtension(completedForm.getDataFormToSend());
93 
94         PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(search.getPacketID()));
95         connection.sendPacket(search);
96 
97         TranscriptSearch response = (TranscriptSearch) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
98 
99         // Cancel the collector.
100         collector.cancel();
101         if (response == null) {
102             throw new XMPPException("No response from server on status set.");
103         }
104         if (response.getError() != null) {
105             throw new XMPPException(response.getError());
106         }
107         return ReportedData.getReportedDataFrom(response);
108     }
109 }
110 
111 
112