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.packet;
019
020import org.jivesoftware.smack.packet.IQ;
021import org.jivesoftware.smack.packet.Packet;
022
023import java.util.ArrayList;
024import java.util.Collections;
025import java.util.Iterator;
026import java.util.List;
027
028/**
029 * Represents the conversation transcript that occured in a group chat room between an Agent
030 * and a user that requested assistance. The transcript contains all the Messages that were sent
031 * to the room as well as the sent presences. 
032 *
033 * @author Gaston Dombiak
034 */
035public class Transcript extends IQ {
036    private String sessionID;
037    private List<Packet> packets;
038
039    /**
040     * Creates a transcript request for the given sessionID.
041     *
042     * @param sessionID the id of the session to get the conversation transcript.
043     */
044    public Transcript(String sessionID) {
045        this.sessionID = sessionID;
046        this.packets = new ArrayList<Packet>();
047    }
048
049    /**
050     * Creates a new transcript for the given sessionID and list of packets. The list of packets
051     * may include Messages and/or Presences.
052     *
053     * @param sessionID the id of the session that generated this conversation transcript.
054     * @param packets the list of messages and presences send to the room.
055     */
056    public Transcript(String sessionID, List<Packet> packets) {
057        this.sessionID = sessionID;
058        this.packets = packets;
059    }
060
061    /**
062     * Returns id of the session that generated this conversation transcript. The sessionID is a
063     * value generated by the server when a new request is received.
064     *
065     * @return id of the session that generated this conversation transcript.
066     */
067    public String getSessionID() {
068        return sessionID;
069    }
070
071    /**
072     * Returns the list of Messages and Presences that were sent to the room.
073     *
074     * @return the list of Messages and Presences that were sent to the room.
075     */
076    public List<Packet> getPackets() {
077        return Collections.unmodifiableList(packets);
078    }
079
080    public String getChildElementXML() {
081        StringBuilder buf = new StringBuilder();
082
083        buf.append("<transcript xmlns=\"http://jivesoftware.com/protocol/workgroup\" sessionID=\"")
084                .append(sessionID)
085                .append("\">");
086
087        for (Iterator<Packet> it=packets.iterator(); it.hasNext();) {
088            Packet packet = it.next();
089            buf.append(packet.toXML());
090        }
091
092        buf.append("</transcript>");
093
094        return buf.toString();
095    }
096}