WorkgroupQueue.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.jivesoftware.smackx.workgroup.agent;

  18. import java.util.Collections;
  19. import java.util.Date;
  20. import java.util.HashSet;
  21. import java.util.Iterator;
  22. import java.util.Locale;
  23. import java.util.Set;

  24. import org.jivesoftware.smackx.workgroup.QueueUser;
  25. import org.jxmpp.jid.parts.Resourcepart;

  26. /**
  27.  * A queue in a workgroup, which is a pool of agents that are routed  a specific type of
  28.  * chat request.
  29.  */
  30. public class WorkgroupQueue {

  31.     private Resourcepart name;
  32.     private Status status = Status.CLOSED;

  33.     private int averageWaitTime = -1;
  34.     private Date oldestEntry = null;
  35.     private Set<QueueUser> users = Collections.emptySet();

  36.     private int maxChats = 0;
  37.     private int currentChats = 0;

  38.     /**
  39.      * Creates a new workgroup queue instance.
  40.      *
  41.      * @param name the name of the queue.
  42.      */
  43.     WorkgroupQueue(Resourcepart name) {
  44.         this.name = name;
  45.     }

  46.     /**
  47.      * Returns the name of the queue.
  48.      *
  49.      * @return the name of the queue.
  50.      */
  51.     public Resourcepart getName() {
  52.         return name;
  53.     }

  54.     /**
  55.      * Returns the status of the queue.
  56.      *
  57.      * @return the status of the queue.
  58.      */
  59.     public Status getStatus() {
  60.         return status;
  61.     }

  62.     void setStatus(Status status) {
  63.         this.status = status;
  64.     }

  65.     /**
  66.      * Returns the number of users waiting in the queue waiting to be routed to
  67.      * an agent.
  68.      *
  69.      * @return the number of users waiting in the queue.
  70.      */
  71.     public int getUserCount() {
  72.         if (users == null) {
  73.             return 0;
  74.         }
  75.         return users.size();
  76.     }

  77.     /**
  78.      * Returns an Iterator for the users in the queue waiting to be routed to
  79.      * an agent (QueueUser instances).
  80.      *
  81.      * @return an Iterator for the users waiting in the queue.
  82.      */
  83.     public Iterator<QueueUser> getUsers() {
  84.         if (users == null) {
  85.             return new HashSet<QueueUser>().iterator();
  86.         }
  87.         return Collections.unmodifiableSet(users).iterator();
  88.     }

  89.     void setUsers(Set<QueueUser> users) {
  90.         this.users = users;
  91.     }

  92.     /**
  93.      * Returns the average amount of time users wait in the queue before being
  94.      * routed to an agent. If average wait time info isn't available, -1 will
  95.      * be returned.
  96.      *
  97.      * @return the average wait time
  98.      */
  99.     public int getAverageWaitTime() {
  100.         return averageWaitTime;
  101.     }

  102.     void setAverageWaitTime(int averageTime) {
  103.         this.averageWaitTime = averageTime;
  104.     }

  105.     /**
  106.      * Returns the date of the oldest request waiting in the queue. If there
  107.      * are no requests waiting to be routed, this method will return <tt>null</tt>.
  108.      *
  109.      * @return the date of the oldest request in the queue.
  110.      */
  111.     public Date getOldestEntry() {
  112.         return oldestEntry;
  113.     }

  114.     void setOldestEntry(Date oldestEntry) {
  115.         this.oldestEntry = oldestEntry;
  116.     }

  117.     /**
  118.      * Returns the maximum number of simultaneous chats the queue can handle.
  119.      *
  120.      * @return the max number of chats the queue can handle.
  121.      */
  122.     public int getMaxChats() {
  123.         return maxChats;
  124.     }

  125.     void setMaxChats(int maxChats) {
  126.         this.maxChats = maxChats;
  127.     }

  128.     /**
  129.      * Returns the current number of active chat sessions in the queue.
  130.      *
  131.      * @return the current number of active chat sessions in the queue.
  132.      */
  133.     public int getCurrentChats() {
  134.         return currentChats;
  135.     }

  136.     void setCurrentChats(int currentChats) {
  137.         this.currentChats = currentChats;
  138.     }

  139.     /**
  140.      * A class to represent the status of the workgroup. The possible values are:
  141.      *
  142.      * <ul>
  143.      *      <li>WorkgroupQueue.Status.OPEN -- the queue is active and accepting new chat requests.
  144.      *      <li>WorkgroupQueue.Status.ACTIVE -- the queue is active but NOT accepting new chat
  145.      *          requests.
  146.      *      <li>WorkgroupQueue.Status.CLOSED -- the queue is NOT active and NOT accepting new
  147.      *          chat requests.
  148.      * </ul>
  149.      */
  150.     public static class Status {

  151.         /**
  152.          * The queue is active and accepting new chat requests.
  153.          */
  154.         public static final Status OPEN = new Status("open");

  155.         /**
  156.          * The queue is active but NOT accepting new chat requests. This state might
  157.          * occur when the workgroup has closed because regular support hours have closed,
  158.          * but there are still several requests left in the queue.
  159.          */
  160.         public static final Status ACTIVE = new Status("active");

  161.         /**
  162.          * The queue is NOT active and NOT accepting new chat requests.
  163.          */
  164.         public static final Status CLOSED = new Status("closed");

  165.         /**
  166.          * Converts a String into the corresponding status. Valid String values
  167.          * that can be converted to a status are: "open", "active", and "closed".
  168.          *
  169.          * @param type the String value to covert.
  170.          * @return the corresponding Type.
  171.          */
  172.         public static Status fromString(String type) {
  173.             if (type == null) {
  174.                 return null;
  175.             }
  176.             type = type.toLowerCase(Locale.US);
  177.             if (OPEN.toString().equals(type)) {
  178.                 return OPEN;
  179.             }
  180.             else if (ACTIVE.toString().equals(type)) {
  181.                 return ACTIVE;
  182.             }
  183.             else if (CLOSED.toString().equals(type)) {
  184.                 return CLOSED;
  185.             }
  186.             else {
  187.                 return null;
  188.             }
  189.         }

  190.         private String value;

  191.         private Status(String value) {
  192.             this.value = value;
  193.         }

  194.         public String toString() {
  195.             return value;
  196.         }
  197.     }
  198. }