Home | History | Annotate | Download | only in packet
      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.packet;
     21 
     22 import org.jivesoftware.smack.packet.IQ;
     23 import org.jivesoftware.smack.provider.IQProvider;
     24 import org.xmlpull.v1.XmlPullParser;
     25 import org.xmlpull.v1.XmlPullParserException;
     26 
     27 import java.io.IOException;
     28 import java.text.ParseException;
     29 import java.text.SimpleDateFormat;
     30 import java.util.ArrayList;
     31 import java.util.Date;
     32 import java.util.List;
     33 import java.util.TimeZone;
     34 
     35 /**
     36  * An IQProvider for transcripts summaries.
     37  *
     38  * @author Gaston Dombiak
     39  */
     40 public class TranscriptsProvider implements IQProvider {
     41 
     42     private static final SimpleDateFormat UTC_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ss");
     43     static {
     44         UTC_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT+0"));
     45     }
     46 
     47     public TranscriptsProvider() {
     48         super();
     49     }
     50 
     51     public IQ parseIQ(XmlPullParser parser) throws Exception {
     52         String userID = parser.getAttributeValue("", "userID");
     53         List<Transcripts.TranscriptSummary> summaries = new ArrayList<Transcripts.TranscriptSummary>();
     54 
     55         boolean done = false;
     56         while (!done) {
     57             int eventType = parser.next();
     58             if (eventType == XmlPullParser.START_TAG) {
     59                 if (parser.getName().equals("transcript")) {
     60                     summaries.add(parseSummary(parser));
     61                 }
     62             }
     63             else if (eventType == XmlPullParser.END_TAG) {
     64                 if (parser.getName().equals("transcripts")) {
     65                     done = true;
     66                 }
     67             }
     68         }
     69 
     70         return new Transcripts(userID, summaries);
     71     }
     72 
     73     private Transcripts.TranscriptSummary parseSummary(XmlPullParser parser) throws IOException,
     74             XmlPullParserException {
     75         String sessionID =  parser.getAttributeValue("", "sessionID");
     76         Date joinTime = null;
     77         Date leftTime = null;
     78         List<Transcripts.AgentDetail> agents = new ArrayList<Transcripts.AgentDetail>();
     79 
     80         boolean done = false;
     81         while (!done) {
     82             int eventType = parser.next();
     83             if (eventType == XmlPullParser.START_TAG) {
     84                 if (parser.getName().equals("joinTime")) {
     85                     try {
     86                         joinTime = UTC_FORMAT.parse(parser.nextText());
     87                     } catch (ParseException e) {}
     88                 }
     89                 else if (parser.getName().equals("leftTime")) {
     90                     try {
     91                         leftTime = UTC_FORMAT.parse(parser.nextText());
     92                     } catch (ParseException e) {}
     93                 }
     94                 else if (parser.getName().equals("agents")) {
     95                     agents = parseAgents(parser);
     96                 }
     97             }
     98             else if (eventType == XmlPullParser.END_TAG) {
     99                 if (parser.getName().equals("transcript")) {
    100                     done = true;
    101                 }
    102             }
    103         }
    104 
    105         return new Transcripts.TranscriptSummary(sessionID, joinTime, leftTime, agents);
    106     }
    107 
    108     private List<Transcripts.AgentDetail> parseAgents(XmlPullParser parser) throws IOException, XmlPullParserException {
    109         List<Transcripts.AgentDetail> agents = new ArrayList<Transcripts.AgentDetail>();
    110         String agentJID =  null;
    111         Date joinTime = null;
    112         Date leftTime = null;
    113 
    114         boolean done = false;
    115         while (!done) {
    116             int eventType = parser.next();
    117             if (eventType == XmlPullParser.START_TAG) {
    118                 if (parser.getName().equals("agentJID")) {
    119                     agentJID = parser.nextText();
    120                 }
    121                 else if (parser.getName().equals("joinTime")) {
    122                     try {
    123                         joinTime = UTC_FORMAT.parse(parser.nextText());
    124                     } catch (ParseException e) {}
    125                 }
    126                 else if (parser.getName().equals("leftTime")) {
    127                     try {
    128                         leftTime = UTC_FORMAT.parse(parser.nextText());
    129                     } catch (ParseException e) {}
    130                 }
    131                 else if (parser.getName().equals("agent")) {
    132                     agentJID =  null;
    133                     joinTime = null;
    134                     leftTime = null;
    135                 }
    136             }
    137             else if (eventType == XmlPullParser.END_TAG) {
    138                 if (parser.getName().equals("agents")) {
    139                     done = true;
    140                 }
    141                 else if (parser.getName().equals("agent")) {
    142                     agents.add(new Transcripts.AgentDetail(agentJID, joinTime, leftTime));
    143                 }
    144             }
    145         }
    146         return agents;
    147     }
    148 }
    149