org/sonews/daemon/storage/Group.java
changeset 2 1090e2141798
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/org/sonews/daemon/storage/Group.java	Wed Jul 01 10:48:22 2009 +0200
     1.3 @@ -0,0 +1,186 @@
     1.4 +/*
     1.5 + *   SONEWS 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 org.sonews.daemon.storage;
    1.23 +
    1.24 +import java.sql.SQLException;
    1.25 +import java.util.List;
    1.26 +import org.sonews.util.Log;
    1.27 +import org.sonews.util.Pair;
    1.28 +
    1.29 +/**
    1.30 + * Represents a logical Group within this newsserver.
    1.31 + * @author Christian Lins
    1.32 + * @since sonews/0.5.0
    1.33 + */
    1.34 +public class Group
    1.35 +{
    1.36 +
    1.37 +  /** 
    1.38 +   * If this flag is set the Group is no real newsgroup but a mailing list
    1.39 +   * mirror. In that case every posting and receiving mails must go through
    1.40 +   * the mailing list gateway.
    1.41 +   */
    1.42 +  public static final int MAILINGLIST = 0x1;
    1.43 +  
    1.44 +  /**
    1.45 +   * If this flag is set the Group is marked as readonly and the posting
    1.46 +   * is prohibited. This can be useful for groups that are synced only in
    1.47 +   * one direction.
    1.48 +   */
    1.49 +  public static final int READONLY    = 0x2;
    1.50 +
    1.51 +  /**
    1.52 +   * If this flag is set the Group is marked as deleted and must not occur
    1.53 +   * in any output. The deletion is done lazily by a low priority daemon.
    1.54 +   */
    1.55 +  public static final int DELETED     = 0x128;
    1.56 +  
    1.57 +  private long   id     = 0;
    1.58 +  private int    flags  = -1;
    1.59 +  private String name   = null;
    1.60 +  
    1.61 +  /**
    1.62 +   * Returns a Group identified by its full name.
    1.63 +   * @param name
    1.64 +   * @return
    1.65 +   */
    1.66 +  public static Group getByName(final String name)
    1.67 +  {
    1.68 +    try
    1.69 +    {
    1.70 +      return Database.getInstance().getGroup(name);
    1.71 +    }
    1.72 +    catch(SQLException ex)
    1.73 +    {
    1.74 +      ex.printStackTrace();
    1.75 +      return null;
    1.76 +    }
    1.77 +  }
    1.78 +
    1.79 +  /**
    1.80 +   * @return List of all groups this server handles.
    1.81 +   */
    1.82 +  public static List<Group> getAll()
    1.83 +  {
    1.84 +    try
    1.85 +    {
    1.86 +      return Database.getInstance().getGroups();
    1.87 +    }
    1.88 +    catch(SQLException ex)
    1.89 +    {
    1.90 +      Log.msg(ex.getMessage(), false);
    1.91 +      return null;
    1.92 +    }
    1.93 +  }
    1.94 +  
    1.95 +  /**
    1.96 +   * Private constructor.
    1.97 +   * @param name
    1.98 +   * @param id
    1.99 +   */
   1.100 +  Group(final String name, final long id, final int flags)
   1.101 +  {
   1.102 +    this.id    = id;
   1.103 +    this.flags = flags;
   1.104 +    this.name  = name;
   1.105 +  }
   1.106 +
   1.107 +  @Override
   1.108 +  public boolean equals(Object obj)
   1.109 +  {
   1.110 +    if(obj instanceof Group)
   1.111 +    {
   1.112 +      return ((Group)obj).id == this.id;
   1.113 +    }
   1.114 +    else
   1.115 +    {
   1.116 +      return false;
   1.117 +    }
   1.118 +  }
   1.119 +    
   1.120 +  public List<Pair<Long, ArticleHead>> getArticleHeads(final int first, final int last)
   1.121 +    throws SQLException
   1.122 +  {
   1.123 +    return Database.getInstance().getArticleHeads(this, first, last);
   1.124 +  }
   1.125 +  
   1.126 +  public List<Long> getArticleNumbers()
   1.127 +    throws SQLException
   1.128 +  {
   1.129 +    return Database.getInstance().getArticleNumbers(id);
   1.130 +  }
   1.131 +
   1.132 +  public int getFirstArticleNumber()
   1.133 +    throws SQLException
   1.134 +  {
   1.135 +    return Database.getInstance().getFirstArticleNumber(this);
   1.136 +  }
   1.137 +
   1.138 +  /**
   1.139 +   * Returns the group id.
   1.140 +   */
   1.141 +  public long getID()
   1.142 +  {
   1.143 +    assert id > 0;
   1.144 +
   1.145 +    return id;
   1.146 +  }
   1.147 +  
   1.148 +  public boolean isMailingList()
   1.149 +  {
   1.150 +    return (this.flags & MAILINGLIST) != 0;
   1.151 +  }
   1.152 +
   1.153 +  public int getLastArticleNumber()
   1.154 +    throws SQLException
   1.155 +  {
   1.156 +    return Database.getInstance().getLastArticleNumber(this);
   1.157 +  }
   1.158 +
   1.159 +  public String getName()
   1.160 +  {
   1.161 +    return name;
   1.162 +  }
   1.163 +
   1.164 +  /**
   1.165 +   * Performs this.flags |= flag to set a specified flag and updates the data
   1.166 +   * in the Database.
   1.167 +   * @param flag
   1.168 +   */
   1.169 +  public void setFlag(final int flag)
   1.170 +  {
   1.171 +    this.flags |= flag;
   1.172 +  }
   1.173 +
   1.174 +  public void setName(final String name)
   1.175 +  {
   1.176 +    this.name = name;
   1.177 +  }
   1.178 +
   1.179 +  /**
   1.180 +   * @return Number of posted articles in this group.
   1.181 +   * @throws java.sql.SQLException
   1.182 +   */
   1.183 +  public int getPostingsCount()
   1.184 +    throws SQLException
   1.185 +  {
   1.186 +    return Database.getInstance().getPostingsCount(this.name);
   1.187 +  }
   1.188 +
   1.189 +}