org/sonews/daemon/storage/Group.java
author chris <chris@marvin>
Fri, 26 Jun 2009 16:48:50 +0200
changeset 1 6fceb66e1ad7
permissions -rw-r--r--
Hooray... sonews/0.5.0 final

HG: Enter commit message. Lines beginning with 'HG:' are removed.
HG: Remove all lines to abort the collapse operation.
chris@1
     1
/*
chris@1
     2
 *   SONEWS News Server
chris@1
     3
 *   see AUTHORS for the list of contributors
chris@1
     4
 *
chris@1
     5
 *   This program is free software: you can redistribute it and/or modify
chris@1
     6
 *   it under the terms of the GNU General Public License as published by
chris@1
     7
 *   the Free Software Foundation, either version 3 of the License, or
chris@1
     8
 *   (at your option) any later version.
chris@1
     9
 *
chris@1
    10
 *   This program is distributed in the hope that it will be useful,
chris@1
    11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@1
    12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@1
    13
 *   GNU General Public License for more details.
chris@1
    14
 *
chris@1
    15
 *   You should have received a copy of the GNU General Public License
chris@1
    16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@1
    17
 */
chris@1
    18
chris@1
    19
package org.sonews.daemon.storage;
chris@1
    20
chris@1
    21
import java.sql.SQLException;
chris@1
    22
import java.util.List;
chris@1
    23
import org.sonews.util.Log;
chris@1
    24
import org.sonews.util.Pair;
chris@1
    25
chris@1
    26
/**
chris@1
    27
 * Represents a logical Group within this newsserver.
chris@1
    28
 * @author Christian Lins
chris@1
    29
 * @since sonews/0.5.0
chris@1
    30
 */
chris@1
    31
