org/sonews/storage/Group.java
author cli
Thu Aug 20 14:31:19 2009 +0200 (2009-08-20)
changeset 12 bb6990c0dd1a
child 16 5a4a41cfc0a3
permissions -rw-r--r--
Merging fixes from sonews/1.0.3
chris@3
     1
/*
chris@3
     2
 *   SONEWS News Server
chris@3
     3
 *   see AUTHORS for the list of contributors
chris@3
     4
 *
chris@3
     5
 *   This program is free software: you can redistribute it and/or modify
chris@3
     6
 *   it under the terms of the GNU General Public License as published by
chris@3
     7
 *   the Free Software Foundation, either version 3 of the License, or
chris@3
     8
 *   (at your option) any later version.
chris@3
     9
 *
chris@3
    10
 *   This program is distributed in the hope that it will be useful,
chris@3
    11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@3
    12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@3
    13
 *   GNU General Public License for more details.
chris@3
    14
 *
chris@3
    15
 *   You should have received a copy of the GNU General Public License
chris@3
    16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@3
    17
 */
chris@3
    18
chris@3
    19
package org.sonews.storage;
chris@3
    20
chris@3
    21
import java.sql.SQLException;
chris@3
    22
import java.util.List;
chris@3
    23
import org.sonews.util.Log;
chris@3
    24
import org.sonews.util.Pair;
chris@3
    25
chris@3
    26
/**
chris@3
    27
 * Represents a logical Group within this newsserver.
chris@3
    28
 * @author Christian Lins
chris@3
    29
 * @since sonews/0.5.0
chris@3
    30
 */
chris@3
    31
// TODO: This class should not be public!
chris@3
    32
