trunk/com/so/news/storage/Group.java
changeset 0 f907866f0e4b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/trunk/com/so/news/storage/Group.java	Tue Jan 20 10:21:03 2009 +0100
     1.3 @@ -0,0 +1,142 @@
     1.4 +/*
     1.5 + *   StarOffice News Server
     1.6 + *   see AUTHORS for the list of contributors
     1.7 + *
     1.8 + *   This program is free software: you can redistribute it and/or modify
     1.9 + *   it under the terms of the GNU General Public License as published by
    1.10 + *   the Free Software Foundation, either version 3 of the License, or
    1.11 + *   (at your option) any later version.
    1.12 + *
    1.13 + *   This program is distributed in the hope that it will be useful,
    1.14 + *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 + *   GNU General Public License for more details.
    1.17 + *
    1.18 + *   You should have received a copy of the GNU General Public License
    1.19 + *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +
    1.22 +package com.so.news.storage;
    1.23 +
    1.24 +import java.sql.ResultSet;
    1.25 +import java.sql.SQLException;
    1.26 +import java.util.ArrayList;
    1.27 +import java.util.List;
    1.28 +import com.so.news.Debug;
    1.29 +
    1.30 +/**
    1.31 + * Represents a logical Group within this newsserver.
    1.32 + * @author Christian Lins
    1.33 + */
    1.34 +public class Group
    1.35 +{
    1.36 +  private long   id;
    1.37 +  private String name;
    1.38 +
    1.39 +  /**
    1.40 +   * Private constructor.
    1.41 +   * @param name
    1.42 +   * @param id
    1.43 +   */
    1.44 +  Group(String name, long id)
    1.45 +  {
    1.46 +    this.id   = id;
    1.47 +    this.name = name;
    1.48 +  }
    1.49 +  
    1.50 +  /**
    1.51 +   * Returns a Group identified by its full name.
    1.52 +   * @param name
    1.53 +   * @return
    1.54 +   */
    1.55 +  public static Group getByName(String name)
    1.56 +  {
    1.57 +    try
    1.58 +    {
    1.59 +      return Database.getInstance().getGroup(name);
    1.60 +    }
    1.61 +    catch(SQLException ex)
    1.62 +    {
    1.63 +      System.err.println(ex.getLocalizedMessage());
    1.64 +      ex.printStackTrace(Debug.getInstance().getStream());
    1.65 +      return null;
    1.66 +    }
    1.67 +  }
    1.68 +
    1.69 +  /**
    1.70 +   * Returns a list of all groups this server handles.
    1.71 +   * @return
    1.72 +   */
    1.73 +  public static ArrayList<Group> getAll()
    1.74 +  {
    1.75 +    ArrayList<Group> buffer = new ArrayList<Group>();
    1.76 +    
    1.77 +    try
    1.78 +    {
    1.79 +      ResultSet rs = Database.getInstance().getGroups();
    1.80 +      
    1.81 +      while(rs.next())
    1.82 +      {
    1.83 +        String name = rs.getString("name");
    1.84 +        long   id   = rs.getLong("group_id");
    1.85 +        
    1.86 +        Group group = new Group(name, id);
    1.87 +        buffer.add(group);
    1.88 +      }
    1.89 +    }
    1.90 +    catch(SQLException ex)
    1.91 +    {
    1.92 +      ex.printStackTrace(Debug.getInstance().getStream());
    1.93 +      System.err.println(ex.getLocalizedMessage());
    1.94 +    }
    1.95 +    
    1.96 +    return buffer;
    1.97 +  }
    1.98 +
    1.99 +  public List<Article> getAllArticles()
   1.100 +    throws SQLException
   1.101 +  {
   1.102 +    return getAllArticles(getFirstArticle(), getLastArticle());
   1.103 +  }
   1.104 +
   1.105 +  public List<Article> getAllArticles(int first, int last)
   1.106 +  {
   1.107 +    return null;
   1.108 +  }
   1.109 +
   1.110 +  public int getFirstArticle()
   1.111 +    throws SQLException
   1.112 +  {
   1.113 +    return Database.getInstance().getFirstArticleNumber(this);
   1.114 +  }
   1.115 +
   1.116 +  public long getID()
   1.117 +  {
   1.118 +    return id;
   1.119 +  }
   1.120 +
   1.121 +  public int getLastArticle()
   1.122 +    throws SQLException
   1.123 +  {
   1.124 +    return Database.getInstance().getLastArticleNumber(this);
   1.125 +  }
   1.126 +
   1.127 +  public String getName()
   1.128 +  {
   1.129 +    return name;
   1.130 +  }
   1.131 +
   1.132 +  public void setName(String name)
   1.133 +  {
   1.134 +    this.name = name;
   1.135 +  }
   1.136 +
   1.137 +  public int getEstimatedArticleCount()
   1.138 +    throws SQLException
   1.139 +  {
   1.140 +    if (getLastArticle() < getFirstArticle())
   1.141 +      return 0;
   1.142 +    return getLastArticle() - getFirstArticle() + 1;
   1.143 +  }
   1.144 +
   1.145 +}