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.ext.history; 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.util.ArrayList; 027import java.util.Collection; 028import java.util.Date; 029import java.util.List; 030 031/** 032 * IQ provider used to retrieve individual agent information. Each chat session can be mapped 033 * to one or more jids and therefore retrievable. 034 */ 035public class AgentChatHistory extends IQ { 036 037 /** 038 * Element name of the stanza(/packet) extension. 039 */ 040 public static final String ELEMENT_NAME = "chat-sessions"; 041 042 /** 043 * Namespace of the stanza(/packet) extension. 044 */ 045 public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup"; 046 047 private String agentJID; 048 private int maxSessions; 049 private long startDate; 050 051 private List<AgentChatSession> agentChatSessions = new ArrayList<AgentChatSession>(); 052 053 public AgentChatHistory(String agentJID, int maxSessions, Date startDate) { 054 this(); 055 this.agentJID = agentJID; 056 this.maxSessions = maxSessions; 057 this.startDate = startDate.getTime(); 058 } 059 060 public AgentChatHistory(String agentJID, int maxSessions) { 061 this(); 062 this.agentJID = agentJID; 063 this.maxSessions = maxSessions; 064 this.startDate = 0; 065 } 066 067 public AgentChatHistory() { 068 super(ELEMENT_NAME, NAMESPACE); 069 } 070 071 public void addChatSession(AgentChatSession chatSession) { 072 agentChatSessions.add(chatSession); 073 } 074 075 public Collection<AgentChatSession> getAgentChatSessions() { 076 return agentChatSessions; 077 } 078 079 @Override 080 protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) { 081 buf.append(" agentJID=\"" + agentJID + "\""); 082 buf.append(" maxSessions=\"" + maxSessions + "\""); 083 buf.append(" startDate=\"" + startDate + "\""); 084 buf.setEmptyElement(); 085 return buf; 086 } 087 088 /** 089 * Stanza(/Packet) extension provider for AgentHistory packets. 090 */ 091 public static class InternalProvider extends IQProvider<AgentChatHistory> { 092 093 @Override 094 public AgentChatHistory parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException { 095 if (parser.getEventType() != XmlPullParser.START_TAG) { 096 throw new IllegalStateException("Parser not in proper position, or bad XML."); 097 } 098 099 AgentChatHistory agentChatHistory = new AgentChatHistory(); 100 101 boolean done = false; 102 while (!done) { 103 int eventType = parser.next(); 104 if ((eventType == XmlPullParser.START_TAG) && ("chat-session".equals(parser.getName()))) { 105 agentChatHistory.addChatSession(parseChatSetting(parser)); 106 107 } 108 else if (eventType == XmlPullParser.END_TAG && ELEMENT_NAME.equals(parser.getName())) { 109 done = true; 110 } 111 } 112 return agentChatHistory; 113 } 114 115 private AgentChatSession parseChatSetting(XmlPullParser parser) throws XmlPullParserException, IOException { 116 117 boolean done = false; 118 Date date = null; 119 long duration = 0; 120 String visitorsName = null; 121 String visitorsEmail = null; 122 String sessionID = null; 123 String question = null; 124 125 while (!done) { 126 int eventType = parser.next(); 127 if ((eventType == XmlPullParser.START_TAG) && ("date".equals(parser.getName()))) { 128 String dateStr = parser.nextText(); 129 long l = Long.valueOf(dateStr).longValue(); 130 date = new Date(l); 131 } 132 else if ((eventType == XmlPullParser.START_TAG) && ("duration".equals(parser.getName()))) { 133 duration = Long.valueOf(parser.nextText()).longValue(); 134 } 135 else if ((eventType == XmlPullParser.START_TAG) && ("visitorsName".equals(parser.getName()))) { 136 visitorsName = parser.nextText(); 137 } 138 else if ((eventType == XmlPullParser.START_TAG) && ("visitorsEmail".equals(parser.getName()))) { 139 visitorsEmail = parser.nextText(); 140 } 141 else if ((eventType == XmlPullParser.START_TAG) && ("sessionID".equals(parser.getName()))) { 142 sessionID = parser.nextText(); 143 } 144 else if ((eventType == XmlPullParser.START_TAG) && ("question".equals(parser.getName()))) { 145 question = parser.nextText(); 146 } 147 else if (eventType == XmlPullParser.END_TAG && "chat-session".equals(parser.getName())) { 148 done = true; 149 } 150 } 151 return new AgentChatSession(date, duration, visitorsName, visitorsEmail, sessionID, question); 152 } 153 } 154}