src/org/sonews/storage/Group.java
author cli
Sun Aug 29 17:43:58 2010 +0200 (2010-08-29)
changeset 36 c404a87db5b7
parent 31 087ef6fe6a1a
child 37 74139325d305
permissions -rw-r--r--
Add some checks to prevent #13 happen.
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
   * @return List of all groups this server handles.
chris@3
    41
   */
chris@3
    42
  public static List<Channel> getAll()
chris@3
    43
  {
chris@3
    44
    try
chris@3
    45
    {
chris@3
    46
      return StorageManager.current().getGroups();
chris@3
    47
    }
chris@3
    48
    catch(StorageBackendException ex)
chris@3
    49
    {
cli@16
    50
      Log.get().severe(ex.getMessage());
chris@3
    51
      return null;
chris@3
    52
    }
chris@3
    53
  }
chris@3
    54
  
chris@3
    55
  /**
chris@3
    56
   * @param name
chris@3
    57
   * @param id
chris@3
    58
   */
chris@3
    59
  public Group(final String name, final long id, final int flags)
chris@3
    60
  {
chris@3
    61
    this.id    = id;
chris@3
    62
    this.flags = flags;
chris@3
    63
    this.name  = name;
chris@3
    64
  }
chris@3
    65
chris@3
    66
  @Override
chris@3
    67
  public boolean equals(Object obj)
chris@3
    68
  {
chris@3
    69
    if(obj instanceof Group)
chris@3
    70
    {
chris@3
    71
      return ((Group)obj).id == this.id;
chris@3
    72
    }
chris@3
    73
    else
chris@3
    74
    {
chris@3
    75
      return false;
chris@3
    76
    }
chris@3
    77
  }
chris@3
    78
chris@3
    79
  public Article getArticle(long idx)
chris@3
    80
    throws StorageBackendException
chris@3
    81
  {
chris@3
    82
    return StorageManager.current().getArticle(idx, this.id);
chris@3
    83
  }
chris@3
    84
chris@3
    85
  public List<Pair<Long, ArticleHead>> getArticleHeads(final long first, final long last)
chris@3
    86
    throws StorageBackendException
chris@3
    87
  {
chris@3
    88
    return StorageManager.current().getArticleHeads(this, first, last);
chris@3
    89
  }
chris@3
    90
  
chris@3
    91
  public List<Long> getArticleNumbers()
chris@3
    92
    throws StorageBackendException
chris@3
    93
  {
chris@3
    94
    return StorageManager.current().getArticleNumbers(id);
chris@3
    95
  }
chris@3
    96
chris@3
    97
  public long getFirstArticleNumber()
chris@3
    98
    throws StorageBackendException
chris@3
    99
  {
chris@3
   100
    return StorageManager.current().getFirstArticleNumber(this);
chris@3
   101
  }
chris@3
   102
chris@3
   103
  public int getFlags()
chris@3
   104
  {
chris@3
   105
    return this.flags;
chris@3
   106
  }
chris@3
   107
chris@3
   108
  public long getIndexOf(Article art)
chris@3
   109
    throws StorageBackendException
chris@3
   110
  {
chris@3
   111
    return StorageManager.current().getArticleIndex(art, this);
chris@3
   112
  }
chris@3
   113
chris@3
   114
  /**
chris@3
   115
   * Returns the group id.
chris@3
   116
   */
chris@3
   117
  public long getInternalID()
chris@3
   118
  {
chris@3
   119
    assert id > 0;
chris@3
   120
chris@3
   121
    return id;
chris@3
   122
  }
chris@3
   123
chris@3
   124
  public boolean isDeleted()
chris@3
   125
  {
chris@3
   126
    return (this.flags & DELETED) != 0;
chris@3
   127
  }
chris@3
   128
chris@3
   129
  public boolean isMailingList()
chris@3
   130
  {
chris@3
   131
    return (this.flags & MAILINGLIST) != 0;
chris@3
   132
  }
chris@3
   133
chris@3
   134
  public boolean isWriteable()
chris@3
   135
  {
chris@3
   136
    return true;
chris@3
   137
  }
chris@3
   138
chris@3
   139
  public long getLastArticleNumber()
chris@3
   140
    throws StorageBackendException
chris@3
   141
  {
chris@3
   142
    return StorageManager.current().getLastArticleNumber(this);
chris@3
   143
  }
chris@3
   144
chris@3
   145
  public String getName()
chris@3
   146
  {
chris@3
   147
    return name;
chris@3
   148
  }
chris@3
   149
chris@3
   150
  /**
chris@3
   151
   * Performs this.flags |= flag to set a specified flag and updates the data
chris@3
   152
   * in the JDBCDatabase.
chris@3
   153
   * @param flag
chris@3
   154
   */
chris@3
   155
  public void setFlag(final int flag)
chris@3
   156
  {
chris@3
   157
    this.flags |= flag;
chris@3
   158
  }
chris@3
   159
chris@3
   160
  public void setName(final String name)
chris@3
   161
  {
chris@3
   162
    this.name = name;
chris@3
   163
  }
chris@3
   164
chris@3
   165
  /**
chris@3
   166
   * @return Number of posted articles in this group.
chris@3
   167
   * @throws java.sql.SQLException
chris@3
   168
   */
chris@3
   169
  public long getPostingsCount()
chris@3
   170
    throws StorageBackendException
chris@3
   171
  {
chris@3
   172
    return StorageManager.current().getPostingsCount(this.name);
chris@3
   173
  }
chris@3
   174
chris@3
   175
  /**
chris@3
   176
   * Updates flags and name in the backend.
chris@3
   177
   */
chris@3
   178
  public void update()
chris@3
   179
    throws StorageBackendException
chris@3
   180
  {
chris@3
   181
    StorageManager.current().update(this);
chris@3
   182
  }
chris@3
   183
chris@3
   184
}