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.agent;
019
020import java.util.Date;
021import java.util.List;
022import java.util.Map;
023
024import org.jivesoftware.smack.SmackException.NotConnectedException;
025import org.jivesoftware.smack.XMPPConnection;
026import org.jivesoftware.smack.packet.IQ;
027import org.jivesoftware.smack.packet.Stanza;
028
029import org.jxmpp.jid.Jid;
030
031/**
032 * A class embodying the semantic agent chat offer; specific instances allow the acceptance or
033 * rejecting of the offer.<br>
034 *
035 * @author Matt Tucker
036 * @author loki der quaeler
037 * @author Derek DeMoro
038 */
039public class Offer {
040
041    private final XMPPConnection connection;
042    private final AgentSession session;
043
044    private final String sessionID;
045    private final Jid userJID;
046    private final Jid userID;
047    private final Jid workgroupName;
048    private final Date expiresDate;
049    private final Map<String, List<String>> metaData;
050    private final OfferContent content;
051
052    private boolean accepted = false;
053    private boolean rejected = false;
054
055    /**
056     * Creates a new offer.
057     *
058     * @param conn the XMPP connection with which the issuing session was created.
059     * @param agentSession the agent session instance through which this offer was issued.
060     * @param userID  the userID of the user from which the offer originates.
061     * @param userJID the XMPP address of the user from which the offer originates.
062     * @param workgroupName the fully qualified name of the workgroup.
063     * @param sessionID the session id associated with the offer.
064     * @param metaData the metadata associated with the offer.
065     * @param content content of the offer. The content explains the reason for the offer
066     *        (e.g. user request, transfer)
067     */
068    Offer(XMPPConnection conn, AgentSession agentSession, Jid userID,
069            Jid userJID, Jid workgroupName, Date expiresDate,
070            String sessionID, Map<String, List<String>> metaData, OfferContent content)
071    {
072        this.connection = conn;
073        this.session = agentSession;
074        this.userID = userID;
075        this.userJID = userJID;
076        this.workgroupName = workgroupName;
077        this.expiresDate = expiresDate;
078        this.sessionID = sessionID;
079        this.metaData = metaData;
080        this.content = content;
081    }
082
083    /**
084     * Accepts the offer.
085     * @throws NotConnectedException 
086     * @throws InterruptedException 
087     */
088    public void accept() throws NotConnectedException, InterruptedException {
089        Stanza acceptPacket = new AcceptPacket(this.session.getWorkgroupJID());
090        connection.sendStanza(acceptPacket);
091        // TODO: listen for a reply.
092        accepted = true;
093    }
094
095    /**
096     * Rejects the offer.
097     * @throws NotConnectedException 
098     * @throws InterruptedException 
099     */
100    public void reject() throws NotConnectedException, InterruptedException {
101        RejectPacket rejectPacket = new RejectPacket(this.session.getWorkgroupJID());
102        connection.sendStanza(rejectPacket);
103        // TODO: listen for a reply.
104        rejected = true;
105    }
106
107    /**
108     * Returns the userID that the offer originates from. In most cases, the
109     * userID will simply be the JID of the requesting user. However, users can
110     * also manually specify a userID for their request. In that case, that value will
111     * be returned.
112     *
113     * @return the userID of the user from which the offer originates.
114     */
115    public Jid getUserID() {
116        return userID;
117    }
118
119    /**
120     * Returns the JID of the user that made the offer request.
121     *
122     * @return the user's JID.
123     */
124    public Jid getUserJID() {
125        return userJID;
126    }
127
128    /**
129     * The fully qualified name of the workgroup (eg support@example.com).
130     *
131     * @return the name of the workgroup.
132     */
133    public Jid getWorkgroupName() {
134        return this.workgroupName;
135    }
136
137    /**
138     * The date when the offer will expire. The agent must {@link #accept()}
139     * the offer before the expiration date or the offer will lapse and be
140     * routed to another agent. Alternatively, the agent can {@link #reject()}
141     * the offer at any time if they don't wish to accept it..
142     *
143     * @return the date at which this offer expires.
144     */
145    public Date getExpiresDate() {
146        return this.expiresDate;
147    }
148
149    /**
150     * The session ID associated with the offer.
151     *
152     * @return the session id associated with the offer.
153     */
154    public String getSessionID() {
155        return this.sessionID;
156    }
157
158    /**
159     * The meta-data associated with the offer.
160     *
161     * @return the offer meta-data.
162     */
163    public Map<String, List<String>> getMetaData() {
164        return this.metaData;
165    }
166
167    /**
168     * Returns the content of the offer. The content explains the reason for the offer
169     * (e.g. user request, transfer)
170     *
171     * @return the content of the offer.
172     */
173    public OfferContent getContent() {
174        return content;
175    }
176
177    /**
178     * Returns true if the agent accepted this offer.
179     *
180     * @return true if the agent accepted this offer.
181     */
182    public boolean isAccepted() {
183        return accepted;
184    }
185
186    /**
187     * Return true if the agent rejected this offer.
188     *
189     * @return true if the agent rejected this offer.
190     */
191    public boolean isRejected() {
192        return rejected;
193    }
194
195    /**
196     * Stanza(/Packet) for rejecting offers.
197     */
198    private class RejectPacket extends IQ {
199
200        RejectPacket(Jid workgroup) {
201            super("offer-reject", "http://jabber.org/protocol/workgroup");
202            this.setTo(workgroup);
203            this.setType(IQ.Type.set);
204        }
205
206        @Override
207        protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
208            xml.attribute("id", Offer.this.getSessionID());
209            xml.setEmptyElement();
210            return xml;
211        }
212    }
213
214    /**
215     * Stanza(/Packet) for accepting an offer.
216     */
217    private class AcceptPacket extends IQ {
218
219        AcceptPacket(Jid workgroup) {
220            super("offer-accept", "http://jabber.org/protocol/workgroup");
221            this.setTo(workgroup);
222            this.setType(IQ.Type.set);
223        }
224
225        @Override
226        protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
227            xml.attribute("id", Offer.this.getSessionID());
228            xml.setEmptyElement();
229            return xml;
230        }
231    }
232
233}