blob: 8a3801fcf1d98987b85930d9d1e7b4f32ff5982c [file] [log] [blame]
Shuyi Chend7955ce2013-05-22 14:51:55 -07001/**
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
20package org.jivesoftware.smackx.workgroup.agent;
21
22import org.jivesoftware.smackx.workgroup.packet.Transcript;
23import org.jivesoftware.smackx.workgroup.packet.Transcripts;
24import org.jivesoftware.smack.PacketCollector;
25import org.jivesoftware.smack.SmackConfiguration;
26import org.jivesoftware.smack.Connection;
27import org.jivesoftware.smack.XMPPException;
28import org.jivesoftware.smack.filter.PacketIDFilter;
29
30/**
31 * A TranscriptManager helps to retrieve the full conversation transcript of a given session
32 * {@link #getTranscript(String, String)} or to retrieve a list with the summary of all the
33 * conversations that a user had {@link #getTranscripts(String, String)}.
34 *
35 * @author Gaston Dombiak
36 */
37public class TranscriptManager {
38 private Connection connection;
39
40 public TranscriptManager(Connection connection) {
41 this.connection = connection;
42 }
43
44 /**
45 * Returns the full conversation transcript of a given session.
46 *
47 * @param sessionID the id of the session to get the full transcript.
48 * @param workgroupJID the JID of the workgroup that will process the request.
49 * @return the full conversation transcript of a given session.
50 * @throws XMPPException if an error occurs while getting the information.
51 */
52 public Transcript getTranscript(String workgroupJID, String sessionID) throws XMPPException {
53 Transcript request = new Transcript(sessionID);
54 request.setTo(workgroupJID);
55 PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(request.getPacketID()));
56 // Send the request
57 connection.sendPacket(request);
58
59 Transcript response = (Transcript) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
60
61 // Cancel the collector.
62 collector.cancel();
63 if (response == null) {
64 throw new XMPPException("No response from server on status set.");
65 }
66 if (response.getError() != null) {
67 throw new XMPPException(response.getError());
68 }
69 return response;
70 }
71
72 /**
73 * Returns the transcripts of a given user. The answer will contain the complete history of
74 * conversations that a user had.
75 *
76 * @param userID the id of the user to get his conversations.
77 * @param workgroupJID the JID of the workgroup that will process the request.
78 * @return the transcripts of a given user.
79 * @throws XMPPException if an error occurs while getting the information.
80 */
81 public Transcripts getTranscripts(String workgroupJID, String userID) throws XMPPException {
82 Transcripts request = new Transcripts(userID);
83 request.setTo(workgroupJID);
84 PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(request.getPacketID()));
85 // Send the request
86 connection.sendPacket(request);
87
88 Transcripts response = (Transcripts) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
89
90 // Cancel the collector.
91 collector.cancel();
92 if (response == null) {
93 throw new XMPPException("No response from server on status set.");
94 }
95 if (response.getError() != null) {
96 throw new XMPPException(response.getError());
97 }
98 return response;
99 }
100}