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