Offer.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.jivesoftware.smackx.workgroup.agent;

  18. import org.jivesoftware.smack.SmackException.NotConnectedException;
  19. import org.jivesoftware.smack.XMPPConnection;
  20. import org.jivesoftware.smack.packet.IQ;
  21. import org.jivesoftware.smack.packet.Stanza;
  22. import org.jxmpp.jid.Jid;

  23. import java.util.Date;
  24. import java.util.List;
  25. import java.util.Map;

  26. /**
  27.  * A class embodying the semantic agent chat offer; specific instances allow the acceptance or
  28.  * rejecting of the offer.<br>
  29.  *
  30.  * @author Matt Tucker
  31.  * @author loki der quaeler
  32.  * @author Derek DeMoro
  33.  */
  34. public class Offer {

  35.     private XMPPConnection connection;
  36.     private AgentSession session;

  37.     private String sessionID;
  38.     private Jid userJID;
  39.     private Jid userID;
  40.     private Jid workgroupName;
  41.     private Date expiresDate;
  42.     private Map<String, List<String>> metaData;
  43.     private OfferContent content;

  44.     private boolean accepted = false;
  45.     private boolean rejected = false;

  46.     /**
  47.      * Creates a new offer.
  48.      *
  49.      * @param conn the XMPP connection with which the issuing session was created.
  50.      * @param agentSession the agent session instance through which this offer was issued.
  51.      * @param userID  the userID of the user from which the offer originates.
  52.      * @param userJID the XMPP address of the user from which the offer originates.
  53.      * @param workgroupName the fully qualified name of the workgroup.
  54.      * @param sessionID the session id associated with the offer.
  55.      * @param metaData the metadata associated with the offer.
  56.      * @param content content of the offer. The content explains the reason for the offer
  57.      *        (e.g. user request, transfer)
  58.      */
  59.     Offer(XMPPConnection conn, AgentSession agentSession, Jid userID,
  60.             Jid userJID, Jid workgroupName, Date expiresDate,
  61.             String sessionID, Map<String, List<String>> metaData, OfferContent content)
  62.     {
  63.         this.connection = conn;
  64.         this.session = agentSession;
  65.         this.userID = userID;
  66.         this.userJID = userJID;
  67.         this.workgroupName = workgroupName;
  68.         this.expiresDate = expiresDate;
  69.         this.sessionID = sessionID;
  70.         this.metaData = metaData;
  71.         this.content = content;
  72.     }

  73.     /**
  74.      * Accepts the offer.
  75.      * @throws NotConnectedException
  76.      * @throws InterruptedException
  77.      */
  78.     public void accept() throws NotConnectedException, InterruptedException {
  79.         Stanza acceptPacket = new AcceptPacket(this.session.getWorkgroupJID());
  80.         connection.sendStanza(acceptPacket);
  81.         // TODO: listen for a reply.
  82.         accepted = true;
  83.     }

  84.     /**
  85.      * Rejects the offer.
  86.      * @throws NotConnectedException
  87.      * @throws InterruptedException
  88.      */
  89.     public void reject() throws NotConnectedException, InterruptedException {
  90.         RejectPacket rejectPacket = new RejectPacket(this.session.getWorkgroupJID());
  91.         connection.sendStanza(rejectPacket);
  92.         // TODO: listen for a reply.
  93.         rejected = true;
  94.     }

  95.     /**
  96.      * Returns the userID that the offer originates from. In most cases, the
  97.      * userID will simply be the JID of the requesting user. However, users can
  98.      * also manually specify a userID for their request. In that case, that value will
  99.      * be returned.
  100.      *
  101.      * @return the userID of the user from which the offer originates.
  102.      */
  103.     public Jid getUserID() {
  104.         return userID;
  105.     }

  106.     /**
  107.      * Returns the JID of the user that made the offer request.
  108.      *
  109.      * @return the user's JID.
  110.      */
  111.     public Jid getUserJID() {
  112.         return userJID;
  113.     }

  114.     /**
  115.      * The fully qualified name of the workgroup (eg support@example.com).
  116.      *
  117.      * @return the name of the workgroup.
  118.      */
  119.     public Jid getWorkgroupName() {
  120.         return this.workgroupName;
  121.     }

  122.     /**
  123.      * The date when the offer will expire. The agent must {@link #accept()}
  124.      * the offer before the expiration date or the offer will lapse and be
  125.      * routed to another agent. Alternatively, the agent can {@link #reject()}
  126.      * the offer at any time if they don't wish to accept it..
  127.      *
  128.      * @return the date at which this offer expires.
  129.      */
  130.     public Date getExpiresDate() {
  131.         return this.expiresDate;
  132.     }

  133.     /**
  134.      * The session ID associated with the offer.
  135.      *
  136.      * @return the session id associated with the offer.
  137.      */
  138.     public String getSessionID() {
  139.         return this.sessionID;
  140.     }

  141.     /**
  142.      * The meta-data associated with the offer.
  143.      *
  144.      * @return the offer meta-data.
  145.      */
  146.     public Map<String, List<String>> getMetaData() {
  147.         return this.metaData;
  148.     }

  149.     /**
  150.      * Returns the content of the offer. The content explains the reason for the offer
  151.      * (e.g. user request, transfer)
  152.      *
  153.      * @return the content of the offer.
  154.      */
  155.     public OfferContent getContent() {
  156.         return content;
  157.     }

  158.     /**
  159.      * Returns true if the agent accepted this offer.
  160.      *
  161.      * @return true if the agent accepted this offer.
  162.      */
  163.     public boolean isAccepted() {
  164.         return accepted;
  165.     }

  166.     /**
  167.      * Return true if the agent rejected this offer.
  168.      *
  169.      * @return true if the agent rejected this offer.
  170.      */
  171.     public boolean isRejected() {
  172.         return rejected;
  173.     }

  174.     /**
  175.      * Packet for rejecting offers.
  176.      */
  177.     private class RejectPacket extends IQ {

  178.         RejectPacket(Jid workgroup) {
  179.             super("offer-reject", "http://jabber.org/protocol/workgroup");
  180.             this.setTo(workgroup);
  181.             this.setType(IQ.Type.set);
  182.         }

  183.         @Override
  184.         protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
  185.             xml.attribute("id", Offer.this.getSessionID());
  186.             xml.setEmptyElement();
  187.             return xml;
  188.         }
  189.     }

  190.     /**
  191.      * Packet for accepting an offer.
  192.      */
  193.     private class AcceptPacket extends IQ {

  194.         AcceptPacket(Jid workgroup) {
  195.             super("offer-accept", "http://jabber.org/protocol/workgroup");
  196.             this.setTo(workgroup);
  197.             this.setType(IQ.Type.set);
  198.         }

  199.         @Override
  200.         protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
  201.             xml.attribute("id", Offer.this.getSessionID());
  202.             xml.setEmptyElement();
  203.             return xml;
  204.         }
  205.     }

  206. }