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