📄 userrequest.java
字号:
/**
* $RCSfile$
* $Revision: $
* $Date: $
*
* Copyright (C) 2004-2006 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution, or a commercial license
* agreement with Jive.
*/
package org.jivesoftware.xmpp.workgroup.request;
import org.jivesoftware.xmpp.workgroup.AgentSession;
import org.jivesoftware.xmpp.workgroup.RequestQueue;
import org.jivesoftware.xmpp.workgroup.UserCommunicationMethod;
import org.jivesoftware.xmpp.workgroup.Workgroup;
import org.jivesoftware.xmpp.workgroup.chatbot.ChatbotSession;
import org.jivesoftware.xmpp.workgroup.spi.WorkgroupCompatibleClient;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.QName;
import org.jivesoftware.database.DbConnectionManager;
import org.jivesoftware.util.NotFoundException;
import org.jivesoftware.util.StringUtils;
import org.xmpp.component.ComponentManagerFactory;
import org.xmpp.forms.DataForm;
import org.xmpp.forms.FormField;
import org.xmpp.packet.IQ;
import org.xmpp.packet.JID;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
* Requests made by users to get support by some agent.
*
* @author Gaston Dombiak
*/
public class UserRequest extends Request {
private static final String INSERT_SESSION =
"INSERT INTO fpSession(sessionID, userID, workgroupID, state, queueWaitTime, " +
"startTime, endTime) values(?,?,?,?,?,?,?)";
private static final String UPDATE_SESSION =
"UPDATE fpSession SET state=?, queueWaitTime=?, endTime=? WHERE sessionID=?";
private static final String INSERT_META_DATA =
"INSERT INTO fpSessionMetadata(sessionID, metadataName, metadataValue) VALUES(?,?,?)";
/**
* Flag that indicates if an invitation that was not answered was offered again to the user.
* The way in which the offer will be offered again to the user may vary according to the type
* of client used by the user. For instance, if the user is using a chatbot to join the
* workgroup then a new message may be sent to the user asking if he wants to receive a new
* offer or cancel the request.
*/
private boolean invitationChecked = false;
private JID userJID;
private String userID;
private boolean anonymousUser;
/**
* Keeps the object that represents the type of client that the user is using.
*/
private UserCommunicationMethod communicationMethod;
private RequestQueue queue = null;
private boolean savedToDB = false;
/**
* Timestamp that indicates when an invitation was sent to the user that made this request.
*/
private long invitationSent;
/**
* ID of the room where the user was invited.
*/
private String invitedRoomID;
/**
* Requests that are related to a user request. For instance, an invitation request sent to an
* agent that is related to this user request.
*/
private Queue<Request> relatedRequests = new ConcurrentLinkedQueue<Request>();
/**
* Returns an existing request given the requesting user's address and workgroup or throws
* NotFoundException if none was found.
*
* @param workgroup the workgroup that the user us trying to join.
* @param address the address to check.
* @return a request given the requesting user's address and workgroup.
* @throws org.jivesoftware.util.NotFoundException if the request could not be found.
*/
public static UserRequest getRequest(Workgroup workgroup, JID address) throws NotFoundException {
UserRequest request = null;
for (RequestQueue requestQueue : workgroup.getRequestQueues()) {
if (request == null) {
request = requestQueue.getRequest(address);
}
}
if (request == null) {
ComponentManagerFactory.getComponentManager().getLog().debug("Request not found for " +
address.toString());
throw new NotFoundException();
}
return request;
}
public UserRequest(IQ packet, Workgroup wg) {
super();
this.userJID = packet.getFrom();
this.userID = userJID.toBareJID();
this.anonymousUser = false;
this.workgroup = wg;
// Requests to join a workgroup made using an IQ are assumed to be using a Workgroup
// compatible client
this.communicationMethod = WorkgroupCompatibleClient.getInstance();
Iterator elementIter = packet.getChildElement().elementIterator();
while (elementIter.hasNext()) {
Element element = (Element)elementIter.next();
if ("queue-notifications".equals(element.getName())) {
setNotify(true);
}
else if ("user".equals(element.getName())) {
userID = element.attributeValue("id");
if (userID != null && userID.length() > 0) {
anonymousUser = !userJID.toString().equals(userID) &&
!userJID.toBareJID().equals(userID);
}
}
else if ("metadata".equals(element.getName())) {
for (Iterator i = element.elementIterator(); i.hasNext();) {
Element item = (Element)i.next();
if ("value".equals(item.getName())) {
String name = item.attributeValue("name");
if (name != null) {
metaData.put(name, Arrays.asList(item.getTextTrim()));
}
}
}
}
}
// Create metadata from submitted form.
DataForm submittedForm = (DataForm)packet.getExtension(DataForm.ELEMENT_NAME,
DataForm.NAMESPACE);
for (FormField field : submittedForm.getFields()) {
metaData.put(field.getVariable(), field.getValues());
}
// Omit certain items
metaData.remove("password");
}
/**
* Creates a new request made by a user using a chatbot as the communication media.
*
* @param session the chatbot session that holds all the information sent by the user.
* @param wg the workgroup that the user wants to join.
*/
public UserRequest(ChatbotSession session, Workgroup wg) {
super();
this.userJID = session.getUserJID();
this.userID = userJID.toBareJID();
this.anonymousUser = false;
this.workgroup = wg;
// Use the chatbot of the session as the method to communicate with the user that
// made the request
this.communicationMethod = session.getChatbot();
// Always set that users using a bot want to be notified of the position in the queue
setNotify(true);
// Use the stored attributes in the session as the metadata of the request
metaData.putAll(session.getAttributes());
// Make sure that the following keys are present in the metadata
if (!metaData.containsKey("userID")) {
metaData.put("userID", Arrays.asList(userJID.toString()));
}
}
/**
* Update the current position of the user in the queue. This will send
* the packet directly to the user.
*
* @param isPolling true if using polling mode.
*/
public void updateQueueStatus(boolean isPolling) {
try {
// Notify the user his status in the queue
communicationMethod.notifyQueueStatus(workgroup.getJID(), userJID, this, isPolling);
}
catch (Exception e) {
ComponentManagerFactory.getComponentManager().getLog().error(e);
}
}
public void checkRequest(String roomID) {
if (getInvitationSentTime() != null && !hasJoinedRoom()) {
checkInvitation(roomID);
}
for (Request request : relatedRequests) {
request.checkRequest(roomID);
}
}
public void setRequestQueue(RequestQueue queue) {
this.queue = queue;
}
public RequestQueue getRequestQueue() {
return queue;
}
/**
* Returns the ID of the room where the user was invited to have a chat with an agent or
* <tt>null</tt> if the user was never invited.
*
* @return the ID of the room where the user was invited to have a chat with an agent or
* <tt>null</tt> if the user was never invited.
*/
public String getInvitedRoomID() {
return invitedRoomID;
}
/**
* Returns the Date when an invitation to join a room was sent to the user that made this
* request. Or answer <tt>null</tt> if an invitation was never sent.
*
* @return the Date when an invitation to join a room was sent to the user that made this
* request.
*/
public Date getInvitationSentTime() {
if (invitationSent > 0) {
return new Date(invitationSent);
}
return null;
}
public int getPosition() {
int pos = -1;
if (queue != null) {
pos = queue.getPosition(this);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -