trunk/com/so/news/storage/Group.java
author chris <chris@marvin>
Tue, 20 Jan 2009 10:21:03 +0100
changeset 0 f907866f0e4b
permissions -rw-r--r--
Initial import.
chris@0
     1
/*
chris@0
     2
 *   StarOffice News Server
chris@0
     3
 *   see AUTHORS for the list of contributors
chris@0
     4
 *
chris@0
     5
 *   This program is free software: you can redistribute it and/or modify
chris@0
     6
 *   it under the terms of the GNU General Public License as published by
chris@0
     7
 *   the Free Software Foundation, either version 3 of the License, or
chris@0
     8
 *   (at your option) any later version.
chris@0
     9
 *
chris@0
    10
 *   This program is distributed in the hope that it will be useful,
chris@0
    11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@0
    12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@0
    13
 *   GNU General Public License for more details.
chris@0
    14
 *
chris@0
    15
 *   You should have received a copy of the GNU General Public License
chris@0
    16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@0
    17
 */
chris@0
    18
chris@0
    19
package com.so.news.storage;
chris@0
    20
chris@0
    21
import java.sql.ResultSet;
chris@0
    22
import java.sql.SQLException;
chris@0
    23
import java.util.ArrayList;
chris@0
    24
import java.util.List;
chris@0
    25
import com.so.news.Debug;
chris@0
    26
chris@0
    27
/**
chris@0
    28
 * Represents a logical Group within this newsserver.
chris@0
    29
 * @author Christian Lins
chris@0
    30
 */
chris@0
    31
public class Group
chris@0
    32
{
chris@0
    33
  private long   id;
chris@0
    34
  private String name;
chris@0
    35
chris@0
    36
  /**
chris@0
    37
   * Private constructor.
chris@0
    38
   * @param name
chris@0
    39
   * @param id
chris@0
    40
   */
chris@0
    41
  Group(String name, long id)
chris@0
    42
  {
chris@0
    43
    this.id   = id;
chris@0
    44
    this.name = name;
chris@0
    45
  }
chris@0
    46
  
chris@0
    47
  /**
chris@0
    48
   * Returns a Group identified by its full name.
chris@0
    49
   * @param name
chris@0
    50
   * @return
chris@0
    51
   */
chris@0
    52
  public static Group getByName(String name)
chris@0
    53
  {
chris@0
    54
    try
chris@0
    55
    {
chris@0
    56
      return Database.getInstance().getGroup(name);
chris@0
    57
    }
chris@0
    58
    catch(SQLException ex)
chris@0
    59
    {
chris@0
    60
      System.err.println(ex.getLocalizedMessage());
chris@0
    61
      ex.printStackTrace(Debug.getInstance().getStream());
chris@0
    62
      return null;
chris@0
    63
    }
chris@0
    64
  }
chris@0
    65
chris@0
    66
  /**
chris@0
    67
   * Returns a list of all groups this server handles.
chris@0
    68
   * @return
chris@0
    69
   */
chris@0
    70
  public static ArrayList<Group> getAll()
chris@0
    71
  {
chris@0
    72
    ArrayList<Group> buffer = new ArrayList<Group>();
chris@0
    73
    
chris@0
    74
    try
chris@0
    75
    {
chris@0
    76
      ResultSet rs = Database.getInstance().getGroups();
chris@0
    77
      
chris@0
    78
      while(rs.next())
chris@0
    79
      {
chris@0
    80
        String name = rs.getString("name");
chris@0
    81
        long   id   = rs.getLong("group_id");
chris@0
    82
        
chris@0
    83
        Group group = new Group(name, id);
chris@0
    84
        buffer.add(group);
chris@0
    85
      }
chris@0
    86
    }
chris@0
    87
    catch(SQLException ex)
chris@0
    88
    {
chris@0
    89
      ex.printStackTrace(Debug.getInstance().getStream());
chris@0
    90
      System.err.println(ex.getLocalizedMessage());
chris@0
    91
    }
chris@0
    92
    
chris@0
    93
    return buffer;
chris@0
    94
  }
chris@0
    95
chris@0
    96
  public List<Article> getAllArticles()
chris@0
    97
    throws SQLException
chris@0
    98
  {
chris@0
    99
    return getAllArticles(getFirstArticle(), getLastArticle());
chris@0
   100
  }
chris@0
   101
chris@0
   102
  public List<Article> getAllArticles(int first, int last)
chris@0
   103
  {
chris@0
   104
    return null;
chris@0
   105
  }
chris@0
   106
chris@0
   107
  public int getFirstArticle()
chris@0
   108
    throws SQLException
chris@0
   109
  {
chris@0
   110
    return Database.getInstance().getFirstArticleNumber(this);
chris@0
   111
  }
chris@0
   112
chris@0
   113
  public long getID()
chris@0
   114
  {
chris@0
   115
    return id;
chris@0
   116
  }
chris@0
   117
chris@0
   118
  public int getLastArticle()
chris@0
   119
    throws SQLException
chris@0
   120
  {
chris@0
   121
    return Database.getInstance().getLastArticleNumber(this);
chris@0
   122
  }
chris@0
   123
chris@0
   124
  public String getName()
chris@0
   125
  {
chris@0
   126
    return name;
chris@0
   127
  }
chris@0
   128
chris@0
   129
  public void setName(String name)
chris@0
   130
  {
chris@0
   131
    this.name = name;
chris@0
   132
  }
chris@0
   133
chris@0
   134
  public int getEstimatedArticleCount()
chris@0
   135
    throws SQLException
chris@0
   136
  {
chris@0
   137
    if (getLastArticle() < getFirstArticle())
chris@0
   138
      return 0;
chris@0
   139
    return getLastArticle() - getFirstArticle() + 1;
chris@0
   140
  }
chris@0
   141
chris@0
   142
}