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.Collections;
021import java.util.Date;
022import java.util.HashSet;
023import java.util.Iterator;
024import java.util.Locale;
025import java.util.Set;
026
027import org.jivesoftware.smackx.workgroup.QueueUser;
028import org.jxmpp.jid.parts.Resourcepart;
029
030/**
031 * A queue in a workgroup, which is a pool of agents that are routed  a specific type of
032 * chat request.
033 */
034public class WorkgroupQueue {
035
036    private Resourcepart name;
037    private Status status = Status.CLOSED;
038
039    private int averageWaitTime = -1;
040    private Date oldestEntry = null;
041    private Set<QueueUser> users = Collections.emptySet();
042
043    private int maxChats = 0;
044    private int currentChats = 0;
045
046    /**
047     * Creates a new workgroup queue instance.
048     *
049     * @param name the name of the queue.
050     */
051    WorkgroupQueue(Resourcepart name) {
052        this.name = name;
053    }
054
055    /**
056     * Returns the name of the queue.
057     *
058     * @return the name of the queue.
059     */
060    public Resourcepart getName() {
061        return name;
062    }
063
064    /**
065     * Returns the status of the queue.
066     *
067     * @return the status of the queue.
068     */
069    public Status getStatus() {
070        return status;
071    }
072
073    void setStatus(Status status) {
074        this.status = status;
075    }
076
077    /**
078     * Returns the number of users waiting in the queue waiting to be routed to
079     * an agent.
080     *
081     * @return the number of users waiting in the queue.
082     */
083    public int getUserCount() {
084        if (users == null) {
085            return 0;
086        }
087        return users.size();
088    }
089
090    /**
091     * Returns an Iterator for the users in the queue waiting to be routed to
092     * an agent (QueueUser instances).
093     *
094     * @return an Iterator for the users waiting in the queue.
095     */
096    public Iterator<QueueUser> getUsers() {
097        if (users == null) {
098            return new HashSet<QueueUser>().iterator();
099        }
100        return Collections.unmodifiableSet(users).iterator();
101    }
102
103    void setUsers(Set<QueueUser> users) {
104        this.users = users;
105    }
106
107    /**
108     * Returns the average amount of time users wait in the queue before being
109     * routed to an agent. If average wait time info isn't available, -1 will
110     * be returned.
111     *
112     * @return the average wait time
113     */
114    public int getAverageWaitTime() {
115        return averageWaitTime;
116    }
117
118    void setAverageWaitTime(int averageTime) {
119        this.averageWaitTime = averageTime;
120    }
121
122    /**
123     * Returns the date of the oldest request waiting in the queue. If there
124     * are no requests waiting to be routed, this method will return <tt>null</tt>.
125     *
126     * @return the date of the oldest request in the queue.
127     */
128    public Date getOldestEntry() {
129        return oldestEntry;
130    }
131
132    void setOldestEntry(Date oldestEntry) {
133        this.oldestEntry = oldestEntry;
134    }
135
136    /**
137     * Returns the maximum number of simultaneous chats the queue can handle.
138     *
139     * @return the max number of chats the queue can handle.
140     */
141    public int getMaxChats() {
142        return maxChats;
143    }
144
145    void setMaxChats(int maxChats) {
146        this.maxChats = maxChats;
147    }
148
149    /**
150     * Returns the current number of active chat sessions in the queue.
151     *
152     * @return the current number of active chat sessions in the queue.
153     */
154    public int getCurrentChats() {
155        return currentChats;
156    }
157
158    void setCurrentChats(int currentChats) {
159        this.currentChats = currentChats;
160    }
161
162    /**
163     * A class to represent the status of the workgroup. The possible values are:
164     *
165     * <ul>
166     *      <li>WorkgroupQueue.Status.OPEN -- the queue is active and accepting new chat requests.
167     *      <li>WorkgroupQueue.Status.ACTIVE -- the queue is active but NOT accepting new chat
168     *          requests.
169     *      <li>WorkgroupQueue.Status.CLOSED -- the queue is NOT active and NOT accepting new
170     *          chat requests.
171     * </ul>
172     */
173    public static final class Status {
174
175        /**
176         * The queue is active and accepting new chat requests.
177         */
178        public static final Status OPEN = new Status("open");
179
180        /**
181         * The queue is active but NOT accepting new chat requests. This state might
182         * occur when the workgroup has closed because regular support hours have closed,
183         * but there are still several requests left in the queue.
184         */
185        public static final Status ACTIVE = new Status("active");
186
187        /**
188         * The queue is NOT active and NOT accepting new chat requests.
189         */
190        public static final Status CLOSED = new Status("closed");
191
192        /**
193         * Converts a String into the corresponding status. Valid String values
194         * that can be converted to a status are: "open", "active", and "closed".
195         *
196         * @param type the String value to covert.
197         * @return the corresponding Type.
198         */
199        public static Status fromString(String type) {
200            if (type == null) {
201                return null;
202            }
203            type = type.toLowerCase(Locale.US);
204            if (OPEN.toString().equals(type)) {
205                return OPEN;
206            }
207            else if (ACTIVE.toString().equals(type)) {
208                return ACTIVE;
209            }
210            else if (CLOSED.toString().equals(type)) {
211                return CLOSED;
212            }
213            else {
214                return null;
215            }
216        }
217
218        private String value;
219
220        private Status(String value) {
221            this.value = value;
222        }
223
224        @Override
225        public String toString() {
226            return value;
227        }
228    }
229}