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 java.io.IOException;
021import java.text.ParseException;
022import java.text.SimpleDateFormat;
023import java.util.ArrayList;
024import java.util.Date;
025import java.util.List;
026import java.util.TimeZone;
027
028import org.jivesoftware.smack.provider.IQProvider;
029import org.jivesoftware.smack.util.ParserUtils;
030
031import org.jxmpp.jid.Jid;
032import org.xmlpull.v1.XmlPullParser;
033import org.xmlpull.v1.XmlPullParserException;
034
035/**
036 * An IQProvider for transcripts summaries.
037 *
038 * @author Gaston Dombiak
039 */
040public class TranscriptsProvider extends IQProvider<Transcripts> {
041
042    @SuppressWarnings("DateFormatConstant")
043    private static final SimpleDateFormat UTC_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ss");
044    static {
045        UTC_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT+0"));
046    }
047
048    @Override
049    public Transcripts parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
050        Jid userID = ParserUtils.getJidAttribute(parser, "userID");
051        List<Transcripts.TranscriptSummary> summaries = new ArrayList<>();
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<>();
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                        synchronized (UTC_FORMAT) {
085                            joinTime = UTC_FORMAT.parse(parser.nextText());
086                        }
087                    } catch (ParseException e) { }
088                }
089                else if (parser.getName().equals("leftTime")) {
090                    try {
091                        synchronized (UTC_FORMAT) {
092                            leftTime = UTC_FORMAT.parse(parser.nextText());
093                        }
094                    } catch (ParseException e) { }
095                }
096                else if (parser.getName().equals("agents")) {
097                    agents = parseAgents(parser);
098                }
099            }
100            else if (eventType == XmlPullParser.END_TAG) {
101                if (parser.getName().equals("transcript")) {
102                    done = true;
103                }
104            }
105        }
106
107        return new Transcripts.TranscriptSummary(sessionID, joinTime, leftTime, agents);
108    }
109
110    private List<Transcripts.AgentDetail> parseAgents(XmlPullParser parser) throws IOException, XmlPullParserException {
111        List<Transcripts.AgentDetail> agents = new ArrayList<>();
112        String agentJID =  null;
113        Date joinTime = null;
114        Date leftTime = null;
115
116        boolean done = false;
117        while (!done) {
118            int eventType = parser.next();
119            if (eventType == XmlPullParser.START_TAG) {
120                if (parser.getName().equals("agentJID")) {
121                    agentJID = parser.nextText();
122                }
123                else if (parser.getName().equals("joinTime")) {
124                    try {
125                        synchronized (UTC_FORMAT) {
126                            joinTime = UTC_FORMAT.parse(parser.nextText());
127                        }
128                    } catch (ParseException e) { }
129                }
130                else if (parser.getName().equals("leftTime")) {
131                    try {
132                        synchronized (UTC_FORMAT) {
133                            leftTime = UTC_FORMAT.parse(parser.nextText());
134                        }
135                    } catch (ParseException e) { }
136                }
137                else if (parser.getName().equals("agent")) {
138                    agentJID =  null;
139                    joinTime = null;
140                    leftTime = null;
141                }
142            }
143            else if (eventType == XmlPullParser.END_TAG) {
144                if (parser.getName().equals("agents")) {
145                    done = true;
146                }
147                else if (parser.getName().equals("agent")) {
148                    agents.add(new Transcripts.AgentDetail(agentJID, joinTime, leftTime));
149                }
150            }
151        }
152        return agents;
153    }
154}