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