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