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