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.provider.IQProvider;
022import org.xmlpull.v1.XmlPullParser;
023import org.xmlpull.v1.XmlPullParserException;
024
025import java.io.IOException;
026import java.text.ParseException;
027import java.text.SimpleDateFormat;
028import java.util.ArrayList;
029import java.util.Date;
030import java.util.List;
031import java.util.TimeZone;
032
033/**
034 * An IQProvider for transcripts summaries.
035 *
036 * @author Gaston Dombiak
037 */
038public class TranscriptsProvider implements IQProvider {
039
040    private static final SimpleDateFormat UTC_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ss");
041    static {
042        UTC_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT+0"));
043    }
044
045    public TranscriptsProvider() {
046        super();
047    }
048
049    public IQ parseIQ(XmlPullParser parser) throws Exception {
050        String userID = parser.getAttributeValue("", "userID");
051        List<Transcripts.TranscriptSummary> summaries = new ArrayList<Transcripts.TranscriptSummary>();
052
053        boolean done = false;
054        while (!done) {
055            int eventType = parser.next();
056            if (eventType == XmlPullParser.START_TAG) {
057                if (parser.getName().equals("transcript")) {
058                    summaries.add(parseSummary(parser));
059                }
060            }
061            else if (eventType == XmlPullParser.END_TAG) {
062                if (parser.getName().equals("transcripts")) {
063                    done = true;
064                }
065            }
066        }
067
068        return new Transcripts(userID, summaries);
069    }
070
071    private Transcripts.TranscriptSummary parseSummary(XmlPullParser parser) throws IOException,
072            XmlPullParserException {
073        String sessionID =  parser.getAttributeValue("", "sessionID");
074        Date joinTime = null;
075        Date leftTime = null;
076        List<Transcripts.AgentDetail> agents = new ArrayList<Transcripts.AgentDetail>();
077
078        boolean done = false;
079        while (!done) {
080            int eventType = parser.next();
081            if (eventType == XmlPullParser.START_TAG) {
082                if (parser.getName().equals("joinTime")) {
083                    try {
084                        joinTime = UTC_FORMAT.parse(parser.nextText());
085                    } catch (ParseException e) {}
086                }
087                else if (parser.getName().equals("leftTime")) {
088                    try {
089                        leftTime = UTC_FORMAT.parse(parser.nextText());
090                    } catch (ParseException e) {}
091                }
092                else if (parser.getName().equals("agents")) {
093                    agents = parseAgents(parser);
094                }
095            }
096            else if (eventType == XmlPullParser.END_TAG) {
097                if (parser.getName().equals("transcript")) {
098                    done = true;
099                }
100            }
101        }
102
103        return new Transcripts.TranscriptSummary(sessionID, joinTime, leftTime, agents);
104    }
105
106    private List<Transcripts.AgentDetail> parseAgents(XmlPullParser parser) throws IOException, XmlPullParserException {
107        List<Transcripts.AgentDetail> agents = new ArrayList<Transcripts.AgentDetail>();
108        String agentJID =  null;
109        Date joinTime = null;
110        Date leftTime = null;
111
112        boolean done = false;
113        while (!done) {
114            int eventType = parser.next();
115            if (eventType == XmlPullParser.START_TAG) {
116                if (parser.getName().equals("agentJID")) {
117                    agentJID = parser.nextText();
118                }
119                else if (parser.getName().equals("joinTime")) {
120                    try {
121                        joinTime = UTC_FORMAT.parse(parser.nextText());
122                    } catch (ParseException e) {}
123                }
124                else if (parser.getName().equals("leftTime")) {
125                    try {
126                        leftTime = UTC_FORMAT.parse(parser.nextText());
127                    } catch (ParseException e) {}
128                }
129                else if (parser.getName().equals("agent")) {
130                    agentJID =  null;
131                    joinTime = null;
132                    leftTime = null;
133                }
134            }
135            else if (eventType == XmlPullParser.END_TAG) {
136                if (parser.getName().equals("agents")) {
137                    done = true;
138                }
139                else if (parser.getName().equals("agent")) {
140                    agents.add(new Transcripts.AgentDetail(agentJID, joinTime, leftTime));
141                }
142            }
143        }
144        return agents;
145    }
146}