DiscussionHistory.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.muc;

  18. import java.util.Date;

  19. import org.jivesoftware.smackx.muc.packet.MUCInitialPresence;

  20. /**
  21.  * The DiscussionHistory class controls the number of characters or messages to receive
  22.  * when entering a room. The room will decide the amount of history to return if you don't
  23.  * specify a DiscussionHistory while joining a room.<p>
  24.  *
  25.  * You can use some or all of these variable to control the amount of history to receive:  
  26.  * <ul>
  27.  *  <li>maxchars -> total number of characters to receive in the history.
  28.  *  <li>maxstanzas -> total number of messages to receive in the history.
  29.  *  <li>seconds -> only the messages received in the last "X" seconds will be included in the
  30.  * history.
  31.  *  <li>since -> only the messages received since the datetime specified will be included in
  32.  * the history.
  33.  * </ul>
  34.  *
  35.  * Note: Setting maxchars to 0 indicates that the user requests to receive no history.
  36.  *
  37.  * @author Gaston Dombiak
  38.  */
  39. public class DiscussionHistory {

  40.     private int maxChars = -1;
  41.     private int maxStanzas = -1;
  42.     private int seconds = -1;
  43.     private Date since;

  44.     /**
  45.      * Returns the total number of characters to receive in the history.
  46.      *
  47.      * @return total number of characters to receive in the history.
  48.      */
  49.     public int getMaxChars() {
  50.         return maxChars;
  51.     }

  52.     /**
  53.      * Returns the total number of messages to receive in the history.
  54.      *
  55.      * @return the total number of messages to receive in the history.
  56.      */
  57.     public int getMaxStanzas() {
  58.         return maxStanzas;
  59.     }

  60.     /**
  61.      * Returns the number of seconds to use to filter the messages received during that time.
  62.      * In other words, only the messages received in the last "X" seconds will be included in
  63.      * the history.
  64.      *
  65.      * @return the number of seconds to use to filter the messages received during that time.
  66.      */
  67.     public int getSeconds() {
  68.         return seconds;
  69.     }

  70.     /**
  71.      * Returns the since date to use to filter the messages received during that time.
  72.      * In other words, only the messages received since the datetime specified will be
  73.      * included in the history.
  74.      *
  75.      * @return the since date to use to filter the messages received during that time.
  76.      */
  77.     public Date getSince() {
  78.         return since;
  79.     }

  80.     /**
  81.      * Sets the total number of characters to receive in the history.
  82.      *
  83.      * @param maxChars the total number of characters to receive in the history.
  84.      */
  85.     public void setMaxChars(int maxChars) {
  86.         this.maxChars = maxChars;
  87.     }

  88.     /**
  89.      * Sets the total number of messages to receive in the history.
  90.      *
  91.      * @param maxStanzas the total number of messages to receive in the history.
  92.      */
  93.     public void setMaxStanzas(int maxStanzas) {
  94.         this.maxStanzas = maxStanzas;
  95.     }

  96.     /**
  97.      * Sets the number of seconds to use to filter the messages received during that time.
  98.      * In other words, only the messages received in the last "X" seconds will be included in
  99.      * the history.
  100.      *
  101.      * @param seconds the number of seconds to use to filter the messages received during
  102.      * that time.
  103.      */
  104.     public void setSeconds(int seconds) {
  105.         this.seconds = seconds;
  106.     }

  107.     /**
  108.      * Sets the since date to use to filter the messages received during that time.
  109.      * In other words, only the messages received since the datetime specified will be
  110.      * included in the history.
  111.      *
  112.      * @param since the since date to use to filter the messages received during that time.
  113.      */
  114.     public void setSince(Date since) {
  115.         this.since = since;
  116.     }

  117.     /**
  118.      * Returns true if the history has been configured with some values.
  119.      *
  120.      * @return true if the history has been configured with some values.
  121.      */
  122.     private boolean isConfigured() {
  123.         return maxChars > -1 || maxStanzas > -1 || seconds > -1 || since != null;
  124.     }

  125.     /**
  126.      * Returns the History that manages the amount of discussion history provided on entering a
  127.      * room.
  128.      *
  129.      * @return the History that manages the amount of discussion history provided on entering a
  130.      * room.
  131.      */
  132.     MUCInitialPresence.History getMUCHistory() {
  133.         // Return null if the history was not properly configured  
  134.         if (!isConfigured()) {
  135.             return null;
  136.         }
  137.        
  138.         MUCInitialPresence.History mucHistory = new MUCInitialPresence.History();
  139.         if (maxChars > -1) {
  140.             mucHistory.setMaxChars(maxChars);
  141.         }
  142.         if (maxStanzas > -1) {
  143.             mucHistory.setMaxStanzas(maxStanzas);
  144.         }
  145.         if (seconds > -1) {
  146.             mucHistory.setSeconds(seconds);
  147.         }
  148.         if (since != null) {
  149.             mucHistory.setSince(since);
  150.         }
  151.         return mucHistory;
  152.     }
  153. }