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