1/**
2 * $Revision$
3 * $Date$
4 *
5 * Copyright 2003-2007 Jive Software.
6 *
7 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *     http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20package org.jivesoftware.smackx.workgroup.agent;
21
22import org.jivesoftware.smack.Connection;
23import org.jivesoftware.smack.packet.IQ;
24import org.jivesoftware.smack.packet.Packet;
25
26import java.util.Date;
27import java.util.List;
28import java.util.Map;
29
30/**
31 * A class embodying the semantic agent chat offer; specific instances allow the acceptance or
32 * rejecting of the offer.<br>
33 *
34 * @author Matt Tucker
35 * @author loki der quaeler
36 * @author Derek DeMoro
37 */
38public class Offer {
39
40    private Connection connection;
41    private AgentSession session;
42
43    private String sessionID;
44    private String userJID;
45    private String userID;
46    private String workgroupName;
47    private Date expiresDate;
48    private Map<String, List<String>> metaData;
49    private OfferContent content;
50
51    private boolean accepted = false;
52    private boolean rejected = false;
53
54    /**
55     * Creates a new offer.
56     *
57     * @param conn the XMPP connection with which the issuing session was created.
58     * @param agentSession the agent session instance through which this offer was issued.
59     * @param userID  the userID of the user from which the offer originates.
60     * @param userJID the XMPP address of the user from which the offer originates.
61     * @param workgroupName the fully qualified name of the workgroup.
62     * @param expiresDate the date at which this offer expires.
63     * @param sessionID the session id associated with the offer.
64     * @param metaData the metadata associated with the offer.
65     * @param content content of the offer. The content explains the reason for the offer
66     *        (e.g. user request, transfer)
67     */
68    Offer(Connection conn, AgentSession agentSession, String userID,
69            String userJID, String workgroupName, Date expiresDate,
70            String sessionID, Map<String, List<String>> metaData, OfferContent content)
71    {
72        this.connection = conn;
73        this.session = agentSession;
74        this.userID = userID;
75        this.userJID = userJID;
76        this.workgroupName = workgroupName;
77        this.expiresDate = expiresDate;
78        this.sessionID = sessionID;
79        this.metaData = metaData;
80        this.content = content;
81    }
82
83    /**
84     * Accepts the offer.
85     */
86    public void accept() {
87        Packet acceptPacket = new AcceptPacket(this.session.getWorkgroupJID());
88        connection.sendPacket(acceptPacket);
89        // TODO: listen for a reply.
90        accepted = true;
91    }
92
93    /**
94     * Rejects the offer.
95     */
96    public void reject() {
97        RejectPacket rejectPacket = new RejectPacket(this.session.getWorkgroupJID());
98        connection.sendPacket(rejectPacket);
99        // 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}