public class Group
chris@1
    32
{
chris@1
    33
chris@1
    34
  /** 
chris@1
    35
   * If this flag is set the Group is no real newsgroup but a mailing list
chris@1
    36
   * mirror. In that case every posting and receiving mails must go through
chris@1
    37
   * the mailing list gateway.
chris@1
    38
   */
chris@1
    39
  public static final int MAILINGLIST = 0x1;
chris@1
    40
  
chris@1
    41
  /**
chris@1
    42
   * If this flag is set the Group is marked as readonly and the posting
chris@1
    43
   * is prohibited. This can be useful for groups that are synced only in
chris@1
    44
   * one direction.
chris@1
    45
   */
chris@1
    46
  public static final int READONLY    = 0x2;
chris@1
    47
chris@1
    48
  /**
chris@1
    49
   * If this flag is set the Group is marked as deleted and must not occur
chris@1
    50
   * in any output. The deletion is done lazily by a low priority daemon.
chris@1
    51
   */
chris@1
    52
  public static final int DELETED     = 0x128;
chris@1
    53
  
chris@1
    54
  private long   id     = 0;
chris@1
    55
  private int    flags  = -1;
chris@1
    56
  private String name   = null;
chris@1
    57
  
chris@1
    58
  /**
chris@1
    59
   * Returns a Group identified by its full name.
chris@1
    60
   * @param name
chris@1
    61
   * @return
chris@1
    62
   */
chris@1
    63
  public static Group getByName(final String name)
chris@1
    64
  {
chris@1
    65
    try
chris@1
    66
    {
chris@1
    67
      return Database.getInstance().getGroup(name);
chris@1
    68
    }
chris@1
    69
    catch(SQLException ex)
chris@1
    70
    {
chris@1
    71
      ex.printStackTrace();
chris@1
    72
      return null;
chris@1
    73
    }
chris@1
    74
  }
chris@1
    75
chris@1
    76
  /**
chris@1
    77
   * @return List of all groups this server handles.
chris@1
    78
   */
chris@1
    79
  public static List<Group> getAll()
chris@1
    80
  {
chris@1
    81
    try
chris@1
    82
    {
chris@1
    83
      return Database.getInstance().getGroups();
chris@1
    84
    }
chris@1
    85
    catch(SQLException ex)
chris@1
    86
    {
chris@1
    87
      Log.msg(ex.getMessage(), false);
chris@1
    88
      return null;
chris@1
    89
    }
chris@1
    90
  }
chris@1
    91
  
chris@1
    92
  /**
chris@1
    93
   * Private constructor.
chris@1
    94
   * @param name
chris@1
    95
   * @param id
chris@1
    96
   */
chris@1
    97
  Group(final String name, final long id, final int flags)
chris@1
    98
  {
chris@1
    99
    this.id    = id;
chris@1
   100
    this.flags = flags;
chris@1
   101
    this.name  = name;
chris@1
   102
  }
chris@1
   103
chris@1
   104
  @Override
chris@1
   105
  public boolean equals(Object obj)
chris@1
   106
  {
chris@1
   107
    if(obj instanceof Group)
chris@1
   108
    {
chris@1
   109
      return ((Group)obj).id == this.id;
chris@1
   110
    }
chris@1
   111
    else
chris@1
   112
    {
chris@1
   113
      return false;
chris@1
   114
    }
chris@1
   115
  }
chris@1
   116
    
chris@1
   117
  public List<Pair<Long, ArticleHead>> getArticleHeads(final int first, final int last)
chris@1
   118
    throws SQLException
chris@1
   119
  {
chris@1
   120
    return Database.getInstance().getArticleHeads(this, first, last);
chris@1
   121
  }
chris@1
   122
  
chris@1
   123
  public List<Long> getArticleNumbers()
chris@1
   124
    throws SQLException
chris@1
   125
  {
chris@1
   126
    return Database.getInstance().getArticleNumbers(id);
chris@1
   127
  }
chris@1
   128
chris@1
   129
  public int getFirstArticleNumber()
chris@1
   130
    throws SQLException
chris@1
   131
  {
chris@1
   132
    return Database.getInstance().getFirstArticleNumber(this);
chris@1
   133
  }
chris@1
   134
chris@1
   135
  /**
chris@1
   136
   * Returns the group id.
chris@1
   137
   */
chris@1
   138
  public long getID()
chris@1
   139
  {
chris@1
   140
    assert id > 0;
chris@1
   141
chris@1
   142
    return id;
chris@1
   143
  }
chris@1
   144
  
chris@1
   145
  public boolean isMailingList()
chris@1
   146
  {
chris@1
   147
    return (this.flags & MAILINGLIST) != 0;
chris@1
   148
  }
chris@1
   149
chris@1
   150
  public int getLastArticleNumber()
chris@1
   151
    throws SQLException
chris@1
   152
  {
chris@1
   153
    return Database.getInstance().getLastArticleNumber(this);
chris@1
   154
  }
chris@1
   155
chris@1
   156
  public String getName()
chris@1
   157
  {
chris@1
   158
    return name;
chris@1
   159
  }
chris@1
   160
chris@1
   161
  /**
chris@1
   162
   * Performs this.flags |= flag to set a specified flag and updates the data
chris@1
   163
   * in the Database.
chris@1
   164
   * @param flag
chris@1
   165
   */
chris@1
   166
  public void setFlag(final int flag)
chris@1
   167
  {
chris@1
   168
    this.flags |= flag;
chris@1
   169
  }
chris@1
   170
chris@1
   171
  public void setName(final String name)
chris@1
   172
  {
chris@1
   173
    this.name = name;
chris@1
   174
  }
chris@1
   175
chris@1
   176
  /**
chris@1
   177
   * @return Number of posted articles in this group.
chris@1
   178
   * @throws java.sql.SQLException
chris@1
   179
   */
chris@1
   180
  public int getPostingsCount()
chris@1
   181
    throws SQLException
chris@1
   182
  {
chris@1
   183
    return Database.getInstance().getPostingsCount(this.name);
chris@1
   184
  }
chris@1
   185
chris@1
   186
}