public class Group extends Channel
chris@3
    33
{
chris@3
    34
  
chris@3
    35
  private long   id     = 0;
chris@3
    36
  private int    flags  = -1;
chris@3
    37
  private String name   = null;
chris@3
    38
  
chris@3
    39
  /**
chris@3
    40
   * Returns a Group identified by its full name.
chris@3
    41
   * @param name
chris@3
    42
   * @return
chris@3
    43
   */
chris@3
    44
  public static Group getByName(final String name)
chris@3
    45
  {
chris@3
    46
    try
chris@3
    47
    {
chris@3
    48
      return StorageManager.current().getGroup(name);
chris@3
    49
    }
chris@3
    50
    catch(StorageBackendException ex)
chris@3
    51
    {
chris@3
    52
      ex.printStackTrace();
chris@3
    53
      return null;
chris@3
    54
    }
chris@3
    55
  }
chris@3
    56
chris@3
    57
  /**
chris@3
    58
   * @return List of all groups this server handles.
chris@3
    59
   */
chris@3
    60
  public static List<Channel> getAll()
chris@3
    61
  {
chris@3
    62
    try
chris@3
    63
    {
chris@3
    64
      return StorageManager.current().getGroups();
chris@3
    65
    }
chris@3
    66
    catch(StorageBackendException ex)
chris@3
    67
    {
chris@3
    68
      Log.msg(ex.getMessage(), false);
chris@3
    69
      return null;
chris@3
    70
    }
chris@3
    71
  }
chris@3
    72
  
chris@3
    73
  /**
chris@3
    74
   * @param name
chris@3
    75
   * @param id
chris@3
    76
   */
chris@3
    77
  public Group(final String name, final long id, final int flags)
chris@3
    78
  {
chris@3
    79
    this.id    = id;
chris@3
    80
    this.flags = flags;
chris@3
    81
    this.name  = name;
chris@3
    82
  }
chris@3
    83
chris@3
    84
  @Override
chris@3
    85
  public boolean equals(Object obj)
chris@3
    86
  {
chris@3
    87
    if(obj instanceof Group)
chris@3
    88
    {
chris@3
    89
      return ((Group)obj).id == this.id;
chris@3
    90
    }
chris@3
    91
    else
chris@3
    92
    {
chris@3
    93
      return false;
chris@3
    94
    }
chris@3
    95
  }
chris@3
    96
chris@3
    97
  public Article getArticle(long idx)
chris@3
    98
    throws StorageBackendException
chris@3
    99
  {
chris@3
   100
    return StorageManager.current().getArticle(idx, this.id);
chris@3
   101
  }
chris@3
   102
chris@3
   103
  public List<Pair<Long, ArticleHead>> getArticleHeads(final long first, final long last)
chris@3
   104
    throws StorageBackendException
chris@3
   105
  {
chris@3
   106
    return StorageManager.current().getArticleHeads(this, first, last);
chris@3
   107
  }
chris@3
   108
  
chris@3
   109
  public List<Long> getArticleNumbers()
chris@3
   110
    throws StorageBackendException
chris@3
   111
  {
chris@3
   112
    return StorageManager.current().getArticleNumbers(id);
chris@3
   113
  }
chris@3
   114
chris@3
   115
  public long getFirstArticleNumber()
chris@3
   116
    throws StorageBackendException
chris@3
   117
  {
chris@3
   118
    return StorageManager.current().getFirstArticleNumber(this);
chris@3
   119
  }
chris@3
   120
chris@3
   121
  public int getFlags()
chris@3
   122
  {
chris@3
   123
    return this.flags;
chris@3
   124
  }
chris@3
   125
chris@3
   126
  public long getIndexOf(Article art)
chris@3
   127
    throws StorageBackendException
chris@3
   128
  {
chris@3
   129
    return StorageManager.current().getArticleIndex(art, this);
chris@3
   130
  }
chris@3
   131
chris@3
   132
  /**
chris@3
   133
   * Returns the group id.
chris@3
   134
   */
chris@3
   135
  public long getInternalID()
chris@3
   136
  {
chris@3
   137
    assert id > 0;
chris@3
   138
chris@3
   139
    return id;
chris@3
   140
  }
chris@3
   141
chris@3
   142
  public boolean isDeleted()
chris@3
   143
  {
chris@3
   144
    return (this.flags & DELETED) != 0;
chris@3
   145
  }
chris@3
   146
chris@3
   147
  public boolean isMailingList()
chris@3
   148
  {
chris@3
   149
    return (this.flags & MAILINGLIST) != 0;
chris@3
   150
  }
chris@3
   151
chris@3
   152
  public boolean isWriteable()
chris@3
   153
  {
chris@3
   154
    return true;
chris@3
   155
  }
chris@3
   156
chris@3
   157
  public long getLastArticleNumber()
chris@3
   158
    throws StorageBackendException
chris@3
   159
  {
chris@3
   160
    return StorageManager.current().getLastArticleNumber(this);
chris@3
   161
  }
chris@3
   162
chris@3
   163
  public String getName()
chris@3
   164
  {
chris@3
   165
    return name;
chris@3
   166
  }
chris@3
   167
chris@3
   168
  /**
chris@3
   169
   * Performs this.flags |= flag to set a specified flag and updates the data
chris@3
   170
   * in the JDBCDatabase.
chris@3
   171
   * @param flag
chris@3
   172
   */
chris@3
   173
  public void setFlag(final int flag)
chris@3
   174
  {
chris@3
   175
    this.flags |= flag;
chris@3
   176
  }
chris@3
   177
chris@3
   178
  public void setName(final String name)
chris@3
   179
  {
chris@3
   180
    this.name = name;
chris@3
   181
  }
chris@3
   182
chris@3
   183
  /**
chris@3
   184
   * @return Number of posted articles in this group.
chris@3
   185
   * @throws java.sql.SQLException
chris@3
   186
   */
chris@3
   187
  public long getPostingsCount()
chris@3
   188
    throws StorageBackendException
chris@3
   189
  {
chris@3
   190
    return StorageManager.current().getPostingsCount(this.name);
chris@3
   191
  }
chris@3
   192
chris@3
   193
  /**
chris@3
   194
   * Updates flags and name in the backend.
chris@3
   195
   */
chris@3
   196
  public void update()
chris@3
   197
    throws StorageBackendException
chris@3
   198
  {
chris@3
   199
    StorageManager.current().update(this);
chris@3
   200
  }
chris@3
   201
chris@3
   202
}